/*
 * xhttp.js
 *
 * Content:	Generalized HTTPRequestObject function. The core object functionality
 *			contained in this file has been tested against Firefox, IE6, IE7, 
 *			and Safari. If you use this and something isn't working, it's probably
 *			your response handler.
 *
 * Notes:	I should probably write a small set of instructions for using this.
 *			Also, decide on where the @#$!@#* error handling should go.
 *
 * Status:	UP TO DATE
 *
 *
 */

function xobj(url) {
	// Error handling
	this.failedAlert 		= 'Your browser is not currently supported.\n';
	this.failed				= false;

	// Method defaults
	this.method 			= 'POST';
	this.encodeURIString 	= true;

	// Browser flag
	this.requestType		= '';

	// Defaults
	this.requestFile 		= url;
	this.URLString 			= '';
	this.callBackArg		= '';
	this.oncompletion 		= function() { };

	this.createXObj = function() {
		// Standard Object
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined") {
            this.xmlhttp = new XMLHttpRequest();
		}

		// IE's Object
		else if(window.ActiveXObject) {
			// No, this isn't all of the available MS request objects. Adding the
			// others sent ALL versions of IE into hysterics.
			var MSXMLArray = new Array(
				'MSXML2.XMLHTTP',
				'Microsoft.XMLHTTP');
			for(i in MSXMLArray) {
				try {
					this.xmlhttp = new ActiveXObject(MSXMLArray[i]);
					break;
				}
				catch(e) {
					alert('Unable to create an MS XMLHTTP instance!');
				}
			}
			this.requestType = 'ie';
		}

		// No object. What fscking browser are you using?
		else {
			this.failed = true;
		}
	};

	// To be or not to be. Should error handling be built into the object, the handler,
	// or an entirely seperate function?
	this.errorReport = function(status) {
		alert("I'll do more later.");
	}

/*
	// Component of encodeURLString
	this.encVar = function(name, value) {
		var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
		return varString;
	}

	// Encode POST variables
	this.encodeURLString = function(string) {
		varArray = string.split('&');
		for(i = 0; i < varArray.length; i++) {
		urlVars = varArray[i].split('=');
			if(urlVars[0].indexOf('amp;') != -1) {
				urlVars[0] = urlVars[0].substring(4);
			}
			varArray[i] = this.encVar(urlVars[0], urlVars[1]);
		}
		return varArray.join('&');
	}
*/

	// Object execution
	this.runXObj = function() {
		this.responseStatus = new Array(2);
		if(this.failed && this.failedAlert) {
			alert(this.failedAlert);
		}
		else {
			if(this.xmlhttp) {
				var self = this;
				if(this.method == "GET") {
					var totalurlstring = this.requestFile + "?" + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
					this.xmlhttp.send();
				} else {
					this.xmlhttp.open(this.method, (this.requestFile ? this.requestFile : '?'), true);
					try {
						this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					} catch(e) {
						alert('There was a problem setting the header type.'); 
					}
					this.xmlhttp.send(this.URLString);
				}
				this.xmlhttp.onreadystatechange = function parseResponse() {
					if(self.xmlhttp.readyState == 4) {
						self.responseText = self.xmlhttp.responseText;
						self.responseXML = self.xmlhttp.responseXML;
						self.status = self.xmlhttp.status;
						self.statusText = self.xmlhttp.statusText;

						// Execute the handler if there is one.
						self.oncompletion();
					}
				};
			}
		}
	};
	this.createXObj();
}


