﻿// JScript File
    
function jx(addr) {
	this.xmlhttp = null;
	this.address = addr;
	this.queryString = "";
	this.responseStatus = new Array(2);
	
	this.resetParams = function() {
		this.method = "POST";
  		this.querySymbol = "?";
		this.querySeparator = "&";
				
		this.encodeQuery = true;
  		this.execute = false;
  	};

	this.resetEvents = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetEvents();
		this.resetParams();
	};

	this.create = function() {
	    if (window.XMLHttpRequest) { // Mozilla, Safari,...
            this.xmlhttp = new XMLHttpRequest();
            if (this.xmlhttp.overrideMimeType) {
                this.xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0','Microsoft.XMLDOM', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++){
                try{
                    this.xmlhttp = new ActiveXObject(MSXML[n]);
                    break;
                }
                catch(e){
                }
            }
        }    
	};

	this.generateQuery = function(queries, arguments) {
	    this.queryString = "";
	    if (!queries)
	        return;
	    if (!queries.length)
	    {	
	        this.queryString = queries;
	    }
	    else if (queries.length > 0)
	    {
	        this.queryString = queries[0];
	        for (i = 1; i < queries.length; i++)
	            this.queryString += this.querySeparator + queries[i];
	    }
	    for (i = 1; i < arguments.length; i++)
	    {
	        this.queryString += this.querySeparator + arguments[i];
	    }
	}

	this.executeScript = function() {
		eval(this.response);
	}

	this.invoke = function(queries) {
	    if (this.failed) {
			this.onFail();
		} else {
		    this.generateQuery(queries, arguments);
			if (this.xmlhttp) {
			    var self = this;
			    
				if (this.method == "GET") {
					var url = this.address;
					if (this.queryString.length > 0)
					    url += this.querySymbol + this.queryString;
					this.xmlhttp.open(this.method, url, true);
				} else {
					this.xmlhttp.open(this.method, this.address, true);					
				}
				
				try {
					this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					this.xmlhttp.setRequestHeader("If-Modified-Since", "0");
				} 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.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.xmlhttp.status == 200) {
							    self.onCompletion();
							} else {
								self.onError();
							}
							break;
					}
				};
				this.xmlhttp.send(this.queryString);
			}
		}
	};   
	this.reset();
	this.create();
}

