// The AJAX server URI.
// Firefox requires an absolute path for Ajax.
var serverURI;

// Set the server URI.
// Firefox requires an absolute path for Ajax.
function setServerURI(URI)
{
    serverURI = URI;
}

// Create the browser-specific XML HTTP Request object.
function ajaxFunction(method, url, async, callback)
{
    var xmlHttp = new Object;

    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp.request = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp.request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp.request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");

                return false;
            }
        }
    }

    // Now connect the xmlHttp object's onreadystatechange method with
    // the callback parameter.
    // This isn't supported for synchronous function calls on Firefox and
    // IE6 generates a mysterious error.
    if(async)
        xmlHttp.request.onreadystatechange = function()
        {
            if(xmlHttp.request.readyState == 4)
                callback(xmlHttp);
        }

    // I'll need these later.
    xmlHttp.method = method;
    xmlHttp.url = url;
    xmlHttp.async = async;
    xmlHttp.callback = callback;
    xmlHttp.invoke = invoke;

    return xmlHttp;
}

// Invoke the AJAX function call.
function invoke(params)
{
    var formattedParams = '';

    if(params);
    {
        // Build a parameters string.
        for (var key in params) 
        {
            if(formattedParams)
                formattedParams += '&';

            formattedParams += key + '=' + escape(params[key]);
        }
    }

    if(this.method == 'POST')
    {
        // Open the request as initially provided.
        this.request.open(this.method, this.url, this.async);

        // Set the content type to reflect the fact that I have a body.
        this.request.setRequestHeader(
            "Content-type", "application/x-www-form-urlencoded");

        // Send the request with the formatted parameters as the body.
        this.request.send(formattedParams);
    }
    else
    {
        // If I have formatted params, prepend a ? so I can tack it onto
        // the initial request URI.
        formattedParams = '?' + formattedParams;

        // Now open the request with the original settings and the new URI.
        this.request.open(
            this.method, this.url + formattedParams, this.async);

        // Send the request with no body.
        this.request.send(null);
    }

    // Firefox doesn't call onreadystatechange for synchronous calls and 
    // IE 6 generates a mysterious error.
    if(!this.async && this.callback)
        this.callback(this);
}

