var lastFocusedControlId = "";

function focusHandler(e) 
{   
    if (e.srcElement)
    {
        lastFocusedControlId = e.srcElement.id;
    }
    else if (e.originalTarget)
    {
        lastFocusedControlId = e.originalTarget.id;
    }
}

function appInit() 
{    
    if (typeof(window.addEventListener) !== "undefined") 
    {        
        window.addEventListener("focus", focusHandler, true);
    }    
    
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(pageInitializeRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoading);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageEndRequest);
}

function pageInitializeRequest(sender, args) 
{    
    //Guardar el elemento con el foco
    if (((navigator.appName=="Opera") || (navigator.appName=="Microsoft Internet Explorer")) && (typeof(document.activeElement) !== "undefined"))
    {
        lastFocusedControlId = document.activeElement.id;
    }
    
    //Deshabilitar los SELECT y los Flash/Applets
    SendToBackTelepizzaControls(null);
    
    //Configurar el UpdateProgress
    pnlWait = document.getElementById('pnlWaiting');
    if (pnlWait)
    {
        var size = getPageSize();
        pnlWait.style.width = (size[0] + 'px');
        pnlWait.style.height = (size[1] + 'px');
        pnlWait.style.top = "0";
        pnlWait.style.left = "0";
        pnlWait.style.zIndex = "100";
        
        document.body.onresize=function()
        {
            var pnlWait = document.getElementById("pnlWaiting");
            if (pnlWait)
            {
                var size = getPageSize();
                pnlWait.style.width = (size[0] + 'px');
            }
        }
    }
}

function pageEndRequest(sender, args) 
{
    if ((args!=null) && (args.get_error()!=null))
    {
         //Recuperar los SELECT y los Flash/Applets
        BringToTopTelepizzaControls();
    }
    
    //Reestablecer el foco
    if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") 
    {        
        var newFocused = document.getElementById(lastFocusedControlId);        
        if (newFocused) 
        {            
            focusControl(newFocused);        
        }  
    }
}

function pageLoading(sender, args) 
{
     //Recuperar los SELECT y los Flash/Applets
    BringToTopTelepizzaControls();
}

function focusControl(targetControl) 
{    
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) 
    {        
        var focusTarget = targetControl;        
        if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined")) 
        {            
            oldContentEditableSetting = focusTarget.contentEditable;            
            focusTarget.contentEditable = false;        
        }        
        else 
        {            
            focusTarget = null;        
        }        
        if (targetControl)
        {
            try
            {
                targetControl.focus();        
            }
            catch(err){}
        }
        if (focusTarget) 
        {            
            focusTarget.contentEditable = oldContentEditableSetting;        
        }    
    }    
    else 
    {       
        if (targetControl)
        { 
            try
            {
                targetControl.focus();    
            }
            catch(err){}
        }
    }
}

Sys.Application.add_init(appInit);