﻿var beginRequestFunctions = new Array();
var endRequestFunctions = new Array();
var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
pageRequestManager.add_beginRequest(suppressUpdateProgress_beginRequest);
pageRequestManager.add_endRequest(suppressUpdateProgress_endRequest);

function suppressUpdateProgress_beginRequest(sender, args)
{
    for (var index = 0; index < beginRequestFunctions.length; index++)
    {
        var func = beginRequestFunctions[index];
        func(sender, args);
    }
}

function suppressUpdateProgress_endRequest(sender, args)
{
    for (var index = 0; index < endRequestFunctions.length; index++)
    {
        var func = endRequestFunctions[index];
        func(sender, args);
    }
}

function SuppressUpdateProgress(subUpdatePanelID, masterUpdatePanelID, masterUpdateProgressID)
{
    this.subUpdatePanelID = subUpdatePanelID;
    this.masterUpdatePanelID = masterUpdatePanelID;
    this.masterUpdateProgressID = masterUpdateProgressID;
    this.suppressPostBackElementID = null;
    this.enabled = true;
    var obj = this;
    beginRequestFunctions.push(function(sender, args) { obj.suppress_beginRequest(sender, args); });
    endRequestFunctions.push(function(sender, args) { obj.suppress_endRequest(sender, args); });
}

SuppressUpdateProgress.prototype.suppress_beginRequest = function(sender, args)
{
    if (!this.enabled)
        return;
    this.suppressPostBackElementID = args.get_postBackElement().id;
    if (this.isSuppressedPostBack())
    {
        $find(this.masterUpdateProgressID).set_associatedUpdatePanelId("non_existant_control_id");
    }
}

SuppressUpdateProgress.prototype.suppress_endRequest = function(sender, args)
{
    if (this.suppressPostBackElementID)
    {
        $find(this.masterUpdateProgressID).set_associatedUpdatePanelId(this.masterUpdatePanelID);
    }
    this.suppressPostBackElementID = null;
}

SuppressUpdateProgress.prototype.isSuppressedPostBack = function()
{
    if ($get(this.subUpdatePanelID) && this.suppressPostBackElementID && recursiveFind($get(this.subUpdatePanelID), this.suppressPostBackElementID))
        return true;
    else
        return false;
}