﻿function checkMail(value) {
    var mail = value;
    if (mail == "")
        return true;
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (er.test(mail))
        return true;
    else {
        return false;
    }
}
function validaCpf(field) {
    var cpf = Ext.getCmp(field).getValue();
    if (cpf == "" || cpf == "undefined")
        return true;
    if (v_cpf(unformatNumber(cpf)) == false) {
        Ext.getCmp(field).markInvalid("CPF Incorreto!");
        return false;
    }
    return true;
}
function validaSenha(fieldSenha, fieldSenhaRepetida) {
    var senha = Ext.getCmp(fieldSenha).getValue();
    var senhaRepetida = Ext.getCmp(fieldSenhaRepetida).getValue();
    if (senha != senhaRepetida) {
        Ext.getCmp(fieldSenhaRepetida).markInvalid("Por favor, digite novamente as senhas");
        return false;
    } else
        return true;
}
function v_cpf(cpf) {
    var numeros, digitos, soma, i, resultado, digitos_iguais;
    digitos_iguais = 1;
    if (cpf.length < 11)
        return false;
    for (i = 0; i < cpf.length - 1; i++)
        if (cpf.charAt(i) != cpf.charAt(i + 1)) {
            digitos_iguais = 0;
            break;
        }
    if (!digitos_iguais) {
        numeros = cpf.substring(0, 9);
        digitos = cpf.substring(9);
        soma = 0;
        for (i = 10; i > 1; i--)
            soma += numeros.charAt(10 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0))
            return false;
        numeros = cpf.substring(0, 10);
        soma = 0;
        for (i = 11; i > 1; i--)
            soma += numeros.charAt(11 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
            return false;
        return true;
    }
    else
        return false;
}
function unformatNumber(pNum) {
    return String(pNum).replace(/\D/g, "");
}
function execmascara() {
    obj.setValue(fun(obj.getValue()));
}
function mascara(objeto, funcao) {
    obj = objeto;
    fun = funcao;
    setTimeout("execmascara()", 0);
}
function mascaraCpf(field) {
    obj = Ext.getCmp(field);
    if (obj.getValue().length > 14) {
        obj.setValue(obj.getValue().substr(0, 14));
        return;
    }
    mascara(obj, cpf);
}
function mascaraTelefone(field) {
    obj = Ext.getCmp(field);
    if (obj.getValue().length > 14) {
        obj.setValue(obj.getValue().substr(0, 14));
        return;
    }
    mascara(obj, telefone);
}
function mascaraMonetaria(field) {
    obj = Ext.getCmp(field);
    if (obj.getValue().length > 14) {
        obj.setValue(obj.getValue().substr(0, 18));
        return;
    }
    mascara(obj, monetaria);
}
function cpf(v) {
    v = v.replace(/\D/g, "");
    v = v.replace(/(\d{3})(\d)/, "$1.$2");
    v = v.replace(/(\d{3})(\d)/, "$1.$2");
    v = v.replace(/(\d{3})(\d{1,2})$/, "$1-$2");
    return v;
}
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;
}
function monetaria(v) {
    v = v.replace(/\D/g, "");
    v = v.replace(/(\d{1})(\d{13})$/, "$2");
    v = v.replace(/(\d{1})(\d{11})$/, "$1.$2");
    v = v.replace(/(\d{1})(\d{8})$/, "$1.$2");
    v = v.replace(/(\d{1})(\d{5})$/, "$1.$2");
    v = v.replace(/(\d{1})(\d{1,2})$/, "$1,$2")
    return v;
}
