// ----------------------------------------------------------------------------
// class that used by Briefing pages that request xml data
// ----------------------------------------------------------------------------
function BriefingAjax()
{
    // XmlDocument object used to read updates from aspx page
    var xmlDocument = null;
    // do we use xml document or browser dom
    var useXmlDocument = null;
    function checkXmlDocument()
    {
        useXmlDocument = true;
        // see if we can use the xmlDocument object
        try
        {
            xmlDocument = XmlDocument.create();
        }
        catch(e)
        {
            useXmlDocument = false;
        }

        if (useXmlDocument)
        {
            try
            {
                xmlDocument.loadXML("<test/>");
            }
            catch(e)
            {
                useXmlDocument = false;
            }
        }
    }
    checkXmlDocument();
    // ------------------------------------------------------------------------
    // make the request via XmlHttpRequest
    // ------------------------------------------------------------------------
    this.MakeRequest = MakeRequest;
    function MakeRequest(
        url,               // url for the request
        parameters,        // name/value pairs which will be added to the url
        generateUniqueUrl, // add an additional parameter to create unique request to prevent caching
        async,             // use asynchronous mode
        timeout,           // timeout period (in ms) until an async request will be aborted
        method)            // method which will recieve the xml to process
    {
        try
        {
            // if there is a request being processed we do not start a new request
            if (AjaxRequest.numActiveAjaxRequests == 0)
            {
                // make the request using the AjaxRequest class
                AjaxRequest.get({
                "url"               : url,
                "parameters"        : parameters,
                "generateUniqueUrl" : generateUniqueUrl,
                "async"             : async,
                "timeout"           : timeout,
                "onSuccess"         : function(req){method(GetXmlFromResponse(req));},
                "onError"           : function(req){if (debugVerbose) Debug("MakeRequest(): Error: "   + req.statusText + ", Url: " + url);},
                "onTimeout"         : function(req){if (debugVerbose) Debug("MakeRequest(): Timeout: " + req.timeout + "ms");}
                });
            }
            else
            {
                if (debugVerbose)
                {
                    Debug("MakeRequest(): Skipped");
                }
            }
        }
        catch(exception)
        {
            Debug("MakeRequest(): " + exception.message + "<br>url: " + url);
            BrowserError(false, exception.message);
        }
    }
    // ------------------------------------------------------------------------
    // extract the xml from the response
    // ------------------------------------------------------------------------
    function GetXmlFromResponse(req)
    {
        try
        {
            var responseXML = null;
            // do we need to set useXmlDocument?
            if (useXmlDocument == null)
            {
                checkXmlDocument();
            }
            // use XmlDocument if supported
            if (useXmlDocument)
            {
                var responseText = req.responseText;

                if (responseText != null && responseText != "")
                {
                    xmlDocument = XmlDocument.create();
                    xmlDocument.loadXML(responseText);
                    responseXML = xmlDocument;
                }
            }
            // otherwise use responseXML
            else
            {
                responseXML = req.responseXML;
            }
            // done
            return responseXML;
        }
        catch(exception)
        {
            Debug("GetXmlFromResponse(): " + exception.message);
        }
    }
}
// ----------------------------------------------------------------------------
