// Author : Chris Tolley
// Date   : 12/13/2005
// Desc   : ajaxRequest.js contains the class definition for 
// 			a class to encapuslate the functionality of an xmlHttpRequest object

/*  Example:
// A function to handle state changes of the request object
function processReqChange( req )
{
	alert( req.responseText );
}// End if

// Create and send a post request
function doAjax()
{	
	var req = new ajaxRequest;
	req.setCallBackFunc( function(){ processReqChange( req ); } );
	
	req.doPost( "http://dev.boattraderonline.com/ajaxtest.php", "queryName=getCat&realmId=6" );
}// End function doAjax
*/

// The constructor for the ajaxRequest object.
function ajaxRequest()
{
	// Create the actual xmlHttpRequest object
	this.req = false;
	
	// Branch for native XMLHttpRequest object
	if( window.XMLHttpRequest )
	{
		try
		{
			this.req = new XMLHttpRequest();
		}// End try
		catch( e )
		{
			this.req = false;
		}// End catch
	// branch for IE/Windows ActiveX version
	} 
	else if( window.ActiveXObject )
	{
		try
		{
			this.req = new ActiveXObject( "Msxml2.XMLHTTP" );
		}// End try
		catch( e )
		{
			try
			{
				this.req = new ActiveXObject( "Microsoft.XMLHTTP" );
			}// End try
			catch( e )
			{
				this.req = false;
			}// End catch
		}
	}// End else if
	
	// Create a named instance of this object
	var self = this;
	
	// Sets the function to call when the request returns from the server
	this.setCallBackFunc = function( f )
	{
	   self.callBack = f;
	}// End ajaxRequest::setCallBackFunc
	
	// The function that is called when the request returns.  This function also
	// calls the function that the user set thru setCallBackFunc
	this.processReqChange = function()
	{
		// If req's state is "loaded"
		if( self.req.readyState == 4 )
		{	    
			// If the request's status is "OK"
			if( self.req.status == 200 )
			{
				// Get the response and call the call back function
				self.responseText = self.req.responseText;
				self.responseXML  = self.req.responseXML;

				// If there is a callback function, call it.
				if( self.callBack )
					self.callBack();
			}// End if
			else
			{
				alert( "There was a problem retrieving the XML data:\n" +
					   self.req.status + ': ' + self.req.statusText );
			}// End else
		}// End if
	}// End ajaxRequest::processReqChange
	
	this.req.onreadystatechange = this.processReqChange;
	
	// Sets the request headers
	this.setRequestHeader = function( type, data )
	{
		self.req.setRequestHeader( type, data );
	}// End ajaxRequest::setRequestHeader
	
	// Opens the request
	this.open = function( method, address, asynch )
	{
		self.req.open( method, address, asynch );
	}// End ajaxRequest::open

	// Sends this request along with the data
	this.send = function( data )
	{
		self.req.send( data );
	}// End ajaxRequest::send
	
	// Does a post request to url and sends data
	this.doPost = function( url, data, bASynch )
	{
		if( bASynch == null )
			bASynch = true;

		var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

		self.req.open( "POST", url, bASynch );
		self.req.setRequestHeader( "Content-Type", contentType );
		self.req.send( data );
	}// End ajaxRequest::doPost
	
	// Terminates the current request, if there is one
	this.abort = function()
	{
		self.req.abort();
	}// End self::abort
	
	// Debugs the results from the ajax request object
	this.debug = function()
	{
		// Create a div tag to hold the response text and 
		// append it to the body of the document
		var debugDiv = document.createElement( 'div' );
		debugDiv.innerHTML = self.req.responseText;
		debugDiv.style.border = 'thin solid #000000';
		document.body.appendChild( debugDiv );
	}// End ajaxRequest::debug
}// End ajaxRequest::Constructor

