AjaxRequest.prototype.MS_PROGIDS = new Array(
        "Msxml2.XMLHTTP.7.0",
        "Msxml2.XMLHTTP.6.0",
        "Msxml2.XMLHTTP.5.0",
        "Msxml2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0",
        "MSXML2.XMLHTTP",
        "Microsoft.XMLHTTP"
        );

AjaxRequest.prototype.READY_STATE_UNINITIALIZED = 0;
AjaxRequest.prototype.READY_STATE_LOADING = 1;
AjaxRequest.prototype.READY_STATE_LOADED = 2;
AjaxRequest.prototype.READY_STATE_INTERACTIVE = 3;
AjaxRequest.prototype.READY_STATE_COMPLETED = 4;

AjaxRequest.prototype.successCallback = null;
AjaxRequest.prototype.loadingCallback = null;
AjaxRequest.prototype.failureCallback = null;
AjaxRequest.prototype.url = null;
AjaxRequest.prototype.username = null;
AjaxRequest.prototype.password = null;
AjaxRequest.prototype.requestHeaders = new Array();
AjaxRequest.prototype.status = null;
AjaxRequest.prototype.statusText = null;
AjaxRequest.prototype.responseXML = null;
AjaxRequest.prototype.responseText = null;

AjaxRequest.prototype.abort = AjaxRequestAbort;
AjaxRequest.prototype.setRequestHeader = AjaxRequestSetRequestHeader;
AjaxRequest.prototype.clearRequestHeaders = AjaxRequestClearRequestHeaders;
AjaxRequest.prototype.get = AjaxRequestGet;
AjaxRequest.prototype.post = AjaxRequestPost;
AjaxRequest.prototype.initiateRequest = AjaxRequestInitiateRequest;
AjaxRequest.prototype.getResponseHeader = AjaxRequestGetResponseHeader;
AjaxRequest.prototype.getAllResponseHeaders = AjaxRequestGetAllResponseHeaders;

//=============================================================================
// Contructor
//=============================================================================

function AjaxRequest()
{
    // Create the appropriate HttpRequest object for the browser.
    this.xmlHttpRequest = null;

    if (window.XMLHttpRequest != null)
        this.xmlHttpRequest = new window.XMLHttpRequest();
    else if (window.ActiveXObject != null)
    {
        // Must be IE, find the right ActiveXObject.
        var success = false;
        for (var i = 0; i < AjaxRequest.prototype.MS_PROGIDS.length && !success; i++)
        {
            try
            {
                this.xmlHttpRequest = new ActiveXObject(AjaxRequest.prototype.MS_PROGIDS[i]);
                success = true;
            }
            catch (ex)
            {
            }
        }
    }

	// If we couldn't create one, display an error and exit
    if (this.xmlHttpRequest == null)
    {
        alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");
        return;
    }
}

//=============================================================================
// Methods
//=============================================================================

function AjaxRequestAbort()
{
    this.xmlHttpRequest.abort();
}

function AjaxRequestSetRequestHeader(name, value)
{
    // If the header name already exists, replace the value.
    for (var i = 0; i < this.requestHeaders.length; i++)
    {
        var pair = this.requestHeaders[i].split("\n");
        if (pair[0].toLowerCase() == name.toLowerCase())
        {
            this.requestHeaders[i] = name + "\n" + value;
            return;
        }
    }

	// Otherwise, add it as a new item.
    var n = this.requestHeaders.length;
    this.requestHeaders.push(name + "\n" + value);
}

function AjaxRequestClearRequestHeaders()
{
    this.requestHeaders = new Array();
}

function AjaxRequestGet()
{
    this.initiateRequest("GET", null);
}

function AjaxRequestPost(data)
{
    this.initiateRequest("POST", data);
}

function AjaxRequestGetResponseHeader(name)
{
    return this.xmlHttpRequest.getResponseHeader(name);
}

function AjaxRequestGetAllResponseHeaders()
{
    return this.xmlHttpRequest.getAllResponseHeaders();
}

//=============================================================================
// Internal method to make the actual request
//=============================================================================
function AjaxRequestInitiateRequest(method, data)
{
    // For IE, abort any current request.
    if (window.ActiveXObject != null)
        this.abort();

	// Clear all response fields.
    this.status = null;
    this.statusText = null;
    this.responseText = null;
    this.responseXML = null;

	// Set up the callback functions.
    var refObj = this;
    this.xmlHttpRequest.onreadystatechange =
    function()
    {
        refObj.readyState = refObj.xmlHttpRequest.readyState;
        if (refObj.readyState == AjaxRequest.prototype.READY_STATE_LOADING ||
                refObj.readyState == AjaxRequest.prototype.READY_STATE_LOADED ||
                refObj.readyState == AjaxRequest.prototype.READY_STATE_INTERACTIVE)
        {
            if (refObj.loadingCallback != null)
                refObj.loadingCallback(refObj);
        }
        else if (refObj.readyState == AjaxRequest.prototype.READY_STATE_COMPLETED)
        {
            refObj.status = refObj.xmlHttpRequest.status;
            refObj.statusText = refObj.xmlHttpRequest.statusText;
            refObj.responseText = refObj.xmlHttpRequest.responseText;
            refObj.responseXML = refObj.xmlHttpRequest.responseXML;
            if (refObj.status == 200)
            {
                if (refObj.successCallback != null)
                    refObj.successCallback(refObj);
            }
            else
            {
                if (refObj.failureCallback != null)
                    refObj.failureCallback(refObj);
            }
        }
    }

	// Initialize the request.
    var url = this.url;
    if (this.queryString != null)
        url = url + "?" + this.queryString;
    this.xmlHttpRequest.open(method, url, true, this.username, this.password);

	// Set request headers (this must be done after the request is opened).
    for (var i = 0; i < this.requestHeaders.length; i++)
    {
        var pair = this.requestHeaders[i].split("\n");
        this.xmlHttpRequest.setRequestHeader(pair[0], pair[1]);
    }

	// Start the request, passing any POST data.
    this.xmlHttpRequest.send(data);
}
