function xmlhttpPost(strURL, parameters, spanid) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.onreadystatechange = function() {
        
        if (self.xmlHttpReq.readyState == 4) {
            if (self.xmlHttpReq.status == 200) {
                updatepage(self.xmlHttpReq.responseText, spanid);
                self.xmlHttpReq.onreadystatechange = null;
            }
        }else{
            updatepage("<table><tr><td><p>Loading...</p></td></tr></table>", spanid);
    }
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(parameters);
}

function xmlhttpGet(strURL, spanid, loadingMsg) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    updatepage(loadingMsg, spanid);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            if (self.xmlHttpReq.status == 200) {
                updatepage(self.xmlHttpReq.responseText, spanid);
                self.xmlHttpReq.onreadystatechange = null;
            }
        }else if(self.xmlHttpReq.readyState == 3){
        if (self.xmlHttpReq.status == 301) {
            self.xmlHttpReq.onreadystatechange = null;
        }
    }
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(null);
}

function xmlhttpGetAndRun(strURL, spanid, loadingMsg, func) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    updatepage(loadingMsg, spanid);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            if (self.xmlHttpReq.status == 200) {
                updatepage(self.xmlHttpReq.responseText, spanid);
                eval(func);
                self.xmlHttpReq.onreadystatechange = null;
            }
        }else if(self.xmlHttpReq.readyState == 3){
        if (self.xmlHttpReq.status == 301) {
            self.xmlHttpReq.onreadystatechange = null;
        }
    }
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(null);
}


function xmlhttpPost(strURL, parameters, spanid, loadingMsg) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    updatepage(loadingMsg, spanid);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            if (self.xmlHttpReq.status == 200) {
                updatepage(self.xmlHttpReq.responseText, spanid);
                self.xmlHttpReq.onreadystatechange = null;
            }
        }else if(self.xmlHttpReq.readyState == 3){
        if (self.xmlHttpReq.status == 301) {
            self.xmlHttpReq.onreadystatechange = null;
        }
    }
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(parameters);
}

//Sends ajax request and doesn't display the response.
function xmlhttpPostWithoutResponse(strURL, parameters, spanid, loadingMsg) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    updatepage(loadingMsg, spanid);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            if (self.xmlHttpReq.status == 200) {
                self.xmlHttpReq.onreadystatechange = null;
            }
        }else if(self.xmlHttpReq.readyState == 3){
        if (self.xmlHttpReq.status == 301) {
            self.xmlHttpReq.onreadystatechange = null;
        }
    }
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.send(parameters);
}

function updatepage(str, spanid){
    if(spanid!=null && spanid!="" && document.getElementById(spanid)!=null){
        if(str!=null && str!=''){
            document.getElementById(spanid).innerHTML = str;
        }
    }
}

function ajaxSubmit(formObj, spanid, loadingMsg){
    var paramStr = "";
    for(var i=0; i<formObj.elements.length; i++){
        if(formObj.elements[i].type != "button" && formObj.elements[i].type != "submit"){
            paramStr = paramStr + (i!=0?'&':'') + formObj.elements[i].name + "=" + replaceAll(escape(utf8_encode(formObj.elements[i].value)), "/", "%2F");
        }
    }
    xmlhttpPost(formObj.action, paramStr, spanid, loadingMsg);
    return false;
}

function ajaxJustSubmit(formObj, spanid, loadingMsg){
    var paramStr = "";
    for(var i=0; i<formObj.elements.length; i++){
        if(formObj.elements[i].type != "button" && formObj.elements[i].type != "submit"){
            paramStr = paramStr + (i!=0?'&':'') + formObj.elements[i].name + "=" + replaceAll(escape(utf8_encode(formObj.elements[i].value)), "/", "%2F");
        }
    }
    xmlhttpPostWithoutResponse(formObj.action, paramStr, spanid, loadingMsg);
    return false;
}

function utf8_encode(string){
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    
    for (var n = 0; n < string.length; n++) {
        
        var c = string.charCodeAt(n);
        
        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
    return utftext;
}

function replaceAll(txt, str1, str2){
    if(str1==str2){
        return txt;
    }
    while (txt.indexOf(str1)!=-1){
        txt = txt.replace(str1, str2);
    }
    return txt;
}

function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}
//Suppress javascript errors
//window.onerror = function(){return true;}

