
/*extern ActiveXObject */ // For jslint.com

//////////////////////////////////////////////////////////////////////
//
// AJAX Infrastructure
//
//////////////////////////////////////////////////////////////////////

//----------------------------------------------------------------------
// XmlHttpRequest Constants 
//----------------------------------------------------------------------
var READYSTATE_UNINITIALIZED = 0;
var READYSTATE_LOADING       = 1;
var READYSTATE_LOADED        = 2;
var READYSTATE_INTERACTIVE   = 3;
var READYSTATE_COMPLETE      = 4;

var HTTP_STATUS_ABORTED  =   0;
var HTTP_STATUS_OK       = 200;
var HTTP_STATUS_NOTFOUND = 404;

//----------------------------------------------------------------------
// Object Constructor for async request wrapper
//----------------------------------------------------------------------
function AsyncHttpRequest() 
//----------------------------------------------------------------------
{
    this.RequestObject = 
        ( window.XMLHttpRequest ) ? new XMLHttpRequest() :					 // Mozilla/Safari
        ( window.ActiveXObject  ) ? new ActiveXObject("Microsoft.XMLHTTP") : // Internet Explorer
        null;

    if( !this.RequestObject )
    {
        throw "No XmlHttpRequest support.";
    }
    
    this.headers = {};

    this.SetRequestHeader = function( header, value )
    {
        this.headers[header] = value;
    };

    this.Invoke = function( url, callback )
    {
        this.CallbackFunction = null;
        this.RequestObject.abort();
        
        this.CallbackFunction = callback;

        var oThis = this; // For referencing within the following closure

        this.RequestObject.open( "GET", url, true );
        this.RequestObject.onreadystatechange = function() { oThis.OnReadyStateChange(); };

        for( var header in this.headers ) if( typeof(this.headers[header] === "string" ) )
        {
            this.RequestObject.setRequestHeader( header, this.headers[header] );
        }

        this.RequestObject.send(null);
    };

    this.Cancel = function()
    {
        this.CallbackFunction = null;
        this.RequestObject.abort();
    };

    this.OnReadyStateChange = function() {

        try {
            if (this.RequestObject.readyState === READYSTATE_COMPLETE) {
                if (200 <= this.RequestObject.status && this.RequestObject.status < 400) {
                    if (this.CallbackFunction) {
                        this.CallbackFunction(this.RequestObject);
                    }
                }
            }
        }
        catch (exception) {
            // Mozilla bug 238559: if the transaction is cancelled
            // accessing XMLHttpRequest.status throws an exception
            return;
        }

    };
    
} // AsyncHttpRequest
