/**
@fileoverview
This file defines the XHR "namespace" object that contains useful functions for using
XMLHttpRequest.
*/

//Top level Namespace
var VI;

//create if not already non-null
if(!VI)
{
    VI = {};
}

//Don't polute the namespace with XHRUtils - use this anonymous function to contruct the 
//class and then assign it to the VI.XHR "namespace".
(function() {
    /**
    This setups up the functions on the XHRUtils object and determines which framework
    object to create to get the XMLHttpRequest object.
    */
    function XHRUtils() {
        /**
        * This is the content type needed to talk to microsoft basic web services.
        */
        this._contentType = "application/x-www-form-urlencoded";

        /**
        This function will create an XMLHttpRequest object and then asynchrounsly POST
        to the specified {@link url}.  The responseText is sent to the specified callback function.
        @param {string} url This is the URL to web service this function will POST to.
        @param {function} callback This is the callback function that is called when the asynchronous call
        completes.  The signature of the function is: <code>func(string)</code>
        @param {string} params This is optional and is needed only of the web service call needs parameters.  
        */
        this.getTextAsync = function(url, callback, params) {
            var requestObj = this.createRequest();
            requestObj.onreadystatechange = function() {
                if (requestObj.readyState == 4) {
                    if (requestObj.status == 200) {
                        callback(requestObj.responseText);
                    }
                }
            };
            requestObj.open("Post", url, true);
            requestObj.setRequestHeader("Content-Type", this._contentType);
            requestObj.send(params);
        };

        this.getXMLAsync = function(url, callback, params) {
            var requestObj = this.createRequest();
            requestObj.onreadystatechange = function() {
                if (requestObj.readyState == 4) {
                    if (requestObj.status == 200) {
                        callback(requestObj.responseXML);
                    }
                }
            };
            requestObj.open("Post", url, true);
            requestObj.setRequestHeader("Content-Type", this._contentType);
            requestObj.send(params);
        };

        /**
        This is a simple method to do a synchronous get from a web service.  Note that this is 
        doing a POST operation.
        @returns {string} the XMLHttpRequest.responseText
        */
        this.getTextSync = function(url, params, submeth) {
            var requestObj = this.getRequestObjSync(url, params, submeth);

            return requestObj.responseText;
        };

        this.getXMLSync = function(url, params) {
            var requestObj = this.getRequestObjSync(url, params);
            if (requestObj.status == 200)
                return requestObj.responseXML;
            return null;
        }

        this.getRequestObjSync = function(url, params, postget) {
            var requestObj = this.createRequest();
            var submeth = "Post";
            if (postget) { submeth = postget; }
            requestObj.open(submeth, url, false);
            requestObj.setRequestHeader("Content-Type", this._contentType);
            requestObj.send(params);

            return requestObj;
        }

        /**
        This simply returns the correct browser version of the XMLHttpRequest object.  This will 
        have been set in the initialization code return a real object.  If there are not XMLHttpRequest
        objects available on this browser, then this will throw when called.
        @throws This will throw an exception if a suitable XMLHttpRequest object could not be 
        found during initialization.
        */
        this.createRequest = function() {
            //This is initialized to throw.  If the initializtion code below succeeds, then this function
            //will be redefined to create the correct object.
            throw new Error("XMLHttpRequest could not be created!");
        };

        //Now, determine the internal _createRequest function that calls the framework to create
        //the raw request object.  The function createRequest will hold the result.  
        var possibleXmlHttpRequestCreators = [
        //function() {return new XMLHttpRequest();}, this causes bug in IE7 bec object exists but does not function
    function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
    function() { return new ActiveXObject("Microsoft.XMLHTTP") },
    function() { return new XMLHttpRequest; }
    ];

        for (var i = 0; i < possibleXmlHttpRequestCreators.length; i++) {
            try {
                var createMethod = possibleXmlHttpRequestCreators[i];
                var request = createMethod();
                if (request != null) {
                    this.createRequest = createMethod;
                    return;
                }
            }
            catch (e) {
                continue;
            }
        }
    }

    /**
    Namespace for XHR (XmlHttpRequest) utils.  To use the functions, you call it like this:
    <code>VI.XHR.getTextSync(...);</code>
    */
    VI.XHR = new XHRUtils();

})();
