﻿// JScript File
// ***** AJAX UTILS ****
// must be reference after <AjaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />

// *********** Disable initiator control of postback *************

// Array of controls (client) IDs to disable on postback
// only the initiator will be disabled
controlsToDisableOnPostback = new Array();    

// Get a reference to the PageRequestManager.
if(Sys.WebForms != null)
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
     
    prm.add_initializeRequest(InitializeRequest);  
}

function AddControlToDisableOnPostback(controlID)
{
    controlsToDisableOnPostback.push(controlID);
}
 
// Executed anytime an async postback occurs.
function InitializeRequest(sender, args) 
{        
    if (controlsToDisableOnPostback.length > 0)
    {        
        // get element which caused the post back
        var elm = $get(args._postBackElement.id);
        
        // check if it is one of the registered controls
        var isFound = false;
        for (var i = 0; i < controlsToDisableOnPostback.length; i++)
        {            
            if (elm.id == controlsToDisableOnPostback[i]) 
            {
                isFound = true;
                break;
            }
        }
    
        if (isFound)
        {
            // found - disable all the registered controls
            for (var i = 0; i < controlsToDisableOnPostback.length; i++)
            {
                var elm = $get(controlsToDisableOnPostback[i]);
                if (elm != null) 
                {   
                    elm.disabled = true; 
                    if (elm.href != 'undefined')   
                    {
                        // for linkbutton
                        elm.href = 'javascript:nothing()';                    
                    }
                }
            }
        }
    }
}   
function nothing() {}

function ellipsis(elmID, widthLimit) 
{                                                   
    var e = document.getElementById(elmID);  
    if (e == null)
    {
        return;  
    }                         
    if (e.offsetWidth > widthLimit)                                   
    {
        t = e.innerHTML;                        
        var next = t.length - 1;                                            
        var isFound = false;                        
        var w = e.offsetWidth;
        while (w + 10 > widthLimit)
        {                                            
            var prev = next;        
            var w = e.offsetWidth;
            if (w > widthLimit)
            {                   
                next = parseInt(next * widthLimit / w);
            }                                                        
            if (next + 1 >= prev) break;
            
            var t2 = t.substr(0, next);
            e.innerHTML = t2 + '…';
        }                        
    }                                                                                                                    
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();