var XHR = {
	
	/**
	 * Create new Ajax request instance
	 * 
	 * @param	String		url				The url for the request
	 * @param	Object		settings		Object literal containing options for request as follows:
	 *										method		String		POST/GET
	 *										parameters	String		Query string
	 *										async		Boolean		Whether request is to be sent asynchronously (default is true)
	 *										onComplete	Function	Callback function on success
	 *										onError		Function	Callback function on error
	 **/
	Request: function(url, settings) {
		
		this._req = this._getTransport(); // get correct form of XMLHttpRequest object

		this.url = url;
		
		this.method = (settings.method == null) ? "GET" : settings.method;
		this.parameters = (settings.parameters == null) ? "" : settings.parameters;
		this.async = (settings.asyn == null) ? true : settings.asyn;
		this.onComplete = (settings.onComplete == null) ? this.nullFunction : settings.onComplete;
		this.onError = (settings.onError == null) ? this.nullFunction : settings.onError;

		this._req.onreadystatechange = this._getOnReadyStateChangeHandler(this.onComplete, this.onError);

		this._req.open(this.method, this.url, this.async);

		if (this.method == "POST") {
			this._req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}

	}
}

XHR.Request.prototype = {

	// Get correct form of XMLHttpRequest, depending on browser support
	_getTransport: function() {
		req = false;
		// Create XmlHttpRequest object in non-MS browsers
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try	{
				// Try to create XMLHttpRequest in later versions of IE
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e1) {
				try	{
					// Try older version
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e2) {
					// Unable to create an XMLHttpRequest object
					req = false;
				}
			} // end try one
		} // end if

		return req;

	}, // end _getTransport

	// return a ready state change handler function for the request
	_getOnReadyStateChangeHandler: function(onComplete, onError) {
		// This reference for closure
		_this = this;
		
		return function() {
			
			if (_this._req.readyState == 4) {
   
				// Check that we received a successful response from the server
				if (_this._req.status == undefined || _this._req.status == 0 || (_this._req.status >= 200 && _this._req.status < 400)) {
					
					// Pass the response to the handler function.
					onComplete(_this);
					 
				} else {

					// An HTTP problem has occurred, fire off the error function if it exists
					if (onError != null && typeof onError == "function") {
						onError(_this);
					}

				}

			} // end if

		}; // end returned function

	}, // end _getOnReadyStateChangeHandler

	// Send the request
	send: function() {
		this._req.send(this.parameters);
	},

	// Abort the request at any time
	abort: function() {
		this._req.abort();
	},

	// Get the response text
	getResponseText: function() {
		return this._req.responseText;
	},

	// Get the responseXML
	getResponseXML: function() {
		return this._req.responseXML;
	},

	// Get the status
	getStatus: function() {
		return this._req.status;
	}, 

	// Get the status text
	getStatusText: function() {
		return this._req.statusTest;
	},

	// Null function for empty handler parameters
	nullFunction: function() {}

} // end XHR.Request.prototype






