/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 +
 +  Ajax object
 + John Calabrese
 +
 +
 +
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/



function ajaxObj()
{
    this.url = null;
    this.argString = null;
    this.callBack = null;
    this.type = 'text';
    this.method = 'POST';
    this.debug = 'off';
    this.StatusCallBack = null;
    this.setUrl = function (url){this.url = url;}//URL of the script to execute via ajax call
    this.setArgString = function(argString){this.argString = argString;}//post or get arguments to pass to script
    this.setCallback = function(callBack){this.callBack = callBack;}//callback function to be executed on completion
    this.setType = function(type){this.type = type;}//set return type (text,json,xml)
    this.setMethod = function(method){this.method = method;}//send method (post,get)
    this.setDebug = function(debug){this.debug = debug;}//set debug mode (on,off)
    this.setStatusCallBack = function(StatusCallBack){this.StatusCallBack = StatusCallBack;}//status callback function to be executed while processing ajax call
    this.send = function()
    {
        var xmlhttp =  new XMLHttpRequest();
        var my = this;
        xmlhttp.open(this.method, this.url, true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    if(my.type=='json'){my.callBack(eval(xmlhttp.responseText));}
                    else if(my.type=='xml'){my.callBack(xmlhttp.responseXML);}
                    else if(my.type=='text'){my.callBack(xmlhttp.responseText);}
                }else{
                    if(my.debug=='on'){alert(xmlhttp.responseText);}
                    else{alert("error");}
                }
            }else if(xmlhttp.readyState == 2 && my.StatusCallBack!=null)
            {
                my.StatusCallBack('Processing Request...');
            }else if(xmlhttp.readyState == 3 && my.StatusCallBack!=null)
            {
                my.StatusCallBack('Loading...');
            }
        }
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(this.argString);
        xmlhttp.close;
    }
    
    
    // function that takes arguments... if you dont want to set variables and use the send function
    // this is the way to go.
    this.ajaxTrans = function(url,argString,callBack,type,method,debug,StatusCallBack)
    {
        var xmlhttp =  new XMLHttpRequest();
        xmlhttp.open(method, url, true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    if(type=='json'){callBack(eval(xmlhttp.responseText));}
                    else if(type=='xml'){callBack(xmlhttp.responseXML);}
                    else if(type=='text'){callBack(xmlhttp.responseText);}
                }else{
                    if(debug=='on'){alert(xmlhttp.responseText);}
                    else{alert("error");}
                }
            }else if(xmlhttp.readyState == 2 && StatusCallBack!=null)
            {
                StatusCallBack('<span style="color:red;backgroud:white;">Processing...</span>');
            }else if(xmlhttp.readyState == 3 && StatusCallBack!=null)
            {
                StatusCallBack('<span style="color:red;backgroud:white;">Processing...</span>');
            }
        }
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(argString);
        xmlhttp.close;
    }
}