// JavaScript Document

function validaForm(form, cnpjcpf)
{

	for (i=0;i<form.length;i++)

		{
		if ((form[i].tagName.toUpperCase() != "BUTTON") && (form[i].tagName.toUpperCase() != "FIELDSET"))	//pegar todos os campos diferente de botoes
	        {						

			if (form[i].type != "radio") //pegar todos os campos diferente de radio

				{	
			
				if ((form[i].value == '') || (form[i].value.substring(0,1)==' '))
					{
				  
						var nome = form[i].name;				    
				        nome= nome.substring(7)
				        alert('O campo ' + nome.toUpperCase() + ' é de preenchimento obrigatório!');					       
				        form[i].focus();
				        return false;
				    }
				}

			else //validar somente os campos radio

				{	
				  
					var nome = form[i].name;
					var objCorrente = document.getElementsByName(nome);
					var objMarcado = 0;
					for (x=0;x<objCorrente.length;x++)
					{
						if (objCorrente[x].checked)
						{
							objMarcado = 1
						}
					}
					

					if (objMarcado == 0)
					{
						nome= nome.substring(7)
						alert("O campo " + nome.toUpperCase() + " é de preenchimento obrigatório!");					       
						form[i].focus();
						return false;
					}

				}
					
			}
		
		}
	
	if (cnpjcpf != undefined)
		{
			return ValidarCPFCNPJ(cnpjcpf);
		}
		
			
	
}

function SetarFoco(objAtual, tamanho, objNovo)
{
	//alert(objAtual.value.length)
	if (objAtual.value.length == tamanho)
		{
			objNovo.focus();
		}
}


function ValidarCPFCNPJ(theCPF) 
{ 

  if (theCPF.value == "") 
  { 
    alert("Campo inválido. É necessário informar o CPF ou CNPJ"); 
    theCPF.focus(); 
    return (false); 
  } 
  if (((theCPF.value.length == 11) && (theCPF.value == 11111111111) || (theCPF.value == 22222222222) || (theCPF.value == 33333333333) || (theCPF.value == 44444444444) || (theCPF.value == 55555555555) || (theCPF.value == 66666666666) || (theCPF.value == 77777777777) || (theCPF.value == 88888888888) || (theCPF.value == 99999999999) || (theCPF.value == 00000000000))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 


  if (!((theCPF.value.length == 11) || (theCPF.value.length == 14))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 

  var checkOK = "0123456789"; 
  var checkStr = theCPF.value; 
  var allValid = true; 
  var allNum = ""; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
    allNum += ch; 
  } 
  if (!allValid) 
  { 
    alert("Favor preencher somente com dígitos o campo CPF/CNPJ."); 
    theCPF.focus(); 
    return (false); 
  } 

  var chkVal = allNum; 
  var prsVal = parseFloat(allNum); 
  if (chkVal != "" && !(prsVal > "0")) 
  { 
    alert("CPF zerado !"); 
    theCPF.focus(); 
    return (false); 
  } 

if (theCPF.value.length == 11) 
{ 
  var tot = 0; 

  for (i = 2;  i <= 10;  i++) 
    tot += i * parseInt(checkStr.charAt(10 - i)); 

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(9))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 
  
  tot = 0; 
  
  for (i = 2;  i <= 11;  i++) 
    tot += i * parseInt(checkStr.charAt(11 - i)); 

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(10))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 
} 
else 
{ 
  var tot  = 0; 
  var peso = 2; 
  
  for (i = 0;  i <= 11;  i++) 
  { 
    tot += peso * parseInt(checkStr.charAt(11 - i)); 
    peso++; 
    if (peso == 10) 
    { 
        peso = 2; 
    } 
  } 

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(12))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 
  
  tot  = 0; 
  peso = 2; 
  
  for (i = 0;  i <= 12;  i++) 
  { 
    tot += peso * parseInt(checkStr.charAt(12 - i)); 
    peso++; 
    if (peso == 10) 
    { 
        peso = 2; 
    } 
  } 

  if ((tot * 10 % 11 % 10) != parseInt(checkStr.charAt(13))) 
  { 
    alert("CPF/CNPJ inválido."); 
    theCPF.focus(); 
    return (false); 
  } 
} 
  return(true); 
} 