/* Compatabilidade de Eventos para Outros Browsers */
function CompatibilidadeEvent(componente, evento)
{
    if (navigator.appName.indexOf('Microsoft') == -1)
        componente[evento] = PermitirApenasValoresNumericos;
}

/* Permitir digitar apenas números */
function PermitirApenasValoresNumericos(e, array)
{
    /* Tratamento para eventos. Internet explorer não suporta Events (DOM Nível 2) */
    if (navigator.appName.indexOf('Microsoft') != -1 && e)
    {
        array = e;
        e = undefined;
    }

    var valor = null;
    
    /* Capturando tecla pressionada */
    if (navigator.appName.indexOf('Microsoft') != -1)
        valor = event.keyCode;
    else
    {
        valor = e.which;
        
        if (valor == 0)
            valor = e.keyCode;
    }
    
    if ( !EhTeclaComum(valor) && (valor < 48 || valor > 57) && !BuscarTeclaArray(valor, array) )
    {
        if (e && e.stopPropagation)        
            e.stopPropagation();
        else
            event.cancelBubble = true;
        
        if (e && e.preventDefault)
            e.preventDefault();
        else
            event.returnValue = false;
        
        return;
    }
}

/* Teclas: TAB, ENTER, BACKSPACE, DELETE etc.  */
function EhTeclaComum(valor)
{
    var valores = [8,9,13,27,32,35,36,37,38,39,40,127];
    for(var i = 0; i < valores.length; i++)
    {
        if (valor == valores[i])
        {
            return true;
            break;
        }
    }
    
    return false;
}

/* Pesquisar Valores em Array */
function BuscarTeclaArray(valor, array)
{    
    if (array)
    {
        for(var i = 0; i < array.length; i++)
        {
            if (valor == array[i])
            {
                return true;
                break;
            }
        }
    }
    
    return false;
}


/*
===========================================================
FUNÇÕES PARA INTERATIVIDADE NA INTERFACE COM O USUÁRIO
===========================================================
*/
function ProximoControle(e, proximoComponenteUI, proximoComponenteUIehBotao) {
    var valor = CapturarTeclaPressionada(e);
    if (valor == 13) {
        if (proximoComponenteUIehBotao != null && proximoComponenteUIehBotao)
            CliqueAutomaticonoBotao(proximoComponenteUI)
        else
            DefinirFocus(proximoComponenteUI);
            
        CancelarPropagacaodeEventos(e);
    }
}

function CliqueAutomaticonoBotao(proximoComponenteUI)
{
    var componente = proximoComponenteUI.toString().replace(/_/g, "$")
    __doPostBack(componente, '');
}

function DefinirFocus(componenteUI) {
    var elemento = document.getElementById(componenteUI);
    if (elemento != null) elemento.focus();
}

function EsconderControle(componenteUI) {
    var elemento = document.getElementById(componenteUI);
    if (elemento != null) elemento.style.display = "none";
}

/*
===========================================================
FUNCOES QUE UTILIZAM TECLADO
===========================================================
*/
function CapturarTeclaPressionada(e) {
    var valor;

    if (navigator.appName.indexOf('Microsoft') != -1)
        valor = event.keyCode;
    else
        valor = e.which;

    return valor;
}

/*
===========================================================
FUNÇÕES VOLTADAS PARA MANIPULAÇÃO DE EVENTOS E DERIVADOS
===========================================================
*/
function CancelarPropagacaodeEventos(e) {
    if (e && e.stopPropagation)
        e.stopPropagation();
    else
        event.cancelBubble = true;

    if (e && e.preventDefault)
        e.preventDefault();
    else
        event.returnValue = false;
    
    return;
}
