/*
	#################################################
	#                CONFIG JAVASCRIT               #
	#    Auto: Thiago Silva (Pedreiro Digital)      #
	#          www.pedreirodigital.com.br           #       
	#################################################
*/
//extanciar classe
var objAlerta = new Alerta();
//extanciar classe 
//var objMenu = new Menu();
//extanciar classe
var objAjax = new Ajax();

/**
* Formata o Campo de acordo com a mascara informada.
* Ex de uso: onkeyup="AplicaMascara('HORA', this);".
* @author Igor Escobar (blog@igorescobar.com)
* @param Mascara String que possui o nome da mascara de formatação do campo.
* @param elemento Campo que será formatado de acordo com a mascara, voce pode informar o id direto ou o próprio elemento usando o this.
* @returns {void}
* @methods
   - DATA: 
   - HORA:
   - CEP:
   - CPF:
   - CNPJ
   - TEL
   - TELDDD
*/
function AplicaMascara(Mascara, elemento)
{
        
    // Seta o elemento
    var elemento = (elemento) ? elemento : document.getElementById(elemento); 
    if(!elemento) return false;
    
    // DEFINE AS REGRAS DE FORMATAÇÃO DOS CAMPOS
    var Regexs = {
        DATA: /(([0-9]){2})(([0-9]){2})(([0-9]){4})/,
        HORA: /(([0-9]{2}))(([0-9]){2})(([0-9]){2})/,
        CEP: /(([0-9]{5}))(([0-9]){3})/,
        CPF: /(([0-9]{3}))(([0-9]){3})(([0-9]){3})(([0-9]){2})/,
        CNPJ: /(([0-9]{2}))(([0-9]){3})(([0-9]){3})(([0-9]){4})(([0-9]){2})/,
        TEL: /(([0-9]){4})(([0-9]){4})/,
        TELDDD: /(([0-9]){2})(([0-9]){4})(([0-9]){4})/
    }
    MyRegex = eval('Regexs.'+Mascara);
    
    // INSTANCIA A REGRA REGEX PARA FORMATACAO
    getRegexGroups = new RegExp(MyRegex);
    
    // INSTANCIA A REGRA QUE VERIFICA SE O CAMPO POSSUI SOMENTE NÚMEROS
    onlyNumbers = new RegExp(/^([0-9]+)$/);
    
    // ESTE É O CAMPO COM AS IMPURESAS (DIGITADO PELO USUÁRIO FINAL)
    var CampoSujo = elemento.value;
    
    // RETIRA OS EXCESSOS DOS CAMPOS PARA QUE POSSAMOS APLICAR A MASCARA EM UM CAMPO LIMPO
    var oValue = CampoSujo.replace(/([./;:,\-()]+)/g,"");
    
    // VERIFICA SE A REGRA QUE ELE SE ESCOLHEU SE APLICA AO TEXTO DIGITADO NO CAMPO 
    if(getRegexGroups.test(oValue) == true){
        //APLICA A REGRA NO CAMPO E FORMATA
        switch(Mascara){
            case 'DATA': oNewCampo = oValue.replace(getRegexGroups,"$1/$3/$5"); break;
            case 'HORA': oNewCampo = oValue.replace(getRegexGroups,"$1:$3:$5"); break;
            case 'CEP': oNewCampo = oValue.replace(getRegexGroups,"$1$3-$5"); break;
            case 'CPF': oNewCampo = oValue.replace(getRegexGroups,"$1.$3.$5-$7"); break;
            case 'CNPJ': oNewCampo = oValue.replace(getRegexGroups,"$1.$3.$5/$7-$9"); break;
            case 'TEL': oNewCampo = oValue.replace(getRegexGroups,"$1-$3"); break;
            case 'TELDDD': oNewCampo = oValue.replace(getRegexGroups,"($1)$3-$5"); break;
        }
		
        // Retorna o valor do elemento com seu novo valor
        elemento.value = oNewCampo; 
    } 
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}
