//XmlHttp constructor can receive request settings:
//url - the server url
//contentType - request content type
//type - request type
//data - optional request paramaters
//async - whether the request is asynchronous (default is true)
//showErrors - display errors
//complete - the callback function to call when the request completes
function XmlHttp(settings)
 {
    //store the settings in a class property
    this.settings = settings;

    //override default settings with those received as parameter
    var url = location.href;
    if (settings.url)
    url = settings.url;

    //default content type is the content type for forms
    var contentType = "application/x-www-form-urlencoded";
    if (settings.contentType)
    contentType = settings.contentType;

    //by default the request is done through GET
    var type = "GET";
    if (settings.type)
    type = settings.type;

    //by default no parameters are set
    var data = null;
    if (settings.data)
    {
        data = settings.data;
        //if we go through GET  we properly adjust the URL
        if (type == "GET")
        url = url + "?" + data;
    }

    //default postback is asynchronous
    var async = true;
    if (settings.async)
    async = settings.async;

    //show all infrastructure errors
    var showErrors = true;
    if (settings.showErrors)
    showErrors = settings.showErrors;

    //create xmlhttprequest object
    var xhr = XmlHttp.create();

    //set postback properties
    xhr.open(type, url, async);
    xhr.onreadystatechange = onreadystatechange;
    xhr.setRequestHeader("Content-Type", contentType);
    xhr.send(data);

    //function to display errors
    function displayError(messages)
    {
        //ignore errors if showErrors is false
        if (showErrors)
        {
            alert("Error encountered: \n" + message);
        }
    }

    function readResponse()
    {
        try
        {
            //retrieve the response content type
            var contentType = xhr.getResponseHeader("Content-Type");
            //build the json object if the response has one
            if (contentType == "application/json")
            {
                response = JSON.parse(xhr.responseText);
            }
            else if (contentType == "text/xml")
            {
                response = xhr.responseXml;
            }
            else
            {
                response = xhr.responseText;
            }
            if (settings.complete)
            settings.complete(xhr, response, xhr.status);
        }
        catch(e)
        {
            displayError(e.toString());
        }
    }

    function onreadystatechange()
    {
        if (xhr.readyState == 4)
        {
            if (xhr.status == 200)
            {
                try
                {
                    readResponse();
                }
                catch(e)
                {
                    displayError(e.toString());
                }
            }
            else
            {
                displayError(xhr.statusText);
            }
        }
    }

}

XmlHttp.create = function()
{

        //stores reference to xmlhttprequest object
        var xmlHttp;

        try
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
            }
            catch(e) {}
        }
        if (!xmlHttp)
        alert("Error creating the XMLHttpRequest object.");
        else
        return xmlHttp;
}
