/*
function $(id) {
        return document.getElementById(id);
}
*/

//-------------------------------------- global ajax functions -------------------------------------

function BE_GetXmlHttpObject() {
        var objXMLHttp=null;
        if (window.XMLHttpRequest) {
                objXMLHttp=new XMLHttpRequest();
        } else if (window.ActiveXObject) {
                objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        return objXMLHttp;
}

var BE_xmlHttp;

function BE_getRequest(url,func,e,o,id) {
        BE_xmlHttp=BE_GetXmlHttpObject();
        if (BE_xmlHttp==null) {
                //alert("Browser doesn't support AJAX");
                func("Error: Browser doesn't support AJAX",e,o,id);
                return;
        }
        BE_xmlHttp.onreadystatechange=function() {
                if (BE_xmlHttp.readyState==4 || BE_xmlHttp.readyState=="complete") {
                        if (BE_xmlHttp.status==200) {
                                func(BE_xmlHttp.responseText, e, o, id);
                        } else {
                                func("Error: Page ("+url+") gave bad response.\nIf this continues, please contact support.\nResponse: " + BE_xmlHttp.status,e,o,id);
                        }
                }
        }
        BE_xmlHttp.open("GET",url,true);
        BE_xmlHttp.send(null);
}


//----------------------------------- specific ajax functions ---------------------------------------


function BE_setContract(e,o,id) {
        text = o.value;
        url='/_ajax/set-session-var.php?filter_contract='+text;
        //send the request
        BE_getRequest(url,BE_doneContract,e,o,id);
}

function BE_doneContract(response,e,o,id) {
        if(response.substring(0,2)=='ok') {
                location.href=location.href;
        } else {
                qs=location.search;
                loc=location.href;
                last=loc.substring((loc.length - 1),loc.length);
                if(qs.length > 0) {
                        qsChar='&';
                        //need to search for existing filter_contract here! Otherwise we end up with multiples
                        //should put this all into a function
                } else if(last=='?') qsChar='';
                else qsChar='?';
                location.href+=qsChar + 'filter_contract=' + o.value;
        }
}


function BE_getPage(ajaxpage,loc,e,o) {
        //send the request
        BE_getRequest(ajaxpage+'&loc='+loc,BE_donePage,e,o,'');
}

function BE_donePage(response,e,o,id) {
        if(document.getElementById) {
                o.innerHTML=response;
                //document.write(response);
                //return response;
        }
}
