/**
 * @name	-	AjaxRequest
 * @desc	-	will be used to create and process all XHR requests
 * @version	-	1.1.0 - 
 
 				updated on 2-24-2010 to be more objected based.  no longer have to provide function variables during intialization
 				updated 9-9-2010 to include the 'use_post' option to allow ajax requests to utilize the POST method. (default = GET)
 				
 				updated 9-11-2010
 				
 				DROPPED:
 				loading_object
 				loading_msg
 				error_object
 				
 				-these parameters are no longer needed, will be handled outside the scope of this class
 */

function AjaxRequest(){
	this.url;			//url of request
	//this.loading_object;
	//this.loading_msg;	
	this.callback;
	this.error_callback;
	//this.error_object	=	null;
	this.uid;
	this.fid;
	this.options_array	=	null;
	this.use_post		=	false;
	this.post_data		=	null;
	
	
	this.ProcessRequest			=	function(){
	
			
		pageRequest = false;
		if (window.XMLHttpRequest){
		
			pageRequest = new XMLHttpRequest();
		
		}
		else if (window.ActiveXObject){
		
			pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		else {
		return false;
		}
		
		
		//declare global variables for dynamic function from object properties
		var url					=	this.url;
		//var loading_object		=	this.loading_object;
		//var loading_msg			=	this.loading_msg;	
		var callback			=	this.callback;
		var error_callback		=	this.error_callback;
		//var error_object		=	this.error_object;
		var options_array		=	this.options_array;
		
		var uid					=	this.uid;
		var fid					=	this.fid;

		pageRequest.onreadystatechange = function(){
		
				if (pageRequest.readyState == 4 && pageRequest.status == 200){
					
					var data			=	pageRequest.responseText;
					var xml				=	pageRequest.responseXML;
					
					
					
					callback(data,xml,uid,fid,options_array);
					
		
				}//end successful response
				
				else if(pageRequest.readyState == 4 && pageRequest.status !== 200){
					
					//alert(pageRequest.status);
				
					var code				=	pageRequest.status;
					var msg					=	pageRequest.statusText;

					error_callback(code,msg);	
					
					
				}//end error handler
				
				else if(pageRequest.readyState == 0 || pageRequest.readyState == 1 || pageRequest.readyState == 2 || pageRequest.readyState == 3){
					
					//basically do nothing until readystate is complete
				
				}//end loading
				
				else{
					var code				=	'000';
					var msg					=	'INTERNAL JS ERROR - SERVER RETURNED UNKNOWN READYSTATE CODE.';
					
					error_callback(code,msg);
					

				}
		}//end function
		

		//determine if POST or GET should be used
		
		if(this.use_post){
			
			//var params		=	'post_data='+this.post_data;
			var params		=	this.post_data;
		
			pageRequest.open('POST',this.url+"&ms="+ new Date().getTime(),true); //ms=timestamp to prevent url caching in some browsers(ie)
			
			pageRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			pageRequest.setRequestHeader("Content-length", params.length);
			pageRequest.setRequestHeader("Connection", "close");
			
			pageRequest.send(params);
		}//end if
		else{	
			pageRequest.open('GET',this.url+"&ms="+ new Date().getTime(),true); //ms=timestamp to prevent url caching in some browsers(ie)
			pageRequest.send(null);
		}//end else
					

	}//function to process the XHR request
}//end AjaxRequest


