// adgaAJAX : Written by Bryan Lenihan
// Date Written: 04/19/2006
//
//  Purpose:
//    The purpose of this class is to create a generic methodology for the 
//    calling of xmlHttpRequest().  This class will also be used to make
//    the object suitable for the web application. 
//

//  Revision:
//    04-19-2006   Bryan Lenihan        Version 1.0

// constructor:  
//    Parameter: 
//				url =>  The url that will be called when all the setup is completed.
//				destination => The DOM Element to insert the results.  If not desired, then set to undefined.
//				async =>  Set to true to run asynchronously, Set to False to run Synchronously.  
function adgaAJAX(url, destination, async) 
{
		this._utilAJAX = new utilAJAX();
		if (url != undefined) 
		{
			this._url = url;
		}
	
		if (destination != undefined) 
		{
			this._destination = destination;
		}
	
		if (async != undefined)
		{
			this._async = async;
		}
		
		
} // End adgaAJAX

adgaAJAX.prototype._url = undefined;				// Private variable to store the URL
adgaAJAX.prototype._request = undefined;			// Private variable to store the _request information
adgaAJAX.prototype._destination = undefined;		// Private variable to store the Element destination
adgaAJAX.prototype._async = undefined;				// Private variable to store the async command
adgaAJAX.prototype._results = undefined;			// Private variable to store the results
adgaAJAX.prototype._utilAJAX = undefined;
//  Public API calls

//  adgaAJAX:load
//		Function actually performs the request and loads the results
adgaAJAX.prototype.load = function() 
{
    
	if (this._request) {this._request.abort();}
	this._request = this._utilAJAX.getXMLHTTPRequest();
	
	var _this = this;
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._generateDataUrl(), this._async);
	this._request.send(null);	
}

adgaAJAX.prototype.cancel = function()
{
    if (this._request!=null){this._request.abort();}
}

// adgaAJAX:resultValue
//      When the process has completed, allows the pulling of the results. 
adgaAJAX.prototype.resultValue = function()
{
	return this._results;
}

// Private API Calls - internal use

// adgaAJAX:_generateDataUrl
//   Formats the URL to a specific URL and includes a random
//   generator to handle the IE caching issues. 
adgaAJAX.prototype._generateDataUrl = function()
{
	return this._url + this._utilAJAX.randomKeyTag();
}

// adgaAJAX:_render
//    If a DOM destination is provided, the results will be inserted.
adgaAJAX.prototype._render = function(output)
{
	if (this._destination != undefined)
	{
		document.getElementById(this._destination).innerHTML = output;
	}
}

// adgaAJAX:_onData
//		The onreadystatechange function to check the status of the request
//      and to send the results to the proper functions. 
adgaAJAX.prototype._onData = function()
{
	if (this._request.readyState == 4)
	{
		if (this._request.status == "200")
		{
			this._results = this._request.responseText;
			this._render(this._request.responseText);
			if (this.onDraw != undefined) 
			{
				this.onDraw();
			}
		}
		else
		{
			if (this.onError != undefined)
			{
				this.onError({status:this._request.status, statusText:this._request.statusText});
			}
		}
		
		// Cleanup the request
		delete this._request;
	} 
}

