var v_obj=''
var v_fun=''
var verbose_name = {};
var to_validate = [];


function mascara(o,f){
    v_obj=o
    v_fun=f 
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function only_number(val){
    return val.replace(/\D/g,"");
}

function vcpf(val){
    val=val.replace(/\D/g,"")                    //Remove tudo o que nao e digito
    val=val.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    val=val.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    val=val.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hifen entre o terceiro e o quarto dígitos
    return val
}

function vcnpj(val){
    val=val.replace(/\D/g,"")                    //Remove tudo o que nao e digito
    val=val.replace(/(\d{2})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto digitos
    val=val.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto digitos
    val=val.replace(/(\d{3})(\d)/,"$1/$2")       //Coloca um ponto entre o terceiro e o quarto digitos
    val=val.replace(/(\d{4})(\d{1,2})$/,"$1-$2") //Coloca um hifen entre o terceiro e o quarto digitos
    return val
}

function vcep(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{5})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}
 

function vtelefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function vdata(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
    v=v.replace(/(\d{2})(\d)/,"$1/$2")    //Coloca barra entre o segundo e terceiro
    v=v.replace(/(\d{2})(\d)/,"$1/$2")    //Coloca barra entre o quarto e o quinto dígitos
    return v
}

function emailCheck(id) {
    /*
        Funcao que valida o email
    */
	var emailStr = _gel(id).value
    var emailPat=/^(.+)@(.+)$/
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars="\[^\\s" + specialChars + "\]"
    var quotedUser="(\"[^\"]*\")"
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom=validChars + '+'
    var word="(" + atom + "|" + quotedUser + ")"
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
    var matchArray=emailStr.match(emailPat)
	if (_gel(id).value != '')
	{
    if (matchArray==null) {
    	alert("Endereço de Email parece incorreto (verifique \"@\" e \".\")")
    	return false
    }
    var user=matchArray[1]
    var domain=matchArray[2]
    if (user.match(userPat)==null) {
        alert("Conta de email parece ser inválida.")
        return false
    }
    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
    	  for (var i=1;i<=4;i++) {
    	    if (IPArray[i]>255) {
    	        alert("Destino de IP do email inválido.")
    		return false
    	    }
        }
        return true
    }
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
    	alert("Domínio do email inválido.")
        return false
    }
    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 || 
        domArr[domArr.length-1].length>3) {
       alert("O endereço do email deve ter 3 letras de domínio, ou 2 letras de país.")
       return false
    }
    if (len<2) {
       var errStr="Falta hostname do email."
       alert(errStr)
       return false
    }
	}
    return true;
}

function validate_all(element){
    for (var validate in to_validate){
        if ((_gel(element)[to_validate[validate]].value == "") || (_gel(element)[to_validate[validate]].value == "#") || (_gel(element)[to_validate[validate]].value == "0")){ 
            alert("O campo "+ verbose_name[to_validate[validate]] +" é obrigatório!");
            return false;
        }
        if (_gel(element)[to_validate[validate]].name == "email"){
            
            ec = emailCheck(_gel(element)[to_validate[validate]].id);
            if (!ec){
                return false;            
            }

        }
    }
	if (channel_selected && channel_selected == 4){
		if (_gel('territory') && parseInt(_gel('territory').value) == 0){
			alert("O campo território é obrigatório!");
		}
	}
    return valida_endereco('cpf');
}

