// The CRequestObject class implements the XHTML-Request-Object class

function CRequestObject () 
{
    // Create an XMLHttpRequest object
    this.XHRObject = null;
    this.CreateNewXHRObject();
}

// CreateNewXHRObject
// Creates a new XMLHttpRequest object
// Each object may only be used for one request, some browsers don't send a second request, once
// the ready state has changed to 4

CRequestObject.prototype.CreateNewXHRObject = function()
{
    // Create an XMLHttpRequest-object (browser implementation dependend)
    if (window.XMLHttpRequest) {     // is Mozilla Firefox, Opera, Internet Explorer 7
        this.XHRObject = new XMLHttpRequest();
    }
    else {
            if (window.ActiveXObject) { // is Internet Explorer 6 or earlier
                this.XHRObject = new ActiveXObject("Microsoft.XMLHTTP");
            }
    }
    // if there is no XMLHttpRequest object implemented, this function will throw an error
};

// RequestFile()
// Requests a script file from the server

CRequestObject.prototype.RequestFile = function (File, onLoadFunction)
{
    var _this = this;
    if (this.XHRObject === null) {
        this.CreateNewXHRObject();
    }
    // Set up the ready change function
    this.XHRObject.onreadystatechange = function ()
    {
        if(_this.XHRObject.readyState == 4)  // complete
        {
            // Do something with the file 
            if (_this.XHRObject.status < 400) {
                onLoadFunction(_this.XHRObject);
            }
            
            // Dispose off the XHRObject
            _this.XHRObject = null;
        }
        // else { arguments.callee.onError(); }
    };
    // Append a timestamp to the file's URL if you want to avoid browser caching
    File += '?timeStamp=' + new Date().getTime();
    // Alternatively:
    // this.XHRObject.setRequestHeader('If-Modified-Since', 'Sat, 11 Jan 2011 00:00:00 GMT');

    this.XHRObject.open('GET',      // HTTP-method
                        File,       // requested file
                        true);      // asynchronous call
                        
    //this.XHRObject.setRequestHeader('Referrer', 'http://www.labinsky-achenbach.de/');
    this.XHRObject.send(null);      // null == no http body included (you may include a DOM document)
};

// RequestAndRunScript()
// Requests a script file from the server and runs the script

CRequestObject.prototype.RequestAndRunScript = function (File)
{
    var _this = this;
         
    var onLoadFunction = function(XHRObject)
                         {
                            // in this case, we ignore JSLINT's warning intentionally:
                            eval( XHRObject.responseText );  // ignore JSLINT evil message
                            if (_this.onCompletion) {
                                _this.onCompletion();
                            }
                         };
                         
    this.RequestFile(File, onLoadFunction);
};

// PostString()
// Posts a string to a server script

CRequestObject.prototype.PostString = function (string, URL)
{
    var _this = this;
//    var adsenseDiv = document.getElementById('adsenseDiv');
//    adsenseDiv.innerHTML = string;

    if (this.XHRObject === null) {
        this.CreateNewXHRObject();
    }
    // Set up the ready change function
    this.XHRObject.onreadystatechange = function ()
    {
        if(_this.XHRObject.readyState == 4)  // complete
        {
            // Do something with the result
            if (_this.XHRObject.status < 400) {
//                adsenseDiv.innerHTML +=' OK';
            }
//            else adsenseDiv.innerHTML += ' ERROR: ' + _this.XHRObject.responseText;

            // Dispose off the XHRObject
            _this.XHRObject = null;
        }
        // else { arguments.callee.onError(); }
    };

    this.XHRObject.open('POST',      // HTTP-method
                        URL,       // the server script URL
                        true);      // asynchronous call

    this.XHRObject.setRequestHeader('Content-Type', 'text/html; charset=UTF-8');
    this.XHRObject.send(string);      // null == no http body included (you may include a DOM document)
};


