/* HttpRequest constructor

   Parameters:
   reqType ... the HTTP request type
   url     ... the URL to be opened
   asynch  ... should the request be send asynchronously
   postStr ... query string of a POST request
*/

function HttpRequest(reqType, url, asynch, queryStr) {
    this.reqType  = reqType.toUpperCase();
    this.url      = url;
    this.asynch   = asynch;
    this.queryStr = queryStr;
    this.error    = null;
    
    // Mozilla-based browsers
    if (window.XMLHttpRequest) {
        this.reqObj = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        this.reqObj = new ActiveXObject("Msxml2.XMLHTTP");
        
        if (!this.reqObj) {
            this.reqObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
}

/* Method to open and send HttpRequest

   Parameters:
   respHandler ... the response handler
 */

HttpRequest.prototype.send = function(respHandler) {
    try {
        this.reqObj.onreadystatechange = respHandler;
        this.reqObj.open(this.reqType, this.url, this.asynch);
        
        if (this.reqType == "POST" && this.queryStr.length > 0) {
            this.reqObj.setRequestHeader("Content-Type",
                                         "application/x-www-form-urlencoded; charset=UTF-8");
            this.reqObj.send(encodeURI(this.queryStr));
        }
        else {
            this.reqObj.send(null);
        }	  
    }
    catch (errv) {
        this.error = errv;
    }
}

/* Getter for the HTTP status */

HttpRequest.prototype.status = function() {
    return this.reqObj.status;
}

/* Getter for the request state */

HttpRequest.prototype.state = function() {
    switch (this.reqObj.readyState) {
        case 0:  return "uninitialized";
        case 1:  return "loading";
        case 2:  return "loaded";
        case 3:  return "interactive";
        case 4:  return "complete";
        default: return null;
    }
}

/* Getter for the response text */

HttpRequest.prototype.text = function() {
    return this.reqObj.responseText;
}

function doGetRequest(url) {
    var request = new HttpRequest("GET", url, true);
    request.send( function () {} );
}

