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
  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){
  alert(erro);
  return false;
  } else {
 // alert("CNPJ valido!");
  }
  return true;
}

function validaFrmGeral(){
	obj = document.getElementsByTagName("input");
	
	for (i=0;i<obj.length;i++){
		
		if (obj[i] != null){
			if (obj[i].className == "Obrigatório"){
				
				switch(obj[i].type){
					
					case "text":
					
						if(obj[i].value == ""){
							
							obj[i].focus();
							alert("Preencha o campo "+obj[i].title)
							return false;
						}
					
					break;
						
				}
		
			}
		}
		
	}
	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 validaEmail(){
	var email=document.formulario.email.value;
	var wemail
	
	if (email==""){
		window.alert("Digite seu e-mail");
		document.formulario.email.focus();
		return false;
	}
	
	if (email.indexOf(" ") > -1){
		window.alert("E-mail inválido. Por favor, digite um e-mail válido.");
		document.formulario.email.focus();
		return false;
	}else{
		if (email.indexOf("@") == -1){
			window.alert("E-mail inválido. Por favor, digite um e-mail válido.");
			document.formulario.email.focus();
			return false;
		}else{
			wemail = email.split("@")
			
			if (wemail[1].indexOf(".") == -1){
				window.alert("E-mail inválido. Por favor, digite um e-mail válido.");
				document.formulario.email.focus();
				return false;
			}
		}
	}
	
	return true;
	document.formulario.submit();
}

function validaFormGeral(id)
{
	if(id != "")

	{

		//obj = document.getElementById(id).getElementsByTagName("select");	

		obj = $("#"+id + " .obrigatorio")

	}

	else

	{

		//obj = document.getElementsByTagName("select");

		obj = $(".obrigatorio")

	}	

	
	
	for (i=0;i<obj.length;i++)

	{

		if(obj[i].value == "" && obj[i].type != "radio" && obj[i].type != "checkbox")
		{

			alert("Preencha o campo "+obj[i].title)

			objFocus = obj[i]
			
			return false

			break;

		}
		if( obj[i].type == "radio" || obj[i].type == "checkbox")
		{
			
			if( $("input[name='" + obj[i].name + "']:checked").length ==0 )	
			{
				alert("Selecione - "+obj[i].title)

				objFocus = obj[i]
				
				return false
	
				break;		
			}
		}
		

	}
	
	if (validaEmails() == false)

	{

		return false

	}

	if (validaDatas() == false)

	{

		return false

	}

	if (validaValorFloat() == false)

	{

		return false

	}
	if( $("input[name='cpf']").length > 0)
	{
		if( $("input[name='cpf']").val() != "")
		{
			var wcpf = $("input[name='cpf']").val()
			wcpf = wcpf.replace(".","")
			wcpf = wcpf.replace(".","")
			wcpf = wcpf.replace(".","")
			wcpf = wcpf.replace("-","")	
			
			if( validaCPF(wcpf)	== false )
			{
				objFocus = $("input[name='cpf']")
				alert("CPF inválido, digite novamente")
				return false;
			}
		}
	}
	if( $("input[name='cnpj']").length > 0)
	{
		if( $("input[name='cnpj']").val() != "")
		{
			var wcpf = $("input[name='cnpj']").val()
						
			if( validaCNPJ(wcpf) == false )
			{
				objFocus = $("input[name='cpf']")
				alert("CNPJ inválido, digite novamente")
				return false;
			}
		}
	}
	if ( ( $("input[name='senha2']").length > 0 ||  $("input[name='senhac']").length > 0) && ( $("#senha").length > 0 ))
	{							
		var senhaConf = "";
		if( $("input[name='senha2']").length > 0)
		{
			senhaConf = $("input[name='senha2']").val()
		}
		else
		{
			senhaConf = $("input[name='senhac']").val()	
		}
		
		if( senhaConf != $("#senha").val() )
		{
			objFocus = $("#senha")
			alert("Senhas não correspondem, digite novamente");
			return false;
		}
	}	
	
	if( tipo_arq(id) == false )
	{
		return false	
	}
	return true
}

function validaEmails(){

	obj = document.getElementsByTagName("input");

	for (i=0;i<obj.length;i++)

	{		

		if (obj[i])

		{

			if( (obj[i].alt == "email") && (obj[i].value != "") )  

			{

				if (validaEmail(obj[i].value) == false)

				{

					alert("E-mail inválido");

					objFocus = obj[i]

					return false

					break;

				}

			}

			

		}

	}

	return true;
}

function validaDatas()
{
	
	obj = document.getElementsByTagName("input");
	for (i=0;i<obj.length;i++)
	{		
		if (obj[i])
		{
			if( (obj[i].value != "") && (obj[i].alt == "data") )
			{
				if( validaData(obj[i].value) == false)
				{
					alert(obj[i].title + " inválida")
					objFocus = obj[i]
					return false
					break;
				}
			}

		}
	}
	return true;
}

function validaValorFloat()
{
	obj = document.getElementsByTagName("input");
	for (i=0;i<obj.length;i++)
	{		
		if (obj[i])
		{
			if( (obj[i].alt == "valor") && (obj[i].value != "") )  
			{
				if (isFloat(obj[i].value) == false)
				{
					objFocus = obj[i]
					alert("Valor inválido");
					return false
					break;
				}
			}
			
		}
	}
	return true;
}

function validaCPF(cpf) {
		
		 erro = new String;
		 cpf = cpf.substring(0,11)
		 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificação do CPF! \n\n";
		 var nonNumbers = /\D/;
		 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
		 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
				 erro += "Número de CPF inválido!"
	   }
	   var a = [];
	   var b = new Number;
	   var c = 11;
	   for (i=0; i<11; i++){
			   a[i] = cpf.charAt(i);
			   if (i < 9) b += (a[i] * --c);
	   }
	   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	   b = 0;
	   c = 11;
	   for (y=0; y<10; y++) b += (a[y] * c--);
	   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			   erro +="Digito verificador com problema!";
	   }
	   if (erro.length > 0){
			   //alert(erro);
			   return false;
	   }
	   return true;
}
