function vercpf(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")
return false;
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;}



function validaCNPJ(CNPJ) {
  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
 
  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){
  //alert(erro);
  return false;
  } else {
 // alert("CNPJ valido!");
  }
  return true;
}



function confSenha(){
	
	wsenha = document.getElementById("senha");
	wsenhac = document.getElementById("senhac");
	
	if(wsenha.value != ""){
		document.getElementById("alerta").style.display="none";
		if(wsenhac.value != ""){
			if(wsenha.value == wsenhac.value){
				document.getElementById("alerta").style.display="none";
			}else{
				document.getElementById("alerta").style.display='';
				document.getElementById("alerta").innerHTML="Os campos \"Senha\" não coicidem.";
			}
		}else{
			document.getElementById("alerta").style.display='';
			document.getElementById("alerta").innerHTML="O campo \"Senhac\" não pode ser vazio.";
		}
	}else{
		document.getElementById("alerta").style.display='';
		document.getElementById("alerta").innerHTML="O campo \"Senha\" não pode ser vazio.";
	}
}

function confEmail(){
	
	wemail = document.getElementById("alerta");
	
	if(wemail.value == "S"){
		if(senha.value == senhac.value && senha.value != ''){
			return validaFrmGeral()
		}else{
			alert('Os campos Senha não podem ser vazios');
			return false
		}
	}else{
		alert('Digite outro e-mail.');
		return false
	}
}


function isNumeric(valor)
{
   	var str = /^\d+$/;
	return str.test(valor);
}
function validaEmail(email)
{
	
	var reTipo = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	return reTipo.test(email);

}
function validaData(data)
{
	var expre =	/^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
	if (expre.test(data) == false)
	{
		return false
	}	
	return true;
}


function validaFrmGeral(){
	obj = document.getElementsByTagName("input");
	
	for (i=0;i<obj.length;i++){
		
		if (obj[i] != null){
			if (obj[i].className == "obrigatorio")
			{
				
				switch(obj[i].type)
				{
					
					case "text":
					
						if(obj[i].value == ""){
							
							obj[i].focus();
							alert("Preencha o campo "+obj[i].title)
							return false;
						}	
							
						
						
						if (obj[i].name == "end_eletronico")
						{
							if (validaEmail(obj[i].value)== false)
							{
								alert("E-mail inválido");
								obj[i].focus();
								return false;
							}
						}
						if (obj[i].name == "ddd_telefone")
						{
							ddd = obj[i].value;
							if ( (isNumeric(ddd)== false) || (ddd.length < 2) )
							{
								alert("DDD inválido");
								obj[i].focus();
								return false;
							}
						}						
						if (obj[i].name == "telefone")
						{
							tel = obj[i].value;
							if ( (isNumeric(tel)== false) || (tel.length < 8) )
							{
								alert("Telefone inválido");
								obj[i].focus();
								return false;
							}
						}						
						if (obj[i].name == "cep")
						{
							cep = obj[i].value;
							cep2 = cep.replace("-","");
							if( (cep.charAt(5) != "-") || (cep.length < 9) || (isNumeric(cep2)== false) )
							{
								alert("CEP inválido");
								obj[i].focus();
								return false;
							}
						}							
						
						break;						
				}
				
			}
			if (obj[i].className == "caixa_input")
			{
				
				if((obj[i].name == "end_eletronico") && (obj[i].value != ""))
				{
					if (validaEmail(obj[i].value)== false)
					{
						alert("E-mail inválido");
						obj[i].focus();
						return false;
					}
				}
				if ((obj[i].name == "ddd_celular") && (obj[i].value != ""))
				{
					ddd = obj[i].value;
					if ( (isNumeric(ddd)== false) || (ddd.length < 2) )
					{
						alert("DDD inválido");
						obj[i].focus();
						return false;	
					}
				}
				if (obj[i].name == "celular")
				{
					
					if ( (obj[i].value != "") || (document.getElementById("ddd_celular").value != "") )
					{
						if(obj[i].value == "")
						{
							
							alert("Preencha o campo "+obj[i].title)
							obj[i].focus();
							return false;
						}
						cel = obj[i].value;
						if ( (isNumeric(cel)== false) || (cel.length < 8) )
						{
							alert("Celular inválido");
							obj[i].focus();
							return false;	
						}
					}
				}
				if ( (obj[i].name == "ddd_telefone") && (obj[i].value != ""))
				{
					ddd = obj[i].value;
					if ( (isNumeric(ddd)== false) || (ddd.length < 2) )
					{
						alert("DDD inválido");
						obj[i].focus();
						return false;
					}
				}
				if (obj[i].name == "telefone")
				{
					if ( (obj[i].value != "") || (document.getElementById("ddd_telefone").value != "") )
					{
						
						if(obj[i].value == "")
						{
							
							alert("Preencha o campo "+obj[i].title)
							obj[i].focus();
							return false;
						}
						tel = obj[i].value;
						if ( (isNumeric(tel)== false) || (tel.length < 8) )
						{
							alert("Telefone inválido");
							obj[i].focus();
							return false;
						}
					}
				}
				if ( (obj[i].name == "ddd_fax") && (obj[i].value != ""))
				{
					ddd = obj[i].value;
					if ( (isNumeric(ddd)== false) || (ddd.length < 2) )
					{
						alert("DDD inválido");
						obj[i].focus();
						return false;
					}
				}
				if (obj[i].name == "fax")
				{
					if ( (obj[i].value != "") || (document.getElementById("ddd_fax").value != "") )
					{
						
						if(obj[i].value == "")
						{
							
							alert("Preencha o campo "+obj[i].title)
							obj[i].focus();
							return false;
						}
						fax = obj[i].value;
						if ( (isNumeric(fax)== false) || (fax.length < 8) )
						{
							alert("Fax inválido");
							obj[i].focus();
							return false;
						}
					}
				}
			}
		}
		
	}
	return true;
}

function validaIndicacao()
{
	if (validaFrmGeral() == false)
	{
		return false;
	}
	data = document.getElementById("data")
	if (validaData(data.value) == false)
	{
		alert("Data inválida");
		data.focus();
		return false;
	}
	
	posicao = document.getElementById("posicao").value;
	for (i=0;i < posicao;i++)
	{
		nome = document.getElementById("desc_"+i);
		data = document.getElementById("data_"+i);			
		if( (nome.value != "") || (data.value != "") )
		{											 
			if(nome.value != "")
			{
				if (data.value != "")
				{
					if (validaData(data.value) == false)
					{
						alert("Data de contato inválida");
						data.focus();
						return false;
						break;
					}
				}
				else
				{
					alert("Preencha o campo data de contato");
					data.focus();
					return false;
					break;
				}
			}
			else
			{
				alert("Preecha todos as descrições");
				nome.focus();
				return false;	
				break;
			}
		}
	}	
}

function validaVenda()
{
	if(validaFrmGeral() == false)
	{
		return false;
	}
	data = document.getElementById("data")
	corretor = document.getElementById("corretor")
	empresa = document.getElementById("empresaPlano")
	if (validaData(data.value) == false)
	{
		alert("Data inválida");
		data.focus();
		return false;
	}
	if(empresa.value == "0")
	{
		alert("Selecione uma empresa")
		empresa.focus();
		return false;
	}
	if(corretor.value == "")
	{
		alert("Selecione um corretor")
		corretor.focus();
		return false;
	}
	if(document.getElementById("pessoaF").checked == true)
	{
		nome = document.getElementById("nome");
		cpf = document.getElementById("cpf");
		nascimento = document.getElementById("data_nac");		
		if (nome.value == "")
		{
			alert("Preencha o campo Nome");
			nome.focus();
			return false;
		}
		
		if (cpf.value == "")
		{
			alert("Preencha o campo CPF");
			cpf.focus();
			return false;
		}
		cpf_aux = cpf.value;
		cpf_aux = cpf_aux.replace(".","");
		cpf_aux = cpf_aux.replace(".","");
		cpf_aux = cpf_aux.replace("-","");
		if (vercpf(cpf_aux) == false)
		{
			alert("CPF inválido");
			cpf.focus();
			return false;
		}
		if (nascimento.value == "")
		{
			alert("Preencha o campo Data de Nascimento");
			nascimento.focus();
			return false;
		}
		if (validaData(nascimento.value) == false)
		{
			alert("Data de nascimento inválida");
			nascimento.focus();
			return false;
		}
	}
	if(document.getElementById("pessoaJ").checked == true)
	{
		razao = document.getElementById("razao");
		cnpj = document.getElementById("cnpj");
		if (razao.value == "")
		{
			alert("Preencha o campo Razão Social");
			razao.focus();
			return false;
		}
		if (cnpj.value == "")
		{
			alert("Preencha o campo CNPJ");
			cnpj.focus();
			return false;
		}
		if (validaCNPJ(cnpj.value) == false)
		{
			alert("CNPJ inválido");
			cnpj.focus();
			return false;
		}	
	}
	posUsuario = document.getElementById("posicao").value;
	cont = 0
	for(i=0;i < posUsuario ;i++)
	{
		if (document.getElementById("nome_"+i).value != "")
		{
			cont = 1;
			break;
		}	
	}
	if (cont == 0)
	{
		alert("Nenhum usuário incluído");
		return false;
	}
	if (validaUsuarios() == false)
	{
		return false;	
	}
	if (vendaCalcula == false)
	{
		alert("Efetue o calculo da venda")
		return false;
	}
	posComissaoEmp = document.getElementById("totEmp").value;
	posComissaoCorret = document.getElementById("totCorret").value;
	for(i=1;i < posComissaoEmp ;i++)
	{
		data = document.getElementById("vencimentoEmp_"+i);
		if (data.value == "")
		{
			alert("Preencha todas as datas de vencimento");
			data.focus();
			return false;
			break;
		}
		else
		{
			if (validaData(data.value) == false)
			{
				alert("Data de vencimento inválida");
				data.focus();
				return false;
				break;
			}
		}
	}
	for(i=1;i < posComissaoCorret ;i++)
	{
		data = document.getElementById("vencimentoCorret_"+i);
		if (data.value == "")
		{
			alert("Preencha todas as datas de vencimento");
			data.focus();
			return false;
			break;
		}
		else
		{
			if (validaData(data.value) == false)
			{
				alert("Data de vencimento inválida");
				data.focus();
				return false;
				break;
			}
		}
	}
	empresa = document.getElementById("empresaPlano");
	var comissao = document.getElementById("comissaoEmp");
	var comissaoCorret = document.getElementById("comissaoCorret");
	if(empresa.value == "0")
	{
		alert("Selecione uma empresa");
		empresa.focus();
		return false
	}
	if(comissao.value == "0")
	{
		alert("Selecione uma comissão de empresa");
		empresa.focus();
		return false
	}
	if(comissaoCorret.value == "0")
	{
		alert("Selecione uma comissão de corretor");
		comissaoCorret.focus();
		return false
	}

}

