// JavaScript Document

function setType(type,nam,val){
		document.getElementById('camponome').innerHTML= type;
		document.getElementById('campocpf').innerHTML = nam;
		document.getElementById('pes').value=val;
		document.getElementById('cpfcnpj').value='';
}

	
function maskTel(id){
	var tel = document.getElementById(id);
	valor = tel.value;
	
	if(valor.length==2){
	  temp = '('+valor+') ';
	  tel.value=temp;
	}
	
	if(valor.length==9){
		temp=valor+'-';
		tel.value=temp;
	}
	
	if(valor.length==14){
		if(!valTel(valor)){
			alert('Somente Valores Numericos');
			tel.value='';
			return false;
		}else{
			return true;
		}
	}
}

function maskTel2(id){
	var tel = document.getElementById(id);
	valor = tel.value;
	
	if(valor.length==9){
		temp=valor+'-';
		tel.value=temp;
	}
	
	if(valor.length==9){
		if(!valTel(valor)){
			alert('Somente Valores Numericos');
			tel.value='';
			return false;
		}else{
			return true;
		}
	}
}

function valTel(val){
	ddd = val.substr(1,2);
	pre = val.substr(5,4);
	suf = val.substr(10,4);
	all=ddd+pre+suf;
	if(!verCar(all)){
	  return false;
   }else{
	   return true;
   }
}


function verCar(ver){
	for(i=0;i<ver.length;i++){
		v=ver.substr(i,1);
		if(isNaN(v)){
			return false;
		}
	}
	return true;
}





function maskData(idd){
	dt = document.getElementById(idd);
	vdt = dt.value;
	tam = vdt.length;

	if(tam==2){
		tmp = vdt + "/";
		dt.value=tmp;
	}
	
	if(tam==5){
		tmp = vdt+"/";
		dt.value=tmp;
	}
	
	if(tam==10){
		valDt(vdt,dt);
	}
}

function valDt(valor,idt){
	mess = document.getElementById('msgdata');
	var dts = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	
	
	dia = valor.substr(0,2);
	mes = valor.substr(3,2);
	ano = valor.substr(6,4);
	
	if(mes=="08"){
		mes=8;
	}else if(mes=="09"){
		mes=9;
	}else{
		mes = parseInt(mes);
	}
	
	dtSys = new Date();
	anoAt = dtSys.getFullYear();
		
	if((ano>anoAt)||(ano<(anoAt-1))){
		alert("Ano Invalido");
		idt.value="";
		return false;
	}else if((mes>12)||(mes<0)){	
		alert("Mes Invalido");
		idt.value="";
		return false;
	}else if(dia>dts[mes]){
		alert("Data Invalida");
		idt.value="";
		return false;
	}else{
		return true;
	}
}

function maskCPFCNPJ(id,tp){
	document.getElementById('msgc').innerHTML='';
	tps = document.getElementById(tp).value;
	if(tps==0){
		maskCPF(id);
	}else{
		maskCNPJ(id);
	}
}

function maskCPF(id){
	tcpf = document.getElementById(id);
	cpfv = tcpf.value;
	
	if(cpfv.length==3){
		cpfv+=".";
		tcpf.value=cpfv;
	}else if(cpfv.length==7){
		cpfv+=".";
		tcpf.value=cpfv;
	}else if(cpfv.length==11){
		cpfv+="-";
		tcpf.value=cpfv;
	}else if(cpfv.length==14){
		validarCPF(cpfv);
	}
}

function maskCNPJ(id){
	tcpf = document.getElementById(id);
	cpfv = tcpf.value;
	
	if(cpfv.length==2){
		cpfv+=".";
		tcpf.value=cpfv;
	}else if(cpfv.length==6){
		cpfv+=".";
		tcpf.value=cpfv;
	}else if(cpfv.length==10){
		cpfv+="/";
		tcpf.value=cpfv;
	}else if(cpfv.length==15){
		cpfv+="-";
		tcpf.value=cpfv;
	}else if(cpfv.length==18){
		validaCNPJ(cpfv);
	}
}

function validarCPF(cpf){
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   mess = document.getElementById('msgc');
   if(!filtro.test(cpf)){
     mess.innerHTML = "CPF inválido. Tente novamente.";
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  mess.innerHTML ="CPF inválido. Tente novamente.";
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 mess.innerHTML ="CPF inválido. Tente novamente.";
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     mess.innerHTML ="CPF inválido. Tente novamente.";
	 return false;
   }
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }


function validaCNPJ(CNPJ) {
mess = document.getElementById('msgc');	
erro = new String;
if (CNPJ.length < 18) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
if (erro.length == 0) erro += "E' necessarios preencher corretamente o numero do CNPJ! \n\n";
}
//substituir os caracteres que nao sao numeros
if(document.layers && parseInt(navigator.appVersion) == 4){
x = CNPJ.substring(0,2);
x += CNPJ.substring(3,6);
x += CNPJ.substring(7,10);
x += CNPJ.substring(11,15);
x += CNPJ.substring(16,18);
CNPJ = x;
} else {
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace(".","");
CNPJ = CNPJ.replace("-","");
CNPJ = CNPJ.replace("/","");
}
var nonNumbers = /\D/;
if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n";
var a = [];
var b = new Number;
var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
for (i=0; i<12; i++){
a[i] = CNPJ.charAt(i);
b += a[i] * c[i+1];
}
if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
b = 0;
for (y=0; y<13; y++) {
b += (a[y] * c[y]);
}
if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
erro +="Digito verificador com problema!";
}
if (erro.length > 0){
	mess.innerHTML=erro;
	return false;
} 
return true;
}


function setData(idt){
	dt = document.getElementById(idt);
	data = new Date();
	dia=data.getDate();
	
	if(dia<10){
		dia = "0"+dia;
	}
		
	mes=(data.getMonth()+1);
	
	if(mes<10){
		mes="0"+mes
	}
	
	ano=(data.getFullYear());
	dts = dia+"/"+mes+"/"+ano;
	dt.value=dts;
}


function maskCep(cmp){
	var v=document.getElementById(cmp);
	valor=v.value;
	num=valor.length;
	if (num==5){
		valor+="-";
		v.value=valor;
	}
	if(num==9){
		if(!valCep(valor)){
			alert('Somente Valores Numericos');
			v.value='';
			return false;
		}else{
			return true;
		}
	}
}

function valCep(val){
	cepa = val.substr(0,5);
	cepb = val.substr(7,3);
	all=cepa+cepb;
	if(!verCar(all)){
	  return false;
   }else{
	   return true;
   }
}
