/* ©2005 Gregory Wild-Smith (www.twilightuniverse.com) */

function Ajax() {
    this.Version = '1.0 BETA 2';
	this.XmlHttp = null;

    this.ResetData = function() {
		this.HttpMethod = 'POST';
		this.HttpRequest = '';
		this.HttpQuery = '';
		this.HttpVars = new Object();
  		this.SeparatorQuery = '?';
		this.SeparatorArgs = '&';
  		this.TargetElementId = null;
		this.TargetElement = null;
		this.StatusCode = '';
		this.StatusText = '';
  	};

	this.ResetFunctions = function() {
  		this.OnLoading                  = function() {};
  		this.OnLoaded                   = function() {};
  		this.OnInteractive              = function() {};
  		this.OnCompletion               = function() {};
  		//this.OnError                    = function() {};
		//this.OnFail                     = function() {};
	};

	this.ResetAjax = function() {
		this.ResetFunctions();
		this.ResetData();
	};

	this.StartAjax = function() {
		try {
			this.XmlHttp = new ActiveXObject("Msxml2.XmlHttp");
		} catch (e1) {
			try {
				this.XmlHttp = new ActiveXObject("Microsoft.XmlHttp");
			} catch (e2) {
				this.XmlHttp = null;
			}
		}
		if (! this.XmlHttp) {
			if (typeof XmlHttpRequest != "undefined") {
				this.XmlHttp = new XmlHttpRequest();
			} else {
				this.AjaxFailed = true;
			}
		}
	};

	this.SetVar = function(name, value) {
		this.HttpVars[name] = Array(value, false);
	};

	this.SetHttpQuery = function() {
		// prevents caching of HttpQuery
		this.SetVar("AjaxRequestId", new Date().getTime());
		Temp = new Array();
		for (Key in this.HttpVars) {
            Temp[Temp.length] = Key + "=" + this.HttpVars[Key][0];
		}
        this.HttpQuery = Temp.join(this.SeparatorArgs);
        //alert(this.HttpQuery);
	}

	this.RunAjax = function(HttpQuery) {
		if (this.AjaxFailed) {
            alert('Error: ActiveAjax failed to load.');
		} else {
			this.SetHttpQuery();
			if (this.TargetElementId) {
                //alert(this.TargetElementId);
				this.TargetElement = document.getElementById(this.TargetElementId);
                //alert(this.TargetElement.id);
			}
			if (this.XmlHttp) {
				var self = this;
				if (this.HttpMethod == 'GET') {
					var Url = this.HttpRequest + this.SeparatorQuery + this.HttpQuery;
                    //alert(Url);
					this.XmlHttp.open(this.HttpMethod, Url, true);
				} else {
					this.XmlHttp.open(this.HttpMethod, this.HttpRequest, true);
					try {
						this.XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.XmlHttp.onreadystatechange = function() {
					switch (self.XmlHttp.readyState) {
            		case 1:
            			self.OnLoading();
            			break;
            		case 2:
            			self.OnLoaded();
            			break;
            		case 3:
            			self.OnInteractive();
            			break;
            		case 4:
                        //alert('OK');
            			self.Response = self.XmlHttp.responseText;
            			self.ResponseXML = self.XmlHttp.responseXML;
            			self.StatusCode = self.XmlHttp.status;
            			self.StatusText = self.XmlHttp.statusText;

            			if (self.TargetElement) {
                            //alert(self.Response);
            				elemNodeName = self.TargetElement.nodeName;
            				elemNodeName.toLowerCase();
            				if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
                                self.TargetElement.value = self.Response;
            				} else {
                                self.TargetElement.innerHTML = self.Response;
                                //alert(self.TargetElement.id);
            				}
            			}
            			if (self.StatusCode == "200") {
            				self.OnCompletion();
            			} else {
            				//self.OnError();
                            alert('ActiveAjax Error: "' + self.StatusText + '".');
            			}
            			self.HttpQuery = "";
            			break;
					}
				};
				this.XmlHttp.send(this.HttpQuery);
			}
		}
	};
	this.ResetAjax();
	this.StartAjax();
}
