/** Be sure to set the proper request variable */

var request;

AJAX = function() {
	
	// this.request;
	
	/** This is our compatibility checker for XML request
 	* 
 	* This is also a priveledged method, meaning it can be called from
	* outside this namespace and can read the private classes and 
	* methods of this namespace.
	* 
 	* Wrapper function for constructing a request object.
 	* Parameters:
 	*	reqType: the HTTP request type, suc as GET or POST.
 	*	url: the URL of the server program.
 	*	asynch: wheter to send the request ashnchronously or not.
 	**/ 	
	this.httpRequest = function(reqType,url,asynch,respHandle) {
		///Mozilla-based browsers
		if(window.XMLHttpRequest){
			request = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			/// IE browsers
			request = new ActiveXObject("Msxml2.XMLHTTP");
			if (! this.request) {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		///the request could still be null if neither ActiveXObject initialization succeeded
		if(request) {
			if(reqType.toLowerCase() != "post"){
				initReq(reqType,url,asynch,respHandle);
			} else {
				///the POSTed data
				var args = arguments[4];
				if(args != null && args.length > 0){
					initReq(reqType,url,asynch,respHandle,args);
				}
			}
		} else {
			alert("Your browser does not permit the use of all of this Website's features");
		}
	}
	
	/** Initialize a request object that is already constructed 
	 * This is a private function and can only be called from within
	 * this namespace
	 **/ 
	function initReq(reqType,url,bool,respHandle,args) {
		try {
			/// Specify the function that will handle the HTTP reponse 
			request.onreadystatechange=respHandle;
			request.open(reqType,url,bool);
			
			if(reqType.toLowerCase() != "post"){
				request.send(null);
			} else {
				request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				request.send(args);
			}
		} catch (errv) {
			alert("The application cannot contact the server at the moment. Please try again in a view seconds."+"Error detail: "+errv.message);
		}
	}

}

/** use this function to active an AJAX call with the AJAX class
 * _TYPE: can be "GET" or "POST", "GET" is currently preferable
 * _URL: The backend file or site you are calling
 * respHandle: name of the function that will process the response.
 * 				DO NOT include arguments just the name.
 *              An example function is listed below as handlResponse
 **/
function callAjax(_TYPE,_URL, respHandle) {
		var myAjaxRequest = new AJAX;
		myAjaxRequest.httpRequest(_TYPE,_URL,true,respHandle);
}

/** sample event handler for XMLHttpRequest */
handleResponse=function(){
	if(request.readyState == 4){
		if(request.status == 200){
			xmlContainer = request.responseXML; /* or = request.responseText */
			
			/* your code goes here */
			
		} else {
			alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
		}
	}
}
