﻿
function ValidaContrato(componente) {
    if (!componente.checked) {
        alert("Selecione a opção 'Li e aceito o contrato' antes de cadastrar");
    }
}

function busca() {

  document.getElementById("btnSalvar_Click").click();
}

function LimparComponente(pLimpar, idCodigo, idDescricao) {

    if (pLimpar == true) {

        if (idCodigo.value == "") {
            return false
        }
        document.getElementById("btnLimpar").click();

    }
    else {
        document.getElementById(idCodigo.id).value = "";
        document.getElementById(idDescricao.id).value = "";
    }

    //      if (document.getElementById('FCKeditor1') != null) {
    //       var oEditor = FCKeditorAPI.GetInstance('FCKeditor1');
    //        oEditor.EditorDocument.body.innerHTML = "";
    //    }

}

function Limpa() {
    document.getElementById("btnLimpar").click();

}

function Novo(idCodigo) {

    if (idCodigo.value == "") {
        idCodigo.value = "#NOVO#"
    }

}
function BloqueiaLetras(evento) {
    var tecla;

    if (window.event) { // Internet Explorer
        tecla = event.keyCode;
    }
    else { // Firefox
        tecla = evento.which;
    }

    //if (tecla >= 97 && tecla <= 105 || tecla >= 48 && tecla <= 57 || tecla == 8 || tecla == 9 || tecla == 35 || tecla == 36 || tecla == 46 || tecla == 16) return true;
    //return false;
}



function moeda(componente) {
    valor = componente.value;

    if (valor.length > 9 && valor.indexOf(",") == -1) {
        valor = valor.substring(0, 9)
    }

    valor = valor.replace(/\D/g, "")  //permite digitar apenas números
    valor = valor.replace(/[0-9]{12}/, "inválido")   //limita pra máximo 999.999.999,99
    valor = valor.replace(/(\d{1})(\d{8})$/, "$1.$2")  //coloca ponto antes dos últimos 8 digitos
    valor = valor.replace(/(\d{1})(\d{5})$/, "$1.$2")  //coloca ponto antes dos últimos 5 digitos
    valor = valor.replace(/(\d{1})(\d{1,2})$/, "$1,$2")    //coloca virgula antes dos últimos 2 digitos
    componente.value = valor;
}


// JavaScript Document
//adiciona mascara de cnpj
function MascaraCNPJ(cnpj) {
    if (mascaraInteiro(cnpj) == false) {
        event.returnValue = false;
    }
    return formataCampo(cnpj, '00.000.000/0000-00', event);
}

//adiciona mascara de cep
function MascaraCep(cep) {
    if (mascaraInteiro(cep) == false) {
        event.returnValue = false;
    }
    return formataCampo(cep, '00.000-000', event);
}

//adiciona mascara de data
function MascaraData(data) {
    if (mascaraInteiro(data) == false) {
        event.returnValue = false;
    }
    return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel) {
    if (mascaraInteiro(tel) == false) {
        event.returnValue = false;
    }
    return formataCampo(tel, '(00) 0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf) {
    if (mascaraInteiro(cpf) == false) {
        event.returnValue = false;
    }
    return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel) {
    exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if (!exp.test(tel.value))
        alert('Numero de Telefone Invalido!');
}

//valida CEP
function ValidaCep(cep) {
    exp = /\d{2}\.\d{3}\-\d{3}/
    if (!exp.test(cep.value))
        alert('Numero de Cep Invalido!');
}

//valida data
function ValidaData(data) {
    exp = /\d{2}\/\d{2}\/\d{4}/
    if (!exp.test(data.value))
        alert('Data Invalida!');
}

//valida o CPF digitado
function ValidarCPF(Objcpf) {
    var cpf = Objcpf.value;
    exp = /\.|\-/g
    cpf = cpf.toString().replace(exp, "");
    var digitoDigitado = eval(cpf.charAt(9) + cpf.charAt(10));
    var soma1 = 0, soma2 = 0;
    var vlr = 11;

    for (i = 0; i < 9; i++) {
        soma1 += eval(cpf.charAt(i) * (vlr - 1));
        soma2 += eval(cpf.charAt(i) * vlr);
        vlr--;
    }
    soma1 = (((soma1 * 10) % 11) == 10 ? 0 : ((soma1 * 10) % 11));
    soma2 = (((soma2 + (2 * soma1)) * 10) % 11);

    var digitoGerado = (soma1 * 10) + soma2;
    if (digitoGerado != digitoDigitado)
        alert('CPF Invalido!');
}

//valida numero inteiro com mascara
function mascaraInteiro() {
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false;
        return false;
    }
    return true;
}

//valida o CNPJ digitado
function ValidarCNPJ(ObjCnpj) {
    var cnpj = ObjCnpj.value;
    var valida = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
    var dig1 = new Number;
    var dig2 = new Number;

    exp = /\.|\-|\//g
    cnpj = cnpj.toString().replace(exp, "");
    var digito = new Number(eval(cnpj.charAt(12) + cnpj.charAt(13)));

    for (i = 0; i < valida.length; i++) {
        dig1 += (i > 0 ? (cnpj.charAt(i - 1) * valida[i]) : 0);
        dig2 += cnpj.charAt(i) * valida[i];
    }
    dig1 = (((dig1 % 11) < 2) ? 0 : (11 - (dig1 % 11)));
    dig2 = (((dig2 % 11) < 2) ? 0 : (11 - (dig2 % 11)));

    if (((dig1 * 10) + dig2) != digito)
        alert('CNPJ Invalido!');

}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento) {
    var boleanoMascara;

    var Digitato = evento.keyCode;
    exp = /\-|\.|\/|\(|\)| /g
    campoSoNumeros = campo.value.toString().replace(exp, "");

    var posicaoCampo = 0;
    var NovoValorCampo = "";
    var TamanhoMascara = campoSoNumeros.length; ;

    if (Digitato != 8) { // backspace
        for (i = 0; i <= TamanhoMascara; i++) {
            boleanoMascara = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
                                || (Mascara.charAt(i) == "/"))
            boleanoMascara = boleanoMascara || ((Mascara.charAt(i) == "(")
                                || (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " "))
            if (boleanoMascara) {
                NovoValorCampo += Mascara.charAt(i);
                TamanhoMascara++;
            } else {
                NovoValorCampo += campoSoNumeros.charAt(posicaoCampo);
                posicaoCampo++;
            }
        }
        campo.value = NovoValorCampo;
        return true;
    } else {
        return true;
    }
}

// Limpa um string
function limpar(valor, conjuntoVerdade) {
    var resultado = "";
    for (var a = 0; a < valor.length; a++) {
        if (conjuntoVerdade.indexOf(valor.substring(a, a + 1)) >= 0) {
            resultado += valor.substring(a, a + 1);
        }
    }
    return resultado;
}

function validaSenha(campo) {
    if (campo.value.length != 0) {
        if (campo.value.length < 6) {
            alert('Por favor, sua senha tem quer ter pelo menos 6 digitos');
            campo.focus();
            return false;
        }
        else {
            return true;
        }
    }
}

function retiraEspacos(string) {
    var i = 0;
    var final = '';
    while (i < string.length) {
        if (string.charAt(i) == ' ') {
            final += string.substr(0, i);
            string = string.substr(i + 1, string.length - (i + 1));
            i = 0;
        }
        else {
            i++;
        }
    }
    return final + string;
}