/*
  [*] Script: Valida Contato
  [*] Author: PictureWeb - Web System Projects
  [*] Copyright 2002 © PictureWeb http://www.pictureweb.com.br
*/

<!--// Preenchendo o formulário... //-->

function envia() {

	// Verifica se NOME está preenchido
	if (document.contato.Nome.value == "")
	{
		alert("Digite seu NOME.");
		document.contato.Nome.focus();
		return (false);
	}

	// Verifica se a Empresa está preenchido
	if (document.contato.Empresa.value == "")
	{
		alert("Por favor preencha o campo EMPRESA.");
		document.contato.Empresa.focus();
		return (false);
	}

	// Verifica se CNPJ está preenchido
	/*
	// Verifica se o campo CNPJ tem 11 dígitos
	if(document.contato.cnpj.value.length < 11){
		alert("O CNPJ não deve ter menos de onze dígitos.");
		document.contato.cnpj.focus();
		return (false);
	}
	
	// Verifica se o campo CNPJ tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.cnpj.value;
	checkStr = checkStr.replace(/ /g, '');
	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;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{
	alert("O CNPJ deve conter apenas números");
	document.contato.cnpj.focus();
	return (false);
	}
	*/


	// Verifica se E-MAIL está preenchido
	if (!document.contato.Email.value == ""){
	// Verifica se o E-MAIL contem @ e pelo menos 1 .
	var checkcliemail = "@.";
	var checkStr = document.contato.Email.value;
	checkStr = checkStr.replace(/ /g, '');
	var cliemailValid = false;
	var cliemailAt = false;
	var cliemailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkcliemail.length;  j++)
	{
	if (ch == checkcliemail.charAt(j) && ch == "@")
	cliemailAt = true;
	if (ch == checkcliemail.charAt(j) && ch == ".")
	cliemailPeriod = true;
		  if (cliemailAt && cliemailPeriod)
			break;
		  if (j == checkcliemail.length)
			break;
		}
	
	// Se existir @ na string
	if (cliemailAt && cliemailPeriod)
	{
			cliemailValid = true
			break;
		}
	}
	if (!cliemailValid){
	alert("Por favor, digite um E-MAIL válido!");
	document.contato.Email.focus();
	return (false);
	}
	}

	/*
	// Verifica se DDD está preenchido
	if (!document.contato.DDD.value == ""){
		// Verifica se o campo DDD tem dois dígitos
		if(document.contato.DDD.value.length < 2){
			alert("O DDD deve ter dois dígitos.");
			document.contato.DDD.focus();
			return (false);
		}
	
		// Verifica se o campo DDD tem somente números
		var checkOK = "0123456789";
		var checkStr = document.contato.DDD.value;
		checkStr = checkStr.replace(/ /g, '');
		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;
			}
			if (ch != ",")
			allNum += ch;
		}
		if (!allValid){
			alert("O DDD deve conter apenas números");
			document.contato.DDD.focus();
			return (false);
		}
	}
	*/
	
	// Verifica se TELEFONE está preenchido
	if (!document.contato.Telefone.value == ""){
	// Verifica se o campo TELEFONE tem 7 ou 8 dígitos
	if(document.contato.Telefone.value.length < 2){
	alert("O TELEFONE deve conter de 7 a 8 dígitos.");
	document.contato.Telefone.focus();
	return (false);
	}

	// Verifica se o campo TELEFONE tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.Telefone.value;
	checkStr = checkStr.replace(/ /g, '');
	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;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid)
	{
	alert("O TELEFONE deve conter apenas números");
	document.contato.Telefone.focus();
	return (false);
	}
	}

	// Verifica se Email ou Telefone está preenchido
	if ((document.contato.Email.value == "")&&(document.contato.Telefone.value == "")){
		alert("Digite seu Email ou Telefone.");
		document.contato.Email.focus();
		return (false);
	}


	/*
	// Verifica se Telefone está preenchido então DDD tem que estar também
	if ((!document.contato.Telefone.value == "")&&(document.contato.DDD.value == "")){
		alert("Digite seu DDD.");
		document.contato.DDD.focus();
		return (false);
	}
	*/


	if (document.contato.Comentarios.value == ""){
		alert("Digite seu COMENTÁRIO ou MENSAGEM.");
		document.contato.Comentarios.focus();
		return (false);
	}


	//alert(document.contato.cliente.length);
	selecionou_cliente = false;
	for (z=0; z<document.contato.cliente.length;  z++) {
		if (document.contato.cliente(z).checked == true) {
			//alert(document.contato.cliente(z).value);
			selecionou_cliente = true;
		}
	}
	if (selecionou_cliente == false) {
		alert("Por favor selecione se é ou não cliente corcerama.");
		return false;
	}

	//document.contato.submit();	
}


