﻿// These Array hold the FunctionName for the BeginRequest and the EndRequest of Different UpdatePanel
// The Value gets added as the Control is added and the Begin and End Request Methods are assigned to the
// UpdatePanelAnimation Control.
var strUpdatedPanelId = "";

// Register the BeginRequest and the EndRequest Methods.
var objPageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
objPageRequestManager.add_beginRequest(BeginRequest);
objPageRequestManager.add_endRequest(EndRequest);
function BeginRequest(sender, beginRequestEventArgs) {
    ///<summary>Event is called before Update begins</summary>
    ///<parm name = "sender">Event sender object.</parm>
    ///<parm name = "beginRequestEventArgs">Begin request Eventargs.</parm>

    // Here get the id of the Element which caused the PostBack, with the Id get the Index of the UpdatePanel
    // from the List of UpdatePanel        
    var strKeyValue = GetUpdatePanelClientId(arrBeginRequest, sender._updatePanelClientIDs, beginRequestEventArgs.get_postBackElement().id);
    if (strKeyValue == "") {
        var intKeyIndex = GetIndex(sender._asyncPostBackControlClientIDs, beginRequestEventArgs.get_postBackElement().id);
        if (intKeyIndex > -1) {
            // Get the Name of the UpdatePanel, which caused the PostBack
            strKeyValue = GetValueByIndex(sender._updatePanelClientIDs, intKeyIndex);
        }
    }
    if (strKeyValue != "") {
        strUpdatedPanelId = strKeyValue;
        // With the UpdatePanelId, get the BeginRequest Function Name from the Array arrBeginRequest.
        if ((typeof (window["arrBeginRequest"]) != "undefined")) {
            var arrBeginRequestAttributes = GetValueByKey(arrBeginRequest, strKeyValue);
            if (arrBeginRequestAttributes != null) {
                var strBeginRequest = arrBeginRequestAttributes[0];
                var strUpdatePanelAnimationId = arrBeginRequestAttributes[1];

                if (strUpdatePanelAnimationId != "") {
                    var blnShowAnimation = document.getElementById(strUpdatePanelAnimationId + "_hdnShowAnimation").value;
                    if (blnShowAnimation == 'True') {
                        var strProgressType = document.getElementById(strUpdatePanelAnimationId + "_hdnProgressType").value;
                        ShowAnimation(strUpdatePanelAnimationId, strProgressType);                        
                    }
                }

                // Call the Page Specific Client Function for the BegiinRequest    
                if (strBeginRequest != "") {
                    if (eval(strBeginRequest)) {
                        eval(strBeginRequest)();
                    }
                }
            }
        }
    }
}

function ShowAnimation(strUpdatePanelAnimationId, strProgressType) {
    if (Type.isClass(Arcadix.Framework.Controls.Animation)) {
        var objAnimation = new Arcadix.Framework.Controls.Animation(strUpdatePanelAnimationId);
        if (strProgressType == "Loading") {
            objAnimation.ProgressTextType = enumProgressTextType.Loading;
        }
        else if (strProgressType == "Saving") {
            objAnimation.ProgressTextType = enumProgressTextType.Saving;
        }
        objAnimation.Show();
    }
    else {
        window.setTimeout("ShowAnimation('" + strUpdatePanelAnimationId + "','" + strProgressType + "')", 1000);
    }
}

function EndRequest(sender, endRequestEventArgs) {
    ///<summary>Event is called after Update ends</summary>
    ///<parm name = "sender">Event sender object.</parm>
    ///<parm name = "beginRequestEventArgs">End request Eventargs.</parm>

    // TO GET THE ENDREQUEST FUNCTION FOR THE CURRENT UPDATEPANEL.    
    var strKeyValue = strUpdatedPanelId;
    strUpdatedPanelId = "";
    if (strKeyValue != "") {
        // With the UpdatePanelId, get the EndRequest Function Name from the Array arrEndRequest.
        if ((typeof (window["arrEndRequest"]) != "undefined")) {
            var arrEndRequestAttributes = GetValueByKey(arrEndRequest, strKeyValue);
            if (arrEndRequestAttributes != null) {
                var strEndRequest = arrEndRequestAttributes[0];
                var strUpdatePanelAnimationId = arrEndRequestAttributes[1];

                // Call the Page Specific Client Function for the EndRequest        
                if (eval(strEndRequest)) {
                    eval(strEndRequest)();
                }

                if (strUpdatePanelAnimationId != "") {
                    // Create an object of Animation and hide the animation control       

                    var blnShowAnimation = document.getElementById(strUpdatePanelAnimationId + "_hdnShowAnimation").value;
                    if (blnShowAnimation == 'True') {
                        var objAnimation = new Arcadix.Framework.Controls.Animation(strUpdatePanelAnimationId);
                        objAnimation.Hide();                        
                    }
                }
            }
        }
    }
}

function GetUpdatePanelClientId(arrCustomBeginRequest, arrUpdatePanelClientIds, strValue) {
    var strReturnValue = "";
    if (arrCustomBeginRequest != null) {
        for (var intIndex = 0; intIndex < arrUpdatePanelClientIds.length; intIndex++) {
            var strClientId = arrUpdatePanelClientIds[intIndex];
            var objArray = arrCustomBeginRequest[strClientId];
            if (objArray != null) {
                if (objArray.length > 2) {
                    if (objArray[2] == strValue) {
                        strReturnValue = strClientId;
                    }
                }
            }
        }
    }
    return strReturnValue;
}

function GetIndex(arrObject, strValue) {
    ///<summary>The Method returns the Index for the Given Value from the array of objects</summary>
    ///<parm name = "sender">Array of Objects.</parm>
    ///<parm name = "beginRequestEventArgs">Value for which the Index has to be returned.</parm>

    var intReturnIndex = -1;
    if (arrObject) {
        if (arrObject.length > 0) {
            for (var intIndex = 0; intIndex < arrObject.length; intIndex++) {
                if (arrObject[intIndex] == strValue) {
                    intReturnIndex = intIndex;
                }
            }
        }
    }
    return intReturnIndex;
}

function GetValueByIndex(arrObject, intIndex) {
    ///<summary>The Method returns the value for the Given Index from the array of objects</summary>
    ///<parm name = "sender">Array of Objects.</parm>
    ///<parm name = "beginRequestEventArgs">Index for which the Value has to be returned.</parm>

    var strReturnValue = "";
    if (arrObject) {
        if (arrObject.length > intIndex) {
            strReturnValue = arrObject[intIndex];
        }
    }
    return strReturnValue;
}

function GetValueByKey(arrObject,strKey)
{
    ///<summary>The Method returns the value for the Given Key from the array of objects</summary>
    ///<parm name = "sender">Array of Objects.</parm>
    ///<parm name = "beginRequestEventArgs">Key for which the Value has to be returned.</parm>
    
    var objReturnValue = null;
    if(arrObject)
    {        
        if(arrObject[strKey])
        {            
            objReturnValue = arrObject[strKey];
        }
    }
    return objReturnValue;
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
