/*****************************************************************************
 *
 * Very Simple AJAX engine by U1I
 * Version: 0.1
 * copyright of this javascript code to U1I. Do not modify.
 *
 *****************************************************************************/

/*****************************************************************************
 *
 * HOWTO:
 * 
 * 1. Simple Document Request
 *
 *  loadXMLDoc(url, callback) 
 *      In java script code space or event code space of any element,
 *      call this function to request and receive a xml document.
 *
 *    url
 *      The url of a xml document to download into current document.
 *
 *    callback
 *      When the asyncronous response has arrived, this callback function is
 *      called back with one argument arrived string data.
 *
 *  [EXAMPLE HTML CODE]
 *   <script language="javascript" type="text/javascript">
 *   function cb(responseText) {
 *       document.getElementById("simpleReq").innerHTML = responseText;
 *   }
 *   </script>
 *   <div id="simpleReq" onclick="loadXMLDoc('result.html?go=top', cb);">
 *     Click Here
 *   </div>
 *
 * 2. Document Request with Form data
 *
 *  submitFormToXMLDoc(form_id, callback)
 *      In java script code space or event code space of any element,
 *      call this function to request and receive a xml document.
 *
 *    form_id
 *      The id of a form store form-elements containing data to use.
 *
 *    callback
 *      When the asyncronous response has arrived, this callback function is
 *      called back with one argument arrived string data.
 *
 *  [HTML CODE]
 *   <script language="javascript" type="text/javascript">
 *   function cbForm(responseText) {
 *       document.getElementById("test").innerHTML = responseText;
 *   }
 *   </script>
 *   <div id="test">posttest</div>
 *   <form method="post" action="form.php" onsubmit="return submitFormToXMLDoc(this.id, cbForm);" id="frmPostTest">
 *     <input type="text" name="option" value="value of the option element of form">
 *     <input type="submit" value="post test">
 *   </form>
 *
 *
 *****************************************************************************/

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(form_id, cb)
{
    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;
}