// callChain contains a sequence of statements that need to be
// evaluated in order. Processing starts by calling the
// function runNextChain();
var callChain = new Array();

// Add a command to the chain
function addCallChain(call) {
    callChain.push(call);
}

// Take the next scheduled command if any
function runNextChain() {
    if (callChain.length==0) {
        return;
    }
    call = callChain.shift();
    debug('calling: ' + call);
    try {
        eval(call);
    } catch(err) {
        alert('A JavaScript error occured executing: ' + call + '\n\n' + err.description);
    }
}

function createRequestObject() {
    var request;

    if(window.XMLHttpRequest) { 
        request = new XMLHttpRequest(); 
    } else if(window.ActiveXObject) { 
        var ie_versions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", 
        "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp",
        "Microsoft.XMLHttp"];
        for(var i=0; i <ie_versions.length; i++) {
            try {
                request = new ActiveXObject(ie_versions[i]);
            } catch (error) {}
        }
    }
    return request;
} 

// Target is where the response from the server should be stored.
// It can be either the name of an object id, or a global variable.
// For a variable, prefix the name with "var:".
// target = 'name_of_id'; target = 'var:target';
var target = "";

function xmlhttpPost(strURL,strSubmit,target) {
    var xmlHttpReq = false;
    var self = this;
    debug('xmlhttpPost: ' + strURL + '?' + strSubmit);
    
    self.xmlHttpReq = createRequestObject();
    this.target = target;
    self.xmlHttpReq.open('POST', strURL, true);

    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) { // Done
            updatepage(self.xmlHttpReq.responseText, target);
        }
    }
    self.xmlHttpReq.send(strSubmit);
}

function xmlhttpGet(strURL,strSubmit,target) {
    var xmlHttpReq = false;
    var self = this;
    debug('xmlhttpGet URL: ' + strURL);
    debug('xmlhttpGet data: ' + strSubmit);

    self.xmlHttpReq = createRequestObject();
    this.target = target;
    self.xmlHttpReq.open('GET', strURL, true);

    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) { // Done
            updatepage(self.xmlHttpReq.responseText, target);
        }
    }
    try {
        self.xmlHttpReq.send(strSubmit);
    } catch (e) {
        alert(e);
    }
}

function updatepage(str,target){
    if(target.substring(0,4)=='var:') {
        str = target.substring(4) + "='" + 
        str.replace(/^\s+/,'').replace(/\s+$/,'') + "'";
        eval(str);
    } else if(target.substring(0,5)=='xvar:') {
        str = target.substring(5) + "=unescape('" + 
        escape(str.replace(/^\s+/,'').replace(/\s+$/,'')) + "')";
        eval(str);
    } else {
        document.getElementById(target).innerHTML = str;
    }

    // call next in chain
    runNextChain();
}
function debug(msg) {
    obj = document.getElementById('debug');
    if (obj) obj.innerHTML += msg + "<br>";
}