// LGO AJAX libary July 2007

//////////////////////////////////////////////////////////////////////////////////////
// AJAX Class 
//////////////////////////////////////////////////////////////////////////////////////
function ajaxClass(){ 
	// define class vars
	var requestObject;
	var requestURL;
	var requestMethod;
	var freezeBrowser;
	var responseCompleted;
	var returnFormat;
	var xmlDoc;
	var callerScope;
	
	// define class methods
	this.generateRequest = generateRequest;
	this.setupObject = setupObject;
	this.setParameters = setParameters;
	this.sendRequest = sendRequest;
	this.requestListener = requestListener;
	
	//////////////////////////////////////////////////////////////////////////////////////
	function generateRequest(requestURL,requestMethod,freezeBrowser,responseCompleted,returnFormat,callerScope){ // manages the whole process
		
		this.requestURL = requestURL;
		this.requestMethod = requestMethod;
		this.freezeBrowser = freezeBrowser;
		this.returnFormat = returnFormat;
		this.responseCompleted = responseCompleted;
		this.callerScope = callerScope;

		// need to setup sucess
		
		this.setupObject();
		this.setParameters();
		
	}
	//////////////////////////////////////////////////////////////////////////////////////
	function setupObject(){ // returns a new http request object 
	
		// clear current object
		this.requestObject = null;
		
		// Make new object - good Browsers
		try {
			this.requestObject = new XMLHttpRequest();
		} 
		catch (tryMicroSoft){ // Make new object - MS Browsers
			try {
				this.requestObject = new ActiveXObject("Msxml2.XMLHttp");
			} 
			catch (otherMicrosoft){ // Make new object - MS Browsers
				try{
					this.requestObject = new ActiveXObject("Microsoft.XMLHttp");
				}
				catch(failed){ // Other older fails
					this.requestObject = null;	
				}
			} 
		
		}
		// If no object created report error
		if (this.requestObject == null){
			alert("Error creating request object - How old is your web browser?");
		}
		
	}
	//////////////////////////////////////////////////////////////////////////////////////
	function setParameters(){
	
		if (this.requestMethod == "GET"){ // if method GET add reqID to prevent URL Caching issues
				
				// check if we need to be appending using a ? or a &
				var additionJoiner = '?';
				var search_term = '?'; 
				var str = this.requestURL; 
		
				var url_check = str.indexOf( search_term ); 
				if ( url_check != -1 ) { 
					additionJoiner = '&';
				} 

				
				this.requestURL = this.requestURL + additionJoiner +"reqID=" + new Date().getTime();
		}
		
		// set the listener up - because we are in a class we have to call like this.
		var _this = this;
		this.requestObject.onreadystatechange = function(){
															_this.requestListener();
														   } 
		
		this.requestObject.open(this.requestMethod,this.requestURL,this.freezeBrowser); // open the request (does not send)
		
	}
	//////////////////////////////////////////////////////////////////////////////////////
	function sendRequest(data){
		this.requestObject.send(data);
	}
	//////////////////////////////////////////////////////////////////////////////////////
	function requestListener(){ // Example response function
	
		if (this.requestObject.readyState == 4){ // Check the ready state is finshed
			if (this.requestObject.status == 200){ // check the server gave OK status
				
				// Handle text or xml
				if (this.returnFormat == 'text'){
					var data = this.requestObject.responseText; // save the response text to a var
				}
				// xml output
				if (this.returnFormat == 'xml'){
					this.xmlDoc = this.requestObject.responseXML;
				}
				
				// call the method is set for completion
				this.callerScope[this.responseCompleted](this);
				
			}
			else { // incases of !200 staus alert the user
				alert("Server gave a request status of " + this.requestObject.status)
			}
		}
	}
	//////////////////////////////////////////////////////////////////////////////////////

}


