function $(name)
{
    return document.getElementById(name);
}

function Mswitch(OBJ){
    sIndex = OBJ.selectedIndex;
    SRC = OBJ.options[sIndex].value;
    if (SRC != 'nolink'){
        window.location = SRC;
    }
}

function check(){
    // test if empty
    if ($("author").value == ""){
        window.alert("請輸入您的姓名！");
        $("author").focus();
        return false;
    }
    else if ($("message").value == ""){
        window.alert("請輸入您的留言！ Please input your message!");
        $("message").focus();
        return false;
    }


    // test if invalid word
    var invalid_word = is_invalid_message($("message").value);
    if (!window.is_login && invalid_word){
        window.alert("sorry! ``" + invalid_word + "'' is an invalid word.");
        $("message").focus();
        return false;
    }

    return true;
}

// function wordFilter(str)
function is_invalid_message(str)
{
    str = str.toLocaleLowerCase( );
    var forbiddenWords = new Array(
        "a href",
        "http://"
    );

    for (var index in forbiddenWords){
        var forbiddenWord = forbiddenWords[index];
        if (str.indexOf(forbiddenWord) != -1){
            return forbiddenWord;
        }
    }

    return false;
}

window.onload = initial;

function initial(){
    var inputArray = document.getElementsByTagName("input");
    var textareaArray = document.getElementsByTagName("textarea");
    var emArray = document.getElementsByTagName("em");

    for (var i = 0; i < inputArray.length; i++){
        inputArray[i].onkeydown = disableEsc;
    }
    for (var i = 0; i < textareaArray.length; i++){
        textareaArray[i].onkeydown = disableEsc;
    }
    for (var i = 0; i < emArray.length; i++){
        emArray[i].onclick = folding;
    }

    // form check
    if ($("post_form")){
        $("post_form").onsubmit = check;
    }

    if ($("author")){
        $("author").focus();
    }
}

function disableEsc(e){
    if (!e){ // 在argument填e，是為了W3C/Netscape系。window.event則是IE
        e = window.event;
    }
    
    if (e.keyCode == 27){
        //this.focus();
        return false;
        // 事實上，IE在input和textarea按Esc都會清掉，加了return false就都不會
        // 而FireFox 1.0的測試情形是，原本input會清，textare不會，加了return false還是一樣…
    }
}

function folding(){
    var tempObj;
    
    if (document.getElementById){
        tempObj = $("t" + this.id.substring(1, this.id.length));
        tempObj.style.display = (tempObj.style.display == "none") ? "block" : "none" ;
    }
}

