/*********************************************************************************************
File: 	Ajax.js
Author: Kris Felscher (kpf)
Date:	04/24/2007
Description:
	This script holds functions and utilities used for AJAX connections.
	
Change Log:
USER	PROJECT		VERSION		DATE		DESCRIPTION
----------------------------------------------------------------------------------------------
kpf		DYLAN			1.0			04/24/2007	Initial Version

To Do:

Bugs :
#		STATUS		BROWSER		DATE		DESCRIPTION
----------------------------------------------------------------------------------------------
1		UNKNOWN		IE7 		04/26/2007	I was getting a stack overflow in the OnData event, 
											but it seems to have gone away. The error looked 
											like it was happening because IE7 was calling the 
											OnData function	repeatedly.

*********************************************************************************************/

function Ajax() {}

// Declare Properties ************************************************************************
Ajax.prototype._request;				//request object
Ajax.prototype._requestURL;				//the url to access when performing a request
Ajax.prototype._requestMethod;			//the method to use when performing a request
Ajax.prototype._responseData;			//response data from an Ajax query
Ajax.prototype._overrideMimeType;		//the format of the response data (true=XML, false=text)
Ajax.prototype._processor;				//the function responsible for processing the ajax data
Ajax.prototype._errorHandler;			//the caller function responsible for processing errors
Ajax.prototype._suppressErrors = false;	//supprsees errors when no _errorHandler is specified
Ajax.prototype._parser;					//the parser used by the XML connector
//Declare Methods ****************************************************************************

/* Check Parameters *******************************
	Check the required parameters to ensure that
	everything is set correctly before continuing
**************************************************/
Ajax.prototype.CheckParams = function() {
	if (!this._requestURL) {return false;}
	if (!this._processor) {return false;}
	return true;
}

/* Throw An Error *********************************
	A general error handler, uses the caller's
	error handler if set, otherwise throws an alert
	box (unles suppressed).
**************************************************/
Ajax.prototype.ThrowException = function(e) {
	if (this._errorHandler) {
		this._errorHandler(e);
	} else {
		if (!this._suppressErrors) {
			alert(e);	
		}
	}
}

/* Make Connector ********************************
	This method creates the response object based
	on the browser performing the request.
**************************************************/
Ajax.prototype.MakeConnector = function() {
	if (this.CheckParams()) {
		//non-IE Browsers
		if (window.ActiveXObject) {
			//IE Browsers
			try {
				this._request = new ActiveXObject("Msxml2.XMLHTTP");
				this._parser = "ActiveX";
				return true;
			} catch (e) {
				try {
					this._request = new ActiveXObject("Microsoft.XMLHTTP");
				this._parser = "ActiveX";
					return true;
				} catch(e) {
					this._request = false;
					this.ThrowException("Ajax->MakeConnector: Unable to create ActiveX Object");
					return false;
				}
			}
		} else if (window.XMLHttpRequest){
			try {
				this._request = new XMLHttpRequest();
				this._parser = "XMLHttp";
				if (this._request.overrideMimeType && this._overrideMimeType) {
 			           this._request.overrideMimeType('text/xml');
         		}
				return true;
			} catch (e) {
				this._request = false;	
				this.ThrowException("Ajax->MakeConnector: Unable to create XMLHttpRequest Object [" + e + "]");
				return false;
			}
		} else {
			//Unable to create Connection
			this._request = false;
			this.ThrowException("Ajax->MakeConnector: No HttpRequest Classes Available.");
			return false;
		}
	} else {
		this._request = false;
		this.ThrowException("Ajax->MakeConnector: Some Required Parameters are not set");
		return false;
	}
}

/* Connect *****************************************
	Connect to the specified url.
***************************************************/
Ajax.prototype.Connect = function() {
	if (this.MakeConnector()) {
		//use _this to maintain scope when the onReadyStateChange event is fired.
		var _this = this;
		if (this._request) {
			try {
				this._request.open(this._requestMethod,this._requestURL,true);
				this._request.onreadystatechange = function() {_this.OnData();}
				this._request.send(null);
				return true;
			} catch (e) {
				this.ThrowException("Ajax->Connect: Connection has died [" + e + "]");
				return false;			
			}
		} else {
			this.ThrowException("Ajax->Connect: Connection has died");
			return false;
		}	
	}
}

/* OnData Event ***********************************
	Retreives data and passes it to the processing
	function.
**************************************************/
Ajax.prototype.OnData = function() {
	switch(this._request.readyState) {
		case 0: break;
		case 1: break;
		case 2: break;
		case 3: break;
		case 4: 
			if (this._request.status == 200) {
				if (this._overrideMimeType) {
					//Mozilla
					if (this._request.overrideMimeType) {
						this._processor(this._request.responseXML)
					//IE
					} else {
						this._processor(this._request.responseXML)
					}
				} else {
					this._processor(this._request.responseText);
				}
				
			} else {
				this.ThrowException("Ajax->OnData: Invalid Status [" + this._request.status + " : " + this._request.statusText + "]");
			}
			break;
	}
}