//
// Advanced AJAX Engine
//
//  AJAXREQUEST(request, input, returnMethod, responce, data)
//      provides are rapper for all three of the AJAX functions below.  Uses the hash XMLDOCS to find
//      the serverside script associated with the request.  This is a request/responce function, it sends
//      a request and handles the responce.
//
//      Global
//          XMLDOCS
//     
//      Parameters - Request Mode
//          action - key for the hash XMLDOCS
//          input - date to send to the server
//          returnMethod - method to send the returned data to
//          responce - ''
//          data - ignored not undefined
//
//      Parameters - Responce Mode
//          action - key for the hash XMLDOCS
//          input - ignored, not undefined
//          returnMethod - ignored, not undefined
//          responce - indicates that function should be in responce mode
//          data - XML data passed from the server
//
//      Returns - Request Mode
//          issues a loadXMLDoc command
//
//      Returns - Responce Mode
//          hands the XML data to the method specified by returnMethod
//
//
//	newXMLHttpRequest()
//		attempts to create a new XMLHttpRequest, returns browser appropriate XMLHttpRequest object or FALSE
//
//	loadXMLDoc(url)
//		This function creates a XMLHttpRequest object, "sends" that object to specified url,
//		and calls	processReqChange() to handle the return
//
//		Global		
//			req 
//
//		Parameters
//			url - url of the document to load
//
//		Returns
//			none  (throws and alert() on failure)
//
//
//	processReqChange()
//		Pulls the  full XML return from the XMLHttpRequest as well as the <method>, <result_flag>,
//		and <result_data> element of the XML return.  The function evaluates the method 
//      specified in <method>, with the parameter ('', '',<result_flag>,<result_data>).
//
//		Global
//			req
//          resp
//
//		Parameters
//			none 
//
//		Returns
//			none  (throws and alert() on failure)
//
//

var req;
var resp;
var respMethods = new Array();

function AJAXREQUEST(action, input, method, response, data){ 
	if (response != ''){ 
		// Response mode
        eval(respMethods[action]+'(response, data);'); 
	}else{
		// Input mode 
        respMethods[action]= method;
        request ="<request><key>"+key+"</key><method>"+action+"</method><data>"+input+"</data></request>";
        loadXMLDoc(AJAXRequestInterpreter,'request='+encodeURIComponent(request));    
	}
}

function newXMLHttpRequest() {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
		newRequest = new XMLHttpRequest();
    
	// branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
		newRequest = new ActiveXObject('Microsoft.XMLHTTP');
	
	// branch for no XMLHttpRequest
	} else{
		alert('XMLHttpRequest not supported by this browser');
	}
	
	return newRequest;
}
   
function loadXMLDoc(url, data) {
	req = newXMLHttpRequest();
  	req.onreadystatechange = processReqChange;
	req.open("POST", url, true);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	req.send(data ? data + '&' : 'NULL');
} 

function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
	   // only if "OK"
        if (req.status == 200) {
			// ...processing statements
			var response = req.responseXML.documentElement;
		
			var method 	= response.getElementsByTagName('method')[0].firstChild.data;
		
			var result_flag	= response.getElementsByTagName('result_flag')[0].firstChild.data;
			
			if (browser.isIE){
				resp = response.getElementsByTagName('result_data')[0].cloneNode(true);
				var result_data = "user agent error, MSIE expected";
            }else{
				var result_data = response.getElementsByTagName('result_data')[0].cloneNode(true);
            }
			
			eval('AJAXREQUEST(\''+method+'\',\'\', \'\', result_flag, result_data)');
		} 
		else {
			alert("Ajax said: there was a problem retrieving the XML data.\n" + req.statusText);
		}
    }
}