/* ©2005 Gregory Wild-Smith (www.twilightuniverse.com) */

function Ajax() {
    this.Version = '1.0.0.2500';
    this.Release = 'BETA';
	this.XmlHttp = null;
    this.Response = 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:
            			self.Response = self.XmlHttp.responseText;
            			self.ResponseXML = self.XmlHttp.responseXML;
            			self.StatusCode = self.XmlHttp.status;
            			self.StatusText = self.XmlHttp.statusText;
                        //alert('From Ajax: ' + self.Response);
                        //return self.Response;
            			if (self.TargetElement) {
                            switch (self.TargetElement.nodeName) {
                            case 'INPUT':
                            case 'OPTION':
                            case 'TEXTAREA':
                                self.TargetElement.value = self.Response;
                                break;
                            case 'SELECT':
                                var l = self.TargetElement.length;
                                var i;
                                for (i = 0; i < l; i++) {
                                    self.TargetElement.remove(self.TargetElement.length - 1);
                                }
                                //alert(self.Response);
                                var a = new Args(self.Response);
                                for (e in a.Args) {
                                    //alert(e);
                                    var o = document.createElement('OPTION');
                                    o.text = a.Args[e].value;
                                    o.value = e;
                                    self.TargetElement.add(o);
                                }
                                break;
                            default:
                                break;
            				}
            			}
            			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();
}