﻿// JScript File
// Code reference: http://forums.asp.net/p/1024344/1392512.aspx


   //This function must replace the original microsoft function "WebForm_CallbackComplete"
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);
     }

     // Fix: the following statement has been moved down from above;
     WebForm_ExecuteCallback(callbackObject);
   }
 }
}


//this methot will be called automatically at startup
function WebForm_CallbackComplete_AutoFix()
{
    if (typeof (WebForm_CallbackComplete) == "function") {
      // set the original version with fixed version
      WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
    }
}



//attach automatically at startup
if (window.addEventListener)
{ // Non-IE browsers
    window.addEventListener('load', WebForm_CallbackComplete_AutoFix, false);       
}
else if (window.attachEvent)
{ // IE 6+
    window.attachEvent('on' + 'load', WebForm_CallbackComplete_AutoFix);
}
else
{ // Older browsers
    var currentEventHandler = window['on' + 'load'];
    if (currentEventHandler == null)
    {
      window['on' + 'load'] = WebForm_CallbackComplete_AutoFix;
    }
    else
    {
      window['on' + 'load'] = function(e) { currentEventHandler(e); WebForm_CallbackComplete_AutoFix(e); }
    }
}

