﻿jQuery(function ($) {
    $.mask.definitions['~'] = '[ 0-9]';
    $("._cnpj").mask("99.999.999/9999-99");
    $("._cpf").mask("999.999.999-99");
    $("._tel").mask("9999-9999");
    $("._cep").mask("99999-999", { completed: function () { Ajax.ValidaCep(this.val());} });
    $("._data").mask("99/99/9999");
    $("._Numb").bind("keypress keydown keyup", function () { $(this).val($(this).val().replace(/\D/gi, "")); });
    $("._Zero").bind("keypress keydown keyup", function () { $(this).val(($(this).val() == "0" ? "1" : $(this).val())) });
})

// valida email
function ValidaMail() {
    var Retorno = true;
    var Extp = /^([A-Za-z0-9_-][A-Za-z0-9_.-]+@([A-Za-z0-9_-]+\.)+[A-Za-z]{2,4})$/;
    if (!Extp.test(arguments[0]))
        Retorno = false;

    return Retorno;
}

// valida data 
function ValidaData() {
    var Retorno = true;
    var Extp = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
    if (!Extp.test(arguments[0]))
        Retorno = false;

    return Retorno;
}

// valida data 
function ValidaCaracter() {
    var Retorno = true;
    var Extp = /([\W-])/;
    if (!Extp.test(arguments[0]))
        Retorno = false;

    return Retorno;
}

// valida cpf
function ValidaCpf() {
    cpf = arguments[0].replace(/\-/gi, "").replace(/\./gi, "");
    if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
        return false;
    add = 0;
    for (i = 0; i < 9; i++)
        add += parseInt(cpf.charAt(i)) * (10 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11)
        rev = 0;
    if (rev != parseInt(cpf.charAt(9)))
        return false;
    add = 0;
    for (i = 0; i < 10; i++)
        add += parseInt(cpf.charAt(i)) * (11 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11)
        rev = 0;
    if (rev != parseInt(cpf.charAt(10)))
        return false;
    return true;
}

/*Função que padroniza telefone (11) 4184-1241*/
function Telefone(v) {
    v = v.replace(/\D/g, "")
    v = v.replace(/^(\d\d)(\d)/g, "($1) $2")
    v = v.replace(/(\d{4})(\d)/, "$1-$2")
    return v
}
