/*
 * Copied verbatim from page 29 of Java Developers Journal; Vol: 10, Issue: 9
 */
function newXmlHttpRequest() 
{
	var xmlreq = false;
	
	if (window.XMLHttpRequest) 
	{
		xmlreq = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		try 
		{ 
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e1) 
		{ 
			// try again...
			try 
			{
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e2) 
			{
				throw e2;
			} 
		}
 	}
 	
   	return xmlreq;
}

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * request - The XMLHttpRequest whose state is changing
 * responseHandler - Function to pass the XML response to
 * @see IBM
 */
function getReadyStateXmlHandler(request, responseHandler) 
{
	// Return an anonymous function that listens to the 
    // XMLHttpRequest instance
    return function () 
    {
        // If the request's status is "complete"
        if (request.readyState == 4) 
        {
            // Check that a successful server response was received
            if (request.status == 200) 
            {
                // Pass the XML payload of the response to the 
                // handler function
                responseHandler(request.responseXML);
            }
            else
            {
                // An HTTP problem has occurred
                var exceptionMessage = "HTTP error: " + request.statusText + 
                	" [" + request.status + "]";
                //jslog.error(exceptionMessage);
                throw exceptionMessage;
            }
        }
        else
        {
        	//jslog.debug("XMLHttpRequest transitioned to ready state: " + 
        		//request.readyState);
        }
    }
}

/**
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its text response
 * to the given handler function.
 * request - The XMLHttpRequest whose state is changing
 * responseHandler - Function to pass the text response to
 * @see IBM
 */
function getReadyStateTextHandler(request, responseHandler) 
{
	// Return an anonymous function that listens to the 
    // XMLHttpRequest instance
    return function () 
    {
        // If the request's status is "complete"
        if (request.readyState == 4) 
        {
            // Check that a successful server response was received
            if (request.status == 200) 
            {
                // Pass the plain text payload of the response to the 
                // handler function
                responseHandler(request.responseText);
            }
            else
            {
                // An HTTP problem has occurred
                var exceptionMessage = "HTTP error: " + request.statusText + 
                	" [" + request.status + "]";
                //jslog.error(exceptionMessage);
                throw exceptionMessage;
            }
        }
        else
        {
        	//jslog.debug("XMLHttpRequest transitioned to ready state: " + 
        		//request.readyState);
        }
    }
}
