// JScript File
/* AJAX.js
To use this code, place a reference to this code in the page's HTML header:
<script type="text/javascript" src="/SITE_FOLDER/AJAX.js" ></script>

INPUT: Url of server-side script which returns XML, and the CallingPageFunction
name that processes the request
Usage: In the calling page, 
objAJAX.loadXMLDoc("Url?QueryString", "FunctionToProcessXMLFromServer(XMLFromServer)");

OUTPUT: The XML output from the server is returned in a variable XMLFromServer
Usage: In the calling page, 
FunctionToProcessXMLFromServer(XML)
{
    var someVar = XML;
    // ...and continue with the Javascript
}

Basic Concept:

1. Define an XMLHttpRequest object
2. Specify its objXmlReq.onreadystatechange method to the method
    that processes the returned XML - processXMLFromServer 
3. Send the request using "GET"

4. The processXMLFromServer function processes the returned XML from the server side script
 function processXMLFromServer() {
 // process only if objXmlReq is successfully complete
    if (objXmlReq.readyState == 4) {
        // ... and Status is OK (numeric code returned by browser:
        //      404 for "page not found" or 200 for "OK"
        if (objXmlReq.status == 200) {
            // ... Parse the XML using the following to access the tags
            // var XMLFromServer = objXMLReq.responseXML.getElementsByTagName("XML_TAG_NAME");
            
            
            FunctionToProcessXMLFromServer(XMLFromServer))
            // ... and write standard Javascript code within 
            // this function to accomplish 
            // what you want to do with the returned results.
        }
    }
 } // end of processReq

*/

// AJAX code to record the link clicked
var XMLFromServer;
var objXmlReq;
var FunctionToProcessXMLFromServer;
var objAJAX = {
    loadXMLDoc: function(url, strFunctionToProcessXMLFromServer) {

        FunctionToProcessXMLFromServer = strFunctionToProcessXMLFromServer;
	    objXmlReq = false;
        // branch for native XMLHttpRequest object
        if(window.XMLHttpRequest) {
    	    try {
			    objXmlReq = new XMLHttpRequest();
            } catch(e) {
			    objXmlReq = false;
            }
        // branch for IE ActiveX version
        } else if(window.ActiveXObject) {
       	    try {
        	    objXmlReq = new ActiveXObject("Msxml2.XMLHTTP");
      	    } catch(e) {
        	    try {
          		    objXmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        	    } catch(e) {
          		    objXmlReq = false;
        	    }
		    }
        }
	    if(objXmlReq) {

            // Do not wait to get response from server ...
            // ... rather, use the onreadystatechange event
            // to invoke a function which processes request.
            // This allows asynchronous operation and allows the
            // script to continue.	
            if (FunctionToProcessXMLFromServer) {
		        objXmlReq.onreadystatechange = objAJAX.processXMLFromServer;
		    }
		    objXmlReq.open("GET", url, true); // true for asynchronous behavior
		    objXmlReq.send("");
	    }

    }, // end of loadXMLDoc function
     
    processXMLFromServer: function() {
    // process only if objXmlReq is successfully complete
        if (objXmlReq.readyState == 4) {
            // ... and Status is OK (numeric code returned by browser:
            //      404 for "page not found" or 200 for "OK"
            if (objXmlReq.status == 200) {
                // ... process request
                //alert("Ready for function that processes XML from server");
                XMLFromServer =objXmlReq.responseXML;
                eval(FunctionToProcessXMLFromServer);
            } else {
                // error
                //alert("There was a problem getting XML data:\n" + objXmlReq.statusText);
                //alert("There was a problem getting XML data:\n" + objXmlReq.status + " - " + objXmlReq.statusText);
            }
        }
    } // end of processXMLFromServer
   
} // end of objAJAX





