/**********************************************************************************/
/** SimpleRequest Object                                                              **/
/**********************************************************************************/
//This creates an XMLHTTPRequest Object...
//  usage: 
/**
	// This will setup the request object.
	var request = new SimpleRequest();
	
	// This checks to see if the request object was setup properly
	if(request==null){
		alert("AJAX is not supported on this browser. Please try a different browser.");
	}
	
	// This is the command to make a request
	//	"somepage.asp" - This is the server page that will process the request and make a response
	//	"POST" - This is the method used to communicate to the server page. It can ONLY be "GET" or "POST"
	//	"param1=foo&param2=bar" - This is a string that contains the property=value pairs for each parameter. The property=value pairs are separated by the "&".
	//	processRequest - This is the function that will process the response from the server.
	//	responseType - This field is not required. This can be either of the following values if it is passed in:
	//		text - This will return the response to the function specified in processRequest as a string
	//		xml - This will return the response to the function specified in processRequest as an XML object
	request.connect("somepage.asp", "POST", "param1=foo&param2=bar", processXMLResponse, "xml");
	request.connect("somepage.asp", "POST", "param1=foo&param2=bar", processStringResponse, "text");
	request.connect("somepage.asp", "POST", "param1=foo&param2=bar", processStringResponse); //this does the same thing as the call above, except that the response type isn't passed in, so the default response is going to be a string
	
	// This is the function that will process the response from the server. In order for it to work properly, you need to pass it into the connect function of the SimpleRequest object.
	function processXMLResponse(oXML){
		alert("This handler is expecting xml from the response:\n" + oXML);
	}
	
	// This is the function that will process the response from the server. In order for it to work properly, you need to pass it into the connect function of the SimpleRequest object.
	function processStringResponse(sResponse){
		alert("This handler is expecting text from the response:\n" + sResponse);
	}
**/
/**********************************************************************************/
function SimpleRequest(){
	var request = null;
	bComplete = false;
	aRequestTypes = [function(){return new XMLHttpRequest()}, function(){return new ActiveXObject("Msxml2.XMLHTTP")}, function(){return new ActiveXObject("Msxml3.XMLHTTP")}, function(){return new ActiveXObject("Microsoft.XMLHTTP")}];
	
	for(var i=0; i<aRequestTypes.length; i++){
		try{
			request = aRequestTypes[i](); //This statement tries to create the request object
			break;
		}catch(e){
			continue;
		}
	}
	
	this.connect = function(sURL, sMethod, sParams, fProcessRequest, sResponseType){
		sResponseType = (sResponseType==null || sResponseType==undefined || sResponseType=="" && (sResponseType!="text" || sResponseType!="xml"))?"text":sResponseType;
		
		if(request!=null){
			bComplete = false;
			sMethod = sMethod.toUpperCase(); //NOTE: This can only be "GET" or "POST"
			if(sMethod=="GET" || sMethod=="POST"){
				try{
					request.onreadystatechange = function(){ //This will call the function that processes the request with the request object once the request returns a valid completion code/status
							if(request.readyState==4 && request.status==200 && !bComplete){
								bComplete = true;
								var oReturn;
								switch(sResponseType){
									case "xml": //this will force the request to be returned as an XML object
										oReturn = getXMLFromRequest(request);
										break;
									default: //everything else is returned to the processing function as a string
										oReturn = request["responseText"];
										break;
								}
								fProcessRequest(oReturn);
							}
						};
					
					if(sMethod=="GET"){
						sURL += "?" + sParams;
						sParams = "";
					}
					
					request.open(sMethod, sURL, true);
					
					if(sMethod=="POST"){
						request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
						request.setRequestHeader("content-length", sParams.length);
						request.setRequestHeader("connection", "close");
					}
					request.send(sParams);
				}catch(e){
					return false;
				}
				return true;
			}
		}
	};
	
	//This function returns an XML DOM object from a request object. It forces the data to be in the XML DOM format because sometimes the headers aren't passed back properly, so the response doesn't default to xml.
	function getXMLFromRequest(request){
		//this is the responseText from the request object
		var responseDoc = request["responseText"];
		
		//this is the xml document that gets created by the function
		xmlDoc = null;
		
		//this is an array that holds a bunch of functions that try to parse an XML document from the request objects responseText
		aDOMTypes = [function(response){var document = new ActiveXObject("Microsoft.XMLDOM"); document.async = "false"; document.loadXML(response); return document;}, function(response){var parser = new DOMParser(); return parser.parseFromString(response,"text/xml");}];
		
		//This ensures that there is something that can be parsed in the responseText
		if(responseDoc!=null && responseDoc!=undefined && responseDoc!=""){
			//the following section tries to parse the responseText using the functions in the aDOMTypes array...
			for(var i=0; i<aDOMTypes.length; i++){
				try{
					xmlDoc = aDOMTypes[i](responseDoc);
					break;
				}catch(e){
					continue;
				}
			}
		}
		
		return xmlDoc;
	}
	
	return this; //this returns the request object...
}
