﻿
function AsyncXMLHttpRequest() {
    /*********************************************  
    Date: 12-15-2006
    Description: Creates a new XMLHTTP object.
    **********************************************/
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        var msxmls = new Array(
            'Msxml2.XMLHTTP.5.0',
            'Msxml2.XMLHTTP.4.0',
            'Msxml2.XMLHTTP.3.0',
            'Msxml2.XMLHTTP',
            'Microsoft.XMLHTTP');
        for (var i = 0; i < msxmls.length; i++) {
            try {
                return new ActiveXObject(msxmls[i]);
            } catch (e) { /** browser not equiped to handle XMLHTTP **/ }
        }
    }
    throw new Error("Could not instantiate XMLHTTP object.");
}

function Async() {
    /*********************************************  
    Date: 12-15-2006
    Description: Initializes a new Async object.
    **********************************************/
    this._xmlhttp = new AsyncXMLHttpRequest();
    this.username = null;
    this.password = null;
}

function Async_call(action, url, data) {
    /*********************************************  
    Date: 12-15-2006
    Description: Initiates the ajax action.
    **********************************************/
    var instance = this;
    this._xmlhttp.open(action, Async_noCache(url), true, this.username, this.password);
    this.openCallback(this._xmlhttp);
    this._xmlhttp.onreadystatechange = function() {
        switch (instance._xmlhttp.readyState) {
            case 1:
                instance.loading();
                break;
            case 2:
                instance.loaded();
                break;
            case 3:
                instance.interactive();
                break;
            case 4:
                try {
                    instance.complete(instance._xmlhttp.status, instance._xmlhttp.statusText, instance._xmlhttp.responseText, instance._xmlhttp.responseXML);
                } catch (e) { }
                break;
        }
    }
    try {
        this._xmlhttp.send(data);
    } catch (e) { }
}

function Async_get(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "GET" action.
    **********************************************/
    this.call("GET", url, null);
}

function Async_put(url, mimetype, datalength, data) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "PUT" action.
    **********************************************/
    this.openCallback = function(xmlhttp) {
        xmlhttp.setRequestHeader("Content-type", mimetype);
        xmlhttp.setRequestHeader("Content-Length", datalength);
    }
    this.call("PUT", url, data);
}

function Async_delete(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "DELETE" action.
    **********************************************/
    this.call("DELETE", url, null);
}

function Async_post(url, datalength, data) {
    /*********************************************  
    Date: 12-15-2006
    Description: Kicks-off a "POST" action.
    **********************************************/
    var thisReference = this;
    this.userOpenCallback = this.openCallback;
    this.openCallback = function(xmlhttp) {
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-Length", datalength);
        thisReference.userOpenCallback(xmlhttp);
        thisReference.openCallback = thisReference.userOpenCallback;
    }
    this.call("POST", url, data);
}

function Async_noCache(url) {
    /*********************************************  
    Date: 12-15-2006
    Description: Prevents caching of querystring
    **********************************************/
    var qs = new Array();
    var arr = url.split('?');
    var scr = arr[0];
    if (arr[1]) qs = arr[1].split('&');
    qs[qs.length] = 'nocache=' + new Date().getTime();
    return scr + '?' + qs.join('&');
}

function Async_loading() {
    /*********************************************  
    Date: 12-15-2006
    Description: Show "loading..." screen
    **********************************************/
    try {
        document.getElementById("async-loading").style.visibility = "visible";
    } catch (e) { /** browser not equiped to handle runtime style settings **/ }
}

function Async_loaded() {
    /*********************************************  
    Date: 12-15-2006
    Description: Hide "loading..." screen
    **********************************************/
    try {
        document.getElementById("async-loading").style.visibility = "hidden";
    } catch (e) { /** browser not equiped to handle runtime style settings **/ }
}

function Async_interactive() {
    /*********************************************  
    Date: 12-15-2006
    Description: XmlHttp ReadyState "interactive"
    **********************************************/
}

function Async_complete(status, statusText, responseText, responseHTML) {
    /**********************************************  
    Date: 12-15-2006
    Description: XmlHttp ReadyState "complete"
    **********************************************/

}

function Async_openCallback(obj) {
    /*********************************************  
    Date: 12-15-2006
    Description: Explictly open the XMLHTTP object
    **********************************************/
}

/*************************************************
Class definitions / functions
**************************************************/
Async.prototype.openCallback = Async_openCallback;
Async.prototype.loading = Async_loading;
Async.prototype.loaded = Async_loaded;
Async.prototype.interactive = Async_interactive;
Async.prototype.complete = Async_complete;
Async.prototype.get = Async_get;
Async.prototype.put = Async_put;
Async.prototype.del = Async_delete;
Async.prototype.post = Async_post;
Async.prototype.call = Async_call;



