﻿/**************************************************************
/*Class: Jax
/*Author: Bobby Soares
/*Comments: This code is property of Bobby Soares
/**************************************************************/
var GLOBAL_AJAX_METHOD = -1;
Jax = function(uri, hostName){
    this.properties = new Object();
    this.properties.isSupported = false;
    this.properties.URI = uri;
    this.properties.hostName = hostName;
    this.properties.handlingRequest = false;
    this.resources = new Object();
	this.resources.updatePanel = '';
    this.resources.xmlHttp = null;
    if(GLOBAL_AJAX_METHOD>=0 && GLOBAL_AJAX_METHOD<4){
        this.properties.isSupported = true;
        //alert("reentrant");
        switch(GLOBAL_AJAX_METHOD){
            case 0:
                /*@cc_on @*/
                /*@if (@_jscript_version >= 5)
                this.resources.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                @end @*/
                break;
            case 1:
                /*@cc_on @*/
                /*@if (@_jscript_version >= 5)
                this.resources.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                @end @*/
                break;
            case 2:
                this.resources.xmlHttp = new XMLHttpRequest();
                break;
            case 3:
                this.resources.xmlHttp = window.createRequest();
                break;
        }
    }else{
        /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects. (JScript is MS's Javascript)
        try {
            this.resources.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            this.properties.isSupported = true;
            GLOBAL_AJAX_METHOD = 0;
        } catch (e) {
            try {
                this.resources.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                this.properties.isSupported = true;
                GLOBAL_AJAX_METHOD = 1;
            } catch (E) {
                this.resources.xmlHttp = null;
                this.properties.isSupported = false;
            }
        }
        @end @*/
        if (!this.properties.isSupported && this.resources.xmlHttp==null) {
	        try {
		         this.resources.xmlHttp = new XMLHttpRequest();
		         this.properties.isSupported = true;
		         GLOBAL_AJAX_METHOD = 2;
	        } catch (e) {
	             this.resources.xmlHttp = null;
		         this.properties.isSupported = false;
	        }
        }
        if (!this.properties.isSupported && window.createRequest) {
	        try {
		        this.resources.xmlHttp = window.createRequest();
		        GLOBAL_AJAX_METHOD = 3;
	        } catch (e) {
		        this.resources.xmlHttp = null;
                this.properties.isSupported = true;
	        }
        }
    }
    if(this.properties.isSupported){
		var handler = this;
		this.resources.xmlHttp.onreadystatechange = function(){
			/*
			 * xmlhttp states
			 * 0 - Uninitialized - open() has not been called yet.
			 * 1 - Loading - send() has not been called yet.
			 * 2 - Loaded - send() has been called, headers and status are available.
			 * 3 - Interactive - Downloading, responseText holds the partial data.
			 * 4 - Completed - Finished with all operations.
			 */;
			switch(handler.resources.xmlHttp.readyState) {
				case 0:
					break;
				case 1:
					if(handler.onCall!=null){
						handler.onCall();
					}
					break;
				case 2:
					if(handler.onCallComplete!=null){
						handler.onCallComplete();
					}
					break;
				case 3:
					if(handler.onLoading!=null){
						handler.onLoading();
					}
					break;
				case 4:
					if(handler.onComplete!=null){
						if (handler.resources.xmlHttp.status == 200) {
							//arr = this.resources.xmlHttp.responseXML.getElementsByTagName('string');
							//alert(arr[0].firstChild.data);
							rv = handler.resources.xmlHttp.responseText;
							div = document.getElementById(handler.resources.updatePanel);
							if(div!=null)div.innerHTML = rv;
							handler.onComplete(rv);
						} else {
							handler.onHttpError(handler.resources.xmlHttp.statusText);
						}
					}
					break;
			}
		}
	}else{
		//xmlHttp not supported, take action
	}
}
Jax.prototype = new Function();
Jax.prototype.onCall = function(){
    //externally defined event
}
Jax.prototype.onCallComplete = function(){
    //externally defined event
}
Jax.prototype.onLoading = function(){
    //externally defined event
}
Jax.prototype.onComplete = function(){
    //externally defined event
}
Jax.prototype.onSuccess = function(){
    //externally defined event
}
Jax.prototype.onHttpError = function(){
    //externally defined event
}
Jax.prototype.setUpdatePanel = function(id){
	this.resources.updatePanel = id;
}
Jax.prototype.getUpdatePanel = function(){
	return this.resources.updatePanel;	
}
Jax.prototype.GETRequest = function(path, sync){
    if(this.properties.isSupported && !this.properties.handlingRequest){
    if (this.resources.xmlHttp.overrideMimeType) {
      this.resources.xmlHttp.overrideMimeType('text/xml');
    }
        this.resources.xmlHttp.open("GET", this.properties.URI+path, !sync);
	    this.resources.xmlHttp.send(null);
	    if(sync)return this.resources.xmlHttp.responseText;
    }
}
Jax.prototype.POSTRequest = function(path, headers, sync){
    if(this.properties.isSupported && !this.properties.handlingRequest){
		for(i=0; i<headers.length; i++){
			 this.resources.xmlHttp.setRequestHeader(headers[i].name, headers[i].value);
		}
        this.resources.xmlHttp.open("POST", this.properties.URI+path, !sync);
	    this.resources.xmlHttp.send(null);
	    if(sync)return this.resources.xmlHttp.responseText;
	}
}
Jax.prototype.HEADRequest = function(path, sync){
    if(this.properties.isSupported && !this.properties.handlingRequest){
        this.resources.xmlHttp.open("HEAD", this.properties.URI+path, !sync);
	    this.resources.xmlHttp.send(null);
	    if(sync)return this.resources.xmlHttp.responseText;
	}
}
Jax.prototype.rpcViaGET = function(path, func, params, sync){
    if(this.properties.isSupported && !this.properties.handlingRequest){
        path = this.properties.URI+path+"/"+func+"?";
        for(var i=0; i<params.length;i++){
            path+=escape(String(params[i].name))+"="+escape(String(params[i].value))+"&";
        }
        this.resources.xmlHttp.open("GET", path, !sync);
	    this.resources.xmlHttp.send(null);
	    if(sync)return this.getStringFromSOAP(this.resources.xmlHttp.responseText);
    }
}
Jax.prototype.rpcViaPOST = function(path, func, params, sync){
	vars = '';
	len = 0;
	for(var i=0; i<params.length;i++){
		name = String(params[i].name);
		value = String(params[i].value);
		vars+=name+"="+value+"&";
		len += name.length + value.length;
	}
	url = this.properties.URI+path+"/"+func
	this.resources.xmlHttp.open("POST", url, !sync);
	this.resources.xmlHttp.setRequestHeader("Man", "POST "+url+" HTTP/1.1");
	this.resources.xmlHttp.setRequestHeader("Host", this.properties.hostName);
	this.resources.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.resources.xmlHttp.setRequestHeader("Content-Length", len);
	this.resources.xmlHttp.send(vars);
	if(sync)return this.getStringFromSOAP(this.resources.xmlHttp.responseText);
}
Jax.prototype.getStringFromSOAP = function(soap){
    rv = new String(soap.match(/<string [\w\W\n]*>[\w\W\n]*<\/string>/gi));
    beg = rv.indexOf(">")+1;
    rv = rv.substring(beg, rv.length-9);
    return rv;
}
Jax.prototype.getIntFromSOAP = function(soap){
    rv = new String(soap.match(/<int [\w\W\n]*>[\w\W\n]*<\/int>/gi));
    beg = rv.indexOf(">")+1;
    rv = rv.substring(beg, rv.length-6);
    return Number(rv);
}
Jax.prototype.getXMLStringFromSOAP = function(soap){
    rv = this.getStringFromSOAP(soap);
    rv = rv.replace(/&lt;/gi, '<');
    rv = rv.replace(/&gt;/gi, '>');
    return rv;
}