
var Ajax = function()
{
    var httpRequest;

    {
        if (window.XMLHttpRequest)
        { // Mozilla, Safari, ...
            this.httpRequest = new XMLHttpRequest();
            if (this.httpRequest.overrideMimeType)
            {
                //this.httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        }
        else if (window.ActiveXObject)
        { // IE.x
            try
            {
                this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }
        if (!this.httpRequest)
        {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
    }

    this.sendRequest = function (url, CallbackFunction)
    {
        var self=this;
        this.httpRequest.onreadystatechange = function()
            {
              CallbackFunction(self.httpRequest);
              // FIXME: timeout
            };
        this.httpRequest.open('GET', url, true);
        this.httpRequest.send( null );
    }

    this.postRequest = function (url, params, CallbackFunction)
    {
        var self=this;
        this.httpRequest.onreadystatechange = function()
            {
              CallbackFunction(self.httpRequest);
              // FIXME: timeout
            };
        this.httpRequest.open('POST', url, true);

        //Send the proper header information along with the request
        this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        this.httpRequest.setRequestHeader("Content-length", params.length);
        this.httpRequest.setRequestHeader("Connection", "close");
        this.httpRequest.send( params );
    }


    this.getResponse = function ()
    {
        if (this.httpRequest.readyState == 4)
        {
            if (this.httpRequest.status == 200)
            {
                ct = this.httpRequest.getResponseHeader('Content-Type');
                if (ct && ct.indexOf('xml') != -1)
                {
                    return this.httpRequest.responseXML;
                }
                else
                {
                    if (ct.indexOf('json')!=-1)
                    {
                        var json = new JSON();
                        return json.decode(this.httpRequest.responseText);
                    }
                    else
                    {
                        return this.httpRequest.responseText;
                    }
                }
            }
            else
            {
                alert('There was a problem with the request.');
            }
        }
        else
        {
            return 0;
        }
    }
}
