<!--

//##############################################################################
//#                    Funcoes de validacao de formularios                     #
//#                               Versao 1.0.0                                 #
//##############################################################################
//#                         DP+ :: Presenca Digital                            #
//#                          Modificado 15.03.2005                             #
//##############################################################################
//# COPYRIGHT                                                                  #
//# Copyright 2005, DP+. Todos os direitos reservados.                         #
//##############################################################################

function invalido (campo, texto) {
	campo.focus();
    	campo.select();
	alert(texto);
    	return false;
}

function isNumber (c)
{ return ((c >= "0") && (c <= "9")) }

function isInteger(s){
	var i;
	if (isEmpty(s))
		return false;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (!isNumber(c)) return false;
	}
	return true;
}

function temNumero(s){
	var i;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (isNumber(c)) return true;
	}
}

function removeCarac(s){
	var i;
	var ceplimpo="";
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (isNumber(c)) { ceplimpo = ceplimpo + c; }
	}
	return ceplimpo;
}

function StrTranslate(Source)
{
	var ComAcento="ÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÃÕÑÇáéíóúàèìòùâêîôûäëïöüãõñç";
	var SemAcento="AEIOUAEIOUAEIOUAEIOUAONCaeiouaeiouaeiouaeiouaonc";
	var I;		//CONTADOR DE USO GERAL
	var Car;	//CADA CARACTERE DO TEXTO
	var Saida="";	//TEXTO DE SAIDA
	var Pos;	//POSICAO DO ACENTO
	
	for(I=0;I<Source.length;I++)
	{//VERIFICA CADA LETRA
		Car=Source.charAt(I);
		if((Pos=ComAcento.indexOf(Car))!=-1)
		{//E' UM ACENTO
			Car=SemAcento.charAt(Pos);
		}
		Saida=Saida + Car;		
	}
	return(Saida);
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

function vogalAcentuada(s) {
	ls = s.toLowerCase();
	if ((ls.indexOf("á")>=0) || (ls.indexOf("à")>=0) || (ls.indexOf("ã")>=0) || (ls.indexOf("â")>=0) || (ls.indexOf("é")>=0) || (ls.indexOf("í")>=0) || (ls.indexOf("ó")>=0) || (ls.indexOf("õ")>=0) || (ls.indexOf("ô")>=0) || (ls.indexOf("ú")>=0) || (ls.indexOf("ü")>=0))
		return true;
}

function verifUF(s) {
	ls = s.toUpperCase();
	if ((ls.indexOf("AC")>=0) || (ls.indexOf("AL")>=0) || (ls.indexOf("AM")>=0) || (ls.indexOf("AP")>=0) || (ls.indexOf("BA")>=0))
		return true;
	if ((ls.indexOf("CE")>=0) || (ls.indexOf("DF")>=0) || (ls.indexOf("ES")>=0) || (ls.indexOf("GO")>=0) || (ls.indexOf("MA")>=0))
		return true;
	if ((ls.indexOf("MG")>=0) || (ls.indexOf("MS")>=0) || (ls.indexOf("MT")>=0) || (ls.indexOf("PA")>=0) || (ls.indexOf("PB")>=0))
		return true;
	if ((ls.indexOf("PE")>=0) || (ls.indexOf("PI")>=0) || (ls.indexOf("PR")>=0) || (ls.indexOf("RJ")>=0) || (ls.indexOf("RN")>=0))
		return true;
	if ((ls.indexOf("RO")>=0) || (ls.indexOf("RR")>=0) || (ls.indexOf("RS")>=0) || (ls.indexOf("SC")>=0) || (ls.indexOf("SP")>=0) || (ls.indexOf("SE")>=0) || (ls.indexOf("TO")>=0))
		return true;
	return false;
}

function verificaEmail(email) {
	var s = new String(email);
	// { } ( ) < > [ ] | \ /
	if ((s.indexOf("{")>=0) || (s.indexOf("}")>=0) || (s.indexOf("(")>=0) || (s.indexOf(")")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("[")>=0) || (s.indexOf("]")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("/")>=0) )
		return false;
	if (vogalAcentuada(email))
		return false;
	// & * $ % ? ! ^ ~ ` ' "
	if ((s.indexOf("&")>=0) || (s.indexOf("*")>=0) || (s.indexOf("$")>=0) || (s.indexOf("%")>=0) || (s.indexOf("?")>=0) || (s.indexOf("!")>=0) || (s.indexOf("^")>=0) || (s.indexOf("~")>=0) || (s.indexOf("`")>=0) || (s.indexOf("'")>=0) || (s.indexOf(" ")>=0) )
		return false;
	// , ; : = #
	if ((s.indexOf(",")>=0) || (s.indexOf(";")>=0) || (s.indexOf(":")>=0) || (s.indexOf("=")>=0) || (s.indexOf("#")>=0) )
		return false;
	// procura se existe apenas um @
	if ( (s.indexOf("@") < 0) || (s.indexOf("@") != s.lastIndexOf("@")) )
		return false;
	// verifica se tem pelo menos um ponto após o @
	if (s.lastIndexOf(".") < s.indexOf("@"))
		return false;
	return true;
}

function checkField(s) {
	if ((s.indexOf("!")>=0) || (s.indexOf("@")>=0) || (s.indexOf("#")>=0) || (s.indexOf("$")>=0) || (s.indexOf("%")>=0) || (s.indexOf("&")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("?")>=0) ||  (s.indexOf("/")>=0))
		return false;
	return true;
}

function TestaCampo_ExisteTresIguais (str, TestaCampo)
{
var nome=str;
var Pos1 = 0;
while (Pos1 < nome.length - 2){
   if ((nome.substring(Pos1, Pos1 + 1) == nome.substring(Pos1 + 1, Pos1 + 2)) && (nome.substring(Pos1, Pos1 + 1) == nome.substring(Pos1 + 2, Pos1 + 3)) && (nome.substring(Pos1, Pos1 + 1) == nome.substring(Pos1 + 3, Pos1 + 4)))
{
	 TestaCampo = false;
	 break;
   }else
 {
     Pos1++;
   }
}
return (TestaCampo);
}

function confere(){
	var vnome = document.contato.rs_Nome.value;
	if(isEmpty(document.contato.rs_Nome.value) || !checkField(document.contato.rs_Nome.value))
	{
		invalido(document.contato.rs_Nome,'O campo NOME está vazio ou contém caracteres inválidos!');
		return false;
	}
	var str = vnome;
	var TestaCampo=true;
	TestaCampo = TestaCampo_ExisteTresIguais(str, TestaCampo);
	if (!TestaCampo) {
		invalido(document.contato.rs_Nome,'O campo NOME não pode conter mais de 3 caracteres repetidos consecutivamente!');
      		return(false);
  	}
  	if (vnome.length <4) {
		invalido(document.contato.rs_Nome,'O campo NOME deve ter no mínimo 4 caracteres!');
    		return(false);
  	}
	if (vnome.indexOf(" ")  < 0 ) {
		invalido(document.contato.rs_Nome,'Por favor preencha seu NOME completo!');
    		return(false);
  	}
	var checkStr = vnome;
  	var allValid = false;
  	allValid=temNumero(checkStr);
  	if (allValid) {
		invalido(document.contato.rs_Nome,'O campo NOME contém caracteres inválidos!');
    		return (false);
  	}
	var str=StrTranslate(document.contato.res_Email.value);
	document.contato.res_Email.value = str.toLowerCase();
	if (isEmpty(document.contato.res_Email.value) || !verificaEmail(document.contato.res_Email.value))
	{
		invalido(document.contato.res_Email,'O campo EMAIL está vazio ou contém caracteres inválidos!');
		return false;
	}
	if (!checkField(document.contato.s_Cidade.value))
	{
		invalido(document.contato.s_Cidade,'O campo CIDADE contém caracteres inválidos!');
		return false;
	}
	if (!isEmpty(document.contato.s_UF.value)) {
		if (!checkField(document.contato.s_UF.value))
		{
			invalido(document.contato.s_UF,'O campo UF contém caracteres inválidos!');
			return false;
		}
		if (!verifUF(document.contato.s_UF.value))
		{
			invalido(document.contato.s_UF,'A sigla da UF está incorreta!');
			return false;
		}
	}
	if (!isEmpty(document.contato.sd_DDD.value)) {
		document.contato.sd_DDD.value = removeCarac(document.contato.sd_DDD.value);
		var s = document.contato.sd_DDD.value;
		if (s.length == 2) {
			s = "0" + s;
			document.contato.sd_DDD.value = s;
		}
		if (!isInteger(document.contato.sd_DDD.value) || (s.length > 3))
		{
			invalido(document.contato.sd_DDD,'O campo DDD contém caracteres inválidos!');
			return false;
		}
	}
	if (!isEmpty(document.contato.sd_Telefone.value)) {
		document.contato.sd_Telefone.value = removeCarac(document.contato.sd_Telefone.value);
		if (!isInteger(document.contato.sd_Telefone.value))
		{
			invalido(document.contato.sd_Telefone,'O campo TELEFONE contém caracteres inválidos!');
			return false;
		}
	}
	return true;
}

function valida(){
	if (confere())
	{
		document.contato.submit ();
	}
}

/////////////////////////////////////////////////////////
var highlightcolor="#e0e0e0"
var ns6=document.getElementById&&!document.all
var previous=''
var eventobj

var intended=/INPUT|TEXTAREA|SELECT|OPTION/

function checkel(which){
	if (which.style&&intended.test(which.tagName)){
		if (ns6&&eventobj.nodeType==3)
		eventobj=eventobj.parentNode.parentNode
		return true
	}
	else
	return false
}

function highlight(e){
	eventobj=ns6? e.target : event.srcElement
	if (previous!=''){
		if (checkel(previous))
		previous.style.backgroundColor=''
		previous=eventobj
		if (checkel(eventobj))
		eventobj.style.backgroundColor=highlightcolor
	}
	else{
		if (checkel(eventobj))
		eventobj.style.backgroundColor=highlightcolor
		previous=eventobj
	}
}

//-->
