// Implements a simple, cross-browser interface to the XMLHttpRequest object.
// Parameters:
//
// - url: the URL to request
// 
// - onsuccess: a function that is called when the request has successfully
//   completed. The function should take two parameters:
//   - responseXML (defined only if the response is well-formed XML)
//   - responseText
// 
// - onfailure: a function that is called if the HTTP request fails, e.g. if an
//   invalid URL is passed to it. It should take two parameters:
//   - status: the HTTP status code
//   - statusText: a brief description of the error
function simpleXMLHttpRequest(url, onsuccess, onfailure) {
    var STATE_UNINITIALIZED = 0;
    var STATE_LOADING = 1;
    var STATE_LOADED = 2;
    var STATE_INTERACTIVE = 3;
    var STATE_COMPLETE = 4;
    var STATUS_OK = 200;
    var STATUS_NOT_MODIFIED = 304;

    // Stolen from http://jibbering.com/2002/4/httprequest.2004.html

    var xmlhttp = null;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (f) {
            xmlhttp = null;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }

    if (!xmlhttp) {
        return false;
    } else {
        xmlhttp.onreadystatechange = function () {
            if (STATE_COMPLETE == xmlhttp.readyState) {
                if (STATUS_OK == xmlhttp.status || STATUS_NOT_MODIFIED == xmlhttp.status) {
                    onsuccess(xmlhttp.responseXML, xmlhttp.responseText);
                } else {
                    onfailure(xmlhttp.status, xmlhttp.statusText);
                }
            }
        };

        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        return true;
    }
}
