// From JavaScript: The Definitive Guid, 5th ed.
// pp. 480-481

var HTTP = {};
// This is a list of XMLHttpRequest-creation factory functions to try
HTTP._factories =
[
    function() { return new XMLHttpRequest(); },
    function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
    function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
];

// When we find a factory that works, store it here.
HTTP._factory = null;

// Create and return a new XMLHttpRequest Object.
HTTP.newRequest = function()
{
    if (HTTP._factory != null) return HTTP._factory();

    for(var i = 0; i < HTTP._factories.length; i++)
    {
	try
	{
	    var factory = HTTP._factories[i];
	    var request = factory();
	    if (request != null)
	    {
		HTTP._factory = factory;
		return request;
	    }
	}
	catch(e)
	{
	    continue;
	}
    }
    // If we get here, none of the factory candidates succeded,
    // so throw an exception now and faor all future calls.
    HTTP._factory = function()
    {
	throw new Error("XMLHttpRequest not supported");
    }
}

