/**
 * Default constructor.  Used to establish a namespace
 */
function AjaxUtils()
{
}

/**
 * Returns a new XMLHttpRequest object or null if the browser doesn't support it
 */
AjaxUtils.newXMLHttpRequest = function()
{
    var xmlreq = null;

    if (window.XMLHttpRequest)
    {
        // Create XMLHttpRequest object in non-Microsoft browsers
        xmlreq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try
        {
            xmlreq = new window.ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e1)
        {
            try
            {
                xmlreq = new window.ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e2)
            {
                window.alert("unable to create an activeX xmlhttp:  " + e2.message);
            }
        }
    }

    return xmlreq;
};

/**
 * Returns a function that waits for the specified XMLHttpRequest to complete
 * then passes it XML response to the given handler function.
 * @param req The XMLHttpRequest whose state is changing
 * @param responseXmlHandler Function to pass the XML response to
 */
AjaxUtils.getReadyStateHandler = function(req, responseXmlHandler)
{
    // Return an anonymous function that listens to the XMLHttpRequest instance
    return function ()
    {
        // If the request's status is "complete"
        if (req.readyState == 4)
        {
            // Check that we received a successful response from the server
            if (req.status == 200)
            {
                // Pass the XML http request object to the callback
                responseXmlHandler(req);
            }
            else
            {
                // An HTTP problem has occurred
                window.alert("HTTP error " + req.status + ": " + req.statusText);
            }
        }
    };
};

/**
 * Makes a synchornouse get request to the givenURL
 * @param url The URL to request
 */

AjaxUtils.makeSynchronousGetRequest = function(url)
{
    var http_request = AjaxUtils.newXMLHttpRequest();

	http_request.open('GET', url, false);
	http_request.send(null);
	if (http_request.status == 200)
	{
		return http_request.responseText;
	}
	else
	{
		return null;
	}
};

/**
 * Puts together a post request to the given url
 * @param url the url to post to
 * @param data the data being posted
 * @param callback the callback function that will receive the response object
 */
AjaxUtils.makePOSTRequest = function(url, data, callback)
{
    var http_request = AjaxUtils.newXMLHttpRequest();

    http_request.onreadystatechange = AjaxUtils.getReadyStateHandler(http_request, callback);
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", data.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(data);
};

/**
 * Builds an http post from data contained within a form.
 * @param url the URL to post to
 * @param form the FORM object containing the data to post
 * @param callback the callback function that will receive the response object
 */
AjaxUtils.postFormData = function(url, form, callback)
{
    // Build the data to submit to this form
    var formData = "";
    for (var i = 0; i < form.elements.length; ++i)
    {
        var ele = form.elements[i];
        if (ele.type != "checkbox" || ele.checked)
        {
            formData += ele.name + "=" + encodeURI(ele.value);
            if (i < form.elements.length-1)
            {
                formData += "&";
            }
        }
    }

    AjaxUtils.makePOSTRequest(url, formData, callback);
};
