/**
 *
 * "AjaxConnection"
 * a simple class to create AJAX functionality
 *
 * @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);
	}
	/**
	 *
	 * Private AjaxConnection.parseXMLasObject
	 *
	 * @param node:XMLNode
	 *	an xml node to parse
	 *
	 * @return object: a dot-syntax object
	 */
	function parseXMLasObject (xml) {
		var o = new Object();
		for (var i=0; i<xml.childNodes.length; ++i) {
			var node = xml.childNodes[i];
			var name = node.nodeName;
			var hasChildren = node.hasChildNodes();
			var value = node.nodeValue;
			o._name = name;
			o._value = value;
			o.attributes = node.attributes;
			o[name] = arguments.callee(node);
		}
		return o;
	}
	/**
	 *
	 * Public AjaxConnection.load
	 *
	 * @param URI:String
	 *	the url to load in as text/xml
	 */
	this.load = function (URI) {
		XMLHTTP = getXMLHTTPObject();
		XMLHTTP.open("GET", URI, true);
		XMLHTTP.onreadystatechange = function() {
			if (XMLHTTP.readyState == 4) self.onLoad();
		}
		XMLHTTP.send(null);
	}
	/**
	 *
	 * 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) {
		var query = getNameValuePairs();
		if (method==undefined) method = "POST";
		if (method=="GET") {
			URI += "?" + query;
			query = null;
		}
		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(query);
	}
	/**
	 *
	 * Public AjaxConnection.setVariable
	 *
	 * @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.getXML
	 *
	 * @return Object
	 *	the parsed xml object
	 */
	this.getXML = function () {
		return XMLHTTP.responseXML;
	}
	/**
	 *
	 * Public AjaxConnection.getObjectXML
	 *
	 * @return Object
	 *	the parsed xml object
	 */
	this.getObjectXML = function () {
		return parseXMLasObject(this.getXML().firstChild);
	}
	/**
	 *
	 * Public AjaxConnection.onLoad
	 *	onLoad stub
	 */
	this.onLoad = function () {
	}
}