var ajax = new Object();

ajax.StaticField = new Object ();
ajax.StaticField.READY_STATE_UNINITIALIZED=0;
ajax.StaticField.READY_STATE_LOADING=1;
ajax.StaticField.READY_STATE_LOADED=2;
ajax.StaticField.READY_STATE_INTERACTIVE=3;
ajax.StaticField.READY_STATE_COMPLETE=4;
ajax.StaticField.HTTP_STATUS_OK = 200;

ajax.StaticFunction = new Object ();
ajax.StaticFunction.getXMLHttpRequest = function ()
{
    var xmlHttpRequest = null;
    
    if ( window.XMLHttpRequest )
    {
        xmlHttpRequest = new XMLHttpRequest ();
    }
    else if ( typeof ActiveXObject != "undefined" )
    {
        xmlHttpRequest = new ActiveXObject ( "Microsoft.XMLHTTP" );
    }

    return xmlHttpRequest;
}

ajax.ContentLoader = function ( url, onload, onerror, method, params )
{
    this.url = url + "&dummy=" + new Date().getTime();
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.method = method;
    this.params = params;
    
    this.sendHttpRequest ();
}

ajax.ContentLoader.prototype.sendHttpRequest = function ()
{
    this.contentType = "application/x-www-form-urlencoded";
    if ( ! this.method )
    {
        this.method = "GET";
    }
    
    this.xmlHttpRequest = ajax.StaticFunction.getXMLHttpRequest ();
    if ( this.xmlHttpRequest )
    {
        try
        {
            var loader = this;
            this.xmlHttpRequest.onreadystatechange = function ()
            {
                loader.onReadyState.call (loader);
            }
            
            this.xmlHttpRequest.open ( this.method, this.url, true );
            this.xmlHttpRequest.setRequestHeader ( "Content-Type", this.contentType );
            this.xmlHttpRequest.send ( this.params );
        }
        catch ( error )
        {
            this.onerror.call ( this );
        }
    }
}

ajax.ContentLoader.prototype.onReadyState = function ()
{
    if ( this.xmlHttpRequest.readyState == ajax.StaticField.READY_STATE_COMPLETE )
    {
        if ( this.xmlHttpRequest.status == ajax.StaticField.HTTP_STATUS_OK )
        {
            this.onload.call ( this );
        }
        else
        {
            this.onerror.call ( this );
        }
    }
}

ajax.ContentLoader.prototype.defaultError = function ()
{
    alert ( "Error fetching data!"
    + "\n\nreadyState: " + this.xmlHttpRequest.readyState
    + "\nstatus: " + this.xmlHttpRequest.status
    + "\nheaders: "+ this.xmlHttpRequest.getAllResponseHeaders () );
}
