if (__REQUEST_OBJECTS__ == null ){
    var __REQUEST_OBJECTS__ = new Array();
}


/*=========================
#
#       Create HTTP Request Object
#*/
function Request (){

    this.engine     = null;
    this.method     = "GET";
    this.async      = true;
    this.url        = null;
    this.id         = __REQUEST_OBJECTS__.length;
    this.handler    = null;
    this.text       = null;

    this.InitEngine  = Request__InitEngine;
    this.Verify      = Request__Verify;
    this.Method      = Request__Method;
    this.Sync        = Request__Sync;
    this.Url         = Request__Url;
    this.Exec        = Request__Exec;
    this.ConnHandler = Request__ConnHandler;
    this.Handler     = Request__UserHandler;
    this.Text        = Request__Text;
    this.Error       = Request__Error;

    __REQUEST_OBJECTS__[this.id] = this;
}



/*=========================
#
#       Create Transfer Engine Object
#*/
function Request__InitEngine() {
    try {
        this.engine = new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch(ex) {
        try {
            this.engine = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (ex1) {
            this.engine = null;
        }
    }

    if( ! this.engine && typeof XMLHttpRequest != "undefined" ){
        this.engine = new XMLHttpRequest()
    }
}



/*=========================
#
#       Verify Correct functionallity of the object
#*/
function Request__Verify(){
    this.InitEngine();

    if ( ! this.engine ) {
        return false;
    }

    return true;
}



/*=========================
#
#       Set/Get Request Object HTTP Method
#*/
function Request__Method(method){
    if (typeof method != 'undefined' ){
        if ( method != "GET" && method != "POST" ){
            this.Error("Unsupported request method [" + method + "]");
        }
        this.method = method;
    }

    return this.method;
}



/*=========================
#
#       Set Sync/Async mode
#*/
function Request__Sync(bool){
    if (bool){
        this.async = false;
    }else{
        this.async = true;
    }
}



/*=========================
#
#       Set Requested URL
#*/
function Request__Url(url){
    if (typeof url != 'undefined' ){
        this.url = url;
    }

    return this.url;
}



/*=========================
#
#       returned text from connection
#*/
function Request__Text(){
    return this.text;
}



/*=========================
#
#       Initiate a connection
#*/
function Request__Exec(){
    if ( ! this.url ){
        this.Error("URL is not specified");
        return;
    }

    var handler = __REQUEST_OBJECTS__[this.id].ConnHandler;

    // init engine on every request because otherwise Konqueror crash
    this.InitEngine();
    this.engine.open(this.method, this.url , this.async );
    var id = this.id;
    this.engine.onreadystatechange = function () {
                                        __REQUEST_OBJECTS__[id].ConnHandler()
                                        };
    this.engine.send(null)
}



/*=========================
#
#       Internal connection handler
#*/
function Request__ConnHandler(){
    if ( this.engine.readyState == 4 ){
        if ( typeof this.engine.status != 'undefined' && this.engine.status != 200 ){
            this.Error("Error in retrieving [" + this.url + "]\nRemote status:" + this.engine.status );
        }

        this.text = this.engine.responseText;

        if (this.handler){
            eval ( this.handler + '()');
        }
    }
}



/*=========================
#
#       Set user handler
#*/
function Request__UserHandler(handler){
    this.handler = handler;
}



/*=========================
#
#       Send Error messages to client
#*/
function Request__Error(ErrMsg){
    alert ("[Request Error]: " + ErrMsg);
}




