﻿// This code handles requests from the pages using AJAX.

function AjaxHelper() {};   // Create namespace for the AjaxHelper file.

AjaxHelper.AsyncRequest = function(url, postData, callBackFtnName, callBackObj) {
    this.callUrl = url; // The page to call on the server.
    this.postData = postData;   // The data to send to the server when calling.
    this.pageCallBackFtnName = callBackFtnName;   // CallBack to the page that we're done and have data.  CallBack function must take one argument for the data.
    this.pageCallBackObj = callBackObj;     // The object containing the callback method.  Needed so the callback method is called on the object instead of globally.
    this.xmlHttp = false;        // The XMLHTTPRequest object.
}

AjaxHelper.AsyncRequest.prototype.callServerPage = function() {
    // Attach the postData as query string variables on the page url.
    var qsVars = [];
    for (var props in this.postData)
    {
        qsVars = qsVars.concat(props + "=" + this.postData[props]);
    }
    var qsVarStr = qsVars.join("&");
    if (qsVarStr.length > 0)
    {
        this.callUrl += "?" + qsVarStr;
    }
    
    // Get XMLHTTPRequest object.
    this.xmlHttp = AjaxHelper.AsyncRequest.getXmlHttpRequest();
    
    // Call the server page specified by url.
    this.xmlHttp.open("GET", this.callUrl, true);
    var me = this;
    this.xmlHttp.onreadystatechange = function() { me.serverCallBack(); };    // Write it this way so serverCallBack() is called in the context of the object.
    this.xmlHttp.send(null);
    
    // We're done.  When the server replies it will call serverCallBack().
}

AjaxHelper.AsyncRequest.prototype.serverCallBack = function() {
    // Check for complete.
    if (this.xmlHttp.readyState == 4)
    {
        // Check for good response.
        if (this.xmlHttp.status == 200)
        {
            // Convert JSON to a JavaScript object.
            eval("var data = " + this.xmlHttp.responseText);
            
            // CallBack to the page.
            this.callBackCaller(data);
        }
        else
        {
            // CallBack with no data.
            this.callBackCaller(false);
        }
        
        delete this.xmlHttp.onreadystatechange;
        this.xmlHttp = null;
        AjaxHelper.cleanUpRequest(this);
    }
}

AjaxHelper.AsyncRequest.prototype.callBackCaller = function(data) {
    if (this.pageCallBackObj != null)
        this.pageCallBackObj[this.pageCallBackFtnName](data);
    else
        this.pageCallBackFtnName(data);
}

AjaxHelper.AsyncRequest.getXmlHttpRequest = function() {
    var newXmlHttp = false;
    try
    {
        // Try MSXML first.
        newXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e1)
    {
        try
        {
            // Try IE6 version.
            newXmlHttp = new ActiveObject("Microsoft.XMLHTTP");
        }
        catch (e2)
        {
            newXmlHttp = false;
        }
    }
    if (!newXmlHttp && typeof XMLHttpRequest != 'undefined')
    {
        // IE7/Mozilla version.
        newXmlHttp = new XMLHttpRequest();
    }
    
    return newXmlHttp;
}

AjaxHelper.callServer = function(url, postData, callBackFtnName, callBackObj) {
    if (typeof requests == 'undefined')
    {
        requests = [];
    }
    
    var request = new AjaxHelper.AsyncRequest(url, postData, callBackFtnName, callBackObj);
    request.callServerPage();
    requests.push(request);
}

AjaxHelper.cleanUpRequest = function(request) {
    for (var i = 0; i < requests.length; i++)
    {
        if (requests[i] === request)
        {
            requests.splice(i, 1);
            return;
        }
    }
}

