// common function for http communication
function createXMLHttpRequest(cbFunc) {
    var XMLhttpObject = null;
    try {
        XMLhttpObject = new XMLHttpRequest();
    } catch(e) {
        try {
            XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return null;
            }
        }
    }
    if (XMLhttpObject) {
        XMLhttpObject.onreadystatechange = cbFunc;
    }
    return XMLhttpObject;
}

// This function acquire informations for keyword checks.
function searchInit() {
    httpObj = createXMLHttpRequest(initData);
    if (httpObj) {
        httpObj.open("POST",url,"sync");
        httpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=' + encode);
        httpObj.send("searchTipInit=1");
    }
}
// This function analyze the data acquired in searchInit function, and store it in global variable. 
function initData() {
    if ((httpObj.readyState == 4) && (httpObj.status == 200)) {
        xmlData = httpObj.responseXML;
        resultTags = xmlData.getElementsByTagName("result");
        minimamLength = resultTags[0].childNodes[0].firstChild.nodeValue;
        isUseZenbun = ("true" == resultTags[0].childNodes[1].firstChild.nodeValue);
        confirmMessage = resultTags[0].childNodes[2].firstChild.nodeValue;
        requireKeyword = resultTags[0].childNodes[3].firstChild.nodeValue;
    }
}
// This function execute a keyword search.
function dataSend() {
    if (!checkLength()) {
        return false;
    }
    if (type == 2) {
        document.forms['simpleSearchForm'].action = url;
        document.forms['simpleSearchForm'].elements['enc'].value = encode;
        document.forms['simpleSearchForm'].elements['type'].value = type;
        document.forms['simpleSearchForm'].submit();
    } else {
        var textData = document.simpleSearchForm.searchword.value;
        httpObj = createXMLHttpRequest(displayData);
        if (httpObj) {
            httpObj.open("POST",url,"sync");
            httpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=' + encode);
            httpObj.send("searchword=" + textData + "&type=" + type);
        }
    }
}
// This function analyze search results, and display error message or redirect for search result list. 
function displayData() {
    if ((httpObj.readyState == 4) && (httpObj.status == 200)) {
        xmlData = httpObj.responseXML;
        resultTags = xmlData.getElementsByTagName("result");
        flg = resultTags[0].childNodes[0].firstChild.nodeValue;
        if (flg == 0) {
            if (type == 0) {
                document.getElementById('notFoundMessage').innerHTML = "<b><font color=#C14923>" + resultTags[0].childNodes[1].firstChild.nodeValue + "</font></b>";
            } else {
                document.getElementById('notFoundMessage').innerHTML = "&nbsp;";
                alert(resultTags[0].childNodes[1].firstChild.nodeValue);
            }
            selectText();
        } else {
            document.getElementById('notFoundMessage').innerHTML = "&nbsp;";
            document.location = resultTags[0].childNodes[1].firstChild.nodeValue;
        }
    } else {
        document.getElementById('notFoundMessage').innerHTML = "<b>Wait...</b>";
    }
}
//This function checks length of keyword.
function checkLength() {
    var keyword = document.forms['simpleSearchForm'].elements['searchword'].value;
    var fullSpace = document.forms['simpleSearchForm'].elements['fullSpace'].value;
    keyword = keyword.replace(/ /g, fullSpace);
    var flg = false;
    var flg2 = false;
    ary = keyword.split(fullSpace);
    for (var i = 0; i < ary.length; i++) {
        ary[i] = ary[i].replace(/ /g, '');
        if (ary[i] == '') {
            flg2 = true;
            continue;
        }
        if (ary[i].length <= minimamLength) {
            flg = true;
            break;
        }
        flg2 = false;
    }
    if (flg && !isUseZenbun) {
        if (!confirm(confirmMessage)) return false;
    }
    if (flg2) {
        if (type == 0) {
            document.getElementById('notFoundMessage').innerHTML = "<b><font color=#C14923>" + requireKeyword + "</font></b>";
        } else {
            if (type == 1) {
                document.getElementById('notFoundMessage').innerHTML = "&nbsp;";
            }
            alert(requireKeyword);
            setFocusForInputform();
        }
        return false;
    }
    return true;
}
//This function set focus on text area.
function setFocusForInputform() {
    document.forms['simpleSearchForm'].elements['searchword'].focus();
}
// This function makes the character string of the text box a choice state.
function selectText() {
    setFocusForInputform();
    document.forms['simpleSearchForm'].elements['searchword'].select();
}
