// $URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qp/demo/proto/static/proto.js $
// $Id: proto.js 28187 2006-04-12 19:44:12Z dbinger $
// Javascript to demonstrate XMLHTTPRequest callback operation.

function debug_log () {
    // Log a message formed by joining the arguments.
    // The message is appended to the page element with
    // id "debug_log", if there is one.
    javascript_log_element = document.getElementById("debug_log");
    if (javascript_log_element) {    
        var msg = "";
        for (var j=0;j<log.arguments.length; j++) {
            msg += log.arguments[j];
        }
        javascript_log_element.innerHTML += '<br />' + msg;
    }
}

function request (url, callback) {
    // Get an XMLHTTPRequest object and send a GET for the URL.
    // When the complete response has returned, call the callback
    // function with the XMLHTTPRequest object as the argument.
    var req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (exc) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (exc) {
                req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
            }
        }
    }
    req.onreadystatechange = function (e) { 
        if (req.readyState == 4) callback(req); 
    }
    req.open("GET", url, true);
    req.send("");
}

function retrieve (url, id) {
    // Retrieve the url and use the results to replace the content
    // of the document element with the given id.
    request(url, 
        function (req) {
            if (req.status == 200) {
                document.getElementById(id).innerHTML = req.responseText;
            } else {
                document.getElementById(id).innerHTML = req.statusText;
            }
        }
        );
}
