// use this predefined object to access the communication-related functions
var comLibXmlHttp = new Object();

comLibXmlHttp.getXmlHttp = function(onError) {
    var xmlhttp = false;
    if (window.ActiveXObject) {
        // IE case
        try {
            if (navigator.userAgent.toLowerCase().indexOf('msie5') != -1) {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
        } catch (e) {
            xmlhttp = false;
        }
    } else try {
        // Gecko case
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false;
    }

    if (!xmlhttp) {
        if (onError) {
            onError("Unable to create XMLHttpRequest in this browser.");
        }
    }
    return xmlhttp;
}

/**
 * Convert form data into array of data to post
 */
comLibXmlHttp.submitForm = function(frm, data, onError) {
	comLibXmlHttp.submitForm(frm.action, frm, dataa, onError);
}
comLibXmlHttp.submitForm = function(url, frm, data, onError) {
    var xmlhttp = comLibXmlHttp.getXmlHttp(onError);
    
    if (!data) {
        data = new Array();
    }
    for (var i = 0; i < frm.elements.length; i++) {
        if(frm.elements[i] && frm.elements[i].name) {
            if (frm.elements[i].type == 'checkbox') {
                // checkbox
                if (frm.elements[i].checked) {
                    data[frm.elements[i].name] = frm.elements[i].value;
                } else {
                    data[frm.elements[i].name] = '';
                }
            } else {
                data[frm.elements[i].name] = frm.elements[i].value;
            }
        }
    }
    
    this.doHttpPost(xmlhttp, url, data, onError);
}

/**
 * @param data This must be an array in the form of array[data_name] = value
 */
comLibXmlHttp.doHttpPost = function(xmlhttp, url, data, onError) {
    // first put all data in a url parameter string
    var formData = "comLib=true";

    if (data) {
        for (var item in data) {
            formData += "&";
            if (typeof data[item] == "object") {
                for (var i = 0; i < data[item].length; i++) {
                    if (i != 0) {
                        formData += "&";
                    }
                    formData += item + "=" + encodeURIComponent(data[item][i]);
                }
            } else {
                formData += item + "=" + encodeURIComponent(data[item]);
            }
        }
    }

    try {
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4) {
                try {
         
                    eval(xmlhttp.responseText);
                    
                } catch (e) {
                    if (onError) {
                        onError(e + " : " + xmlhttp.responseText);
                    }
                }
            }
        }
        xmlhttp.send(formData);
    } catch (e) {
        if (onError) {
            onError(e);
        }
    }
}

comLibXmlHttp.doHttpGet = function(url, onLoad, onError) {
    var xmlhttp = comLibXmlHttp.getXmlHttp(onError);
    try {

        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState==4) {
                try {
                    if (onLoad) {
                        onLoad(xmlhttp.responseText);
                    } else {
                        eval(xmlhttp.responseText);
                    }
                } catch (e) {
                    if (onError) {
                        onError(e + " : " + xmlhttp.responseText);
                    }
                }
            }
        }
        xmlhttp.send(null);
    } catch (e) {
        if (onError) {
            onError(e);
        }
    }
}