/*
  [*] 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;
	}

	// Verifica se RAZÃO SOCIAL está preenchido
	if (document.contato.Razao.value == "") {
		alert("Digite a RAZÃO SOCIAL.");
		document.contato.Razao.focus();
		return;
	}

	// Verifica se ENDEREÇO está preenchido
	if (document.contato.Endereco.value == "") {
		alert("Digite seu ENDEREÇO.");
		document.contato.Endereco.focus();
		return;
	}

	// Verifica se CIDADE está preenchido
	if (document.contato.Cidade.value == "") {
		alert("Digite sua CIDADE.");
		document.contato.Cidade.focus();
		return;
	}

	// Verifica se ESTADO está preenchido
	if (document.contato.Estado.value == "") {
		alert("Digite sua ESTADO.");
		document.contato.Estado.focus();
		return;
	}

	// Verifica se DDD está preenchido
	if (document.contato.DDD.value == "") {
		alert("Digite o DDD.");
		document.contato.DDD.focus();
		return;
	}

	// 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;
	}

	// Verifica se o campo DDD tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.DDD.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;
		}
		if (ch != ",")
		allNum += ch;
	}
	if (!allValid) {
		alert("O DDD deve conter apenas números");
		document.contato.DDD.focus();
		return;
	}

	// Verifica se TELEFONE está preenchido
	if (document.contato.Telefone.value == "") {
		alert("Digite seu TELEFONE.");
		document.contato.Telefone.focus();
		return;
	}

	// Verifica se o campo TELEFONE tem sete ou oito dígitos
	if (document.contato.Telefone.value.length < 7) {
		alert("O TELEFONE deve ter 7 ou 8 dígitos.");
		document.contato.Telefone.focus();
		return;
	}

	// Verifica se o campo TELEFONE tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.Telefone.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;
		}
		if (ch != ",")
		allNum += ch;
	}
	if (!allValid) {
		alert("O TELEFONE deve conter apenas números");
		document.contato.Telefone.focus();
		return;
	}


	// Verifica se FAX DDD está preenchido
	if (document.contato.FDDD.value == "")
	{
		alert("Digite o DDD.");
		document.contato.FDDD.focus();
		return;
	}

	// Verifica se o campo FAX DDD tem dois dígitos
	if(document.contato.FDDD.value.length < 2){
		alert("O DDD deve ter dois dígitos.");
		document.contato.FDDD.focus();
		return;
	}

	// Verifica se o campo FAX DDD tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.FDDD.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;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid) {
		alert("O DDD deve conter apenas números");
		document.contato.FDDD.focus();
		return;
	}

	// Verifica se FAX está preenchido
	if (document.contato.FAX.value == "") {
		alert("Digite seu FAX.");
		document.contato.FAX.focus();
		return;
	}

	// Verifica se o campo FAX tem sete ou oito dígitos
	if(document.contato.FAX.value.length < 7){
		alert("O FAX deve ter 7 ou 8 dígitos.");
		document.contato.FAX.focus();
		return;
	}

	// Verifica se o campo FAX tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.FAX.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;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid) {
		alert("O FAX deve conter apenas números");
		document.contato.FAX.focus();
		return;
	}

	// Verifica se CNPJ está preenchido
	if (document.contato.CNPJ.value == "")
	{
		alert("Digite seu CNPJ.");
		document.contato.CNPJ.focus();
		return;
	}

	// Verifica se o campo TELEFONE tem somente números
	var checkOK = "0123456789";
	var checkStr = document.contato.CNPJ.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;
	}
	if (ch != ",")
	allNum += ch;
	}
	if (!allValid) {
		alert("O CNPJ deve conter apenas números");
		document.contato.CNPJ.focus();
		return;
	}


	// Verifica se INSCRIÇÃO ESTADUAL está preenchida
	if (document.contato.Inscricao.value == "") {
		alert("Digite a INSCRIÇÃO ESTADUAL.");
		document.contato.Inscricao.focus();
		return;
	}


	// Verifica se E-MAIL está preenchido
	if (document.contato.Email.value == "") {
		alert("Digite seu E-MAIL.");
		document.contato.Email.focus();
		return;
	}

	// Verifica se o E-MAIL contem @ e pelo menos 1 .
	var checkcliemail = "@.";
	var checkStr = document.contato.Email.value;
	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;
	}

	// Verifica se TIPO DE EMPRESA está preenchida
	if (document.contato.TipoEmpresa.value == "") {
		alert("Digite o TIPO DE EMPRESA.");
		document.contato.TipoEmpresa.focus();
		return;
	}

	// VERIFICA SE É CLIENTE OU NÃO
	achou_cliente = "NAO";
	for (x=0; x<document.contato.ja_cliente.length; x++) {
		if (document.contato.ja_cliente(x).checked == true) {
			ja_cliente = document.contato.ja_cliente(x).value;
			achou_cliente = "SIM";
			//alert(document.contato.ja_cliente(x).value);
		}
	}
	if (achou_cliente == "NAO") {
		alert("Por favor, informe a opção de cliente.");
		return;
	} else {
		if (ja_cliente == "SIM") {
			document.contato.action = "cadastro_confirma.asp";
		} else {
			document.contato.action = "cadastro_ok.asp";
		}
		document.contato.submit();		
	}

}