var AJAX = function(_call, _method, _synchronous, _response, _wait)
{
    this.construct(_call, _method, _synchronous, _response, _wait);
}

AJAX.prototype.construct = function (_call, _method, _synchronous, _response, _wait)
{
    if (_method != "GET" && _method != "POST")
    {
        _method = "GET";
        window.alert("Invalid method received (should be GET or POST)"); 
    }

    this.call = _call;
    this.method = _method;
    this.parameters = new Array();
    this.numberParameters = 0;
    this.object = false;
    //this.synchronous = _synchronous;
    this.synchronous = true;
    this.divWait = _wait;
    this.divResponse = _response;

    if (window.XMLHttpRequest)
    {
        this.object = new XMLHttpRequest();
    }
    else if (window.ActiveXthis.object)
    {
        this.object = new ActiveXthis.object("Microsoft.XMLHTTP");
    }
    else
    {
        window.alert("Your browser does not support AJAX");
    }
    
    var caller = this;
    this.object.onreadystatechange = function ()
    {   
        if (caller.object.readyState == 4)
        {
            if (caller.object.status == 200)
            {
                document.getElementById(caller.divWait).innerHTML = "";
                document.getElementById(caller.divResponse).innerHTML = caller.object.responseText;
            }
            else
            {
                document.getElementById(caller.divWait).innerHTML = "Document not found";
            }
        }
        else
        {
            document.getElementById(caller.divWait).innerHTML = "<img src = 'Images/Loader.gif' alt = 'Chargement' />";    
        }
    }
 }

AJAX.prototype.toString = function ()
{
    return "AJAX Class";
}

AJAX.prototype.reset = function ()
{
    this.construct(this.call, this.method, this.synchronous, this.divResponse, this.divWait);
}

AJAX.prototype.addParameter = function (_name, _value)
{
    this.parameters[this.numberParameters++] = new Array(_name, escape(_value));
}

AJAX.prototype.loadResponse = function (_event)
{
    if (this.method == "GET")
    {
        realCall = this.call + "?";
        for (var i = 0; i < this.numberParameters; i++)
        {
            realCall += this.parameters[i][0] + "=" + this.parameters[i][1] + "&";     
        }
        realCall = realCall.substr(0, realCall.length - 1);
        this.object.open(this.method, realCall, this.synchronous);
        this.object.send(null);
    }
    else
    {
        this.object.open(this.method, this.call, this.synchronous);
        this.object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
        realCall = "";
        for (var i = 0; i < this.numberParameters; i++)
        {
            realCall += this.parameters[i][0] + "=" + this.parameters[i][1] + "&";     
        }
        realCall = realCall.substr(0, realCall.length - 1);
        this.object.send(realCall);           
    }
} 