var xmlreqs = []; // Array of XMLHttpRequests

var DIALOG = 1;
var CALLLOG = 2;
var USERCONFIG = 3;
var ACCTSETTING = 4;
var REGISTRATION = 5;
var ROUTING = 6;
var IVRCONFIG = 7;
var ADDSERVICES = 8;
var SUPPORT = 9;
var PHONESELECT = 10;
var FORGOTPASS = 11;
var CUSTOMERS = 12;

function ajaxReset() 
{
    // Abort any active request objects. Call this from your js if you need to back out
    // of any currently running ajax requests.
    for (i in xmlreqs) 
    {
        if (typeof(xmlreqs[i]) != 'undefined'
            && xmlreqs[i].freed == 0
            && xmlreqs[i].xmlhttp.readyState != 4) 
        {
            xmlreqs[i].xmlhttp.abort();
            xmlreqs[i].freed = 1;
        }
    }
}

function createXMLHTTP(freed) 
{
    // Creates the xmlhttprequest objects
    this.freed = freed;
    this.xmlhttp = false;
    if (window.XMLHttpRequest) 
    {
        this.xmlhttp = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject) 
    {
        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function ajaxGET(url, type) 
{
    // Data retrieval; call this for reading data
    // url: the url to the ajax page on the server side
    // type: can be used to track what operation this is and is passed to ajaxResponse
    var pos = -1;
    for (var i=0; i < xmlreqs.length; i++) 
    {
        if (xmlreqs[i].freed == 1) { pos = i; break; }
    }
    if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new createXMLHTTP(1); }
    if (xmlreqs[pos].xmlhttp) 
    {
        xmlreqs[pos].freed = 0;
        xmlreqs[pos].type = type; // Set the type which will be passed to the handle_repsonse function.
        xmlreqs[pos].url = url; // Save this for error handling/messages
        xmlreqs[pos].method = "GET"; // Save this for retry in error handling
        xmlreqs[pos].xmlhttp.open("GET",url,true);
        xmlreqs[pos].xmlhttp.onreadystatechange = function() {
        if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
        }
        if (window.XMLHttpRequest) 
        {
            xmlreqs[pos].xmlhttp.send(null);
        } 
        else if (window.ActiveXObject) 
        {
            xmlreqs[pos].xmlhttp.send();
        }
    }
}

function ajaxPOST(url, data, type) 
{
    // Update/insert data
    var pos = -1;
    for (var i=0; i < xmlreqs.length; i++) 
    {
        if (xmlreqs[i].freed == 1) { pos = i; break; }
    }
    if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new createXMLHTTP(1); }
    if (xmlreqs[pos].xmlhttp) 
    {
        xmlreqs[pos].freed = 0;
        xmlreqs[pos].type = type; // Set the type which will be passed to the ajaxResponse function.
        xmlreqs[pos].url = url; // Save this for error handling/messages
        xmlreqs[pos].method = "POST"; // Save this for retry in error handling
        xmlreqs[pos].data = data; // Save this for the retry in error handling
        xmlreqs[pos].xmlhttp.open('POST',url,true);
        xmlreqs[pos].xmlhttp.onreadystatechange = function() {
        if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
        }
        xmlreqs[pos].xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlreqs[pos].xmlhttp.setRequestHeader('Connection', 'close');
        xmlreqs[pos].xmlhttp.setRequestHeader('Content-length', encodeURI(data).length);
        xmlreqs[pos].xmlhttp.send(encodeURI(data));
    }
}

function xmlhttpChange(pos) 
{
    // xmlhttprequest object readyState change handler. If the request is complete, this will
    // call hanlde_response (in your js code), passing the responseText and type.
    if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) 
    {
        if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) 
        {
            // Successful request, call handling function
            ajaxResponse(xmlreqs[pos].xmlhttp.responseText, xmlreqs[pos].type);
        } 
        else if (xmlreqs[pos].xmlhttp.status == 403) 
        {
            // Getting access forbidden error, so abort current request then retry.
            xmlreqs[pos].xmlhttp.abort();
            // retry the request
            if (xmlreqs[pos].method == "GET") 
            {
                ajaxGET(xmlreqs[pos].url, xmlreqs[pos].type);
                return;
            } 
            else if (xmlreqs[pos].method = "POST") 
            {
                ajaxPOST(xmlreqs[pos].url, xmlreqs[pos].data, xmlreqs[pos].type);
                return;
            }
        } 
        else 
        {
            if (xmlreqs[pos].xmlhttp.status != 0) 
            {
                handle_error('xmlhttp.status: ' + xmlreqs[pos].xmlhttp.status + ' ' + xmlreqs[pos].xmlhttp.statusText + '\r\ntype: ' + xmlreqs[pos].type + '\r\nurl: ' + xmlreqs[pos].url);
            }
        }
        xmlreqs[pos].freed = 1;
    }
} 

function ajaxResponse(text, type) 
{
    if(type == DIALOG) {
        setDialogs(text);
    } else if(type == CALLLOG) {
        handleAJAXCallLog(text);
    } else if(type == USERCONFIG) {
        handleAJAXUserConfig(text);
    } else if(type == ACCTSETTING) {
        handleAJAXAcctSetting(text);
    } else if(type == REGISTRATION) {
        handleAJAXRegistration(text);
    } else if(type == ROUTING) {
        handleAJAXRouting(text);
    } else if(type == IVRCONFIG) {
        handleAJAXIvrConfig(text);
    } else if(type == ADDSERVICES) {
        handleAJAXAddServices(text);
    } else if(type == SUPPORT) {
        handleAJAXSupport(text);
    } else if(type == PHONESELECT) {
        handleAJAXPhoneSelection(text);
    } else if(type == FORGOTPASS) {
        //handleForgotPass(text);
    } else if(type == CUSTOMERS) {
        handleAJAXCustomers(text);
    }
}

