/************************************************
* function ValidaData
* Verifica se a data foi digitada corretamente
* Input: Data a ser verificado
************************************************/
function ValidaData (data, AnoMin, AnoMax) 
{

var datacomposta = data;

//**************declaracao das variaveis***********************************
//*****variaveis que comporao dia/mes/ano*****
var dia="";
var mes="";
var ano="";

//***********variavel para testar caracters validos*********
var caractervalidos="1234567890/. ";

//*****contadores******************************
var c=0;
var i=0;
var p=0;
var cont=1;
var verifica=0;

//************variavel de validacao************
var validadata=true;

var checkOK = "0123456789/";
var checkStr = datacomposta;
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("Por favor entre com a data no formato DD/MM/AAAA .");
   return (false);
 }


//***********************testa caracteres validos*****************************  
        for(c; c<datacomposta.length; c++){
                 //alert("esta e a data composta = " +datacomposta.charAt(c));
	   	 	if ( caractervalidos.indexOf(datacomposta.charAt(c)) == -1 ) {
   	 			   alert("Caracteres invalidos na Data.");
                    return (false);
			   	 } // end if
       		} // end for


//*******************testa a quantidade de verificadores " / " *****************

               
	for(c=0;c<datacomposta.length;c++){
    	if(datacomposta.charAt(c)=="/"){
        	verifica++;
			} // end if
        if (datacomposta.charAt(c)=="."){
            verifica++;
			} // end if
		} // end for
     if (verifica!=2){
	    	alert("Numero de separadores errados na Data.");
        	return (false);
		} // end if
                                        
//****************compoe variavel dia *************

   	for (i=0;i< datacomposta.length;i++){
       	if ((datacomposta.charAt(i)=="/")||(datacomposta.charAt(i)==".")){
           	break;
		} 
 		else {   
			dia=dia+datacomposta.charAt(i);
           } // end if
	} // end for

//****************compoe variavel mes, testa e valida numero de dias  ************
    i++;
    for (i;i< datacomposta.length;i++){
    	if ((datacomposta.charAt(i)=="/")||(datacomposta.charAt(i)==".")){
        	break;
		} 
		else {        
        	mes=mes+datacomposta.charAt(i);
             } // end if
		} // end for

	// Valida os meses e dias
		
     if((mes<=0)||(mes>12)){
	    	alert("Mes invalido na Data.");
            return (false);
	 	} // end if 
                        
        if (mes==1){
        	if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if
                        
        if (mes==2){
        	if (dia>29){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if
                        
        if (mes==3){
          	if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if
                        
        if (mes==4){
            if (dia>30){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if
                        
        if (mes==5){
            if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if
                        
        if (mes==6){
            if (dia>30){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

                                
        if (mes==7){
            if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

            
        if (mes==8){
            if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

                                
        if (mes==9){
        	if (dia>30){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

                        
        if (mes==10){
        	if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

                                                                        
        if (mes==11){
        	if (dia>30){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

                        
        if (mes==12){
        	if (dia>31){
		    	alert("Dia invalido na Data.");
	            return (false);
				} // end if
			} // end if

//******************compoe variavel ano**********************************               
        i++;
        for(i;i<datacomposta.length;i++){
        	if(datacomposta==""){
            	break;
			}
			else {
            	ano=ano+datacomposta.charAt(i);
			} // end if
		} // end for

//****************testa validade da variavel ano*************************                                                       
        if ((ano.length!=2)&(ano.length!=4)){
	    	alert("Ano invalido na Data.");
	        return (false);
		} // end if
			            
        if (ano.length==2){
        	if (ano == 0) {
				ano = 2000;
			} else {
				ano = 19 + ano ;
			} // end if
		} // end if

			            
       	if (ano<AnoMin){
	    	alert("Aceita apenas Data acima de " + AnoMin);
	        return (false);
		}
       	if (ano>AnoMax){
	    	alert("Aceita apenas Data abaixo de " + AnoMax);
	        return (false);
		}

//*********************** retorno padrao **************                         

return (true);
				
}           

function VerificaEmail( mail) {
    //declaracao das variaveis
	var user="";
	var dominio="";
	var i=0;
	var testa="+)(*&%$#!+;='\/`~?<>"; 
	var c=0;
	var checkmail=true;
	var ponto=0;
	var j=0;
	var arroba=0;
	

	//***********testa caracteres invalidos na variavel mail**********
	for(c;c<mail.length;c++){
		for (j=0; j<testa.length;j++){
			if (testa.charAt(j)==mail.charAt(c)){
				checkmail=false;
				alert ("Caracteres invalidos no e-mail");
				return (false);
				}
			}
		}		
	//************ cria variavel user******************************* 
	if (checkmail!=false){
			for (i=0;i< mail.length;i++){
				if (mail.charAt(i)=="@"){
					break;}
				else{	
					user=user+mail.charAt(i);
					}
				}
	
			//************cria variavel dominio**************************
			for (i;i<mail.length;i++){
				if (mail.charAt(i)==""){
					break;}
				else{	
					dominio=dominio+mail.charAt(i);
					}
				}
					
				//***********testa validade da variavel dominio********************		
				for(c=0;c<dominio.length;c++){
					if (dominio.charAt(c)=="@"){
						arroba++;}
					if (dominio.charAt(c)=="."){
						ponto++;}}
					if ((ponto<1)||(ponto>4)){
						alert("Erro no dominio do e-mail");
						return (false);
						}
					if (arroba != 1.0){
						alert("Erro no dominio do e-mail");
						return (false);
						};
				}			

	return (true);		
}

     

function ValidaObrigatorio(campo,nome_campo,tamanho,minimo) {
if (campo.value.length == 0){
	alert("Preencha o campo " + nome_campo);
	campo.focus();
	campo.select();
	return false;
	}
else if(tamanho>0){
		if(campo.value.length < tamanho){
	//	alert (minimo);
			if (minimo>0){
			//alert ("OK1");
				alert("Campo " + nome_campo + " deve ter pelo menos "+tamanho+" caracteres");
			}
			else{
			//alert ("OK2");
				alert("Campo " + nome_campo + " deve ter "+tamanho+" caracteres");
			campo.focus();
			campo.select();
			return false;
			}
		}
		else return true;
	}
	else return true;
}

function TrocaAction(theForm){
	//alert("TrocaAction");
	if (theForm.sistema.value.substr(3,3) == 'cad'){
		//alert("incinclusao");
		//full path, necessario para o dicas da terra
		theForm.action = "/portugues/includes/incinclusao.asp";
	}
	else if (theForm.sistema.value.substr(3,3) == 'edt'){
		//alert("incalteracao");
		theForm.action = "includes/incalteracao.asp";
	}
	else if (theForm.sistema.value.substr(3,3) == 'log'){
		//alert("verificalogin");
		if (theForm.tipo.value == 'adm') {
			theForm.action = "verificalogin.asp?tipo=adm";
		}
		else if (theForm.tipo.value == 'adm') {
			theForm.action = "verificalogin.asp?tipo=con";
		}
	}
}

function ValidaCategoria(theForm){
	if (ValidaObrigatorio(theForm.categoria,"Categoria","0","50")){
		TrocaAction(theForm);
		return (false);
		}
}

function ValidaLogin(theForm){
	  if ((ValidaObrigatorio(theForm.login,"Login","10","0")) && (ValidaObrigatorio(theForm.senha,"Senha","8","4")))
	  	{
		  TrocaAction(theForm);
	  }		
}

function ValidaCommodity(theForm){

	  if (!ValidaData(theForm.data.value, 1901, 2100)) {
		  	theForm.data.focus();
			return (false);
		  } 	
		  
	  if ((ValidaObrigatorio(theForm.texto,"Texto","10","0")) && (ValidaObrigatorio(theForm.titulo,"Título","10","250")))
	  	{
		  TrocaAction(theForm);
		  return (true);
	  }		
}

function ValidaRelease(theForm){
	  if (!ValidaData(theForm.data.value, 1901, 2100)) {
		  	theForm.data.focus();
			return (false);
		  } 	
	  if ((ValidaObrigatorio(theForm.resumo,"Resumo","10","")) && (ValidaObrigatorio(theForm.titulo,"Título","2","5"))){
		  TrocaAction(theForm);
	  	  return true;
	  }		
}

function ValidaCadastro(theForm){

	if(!ValidaObrigatorio(theForm.nome,"Nome","","60")){
		theForm.nome.focus();
		return false;		
	}
	
	if(!VerificaEmail(theForm.email.value)){
		theForm.email.focus();
		return false;
	
	}
	if (!ValidaData(theForm.dtnascimento.value, 1901, 2100)) {
		  	theForm.dtnascimento.focus();
			return (false);
		  } 
		  
	if(!ValidaObrigatorio(theForm.logradouro,"Logradouro","","5")){
		theForm.logradouro.focus();
		return false;		
	}

	if(!ValidaObrigatorio(theForm.numero,"Número","","1")){
		theForm.numero.focus();
		return false;		
	}

//	if(!ValidaObrigatorio(theForm.complemento,"Complemento","","5")){
//		theForm.complemento.focus();
//		return false;		
//	}

	if(!ValidaObrigatorio(theForm.cidade,"Cidade","","3")){
		theForm.cidade.focus();
		return false;		
	}

    if  (theForm.estado[theForm.estado.selectedIndex].value == "") {
		alert("Selecione um Estado.");
		theForm.estado.focus();
		return false;	
	}		

    if  (theForm.atividade[theForm.atividade.selectedIndex].value == "0") {
		alert("Selecione uma Atividade.");
		theForm.atividade.focus();
		return false;	
	}		
    
	if (theForm.status.value == "c") { 
  	   if  (theForm.fazenda[0].checked == false && theForm.fazenda[1].checked == false) {
		   alert("Selecione Sim se você possui uma fazenda, ou Não se não possui.");
		   theForm.fazenda[1].checked=true;
		   return false;
	   }
	}

	TrocaAction(theForm);	
	return true;
}

function ValidaPropriedade(theForm){
	TrocaAction(theForm);	
	return true;
}

function ValidaProduto(theForm){
	TrocaAction(theForm);	
	return true;
}
function ValidaAreaInteresse(theForm){
	TrocaAction(theForm);	
	return true;
}

// VALIDA CIDADES

function ValidaCidade(theForm){
	if (!ValidaObrigatorio(theForm.cidade,"Cidade","","","50")){
		theForm.cidade.focus();
		return false;	
	}
	if (!ValidaObrigatorio(theForm.estado,"Estado","","","2")){
		theForm.estado.focus();
		return false;	
	} 
	TrocaAction(theForm);	
	return true;
}

// VALIDA EMPRESA
function ValidaEmpresa(theForm){
	if (!ValidaObrigatorio(theForm.nome,"Nome","","","200")){
		theForm.nome.focus();
		return false;	
	}
	if (!ValidaObrigatorio(theForm.nomefantasia,"Nome Fantasia","","","200")){
		theForm.nomefantasia.focus();
		return false;	
	} 
	
	if (!ValidaObrigatorio(theForm.contato,"Contato","","","40")){
		theForm.contato.focus();
		return false;	
	} 	
	if (!ValidaObrigatorio(theForm.endereco,"Endereço","","","200")){
		theForm.endereco.focus();
		return false;	
	} 	
	if (!ValidaObrigatorio(theForm.cep,"CEP","","","10")){
		theForm.cep.focus();
		return false;	
	} 
	
	if (!ValidaObrigatorio(theForm.bairro,"Bairro","","","40")){
		theForm.bairro.focus();
		return false;	
	} 
	
	if (!ValidaObrigatorio(theForm.fone1,"Fone 1","","","15")){
		theForm.fone1.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.fone2,"Fone 2","","","15")){
		theForm.fone2.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.fax,"Fax","","","15")){
		theForm.fax.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.logo,"Logotipo","","","50")){
		theForm.logo.focus();
		return false;	
	} 
	if (!verificaEmail(theForm.email.value)){
		alert("Preencha o campo E-mail!");
		theForm.email.focus();
		return (false);
    }  
	if (!ValidaObrigatorio(theForm.foto_principal,"Foto Principal","","","300")){
		theForm.foto_principal.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.foto1,"Foto 1","","","50")){
		theForm.foto1.focus();
		return false;	
	} if (!ValidaObrigatorio(theForm.foto2,"Foto 2","","","50")){
		theForm.foto2.focus();
		return false;	
	} if (!ValidaObrigatorio(theForm.foto3,"Foto 3","","","50")){
		theForm.foto3.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.nomesite,"Nome do site","","","50")){
		theForm.nomesite.focus();
		return false;	
	} 
	if (!ValidaObrigatorio(theForm.texto,"Texto","","","")){
		theForm.texto.focus();
		return false;	
	} 
	TrocaAction(theForm);	
	return true;

}



