function valForm(f)
{
	if(f['nome'].value.length < 2)
	{
		alert("O campo nome deve ser preenchido!");
		f['nome'].focus();
		return false;
	}
	
	if(f['email'].value.length < 2)
	{
		alert("O campo e-mail deve ser preenchido!");
		f['email'].focus();
		return false;
	}
	else
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(f['email'].value))
		{
			alert("O e-mail digitado parece ser inválido!");
			f['email'].focus();
			return (false);
		}
	}
	
	if(f['assunto'].value.length < 2)
	{
		alert("O campo assunto deve ser preenchido!");
		f['assunto'].focus();
		return false;
	}
	
	if(f['mensagem'].value.length < 2)
	{
		alert("O campo mensagem deve ser preenchido!");
		f['mensagem'].focus();
		return false;
	}

	return true;
}

function valForm2(f)
{
	if(f['nome'].value.length < 2)
	{
		alert("O campo Nome deve ser preenchido!");
		f['nome'].focus();
		return false;
	}
	
	if(f['telefone'].value.length < 2)
	{
		alert("O campo Telefone deve ser preenchido!");
		f['telefone'].focus();
		return false;
	}
	
	if(f['email'].value.length < 2)
	{
		alert("O campo E-mail deve ser preenchido!");
		f['email'].focus();
		return false;
	}
	else
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(f['email'].value))
		{
			alert("O e-mail digitado parece ser inválido!");
			f['email'].focus();
			return (false);
		}
	}
	
	if(f['doc'].value.length < 2)
	{
		alert("O campo CPF / CPNJ deve ser preenchido!");
		f['doc'].focus();
		return false;
	}
	else
	{
		// valida o documento
		if(!Validar(f['doc']))
		{
			return false;
		}
	}
	
	if(f['mensagem'].value.length < 2)
	{
		alert("O campo mensagem deve ser preenchido!");
		f['mensagem'].focus();
		return false;
	}

	return true;
}

function Validar(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);
} 

$(document).ready(function(){
	if(msg != "") alert(msg);
});
