// Create a namespace for this code.
if (typeof ajaxbook == "undefined") {
    ajaxbook = {};
}

/**
 * An object to help with creating and using XMLHttpRequest.
 */
ajaxbook.RequestHelper = (function() {
    // Versions of MSXML XMLHTTP to try in IE browsers.
    var msXmlVersions = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0"];
    // The correct XMLHttpRequest function for this browser.
    var xhrFunction = null;

    // Determine what XMLHttpRequest object is available.
    if (window.XMLHttpRequest) {
        xhrFunction = function() { return new XMLHttpRequest(); };
    } else if (window.ActiveXObject) {
        // Try each XMLHTTP version until one works.
        for (var i = 0; i < msXmlVersions.length; i++) {
            xhrFunction = function() {
                return new ActiveXObject(msXmlVersions[i]);
            };

            try {
                xhrFunction();
            } catch (e) {
                continue;
            }

            break;
        }
    }

    // Return an object that defines the public API.
    return {
        /**
         * Creates and returns a new XMLHttpRequest instance.
         * Throws an error if this browser doesn't support
         * XMLHttpRequest or XMLHTTP.
         */
        createXHR: function() {
            var xhrObj = null;
            if (xhrFunction != null) {
                xhrObj = xhrFunction();
            }
            if (xhrObj == null) {
                // The browser doesn't support XMLHttpRequest
                // so throw an error.
                throw new Error("XMLHttpRequest not supported.");
            }
            return xhrObj;
        },
        
        /**
         * Sends an asynchronous GET request to the given URL and
         * invokes either the success callback or the failure callback
         * on success or failure. A timeout value in milliseconds
         * is used for timing out the request. The given object will
         * be passed to the callback functions.
         */
        sendGet: function(url, success, failure, timeout, obj) {
            // Create an XMLHttpRequest object.
            var request = ajaxbook.RequestHelper.createXHR();
            // Set a timer to cancel the request after 15 seconds.
            var abortTimer = setTimeout(function() {
                request.abort();
                failure(request, obj);
            }, timeout);

            // Define the readyState event handler to process the response.
            request.onreadystatechange = function() {
                // We only care about state 4,
                // when the response has been received.
                if (request.readyState == 4) {
                    // Cancel the abort timer.
                    clearTimeout(abortTimer);

                    // A status of 200 tells us the response was OK.
                    // A status of 304 tells us the response was pulled
                    // from the browser cache.
                    if (request.status == 200 || request.status == 304) {
                        success(request, obj);
                    } else {
                        failure(request, obj);
                    }
                }
            }

            // Tell XMLHttpRequest what URL we want to GET.
            request.open("GET", url, true);
            // Send the request.
            request.send(null);
        }
    };
})();
