/* Ajax common routines 
   Last Updated : 2009-07-31
   Created By: Ted Ma
   Updated By: Emma 
*/

/*
get XMLHttpRequest object
this part must be executed before any Ajax function can be called.
*/

var XHR = false;
if (typeof XMLHttpRequest != "undefined") 
     XHR = new XMLHttpRequest();
else if (window.ActiveXObject) {
    var aVersions = [ "MSXML2.XMLHttp.6.0",
        "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"];
    for (var i = 0; i < aVersions.length; i++) {
        try {
            XHR = new ActiveXObject(aVersions[i]);
            break;
        } catch (oError) {
          //alert("Your browser must support XMLHttpRequest, otherwise, some requests cannot be processed.\nPlease contact Support Team for details.");
        }
    }
}     
/*
This function is triggered by user inputs or pressed button.

form : form object, if the document has more than 1 form, 
       use the form object that needed to pass parameters to the server.
	   if you construct your own URLValue or use "GET" method, ignore this parameter.

handler : the server application or file name that processes the browser request
          e.g. fullpath + "chkLogin.jsp", fullpath + "employee.txt"
		  you may also attach parameter to handler. If you do so, method must be set to "GET" and URLValue 

method : "POST" or "GET" (optional, default to "POST")
URLValue : if method="GET", this value will be ignored;
           for method="POST", you may contruct your own URL parameter, pass your parameters to URLValue starting with "&",
	       otherwise, this function will search all elements of the form object and construct a URLValue.

Asyn : set to True if you want continue program execution WITHOUT waiting for the Ajax result
       set to False if you want to wait until Ajax result returns (default to True)

returnFunction : the function to handle the return content/value of the handler. 
                It must have 2 arguement : the return content , the form obj

Having received result from the server, call httpResultHandler to process the result. You may use
responseXML or responseText to retrieve data. Do not write program specific routines, names here. 
*/
function getHttpData(form, handler, method, URLValue, Asyn , endPoint ,event) {
    if (XHR) {
        var ajaxCurDate=new Date();
        var formValue=null;
        var result = true; // return true / false for synchronized process
        if (method==null || method=="" || typeof method == "undefined") method="POST";

        if (method=="GET") {
          if (handler.indexOf("?")>=0) handler += "&ajaxTimeStamp="+ajaxCurDate.getTime(); //add timestamp to override cache
          else handler += "?ajaxTimeStamp="+ajaxCurDate.getTime();
        } else { 
          if (URLValue==null || URLValue=="" || typeof URLValue == "undefined") formValue = getRequestBody(form);
          else formValue = URLValue;
          formValue += "&ajaxTimeStamp="+ajaxCurDate.getTime();
        }
        if (Asyn==null || typeof Asyn == "undefined") Asyn=true;
        XHR.open(method, handler, Asyn);
        XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        XHR.onreadystatechange = function()     {
            if (XHR.readyState == 4 && XHR.status == 200) result = endPoint(form , XHR, event);
        }
        XHR.send(formValue);
    } else {
        alert("Your browser must support XMLHttpRequest, your request is aborted. \nPlease contact Support Team for details.");
        return;
    }
    if (!Asyn)return result;
    return false
}

function getRequestBody(form) {
    var aParams = new Array();
    for (var i=0 ; i < form.elements.length; i++) {
        var myObj = form.elements[i];
        if ( (myObj.type == "checkbox" || myObj.type == "radio" ) && !myObj.checked ) continue;
        var sParam = encodeURIComponent(myObj.name);
        sParam += "=";
        sParam += encodeURIComponent(myObj.value);
        aParams.push(sParam);
    }
    return aParams.join("&");
}

function composeURI(form, fieldArr) {
    var aParams = new Array();
    for (var n=0 ; n < form.elements.length; n++) {
        var myObj = form.elements[n];
        for (var i=0 ; i < fieldArr.length; i++) {
            var ObjName = fieldArr[i];
            if(myObj.name != ObjName) continue;
            if (!myObj[0]){
                if ((myObj.type == "checkbox" || myObj.type == "radio" ) && !myObj.checked ) continue;
                var sParam = encodeURIComponent(ObjName) + "=" + encodeURIComponent(myObj.value);
                aParams.push(sParam);
            }else {
                for (var j=0 ; j < myObj.length; j++) {
                    if ((myObj.type == "checkbox" || myObj.type == "radio" ) && !myObj[j].checked ) continue;
                    var sParam = encodeURIComponent(ObjName) + "=" + encodeURIComponent(myObj[j].value);
                    aParams.push(sParam);
                }
            }
        }
    }
    return "&"+aParams.join("&");
}


