﻿// Declare the namespaces
if (Kinsail.AJAX == null || typeof(Kinsail.AJAX) != "object") {Kinsail.AJAX = new Object();}

Kinsail.AJAX = {

    // JavaScript File to make requests and handle responses
    // to AJAX requests
    MakeRequest: function (URL, Method, Data) {
        if (Kinsail.debugLevel > 0) {
	        alert(URL + " " + Method + " " + Data);
	    }
	    // send to the debug text area, if it exists
        if (document.DebugForm != null && typeof(document.DebugForm) != "undefined") {
            if (document.DebugForm.DebugArea != null && typeof(document.DebugForm.DebugArea) != "undefined") {
                document.DebugForm.DebugArea.value += URL + " " + Method + " " + Data + "\r\n";
            }
        }
	    //document.QuickSearch.ConfirmationNumber.value = URL + " " + Method + " " + Data;
	    // Checking if IE-specific document.all collection exists 
	    // to see if we are running in IE 
	    if (document.all) { 
		    xhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    else { 
	    // Mozilla - based browser 
		    xhttp = new XMLHttpRequest(); 
	    }
	    //hook the event handler
	    xhttp.onreadystatechange = Kinsail.AJAX.HandlerOnReadyStateChange;
	    //prepare the call, http method=POST, asynchronous call=true
	    // we use POST because the data is too long otherwise
	    xhttp.open(Method, URL, true);
	    if (Method == "POST") {
		    // set the request header for the POST operation
		    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    //finally send the call
		    xhttp.send(Data.replace(/ /g, "%20"));
	    }
	    else {
		    xhttp.send();
	    }
    },
    
    // this is the handler for the state change event fired by the XMLHttp Control
    HandlerOnReadyStateChange: function (){
	    // This handler is called 4 times for each 
	    // state change of xmlhttp
	    // States are: 0 uninitialized
	    //      1 loading
	    //      2 loaded
	    //      3 interactive
	    //      4 complete
	    if (xhttp.readyState==4){
		    resp = xhttp.responseText;
		    if (Kinsail.debugLevel > 0) {
		        alert("AJAX_RESPONSE: " + resp);
		    }
		    // send to the debug text area, if it exists
            if (document.DebugForm != null && typeof(document.DebugForm) != "undefined") {
                if (document.DebugForm.DebugArea != null && typeof(document.DebugForm.DebugArea) != "undefined") {
                    document.DebugForm.DebugArea.value += "AJAX_RESPONSE: " + resp + "\r\n";
                }
            }
		    // first, let's parse out the attributes from the first line
		    // Action,UniqueID,RowID
		    strAttributes = resp.substr(0, resp.indexOf("^"));
		    // eliminate the attributes from the recordset that is returned
		    resp = resp.replace(strAttributes + "^", "");
		    // create an array of the attributes to figure out which function to call to process the response
		    strAttributeArray = strAttributes.split(",");
		    // dynamically call the parsing function
		    if (Kinsail.debugLevel > 5) {
			    alert(strAttributeArray[0] + "('" + resp.replace(/\'/g, '&#39;') + "', '" + strAttributes + "')");
		    }
		    // send to the debug text area, if it exists
            if (document.DebugForm != null && typeof(document.DebugForm) != "undefined") {
                if (document.DebugForm.DebugArea != null && typeof(document.DebugForm.DebugArea) != "undefined") {
                    document.DebugForm.DebugArea.value += strAttributeArray[0] + "('" + resp.replace(/\'/g, '&#39;') + "', '" + strAttributes + "')" + "\r\n";
                }
            }
            // reset the timer, if it exists
            if (typeof SetSessionTimer == 'function' && typeof SetExpirationTimer == 'function') {
                if (typeof intWarningTimer != 'undefined') {
                    clearTimeout(intWarningTimer);
                }
                if (typeof intExpiredTimer != 'undefined') {
                    clearTimeout(intExpiredTimer);
                }
                if (typeof intBlink != 'undefined') {
                    clearTimeout(intBlink);
                }
                SetSessionTimer();
                SetExpirationTimer();
            }
		    eval(strAttributeArray[0] + "('" + resp.replace(/\'/g, '&#39;') + "', '" + strAttributes + "')");
	    }
    },
    
    CreateFormData: function (FormObject) {
	    // this is the function that creates the form data to be submitted
	    // we need to set the submit data to nothing so that we can append to it
	    strSubmitData = "";
	    if (!window.strAmp) {
	        strAmp = "&";
	    }
	    if (!window.strEquals) {
	        strEquals = "=";
	    }
	    for (intFormCounter=0;intFormCounter<FormObject.elements.length;intFormCounter++) { 
    	    blnAdd = false;
    	    blnLoop = false;
            // handle checkboxes and radio buttons
	        if (FormObject.elements[intFormCounter].type == 'checkbox' || FormObject.elements[intFormCounter].type == 'radio') {
	            if (FormObject.elements[intFormCounter].checked.toString() == 'true') {
	                blnAdd = true;
	            }
	        }
	        // handle multiple select boxes
	        else if (FormObject.elements[intFormCounter].type == 'select-multiple') {
	            blnAdd = true;
	            blnLoop = true
	        }
	        // handle text boxes, select boxes, hidden inputs, etc.
	        else {
	            blnAdd = true;
	        }
	        if (blnAdd) {
	            if (blnLoop) {
                    for (intSelectLooper=0; intSelectLooper<FormObject.elements[intFormCounter].length; intSelectLooper++) {
                        // only add if this option is selected
                        if (FormObject.elements[intFormCounter][intSelectLooper].selected) {
	                        strSubmitData += strAmp + FormObject.elements[intFormCounter].name + strEquals + escape(FormObject.elements[intFormCounter][intSelectLooper].value);
	                    }   
                    }   
	            }
	            else {
	                strSubmitData += strAmp + FormObject.elements[intFormCounter].name + strEquals + escape(FormObject.elements[intFormCounter].value);
	            }   
	        }   
	    }
	    // remove the initial ampersand from the string and replace it with a ?
	    strSubmitData = strSubmitData.replace("&", "?");
	    return strSubmitData;
    }
    
};// end Kinsail.AJAX