function Form_Validator(theForm){
//****** CAMPO NOME DA EMPRESA ******
// verifica se o campo está vazio
if (theForm.razaosocial.value == false){
	alert("Preencha o campo Razão Social.");
	theForm.razaosocial.focus();
	return (false);
}
//--------------------------------------------------------------------------------------------------------------
if(theForm.cnpj.value != false){
	if(!ChecaForm(theForm.cnpj)){
		theForm.cnpj.focus();
		return (false);
	}
	function ValChar(ch) {
		if (ch=="0") return 0
		else if (ch=="1") return 1
		else if (ch=="2") return 2
		else if (ch=="3") return 3
		else if (ch=="4") return 4
		else if (ch=="5") return 5
		else if (ch=="6") return 6
		else if (ch=="7") return 7
		else if (ch=="8") return 8
		else if (ch=="9") return 9
		else return 10
	}

	//Verifica se o argumento é um CGC válido de 8 dígitos
	function ChecaCGC8 (CKCGC) {
	var CGC = CKCGC;
	var NewCGC = "";
	//Elimina todos os espaços, pontos, barras e traços do CGC
	for (i=0;i<CGC.length;i++) { //>
		if (CGC.charAt(i) != " " && CGC.charAt(i) != "." && CGC.charAt(i) != "/" && CGC.charAt(i) != "-") NewCGC = NewCGC + CGC.charAt(i);
	}
	//Verifica tamanho do CGC
	if (NewCGC.length!=8) {
		return false;
	}
	//verifica se todos os caracteres são numéricos
	var Numerico = false;
	var Numeros = "0123456789";
	for (i=0;i<NewCGC.length;i++) { //>
		Numerico = false;
		for (j=0;j<Numeros.length;j++) { //>
			if (NewCGC.charAt(i) == Numeros.charAt(j)) {
				Numerico = true;
				break;
			}
		}
		if (!Numerico) {
			return false;
		}
	}
	//Calcula os dígitos verificadores
	var s1 = 0;
	aux = 0;
	soma = 0
	for (i=1;i<=8;i++) {
		//alert("i="+i+" - char(i-1)="+NewCGC.charAt(i-1));
		aux = (ValChar(NewCGC.charAt(i-1)))*((i % 2)+1);
		//alert ("aux="+aux);
		if (aux>9) aux = aux-9;
		//alert ("aux="+aux);
		soma = soma + aux;
	}
	r1 = soma % 10;
	//alert("soma="+soma+" - resto="+r1);
	if (r1==0) return (true)
	else {
		return (false);
	}
	}

	//Verifica se o argumento é um CGC válido
	function ChecaCGC (CKCGC) {
	var CGC = CKCGC;
	var NewCGC = "";
	//Verifica tamanho do CGC
	if (CGC.length!=14) {
		return false;
	}
	//Calcula os dígitos verificadores
	//Guarda os 12 primeiros digitos
	var DVCGC = CGC.substring(0,12);
	//calcula o primeiro digito verificador
	var s1 = 0;
	for (i=1;i<=4;i++) s1 = s1 + (ValChar(DVCGC.charAt(i-1))*(6-i));
	for (i=5;i<=12;i++) s1 = s1 + (ValChar(DVCGC.charAt(i-1))*(14-i));
	r1 = s1 % 11;
	if (r1<2) dv1=0;
	else dv1 = 11 - r1;
	//calcula o segundo digito verificador
	var s2 = dv1*2;
	for (i=1;i<=5;i++) s2 = s2 + (ValChar(DVCGC.charAt(i-1))*(7-i));
	for (i=6;i<=12;i++) s2 = s2 + (ValChar(DVCGC.charAt(i-1))*(15-i));
	r2 = s2 % 11;
	if (r2<2) dv2=0;
	else dv2 = 11 - r2;
	//junta os digitos verificadores
	var DV = "";
	DV = DV + dv1 + dv2;
	//guarda os digitos verificadores do CGC digitado (últimas duas posições no string)
	var NewDV = CGC.substring(12,14)
	if (NewDV==DV) { //se o DV calculado for igual ao digitado, retorna true
		return true
	}
	else {
		return false
	}
	}

	function ChecaForm(theField) {
		if(theField.value.length>0){
		var a = theField.value;
		var b = "";
		var c = "";
		var d = 0;
		for(cont=0;cont<a.length;cont++)
		{
			if(	a.charAt(cont)=="0" || a.charAt(cont)=="1" || a.charAt(cont)=="2" || a.charAt(cont)=="3" || a.charAt(cont)=="4" || a.charAt(cont)=="5" || a.charAt(cont)=="6" || a.charAt(cont)=="7" || a.charAt(cont)=="8" || a.charAt(cont)=="9"){
				b = b + a.charAt(cont);
			}
		}
		a = b;

		//if(a==false){
		//		alert("CNPJ Inválido");
		//		return (false);
		//}

		//SE NÃO FOR IDENTIFICADO NENHUM NÚMERO - "CPF Inválido"
		if(a.length==""){
			alert("CNPJ Inválido");
			return (false);
		}


		if (a!="") { //se o campo CGCCPF tiver algum valor, verifica
			//if (a.length > 14)  //verifica se o tamanho não é maior que 14
			//{
			//	alert("O campo \""+theField.name+"\" deve ter no máximo 14 dígitos.");
				//theField.focus();
			//	return (false);
			//}
			// verifica se todos os caracteres digitados são numeros
			var checkOK = "0123456789-";
			var checkStr = a;
			var allValid = true;
			var decPoints = 0;
			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("O campo \""+theField.name+"\" deve conter apenas números.");
				//theField.focus();
				return (false);
			}
			//checa se o campo é CPF válido, retorna true e envia o formulário
			if (ChecaCGC(a)||ChecaCGC8(a)){

			//************************************************************************
				//FORMATAÇÃO DO CGC NA NOVA MASKARA
				b = "99.999.999/9999-99";		// MASCARA QUE VAI SER APLICADA NO CGC

				for(cont=0;cont<b.length;cont++)
				{
				  numero = b.charAt(cont);
					if(numero == "9")
					{

						if(a.charAt(cont-d)!=""){
							c= c + a.charAt(cont-d);
						}
						else
						{
							c= c + "0";
						}
					}
			    else
			    {
						c= c + b.charAt(cont);
						d = d + 1;
			    }
			    numero = "";
				}

				theForm.cnpj.value = c;
			//************************************************************************
			return (true);
			}

			else
			{
				alert("CNPJ inválido!");
				//theField.focus();
				return (false);
			}
		}

	}
	}
}	
	
//--------------------------------------------------------------------------------------------------------------

//****** CAMPO ENDEREÇO ******
// verifica se o campo está vazio
if (theForm.logradouro.value == false){
	alert("Preencha o campo Endereço.");
	theForm.logradouro.focus();
	return (false);
}

//--------------------------------------------------------------------------------------------------------------

	//****** CAMPO CEP ******

	// Valida o primeiro campo

	// verifica se o campo está vazio
	var cont = theForm.cep1.value

	if (theForm.cep1.value == "")
	   {
		 alert("Preencha o campo cep.");
		 theForm.cep1.focus();
		 theForm.cep1.select();
		 return (false);
	   }

	// verifica se existe espaço em branco no campo
	x = cont.indexOf(" ")
	if (x != -1){

		 alert("O campo cep não aceita espaços em branco.");
		 theForm.cep1.focus();
		 theForm.cep1.select();
		 return (false);
	}

	// verifica se o campo contêm menos de 5 caracteres
	if (theForm.cep1.value.length < 5)
		{
		alert("O primeiro campo CEP deve ter 5 dígitos.");
		theForm.cep1.focus();
		theForm.cep1.select();
		return (false);
		}

  	//verifica se o campo só contêm números
	if (theForm.cep1.value != false) {
			// verifica se todos os caracteres digitados são numeros
			var checkOK = "0123456789";
			var checkStr = theForm.cep1.value;
			var allValid = true;
			var decPoints = 0;
			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("O campo CEP só pode conter números, não use o separador (-), nem letras. Ex.: 00000111");
				theForm.cep1.focus();
				theForm.cep1.select();
				return (false);
			}
		}



	// Valida o segundo campo

	// verifica se o campo está vazio
	var cont = theForm.cep2.value
	if (theForm.cep2.value == "")
	   {
		 alert("Preencha o segundo campo cep.");
		 theForm.cep2.focus();
		 theForm.cep2.select();
		 return (false);
	   }

	// verifica se existe espaço em branco no campo
	x = cont.indexOf(" ")
	if (x != -1){

		 alert("O campo cep não aceita espaços em branco.");
		 theForm.cep2.focus();
		 theForm.cep2.select();
		 return (false);
	}

	// verifica se o campo contêm menos de 3 caracteres
	if (theForm.cep2.value.length < 3)
		{
		alert("O segundo campo CEP deve ter 3 dígitos.");
		theForm.cep2.focus();
		theForm.cep2.select();
		return (false);
		}

  	//verifica se o campo só contêm números
	if (theForm.cep2.value != false) {
			// verifica se todos os caracteres digitados são numeros
			var checkOK = "0123456789";
			var checkStr = theForm.cep2.value;
			var allValid = true;
			var decPoints = 0;
			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("O campo CEP só pode conter números, não use o separador (-), nem letras. Ex.: 00000111");
				theForm.cep2.focus();
				theForm.cep2.select();
				return (false);
			}
		}
		
//--------------------------------------------------------------------------------------------------------------		

//****** CAMPO CIDADE ******
// verifica se o campo está vazio
if (theForm.localidade.value == false){
	alert("Preencha o campo Cidade.");
	theForm.localidade.focus();
	return (false);
}

//--------------------------------------------------------------------------------------------------------------		

//****** CAMPO ESTADO ******
// verifica se o campo está vazio
if (theForm.estado.value == false){
	alert("Preencha o campo Estado.");
	theForm.estado.focus();
	return (false);
}

//--------------------------------------------------------------------------------------------------------------		

	//****** CAMPO DDD ******

	// verifica se o campo está vazio
	var cont = theForm.ddd.value

	if (theForm.ddd.value == "")
	   {
		 alert("Preencha o campo DDD.");
		 theForm.ddd.focus();
		 theForm.ddd.select();
		 return (false);
	   }

	// verifica se existe espaço em branco no campo
	x = cont.indexOf(" ")
	if (x != -1){

		 alert("O campo DDD não aceita espaços em branco.");
		 theForm.ddd.focus();
		 theForm.ddd.select();
		 return (false);
	}

	// verifica se o campo contêm menos de 2 caracteres
	if (theForm.ddd.value.length < 2)
		{
		alert("O campo DDD deve conter 2 dígitos.");
		theForm.ddd.focus();
		theForm.ddd.select();
		return (false);
		}

  	//verifica se o campo só contêm números
	if (theForm.ddd.value != false) {
			// verifica se todos os caracteres digitados são numeros
			var checkOK = "0123456789";
			var checkStr = theForm.ddd.value;
			var allValid = true;
			var decPoints = 0;
			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("O campo DDD só pode conter números. Ex.: 11");
				theForm.ddd.focus();
				theForm.ddd.select();
				return (false);
			}
		}

//--------------------------------------------------------------------------------------------------------------		

	//****** CAMPO TELEFONE ******

	// verifica se o campo está vazio
	var cont = theForm.telefone.value

	if (theForm.telefone.value == "")
	   {
		 alert("Preencha o campo telefone.");
		 theForm.telefone.focus();
		 theForm.telefone.select();
		 return (false);
	   }

	// verifica se existe espaço em branco no campo
	x = cont.indexOf(" ")
	if (x != -1){

		 alert("O campo Telefone não aceita espaços em branco.");
		 theForm.telefone.focus();
		 theForm.telefone.select();
		 return (false);
	}

	// verifica se o campo contêm menos de 7 caracteres
	if (theForm.telefone.value.length < 7)
		{
		alert("O campo Telefone deve ter no minimo 7 dígitos.");
		theForm.telefone.focus();
		theForm.telefone.select();
		return (false);
		}

  	//verifica se o campo só contêm números
	if (theForm.telefone.value != false) {
			// verifica se todos os caracteres digitados são numeros
			var checkOK = "0123456789";
			var checkStr = theForm.telefone.value;
			var allValid = true;
			var decPoints = 0;
			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("O campo Telefone só pode conter números, não use o separador (-), nem letras. Ex.: 2222222");
				theForm.telefone.focus();
				theForm.telefone.select();
				return (false);
			}
		}

//--------------------------------------------------------------------------------------------------------------		

	//****** CAMPO NOME ******
	// verifica se o campo está vazio
	if (theForm.nomecompleto.value == false){

		alert("Preencha o campo Nome do Responsável.");
		theForm.nomecompleto.focus();
		theForm.nomecompleto.select();
		return (false);

	  }else{

	// verifica se o campo contêm números
		var contnome = theForm.nomecompleto.value;

		for (var i = 0; i < 10; i = i + 1){

			contnome.indexOf(i);
			if (contnome.indexOf(i) != -1){
			alert("O campo Nome do Responsável não aceita números.");
			theForm.nomecompleto.focus();
			theForm.nomecompleto.select();
			return (false);

			}
		}
	}

//--------------------------------------------------------------------------------------------------------------		

//****** CAMPO CARGO ******
// verifica se o campo está vazio
if (theForm.cargo_id.value == "false"){
	alert("Selecione um Cargo.");
	theForm.cargo_id.focus();
	return (false);
}

//--------------------------------------------------------------------------------------------------------------		

//****** CAMPO EMAIL ******
var texto = theForm.emailexpor.value
// verifica se o campo não está vazio.
if (texto == false) {
	alert("Preencha o campo E-mail corretamente.");
	theForm.emailexpor.focus();
	theForm.emailexpor.select();
	return(false);
	} else {
//VERIFICA SE EXISTE "@" NO E-MAIL
pos = texto.indexOf("@")
if (pos == -1 || pos < 2){
	alert("Preencha o campo E-mail corretamente.");
	theForm.emailexpor.focus();
	return(false);
	} else {

		//VERIFICA SE EXISTE "."(PONTO) NO E-MAIL
		pos = texto.indexOf(".")
		if (pos == -1 || pos == 0) {
			alert("Preencha o campo E-mail corretamente.");
			theForm.emailexpor.focus();
			theForm.emailexpor.select();
			return (false);
		} else {

			ponto = texto.indexOf(".")
			tamanho = texto.length;
			parte = texto.substring(ponto,tamanho);

			//VERIFICA SE EXISTE MAIS DE 2 CARACTERES APÓS O "."(PONTO) NO E-MAIL
			if (parte.length < 3){
				alert("Preencha o campo E-mail corretamente.");
				theForm.emailexpor.focus();
				theForm.emailexpor.select();
				return (false);
			}

		}
	}
}
//--------------------------------------------------------------------------------------------------------------	

//****** CAMPO ÁREA PRETENDIDA ******
// verifica se o campo está vazio
if (theForm.areapretendida.value == false){
	alert("Informe a área pretendida - apenas números.");
	theForm.areapretendida.focus();
	return (false);
}
//--------------------------------------------------------------------------------------------------------------		
}
