var lastFocusedControlId = "";
function focusHandler(e) 
{
    try{
        document.activeElement = e.originalTarget;
    } catch (error) {
       
    }
}

function appInit() {
    try{
        if (typeof (window.addEventListener) !== "undefined") 
        {
            window.addEventListener("focus", focusHandler, true);
        }
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
    }
    catch (error) {
       
    }
}

function pageLoadingHandler(sender, args) 
{
    try{
    lastFocusedControlId = typeof (document.activeElement) === "undefined"
    ? "" : document.activeElement.id;
    }
    catch (error) {
       
    }
}

function focusControl(targetControl) 
{
    try{
    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.disabled !== true) {
            targetControl.focus();
        }
        if (focusTarget) 
        {
            focusTarget.contentEditable = oldContentEditableSetting;
        }
    }
    else 
    {
        targetControl.focus();
    }
    }
    catch (error) {
       
    }
}

function pageLoadedHandler(sender, args) 
{
    try{
        if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") 
        {
            var newFocused = $get(lastFocusedControlId);
            if (newFocused) 
            {
                focusControl(newFocused);
            }
        }
    }
    catch (error) {
       
    }
}


          Sys.Application.add_init(appInit);
