/**
 *
 * "The Essentials"
 * a collection by Theta Interactive Incorporated
 *
 * The "essentials" javascript is a collection of javascript classes and utility functions
 * that can help do some cool stuff.
 *
 * @author: Michael Sablone
 * @version: 1.1
 */
if(typeof thetainteractive == "undefined") var thetainteractive = new Object();
if(typeof thetainteractive.classes == "undefined") thetainteractive.classes = new Object();
if(typeof thetainteractive.classes.AjaxConnection == "undefined") thetainteractive.classes.AjaxConnection = function () {
	/**
	 *
	 * Constructor
	 */
	var self = this;
	var variables = new Object();
	var XMLHTTP;
	/**
	 *
	 * Private AjaxConnection.getXMLHTTPObject
	 * return a new xmlhttp object for this browser
	 *
	 * @return XMLHTTP object
	 */
	function getXMLHTTPObject () {
		var result;
		try { result = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) {
			try { result = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) { result = null; }
		}
		if(!result && typeof XMLHttpRequest!="undefined") result = new XMLHttpRequest();
		if (result) return result;
	}
	/**
	 *
	 * Private AjaxConnection.getNameValuePairs
	 *
	 * @return String: a concatenated, escaped string of name-value pairs
	 */
	function getNameValuePairs () {
		var result = "";
		for (var prop in variables) result += "&" + escape(prop) + "=" + escape(variables[prop]);
		return result.substring(1);
	}
	/**
	 *
	 * Public AjaxConnection.sendAndLoad
	 *
	 * @param URI:String
	 *	the url to load in as text/xml
	 * @return String: a concatenated, escaped string of name-value pairs
	 */
	this.sendAndLoad = function (URI, method) {
		if (method==undefined) method = "POST";
		XMLHTTP = getXMLHTTPObject();
		XMLHTTP.open(method, URI, true);
		XMLHTTP.onreadystatechange = function() {
			if (XMLHTTP.readyState == 4) self.onLoad();
		}
		XMLHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLHTTP.send(getNameValuePairs());
	}
	/**
	 *
	 * Public AjaxConnection.variables
	 *
	 * @param name:String
	 *	the variable name to send and store
	 * @param value:String
	 *	the variable value to send and store
	 */
	this.setVariable = function (name, value) {
		variables[name] = value;
	}
	/**
	 *
	 * Public AjaxConnection.getText
	 *
	 * @return String
	 *	the raw text response
	 */
	this.getText = function () {
		return XMLHTTP.responseText;
	}
	/**
	 *
	 * Public AjaxConnection.getText
	 *
	 * @return Object
	 *	the parsed xml object
	 */
	this.getXML = function () {
		return XMLHTTP.responseXML;
	}
}
