var debugMode = false;           // changing this to 'true' will cause alerts to fire at given times.

function createHttpReqObj(){
    var httprequest=false
    if (window.XMLHttpRequest) { // if Mozilla, Safari etc
        httprequest=new XMLHttpRequest()
        if (httprequest.overrideMimeType) {
            httprequest.overrideMimeType('text/xml')
        }
    } else if (window.ActiveXObject){ // if IE
        try {
            httprequest=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httprequest=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {                
            }
        }
    }
    return httprequest
}
// the constructor
function billosJAX() {
    var billosJAXObj = new Object();
    billosJAXObj.basedomain="http://"+window.location.hostname;
    billosJAXObj.HttpReqObj=createHttpReqObj();
  
    
    billosJAXObj.sendGetRequest = function(url, parameters, callbackfunc){        
        this.HttpReqObj=createHttpReqObj() //recreate ajax object to defeat cache problem in IE    
        if (this.HttpReqObj){
            var parameters=parameters+"&_billosJAXCacheBuster="+new Date().getTime()
            var parameters=parameters+"&_billosJAX=true";
            this.callBackFunc=callbackfunc
            this.HttpReqObj.onreadystatechange = this.processRetunedData
            this.HttpReqObj.open('GET', url+"?"+parameters, true)
            this.HttpReqObj.send(null)
        }
    }
    
    billosJAXObj.sendPostRequest=function(url, parameters, callbackfunc){
        this.HttpReqObj=createHttpReqObj() //recreate ajax object to defeat cache problem in IE
        if (this.HttpReqObj){
            var parameters=parameters+"&_billosJAX=true";
            this.callBackFunc=callbackfunc
            this.HttpReqObj.onreadystatechange = this.processRetunedData;
            this.HttpReqObj.open('POST', url, true);
            this.HttpReqObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this.HttpReqObj.setRequestHeader("Content-length", parameters.length);
            this.HttpReqObj.setRequestHeader("Connection", "close");
            this.HttpReqObj.send(parameters);
        }
    }   

    billosJAXObj.processRetunedData = function() {        
        if (billosJAXObj.HttpReqObj.readyState == 4) { //if request of file completed
            if (billosJAXObj.HttpReqObj.status == 200 || window.location.href.indexOf("http") == -1) { //if request was successful or running script locally
                if (billosJAXObj.callBackFunc) { 
                    if (debugMode) {
                        alert(billosJAXObj.HttpReqObj.responseText);
                    }
                    // execute the callBack Function, passing both the XML returned, and the text returned (so it can do whatever it needs to either either)           
                    eval(billosJAXObj.callBackFunc + "(billosJAXObj.HttpReqObj.responseXML.documentElement, billosJAXObj.HttpReqObj.responseText)");       
                }
            } else if (billosJAXObj.HttpReqObj.status != 200) {
                displayAjaxErrorMsg();
            }
        }       
    }
    return billosJAXObj;
}



function displayAjaxErrorMsg(postRequest) {
    sMsg = "Sorry, there was a problem getting the requested information from the server.\n\nPlease try again later";
    sMsg = (postRequest) ? "Sorry, there was an sending the information to the server.\n\nPlease try again later" : sMsg;
    alert(sMsg);
}
