var ajax;
/**
*	Usage Example:
*	function gatherData() {
*		ajax = new ajax_object('index.cfm', response);
*		ajax.load();
*	}
*	function response() {
*		alert(ajax.responseText);
*	}
*/

/**
*	http://weblogs.macromedia.com/mesh/archives/2006/01/encapsulating_a.html
*/

/**
*	Constructor for the class.
*
*	@param {String} dataURL The path to the data that the class will load (REQUIRED)
*   @param {Function} func	The name of the function to call when data is received (OPTIONAL)
*
*	@constructor
*/
function ajax_object(dataURL, func) {
	this._dataURL = dataURL;
	if (func != undefined) {
		this._callbackFunc = func;
	} else {
		this._callbackFunc = null_func;
	}
}

//var to hold an instance of the XMLHTTPRequest object
ajax_object.prototype._request = undefined;

/**************** Public APIs **********************/


/**
*	Tells the class to load its data and render the results.
*/
ajax_object.prototype.load = function() {
	//get a new XMLHTTPRequest and store it in an instance var.
	this._request = this._getXMLHTTPRequest();
	
	//set the var so we can scope the callback
	var _this = this;
	
	//callback will be an anonymous function that calls back into our class
	//this allows the call back in which we handle the response (_onData())
	// to have the correct scope.
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._generateDataUrl(), true);
	this._request.send(null);

}
/***************Private Data Loading Handlers*******************/

//return the URL from which the data will be loaded
ajax_object.prototype._generateDataUrl = function()
{
	return this._dataURL;
}


//callback for when the data is loaded from the server
ajax_object.prototype._onData = function()
{
	if(this._request.readyState == 4)
	{
		if(this._request.status == "200")
		{
			this.responseText = this._request.responseText;
			this._callbackFunc();
		}
		else
		{	
			//check if an error callback handler has been defined
			if(this.onError != undefined)
			{
				//pass an object to the callback handler with info
				//about the error
				this.onError({status:this_request.status, 
						statusText:this._request.statusText});
			}
		}
		
		//clean up
		delete this._request;
	}
}

/***************Private Data Util Functions ********************/


//returns an XMLHTTPRequest instance (based on browser)
ajax_object.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}
	
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	return xmlHttp;
}

function null_func() { return; }