﻿// checks the PageRequestManager if there is already a postback being processed
// and aborts the postback (question: which postback does it abort?)
// See details here: http://microsoftmiles.blogspot.com/2006/11/maintaining-gridview-scroll-position-in.html
// and http://geekswithblogs.net/rashid/archive/2007/08/08/Asp.net-Ajax-UpdatePanel-Simultaneous-Update---A-Remedy.aspx

var PageRequestManagerEx =
{
    _initialized: false,

    init: function() {
        if (!PageRequestManagerEx._initialized) {
            var _prm = Sys.WebForms.PageRequestManager.getInstance();
            var _callQueue = new Array();
            var _executingElement = null;

            _prm = Sys.WebForms.PageRequestManager.getInstance();

            _prm.add_initializeRequest(initializeRequest);
            _prm.add_endRequest(endRequest);

            PageRequestManagerEx._initialized = true;
        }

        function initializeRequest(sender, args) {
            
            var postBackElement = args.get_postBackElement();
            if (_prm.get_isInAsyncPostBack() == false) {
                executingElement = postBackElement;
            }
            else {
                if (executingElement != postBackElement) {
                    var evArg = $get("__EVENTARGUMENT").value;
                    Array.enqueue(_callQueue, new Array(postBackElement, evArg));
                }
                args.set_cancel(true);
            }



            /*if (_prm.get_isInAsyncPostBack())
            {
            //if we are here that means there already a call pending.

                //Get the element which cause the postback
            var postBackElement = args.get_postBackElement();

                //We need to check this otherwise it will abort the request which we made from the
            //end request
            if (_executingElement != postBackElement)
            {
            // Grab the event argument value
            var evArg = $get("__EVENTARGUMENT").value;
                
            //Item does not match which means it is another control
            //which request the update, so cancel it temporary and 
            //add it in the call queue
            args.set_cancel(true);
            Array.enqueue(_callQueue, new Array(postBackElement, evArg));
            }

                //Reset it as we are done with our matching
            _executingElement = null;
            }*/
        }

        function endRequest(sender, args) {
            //Check if we have a pending call
            if (_callQueue.length > 0) {
                //Get the first item from the call queue and setting it
                //as current executing item
                _executingElement = Array.dequeue(_callQueue);

                var _element = _executingElement[0];
                var _eventArg = _executingElement[1];

                //Now Post the from which will also fire the initializeRequest
                _prm._doPostBack(_element.name, _eventArg);
            }
        }
    }
}

if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}
