﻿//
//esta função abre os popups modais
function popup(Largura, Altura, Endereco) {
    var Retorno;    //recebe o retorno do popup
    var IE;         //internet explorer??    
    var Versao;     //versão do IE
    //
    //Pega o navegador
    IE = document.all != undefined;
    //    
    //se for internet explorer abre o popup
    if (IE) {
        //
        //pega a versão
        Versao = navigator.appVersion.substring(22, 23);
        //
        if (Versao < 7) {
            //            
            Retorno = window.showModalDialog('../popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status:yes;unadorned:no;center:yes;resizable:no;dialogHeight:' + eval(parseInt(Altura) + 50) + 'px;dialogWidth:' + eval(parseInt(Largura) + 10) + 'px;');
        } else {
            Retorno = window.showModalDialog('../popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status:yes;unadorned:1;center:yes;resizable:no;dialogHeight:' + Altura + 'px;dialogWidth:' + Largura + 'px;');
        }
    } else {
        Retorno = window.open('../popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status=yes,scrollbars=yes,width=' + Largura + ',height=' + Altura);
        Retorno = true;
    }
    //
    //retorna o valor
    return Retorno;
}
//esta função abre os popups modais
function popup_site(Largura, Altura, Endereco) {
    var Retorno;    //recebe o retorno do popup
    var IE;         //internet explorer??    
    var Versao;     //versão do IE
    //
    //Pega o navegador
    IE = document.all != undefined;
    //    
    //se for internet explorer abre o popup
    if (IE) {
        //
        //pega a versão
        Versao = navigator.appVersion.substring(22, 23);
        //
        if (Versao < 7) {
            //            
            Retorno = window.showModalDialog('popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status:yes;unadorned:no;center:yes;resizable:no;dialogHeight:' + eval(parseInt(Altura) + 50) + 'px;dialogWidth:' + eval(parseInt(Largura) + 10) + 'px;');
        } else {
            Retorno = window.showModalDialog('popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status:yes;unadorned:1;center:yes;resizable:no;dialogHeight:' + Altura + 'px;dialogWidth:' + Largura + 'px;');
        }
    } else {
        Retorno = window.open('popup.aspx?endereco=' + Endereco + '&altura=' + Altura + '&Largura=' + Largura, '', 'status=yes,scrollbars=yes,width=' + Largura + ',height=' + Altura);
        Retorno = true;
    }
    //
    //retorna o valor
    return Retorno;
}
//
//função que mostra ou oculta objetos
function mostra(objeto) {
    if (document.getElementById(objeto)) {        
        document.getElementById(objeto).style.display = "block";        
    }
};
function oculta(objeto) {
    if (document.getElementById(objeto)) {        
        document.getElementById(objeto).style.display = "none";        
    }
};
function mostra_oculta(objeto) {
    if (document.getElementById(objeto)) {
        if (document.getElementById(objeto).style.display == '' || document.getElementById(objeto).style.display == undefined || document.getElementById(objeto).style.display=='none') {
            document.getElementById(objeto).style.display = "block";
        } else {
            document.getElementById(objeto).style.display = "none";
        };        
    };
};
//
//Formata número tipo moeda usando o evento onKeyDown
function Moeda(campo, tammax, teclapres, decimal) {
    if (teclapres.keyCode) { tecla = teclapres.keyCode; }; //IE
    if (teclapres.charCode) { tecla = teclapres.charCode; }; // firefox
    if (teclapres.witch) { tecla = teclapres.witc; }; //netscape    
    vr = Limpar(campo.value, "0123456789");    
    tam = vr.length;
    dec = decimal
    //    
    if (tam < tammax && tecla != 8) { tam = vr.length + 1; }
    //
    if (tecla == 8)
    { tam = tam - 1; }
    //
    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
        //		
        if (tam <= dec) {
            campo.value = vr;
        }
        //
        if ((tam > dec) && (tam <= 5)) {
            campo.value = vr.substr(0, tam - 2) + "," + vr.substr(tam - dec, tam);
        }
        //
        if ((tam >= 6) && (tam <= 8)) {
            campo.value = vr.substr(0, tam - 5) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
        }
        // 
        if ((tam >= 9) && (tam <= 11)) {
            campo.value = vr.substr(0, tam - 8) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
        }
        //
        if ((tam >= 12) && (tam <= 14)) {
            campo.value = vr.substr(0, tam - 11) + "." + vr.substr(tam - 11, 3) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
        }
        //
        if ((tam >= 15) && (tam <= 17)) {
            campo.value = vr.substr(0, tam - 14) + "." + vr.substr(tam - 14, 3) + "." + vr.substr(tam - 11, 3) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - 2, tam);
        }
    }
}
//
function Limpar(valor, validos) {
    // retira caracteres invalidos da string 
    var result = "";
    var aux;
    for (var i = 0; i < valor.length; i++) {
        aux = validos.indexOf(valor.substring(i, i + 1));
        if (aux >= 0) {
            result += aux;
        };
    };
    return result;
};
//
//Função que só aceita números
function numeros(e) {
    //
    if (e.keyCode) { tecla = e.keyCode; }; //IE
    if (e.charCode) { tecla = e.charCode; }; // firefox
    if (e.witch) { tecla = e.witc; }; //netscape
    //  
    //excessões
    //44 = ","
    //46 = delete e "."
    //8 = backspace
    //35 = home
    //36 = end
    //
    //Verifica se é um valor númerico
    if ((tecla >= 48 && tecla <= 57) || tecla==44 || tecla == 46 || tecla == 8 || tecla==35 || tecla==36) {
        return true;
    } else {    
        return false;
    };
};
//
//Formata os campos utilizando a mascara passada
function formata(strField, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;
    //
    if (document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    }
    else if (document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    }
    //
    //Pega o valor do campo
    sValue = strField.value;
    //		
    //verifica se o campo ja atingiu o tamanho máximo
    if (sValue.length == sMask.length || sValue.length > sMask.length) {
        return false;
    }
    //		
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(" ", "");
    sValue = sValue.toString().replace(" ", "");
    sValue = sValue.toString().replace(":", "");
    fldLen = sValue.length;
    mskLen = sMask.length;
    //
    //Zera os contadores
    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;
    //
    //Loop para o tratamento
    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ":") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
        //
        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        }
        else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }
        i++;
    }
    //
    //Caso a mascara seja ##/#### (Mês/ano) verifica se esta correto)
    if (sMask == "##/####") {
        //
        //verifica se atingiu o tamanho
        if (sCod.length == 6) {
            //
            //Verifica se os 2 primeiros numeros (mes) são menores que 12 (meses)
            ivalor = sCod.substring(0, 2);
            if (ivalor > 12) {
                alert('formato incorreto (mm/aaaa)')
                return false
            }
            //
            //Verifica se o primeiro digito do ano é 0
            if (sCod.substring(3, 4) == '0') {
                alert('formato incorreto (mm/aaaa)');
                return false;
            }
        }
    }
    //
    //Caso  a mascara seja ##/##/#### (dia/mes/ano) verifica se esta correto
    if (sMask == "##/##/####") {
        //
        //verifica se atingiu o tamanho
        if (sCod.length == 9) {
            //
            //verifica se o dia é válido
            if (sCod.substring(0, 2) > 31) {
                alert('formato incorreto (dd/mm/aaaa)');
                return false;
            }
            //
            //verifica se o mes é válido
            if (sCod.substring(3, 5) > 12) {
                alert('formato incorreto (dd/mm/aaaa)');
                return false;
            }
            //
            //verifica se o ano é válido			
            if (sCod.substring(6, 7) == '0') {
                alert('formato incorreto (dd/mm/aaaa)');
                return false;
            }
        }
    }
    //
    strField.value = sCod;
    if (nTecla != 8) { // backspace
        if (sMask.charAt(i - 1) == "9") { // apenas números...
            return ((nTecla > 47) && (nTecla < 58));
        } // números de 0 a 9
        else { // qualquer caracter...
            return true;
        }
    } else {
        return true;
    }
}
//
function hover(objeto, imagem){
    objeto.src = imagem;
}