﻿// xmlhttp.js

var xmlhttp;
var callbackFunction;

function processXMLCall(url, f, method, params) {
    loadXMLDoc(url, method, params);
    callbackFunction = f;
}

function loadXMLDoc(url, method, params)
{
    if (method == undefined)
        method = "GET";
    if (params == undefined)
        params = null;
        
    xmlhttp=null;

    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject)
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp!=null)
    {
        xmlhttp.onreadystatechange=state_Change;
        xmlhttp.open(method,url,true);
        if (params)
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.send(params);
    }
    else
    {
       alert("Your browser does not support XMLHTTP.");
    }
}

function state_Change()
{
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState==4)
    {
        // if "OK"
        if (xmlhttp.status==200)
        {
            eval(callbackFunction);
        }
        else
        {
            // handle the error
            if (document.getElementById('ERROR_DISPLAY')) {
                document.getElementById('ERROR_DISPLAY').innerText = "Problem retrieving XML data:\r\n" + xmlhttp.statusText;
                setTimeout('clearErrorDisplay()', 60000);
            }
        }
    }
}


/*
    FIX FOR BUG 101974 IN MICROSOFT ASP.NET CALLBACK MECHANISM.
    See http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=705049&SiteID=1 and http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101974
 */
function WebForm_CallbackComplete_SyncFixed() {
  // SyncFix: the original version uses "i" as global thereby resulting in javascript errors when "i" is used elsewhere in consuming pages
  for (var i = 0; i < __pendingCallbacks.length; i++) {
   callbackObject = __pendingCallbacks[ i ];
  if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
   // the callback should be executed after releasing all resources
   // associated with this request.
   // Originally if the callback gets executed here and the callback
   // routine makes another ASP.NET ajax request then the pending slots and
   // pending callbacks array gets messed up since the slot is not released
   // before the next ASP.NET request comes.
   // FIX: This statement has been moved below
   // WebForm_ExecuteCallback(callbackObject);
   if (!__pendingCallbacks[ i ].async) {
     __synchronousCallBackIndex = -1;
   }
   __pendingCallbacks[i] = null;

   var callbackFrameID = "__CALLBACKFRAME" + i;
   var xmlRequestFrame = document.getElementById(callbackFrameID);
   if (xmlRequestFrame) {
     xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
   }

   // SyncFix: the following statement has been moved down from above;
   WebForm_ExecuteCallback(callbackObject);
  }
 }
}


/*
    FIX FOR BUG 101974 IN MICROSOFT ASP.NET CALLBACK MECHANISM.
    See http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=705049&SiteID=1 and http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101974
 */
if (typeof (WebForm_CallbackComplete) == "function") {
  // set the original version with fixed version
  WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
}

