function CWIShowTimeoutMessage(sender, args)     
{   
    var error = args.get_error();
    if (error != null)        
    {         
        if(error.name == "Sys.WebForms.PageRequestManagerTimeoutException")
        {
            CWIMessageBoxShow(CWITimeoutMessage, CWIApplicationName, "warning");             
        }
        else
        {
            var message = "<textarea style='width:345px;height:160px;' readonly>" + error.message + "</textarea>";
            CWIMessageBoxShow(message, CWIAsyncErrorCaption, "error");             
        }
        args.set_errorHandled(true); 
    } 
}

function CWISetFocusFirstControl(parentControl)
{
    var children = parentControl.childNodes;
    
    if(children == null)
    {
        return;
    }
    
    for(var i = 0; i < children.length; i++)
    {
        var control = children[i];
        if(control != null && control.tagName != null)
        {            
            if(control.style.display == "none" || control.style.visibility == "hidden")
            {
                continue;
            }
            
            var controlTagName = control.tagName.toUpperCase();
            var isInput = controlTagName == "INPUT";
            if(isInput)
            {
                var controlType = control.type.toUpperCase();
                
                isInput = controlType == "TEXT" || controlType == "CHECKBOX" || controlType == "RADIO" || controlType == "FILE" || controlType == "PASSWORD";
            }
            
            if((isInput || controlTagName == "SELECT" || controlTagName == "TEXTAREA") 
            && !control.disabled && !control.readOnly)
            {
                try
                {                            
                    CWISetFocus(control);
                }
                catch(ex)
                {           
                                                
                    setTimeout("CWISetFocus($get('" + control.id + "');", 500);
                }                                
                return true;
            }
            else
            {
                if(CWISetFocusFirstControl(control))
                {
                    return true;
                }
            }
        }
    }
}

function CWISetFocus(obj)
{
    try
    {        
        if(obj.disabled == false 
        && obj.style.display != "none" 
        && obj.style.visibility != "visibility"
        && obj.tabIndex != -1)
        {                   
            // O IE possui um bug de não setar o foco caso o elemento ativo seja o destino do foco.
            if(document.activeElement != null && document.activeElement == obj)
            {
                document.activeElement.blur();
            }
            obj.focus();
        }
    }
    catch(ex)
    {
        // O IE6 possui um bug de tentar dar foco em um controle quando a página já não está mais vísivel.
    }    
}

function CWIChildPageClosing()
{    
    return true;
}


function ConfigureParentCWIChildPageClosing()
{
    try
    {
        if(parent != null && parent.CWIChildPageClosing != null)
        {
            parent.CWIChildPageClosing = function() {return CWIPageClosing();};       
        }
    }
    catch(ex)
    {
        // Caso tente acessar um endereço de um domínio diferente será gerada uma exceção de segurança.
        // Em páginas de domínio diferentes não é possível utilizar o evento OnClosing.
    }
}
