/*
 * Constructs an instance of a wrapper class for the XMLHttpRequest object.
 *
 * param sUrl String The URL of the requested resource.
 *
 * param fCallback Function The function to be called upon successful
 * completion of the request.
*/

var HttpRequest = function(sUrl, fCallback) {
	this.sUrl = sUrl;
	this.fResponseCallback = fCallback;
	
	var me = this;
	
	// TODO: Replace these error messages with something a little more friendly.
	this.stateChange = function() {
		if (me.oRequest.readyState == 4) {
			try {
				switch (me.oRequest.status.toString().charAt(0)) {
					case '2':
						if (me.fResponseCallback) {
							me.fResponseCallback(me);
						}
						break;
					case '3':
						alert('Error - Redirection detected (' + me.oRequest.status  + ')');
						break;
					case '4':
						alert('Error - Client request (' + me.oRequest.status + ')');
						break;
					case '5':
						alert('Error - Server error (' + me.oRequest.status + ')');
						break;
				}
			} catch (e) {
				// Swallow this exception
			}
		}
	}
}

HttpRequest.prototype.sUrl = null;
HttpRequest.prototype.fResponseCallback = null;
HttpRequest.prototype.oRequest = null;

/*
 * Executes an HTTP request.
 *
 * param oData String|Array (optional) If specified, the data to be
 * sent to the specified URL via GET or POST. If value is a String, it 
 * should be a properly encoded query string with '&' characters separating 
 * each variable. If the value is an Array, it should be an array of 
 * name-value pairs in the form of 'variablename=variablevalue'.
 *
 * param sMethod String Either "post" or "get". Is case-insensitive.
*/
HttpRequest.prototype.execute = function(oData, sMethod) {
	
	// Initialize the request object
	
	// Native XMLHttpRequest object
	if (window.XMLHttpRequest) {
	    this.oRequest = new XMLHttpRequest();
	// IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
	    this.oRequest = new ActiveXObject('Microsoft.XMLHTTP');
    }
	
	if (this.oRequest) {
	
		this.oRequest.onreadystatechange = this.stateChange;
		
		var sData = null;
		var sRequestType = sMethod.toUpperCase().trim();
		var sRequestUrl = this.sUrl;
		var sRequestData = null;
		
		var bIsPost = (sRequestType == 'POST');
		
		if (typeof(oData) == 'object') {
			// Array passed; join elements
			sData = oData.join('&');
		} else {
			sData = oData;
		}
		
		if (bIsPost) {
			sRequestData = sData;
		} else {
			if (sData != null) {
				sRequestUrl += '?' + sData;
			}
		}
		
		// Open the request
		this.oRequest.open(sRequestType, sRequestUrl);
		
		if (bIsPost) {
			this.oRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
			this.oRequest.setRequestHeader('Content-Length', sRequestData.length);
		}
		
		// Should have a way of identifying the client and returning whatever data is appropriate
		// to the function call. This could be done in multiple ways -- appending a value to the
		// query string, setting request headers, etc. This should be done to ensure fallback
		// compatibility with browsers whose XMLHttpRequest objects are broken or non-existent --
		// i.e. the page to which the data posts should not only expect to receive data from this
		// type of setup.
		
		// Send the request
	    this.oRequest.send(sRequestData);
		return true;
	}
	
	return false;
}

/*
 * Returns the response from the server after a request has been made.
 *
 * param bAsXml Boolean If 'true', will return an instance of an XMLDocument,
 * provided the returned data is a proper XML document. If 'false', will return
 * the text of the response.
*/

HttpRequest.prototype.getResponse = function(bAsXML) {
	if (bAsXML) {
		return this.oRequest.responseXML;
	} else {
		return this.oRequest.responseText.trim();
	}
}

/*
 * Saves form data using the HttpRequest class.
 *
 * param oForm Object The form object to be saved.
 *
 * param fCallback Function The function that should be called upon successful
 * completion of the request. Note 'successful completion' does *not* mean
 * that the data was necessarily saved; instead, it means the requested script
 * was found and was run to completion.
*/

/*

function saveForm(oForm, fCallback) {
	// Gets an associative array of all set form values 
	var aSubmitData = collectValues(oForm);
	
	// Get the submission information from the form
	var sMethod = oForm.method;
	var sUrl = oForm.action;
	
	// Create and execute the HTTP request
	var oRequest = new HttpRequest(sUrl, fCallback);
	oRequest.execute(aSubmitData, sMethod);
	
	// Do not trigger an actual submission of the form, since it has been
	// done already in the HTTP request object.
	
	return false;
}

*/