var xmlHTTPReq = null;
var xmlHTTPReqCB = null;

/*
 * some work to use XMLHTTPRequest Object
 */
function prepareXmlHTTPRequest()
{
    if(window.XMLHttpRequest) {
        try {
            xmlHTTPReq = new XMLHttpRequest();
        } catch(e) {
            xmlHTTPReq = false;
        }
    }
    else if(window.ActiveXObject) {
        try {
            xmlHTTPReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHTTPReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                xmlHTTPReq = false;
            }
        }
    }
}

function processReqChange()
{
    if (xmlHTTPReq.readyState == 4) { // Loaded
        if (xmlHTTPReq.status == 200) { // only if "OK"
            xmlHTTPReqCB(unescape(xmlHTTPReq.responseText));
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

/*
 * Simple Call for requesting Document
 */
function loadXMLDoc(url, cb)
{
    prepareXmlHTTPRequest();

    if (xmlHTTPReq) {
        xmlHTTPReqCB = cb;
        xmlHTTPReq.onreadystatechange = processReqChange;
        try {
            xmlHTTPReq.open("GET", url, true);
            xmlHTTPReq.send("");
        } catch (e) {
            alert("There may be one or more problem to execute XML request");
        }
    }

    delete xmlHTTPReq;
}

/*
 * call of Form element for requesting Document
 */
function submitFormToXMLDoc(action, form_id, cb, method)
{
    var form;
    var method;
    var action;
    var postData = "";

    form = document.getElementById(form_id);
    if ( !form ) {
        alert("check form id");
        return false;
    }

    method = form.method.toUpperCase();
    if ( !method ) {
        alert("form must have an attribute 'method'");
        return false;
    }

    //action = form.action;
    if ( !action ) {
        alert("form must have an attribute 'action'");
        return false;
    }

    prepareXmlHTTPRequest();

    if (xmlHTTPReq) {
        xmlHTTPReqCB = cb;
        xmlHTTPReq.onreadystatechange = processReqChange;
    }
    else {
        return false;
    }

    if (method == "POST") {
        xmlHTTPReq.open(method, action, true);
        if (xmlHTTPReq) {
            for (i=0; i<form.elements.length; i++) {
                if (form.elements[i].name) postData = postData + "&" + form.elements[i].name + "=" + escape(form.elements[i].value);
            }
            postData = postData.substr(1);
            xmlHTTPReq.setRequestHeader("Method", "POST " + action + " HTTP/1.1");
            xmlHTTPReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlHTTPReq.setRequestHeader("Content-Length", postData.length);
        }
    }
    else if (method == "GET") {
        if (action.indexOf("?") == -1) action = action + "?";
        else action = action + "&";

        for (i=0; i<form.elements.length; i++) {
            if (form.elements[i].name) {
                action = action + "&" + form.elements[i].name + "=" + escape(form.elements[i].value);
            }
        }
        xmlHTTPReq.open(method, action, true);
        postData = null;
    }
    else {
        alert("Unsupported Form Method: " + method);
        return false;
    }

    try {
        xmlHTTPReq.send(postData);
    } catch (e) {
        alert("There may be one or more problem to execute XML request");
        delete xmlHTTPReq;
    }

    return false;
}

