﻿function Navegar(url) {
    window.location = url;
    return false;
}

function fnOnlyNumber(key) {
    if (document.all) {
        var aKey = key.keyCode;
    }
    else {
        var aKey = key.which;
    }

    if (!(aKey == 0 || aKey == 37 || aKey == 9 || aKey == 8 || (aKey > 47 && aKey < 58))) {
        if (document.all) {
            key.returnValue = false;
        }
        else {
            return false;
        }
    }
}

function SoMoeda(key) {
    if (document.all)
        var aKey = key.keyCode;
    else
        var aKey = key.which;

    if (!(aKey == 0 || aKey == 37 || aKey == 9 || aKey == 8 || aKey == 45 || aKey == 44) && !(aKey > 47 && aKey < 58)) {
        if (document.all)
            key.returnValue = false;
        else
            return false;
    }
}

function Formata(obj_Text, decimais) {
    var posicaoPontoDecimal;
    var campo = '';
    var resultado = '';
    var pos, sep, dec;

    //Retira possiveis separadores de milhar
    for (pos = 0; pos < obj_Text.value.length; pos++) {
        if (obj_Text.value.charAt(pos) != '.')
            campo = campo + obj_Text.value.charAt(pos);
    }

    //Formata valor monetário com decimais
    posicaoPontoDecimal = campo.indexOf(',');
    if (posicaoPontoDecimal != -1) {
        sep = 0;
        for (pos = posicaoPontoDecimal - 1; pos >= 0; pos--) {
            sep++;
            if (sep > 3) {
                resultado = '.' + resultado;
                sep = 1;
            }

            resultado = campo.charAt(pos) + resultado;
        }

        // Trata parte decimal
        if (parseInt(decimais) > 0) {
            resultado = resultado + ',';

            pos = posicaoPontoDecimal + 1;
            for (dec = 1; dec <= parseInt(decimais); dec++) {
                if (pos < campo.length) {
                    resultado = resultado + campo.charAt(pos);
                    pos++;
                }
                else
                    resultado = resultado + '0';
            }

        } // trata decimais
    }
    // Trata valor monetário sem decimais
    else {
        sep = 0;
        for (pos = campo.length - 1; pos >= 0; pos--) {
            sep++;
            if (sep > 3) {
                resultado = '.' + resultado;
                sep = 1;
            }
            resultado = campo.charAt(pos) + resultado;
        }
        // Trata parte decimal
        if (parseInt(decimais) > 0) {
            resultado = resultado + ',';
            for (dec = 1; dec <= parseInt(decimais); dec++) {
                resultado = resultado + '0';
            }
        } // trata decimais
    }

    if (resultado == ",00" || resultado == "") {
        obj_Text.value = "0";
        Formata(obj_Text, decimais);
        return
    }

    obj_Text.value = resultado;
}

function fnIsDate(camp_data) {
    /* ####### Função para Validação de Datas ############ */
    if (camp_data.value == '') return;
    var NumValid = "0123456789";
    var Campo = camp_data;
    var DateValue = "";
    var DateTemp = "";
    var Seperator = "/";
    var Dia;
    var Mes;
    var Ano;
    var leap = 0;
    var err = 0;
    var i;
    var Arruma;
    err = 0;
    Arruma = Campo.value.split("/");
    try {
        if (Arruma[0].length == 1) {
            DateValue = '0' + "" + Arruma[0];
        }
        else {
            DateValue = Arruma[0];
        }
        if (Arruma[1].length == 1) {
            DateValue = DateValue + "/" + '0' + "" + Arruma[1];
        }
        else {
            DateValue = DateValue + "/" + Arruma[1];
        }
        DateValue = DateValue + "/" + Arruma[2];
    }
    catch (e) {
        err = 1;
    }
    /* Deleta todos os caracteres exceto 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (NumValid.indexOf(DateValue.substr(i, 1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i, 1);
        }
    }
    DateValue = DateTemp;
    /* Sempre modifica a data para 8 digitos*/
    /* Se o ano for digitado com 2 digitos assume 20xx */
    if (DateValue.length == 6) {
        if (DateValue.substr(4, 2) > 50) {
            DateValue = DateValue.substr(0, 4) + '19' + DateValue.substr(4, 2);
        }
        else {
            DateValue = DateValue.substr(0, 4) + '20' + DateValue.substr(4, 2);
        }
    }
    else if (DateValue.length == 4) {
        var lngAno = "";
        var myDate = new Date();
        lngAno = myDate.getFullYear();
        DateValue = DateValue.substr(0, 4) + "" + lngAno
    }
    if (DateValue.length != 8) {
        err = 1;
    }
    /* ano é considerado inválido se for = 0000 */
    Ano = DateValue.substr(4, 4);
    if (Ano == 0) {
        err = 1;
    }
    /* Validação do Mês */
    Mes = DateValue.substr(2, 2);
    if ((Mes < 1) || (Mes > 12)) {
        err = 1;
    }
    /* Validação do Dia */
    Dia = DateValue.substr(0, 2);
    if (Dia < 1) {
        err = 1;
    }
    /* Validação do ano bissexto referente ao mês de fevereiro */
    if ((Ano % 4 == 0) || (Ano % 100 == 0) || (Ano % 400 == 0)) {
        leap = 1;
    }

    if ((Mes == 2) && (leap == 1) && (Dia > 29)) {
        err = 1;
    }

    if ((Mes == 2) && (leap != 1) && (Dia > 28)) {
        err = 1;
    }

    /* Validação dos outros meses */
    if ((Dia > 31) && ((Mes == "01") || (Mes == "03") || (Mes == "05") || (Mes == "07") || (Mes == "08") || (Mes == "10") || (Mes == "12"))) {
        err = 1;
    }
    if ((Dia > 30) && ((Mes == "04") || (Mes == "06") || (Mes == "09") || (Mes == "11"))) {
        err = 1;
    }

    /* Se não houver erro escreve a data completa no campo input com os separadores (ex. 07/01/2004) */
    if (err == 0) {
        Campo.value = Dia + Seperator + Mes + Seperator + Ano;
    }

    /* Escreve mensagem de erro se err != 0 */
    else {
        alert("Por favor, preencha a data corretamente.\n\nFormato dd/mm/aaaa.");
        Campo.select();
        Campo.focus();
        return false;
    }
    tempData = '<%=Format(Now,"yyyyMMdd")%>';
    if (Number(Ano + "" + Mes + "" + Dia) > Number(tempData)) {
        alert("Por favor, preencha a data corretamente.\n\nFormato dd/mm/aaaa.");
        Campo.select();
        Campo.focus();
        return false;
    }
}

function mskDate(X, Y, Z, setFocus) {
    var myX = '';
    myX = myX + X;
    if (myX.length == 2) {
        myX = myX + '/';
        Y.value = myX;
    }
    if (myX.length == 5) {
        myX = myX + '/';
        Y.value = myX;
    }
    if (myX.length == 10) {
        if (setFocus == true) {
            Z.focus();
        }
    }
}

function dialogModal(url, nome, nrTamanho, nrLargura) {
    eval(window.showModalDialog(url, nome,'Resizable:no; DialogHeight:'+nrTamanho+'px ; DialogWidth:'+nrLargura+'px; Edge:raised; Help:no; Scroll:no; Status:no; Center:yes;'));
    //eval(caixa);
}

function fnAbrePop(strUrl, strW, x, y) {

    var lado = (screen.width - x) / 2;
    var topo = ((screen.height - 40) - y) / 2;
    var w = window.open(strUrl, strW, "menubar=no, toolbar=no, status=no, location=no, resizable=no, scrollbars=yes, dependent=yes,width=" + x + ",height=" + y + ",top=" + topo + ",left=" + lado);
    try {
        w.focus();
    }
    catch (e) { }
}

function maxLength(ta, limit) {
    if (ta.value.length >= limit) {
        ta.value = ta.value.substring(0, limit - 1);
    }
}
