function mascara(obj, funcao){
    v_obj = obj;
    v_fun = funcao;
    setTimeout("executa_mascara()", 1);
}

function executa_mascara() {
    v_obj.value = v_fun(v_obj.value);
}

function soNumeros(valor) {
    return valor.replace(/\D/g,"")
}

function pct(valor) {
	valor = valor.replace(/\D/g,"");
	if(valor>100)
		valor = valor.replace(/(\d{2})(\d)/,"$1");	
    return valor;
}

function valmes(valor) {
	valor = valor.replace(/\D/g,"");
	if(valor>12)
		valor = valor.replace(/(\d{1})(\d)/,"$1");	
    return valor;
}

function maskcarteira(v){
    v=v.replace(/\D/g,"")
    v=v.replace(/(\d{3})(\d)/,"$1 $2")       
    v=v.replace(/(\d{12})(\d)/,"$1 $2")
    return v
}

function money(valor) {
    valor = valor.replace(/\D/g,"")	
	valor = valor.replace(/(\d{1,10})(\d{2})/,"$1,$2") 
	return valor;
}
function masktelefone(valor) {
    valor = valor.replace(/\D/g,"")
    valor = valor.replace(/^(\d\d)(\d)/g,"($1) $2")
    valor = valor.replace(/(\d{4})(\d)/,"$1-$2")
    return valor
}
function maskrg(valor) {
    // Remove tudo que não é número do valor.
    valor = valor.replace(/\D/g,"")	
	valor = valor.replace(/(\d{1,3})(\d{3})(\d{3})/,"$1.$2.$3") 
	return valor;
}
function maskcpf(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 maskcnpj(v){
   v=v.replace(/\D/g,"")
   v=v.replace(/^(\d{2})(\d)/,"$1.$2")
   v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3")
   v=v.replace(/\.(\d{3})(\d)/,".$1/$2")
   v=v.replace(/(\d{4})(\d)/,"$1-$2")
   return v
}
function maskcep(v){
   v=v.replace(/\D/g,"");
   v=v.replace(/^(\d{5})(\d)/,"$1-$2")
   return v
}
function maskdata(valor){ 
     valor = valor.replace(/\D/g,"")
     valor = valor.replace(/^(\d\d)(\d)/g,"$1/$2")
     valor = valor.replace(/(\d{2})(\d)/,"$1/$2")
     return valor
}

function maskhora(valor){ 
     valor = valor.replace(/\D/g,"")                 
     valor = valor.replace(/^(\d\d)(\d)/g,"$1:$2")	
     return valor
}
function validar_email(obj) {
    var email = obj.value;

    var regEmail = new RegExp(".+?@.+?[.].+?");
    if (regEmail.test(email) || email=='') {
        return true;
    } else {
		mensagemErro(obj,"Por favor insira um e-mail válido.");	
		obj.value = '';
		obj.focus();
    }
}

function iniTexto(obj,txt){
	if(obj.value==""){
		obj.value=txt;
	}
}

function selectAll(obj, sty){
	$$(sty).each(function(ele){		
		if(obj.checked==true)
			ele.checked = true;
		else
			ele.checked = false;
	});
}
function validaAbreviacao(obj){
	if(verificaAbreviacao(obj.value)){
		mensagemErro(obj, "O Nome não pode conter abreviações.");
		return false;
	}
	return true;
}
function verificaAbreviacao(val){
	var nomes = val.split(" ");
	for(var i=0;i<nomes.length;i++){
		if((nomes[i].length < 2 || nomes[i].lastIndexOf(".") > 0) && nomes[i]!='e' && nomes[i]!='E' && nomes[i]!=''){			
			return true; 
		}
	}		
}
var objAtual;
function validarHora(obj){
	val = obj.value;
	objAtual = obj;
	ret = true;
	if(val.length!=0){
		if (val.length != 5) {
			alert('Insira uma hora válida!');
			obj.value='';
			ret = false;
		}else{
			var hora = val.split(":");

			if (hora[0] < 0 || hora[0] >= 24) {
				alert('Insira uma hora válida!');
				obj.value = '';
				ret = false;
			}
			else {
				if (hora[1] < 0 || hora[1] >= 60) {
					alert('Insira uma hora válida!');
					obj.value = '';
					ret = false;
				}
			}
		}
	}	
	if(ret==false){
		window.setTimeout("objAtual.focus()",1);
	}
}

function validarData(obj, nasc){
    val = obj.value;
    if (val.length != 0) {
        if (val.length != 10) {
			setTimeout("$('"+obj.id+"').focus()",1);
            alert('Insira uma data válida!');
            obj.value = '';
			
            return false;
        }
        else {
            dia = val.substr(0, 2);
            mes = val.substr(3, 2)-1;
            ano = val.substr(6, 4);
            var dataN = new Date(ano,mes,dia);
                        
            anoN = dataN.getFullYear();            
            mesN = dataN.getMonth();
            mesN = mesN+1;
            if( mesN<10 ){
            	mesN = '0'+mesN;
            }
            diaN = dataN.getDate();
            if( diaN<10 ){
            	diaN = '0'+diaN;
            }
                       
            var atual = new Date();                                 
            if ( diaN+'/'+mesN+'/'+anoN != val) {
                alert('Insira uma data válida!');
                obj.value = '';
				setTimeout("$('"+obj.id+"').focus()",1);
                return false;
            }            
            if (nasc == true && atual < dataN) {
                alert('A data de nascimento não pode ser maior que a data atual.');
                obj.value = '';
				setTimeout("$('"+obj.id+"').focus()",1);
                return false;
            }                       
        }
    }
}

function verificaIguais(n,str){						
	if(n==str.length){			
		return true;
	}
	else{
		if(str.substr(0,1)==str.substr(n,1)){
			return verificaIguais(++n,str);
		}else{				
			return false;
		}
	}		
}

//Fun��o de valida��o de CPF
function isCPF(st) {
	
	st = st.replace('.','');
	st = st.replace('.','');
	st = st.replace('-','');
	
	
	if(verificaIguais(0,st)) return false;			
	
	

	if (st == "")
		return (false);
	
	l = st.length;
	
	//aleterado para se usu�rio n�o digitar os zeros na frente do CPF, completar sozinho
	if ((l == 9) || (l == 8)){
		for (i = l ; i < 10; i++){
			st = '0' + st
		}
	}

	l = st.length;
	st2 = "";
	for (i = 0; i < l; i++) {
		caracter = st.substring(i,i+1);
		if ((caracter >= '0') && (caracter <= '9'));
		st2 = st2 + caracter;
	}

	if ((st2.length > 11) || (st2.length < 10))
		return (false);

	if (st2.length==10)
		st2 = '0' + st2;

	digito1 = st2.substring(9,10);
	digito2 = st2.substring(10,11);
	digito1 = parseInt(digito1,10);
	digito2 = parseInt(digito2,10);
	sum = 0; mul = 10;

	for (i = 0; i < 9 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10) * mul;
		sum += tproduct;
		mul--;
	}

	dig1 = ( sum % 11 );

	if ( dig1==0 || dig1==1 )
		dig1=0;
	else
		dig1 = 11 - dig1;

	if (dig1!=digito1)
		return (false);

	sum = 0;
	mul = 11;

	for (i = 0; i < 10 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10)*mul;
		sum += tproduct;
		mul--;
	}
	
	dig2 = (sum % 11);
	
	if ( dig2==0 || dig2==1 )
		dig2=0;
	else
		dig2 = 11 - dig2;
	
	if (dig2 != digito2)
		return (false);
	
	if( st == "00000000000" || st == "0000000000" || st == "000000000" || st == "000000000" )
		return (false);
		
	return (true);
}

function validaCPF(obj){
	if(obj.value.length!=14 && obj.value.length>0){
		mensagemErro(obj,"CPF inválido.");
		obj.focus();
		return false;
	}
	if(!isCPF(obj.value) && obj.value.length>0){
		mensagemErro(obj,"CPF inválido.");
		return false;
	}
	return true;
}
function verificaCPF(val){
	if(val.length!=14 && val.length>0){
		return false;
	}
	if(!isCPF(val) && val.length>0){
		return false;
	}
	return true;
}

function fechar(obj,destroy) {
	$(obj).parent().fadeTo(400, 0, function () {
		$(this).slideUp(400,function(){
			if(destroy==1){
				$(obj).parent().remove();
			}
		});
	});
	return false;
}

function fechar2(obj,destroy) {
	$(obj).fadeTo(400, 0, function () {
		$(this).slideUp(400,function(){
			if(destroy==1){
				$(obj).remove();
			}
		});
	});
	return false;
}


var mensagemErro = function(field, message) {
	$(field).parents("div:first").find('.notification').remove();
	$(field).parents("div:first").append('<div class="notification error png_bg"><a class="close" href="javascript: ;" onclick="fechar(this)"><img alt="close" title="Close this notification" src="'+sysUrl+'img/icons/cross_grey_small.png"/></a><div>'  + message +  '</div></div>');
};

function maskpis(v){
    v = v.replace(/\D/g, "")
	v = v.replace(/(\d{3})(\d)/, "$1.$2")
    v = v.replace(/(\d{5})(\d)/, "$1.$2")
    v = v.replace(/(\d{2})(\d{1})$/, "$1-$2")
	
    return v
}

