//####################################################################################################
//******************************* FUNÇÕES PARA O FORMULÁRIO DE INSCRIÇÃO *****************************
//####################################################################################################
function valida(){ 			
	//############## CAMPO NOME #####################			
	if (document.cadastro.vcND_Nome.value == "" || document.cadastro.vcND_Nome.value.length < 5 || Replace(document.cadastro.vcND_Nome.value,",","") == "" || Replace(document.cadastro.vcND_Nome.value,"'","") == "" || Replace(Replace(document.cadastro.vcND_Nome.value,"'","")," ","") == "") 
	{
		alert("O Campo \"Nome\" é de preenchimento obrigatório. Preencha-o corretamente.");
		document.cadastro.vcND_Nome.focus();
		return false;
	} 
				
	//############## CAMPO CPF #####################	
	if (!checaCPF(document.cadastro.vcND_CPF.value)) 
	{
		 errors="1";
		 if (errors) alert('Informe o seu \"CPF\" corretamente. Preencha-o corretamente.');
		 document.cadastro.vcND_CPF.focus();
		 return false;
	}
	
	//############## CAMPO RG #####################			
	if (document.cadastro.vcND_RG.value == ""|| document.cadastro.vcND_RG.value.length < 5 || Replace(document.cadastro.vcND_RG.value,",","") == "" || Replace(document.cadastro.vcND_RG.value,"'","") == "" || Replace(document.cadastro.vcND_RG.value,";","") == "" || Replace(document.cadastro.vcND_RG.value," ","") == "") 
	{
			alert("O Campo \"RG\" é de preenchimento obrigatório. Preencha-o corretamente.");
			document.cadastro.vcND_RG.focus();
			return false;
	}
	
	//############## CAMPO EMAIL #####################
	if (Trim(document.cadastro.vcND_Email.value) != "") {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.cadastro.vcND_Email.value)) && document.cadastro.vcND_Email.value != "") 
		{ 
			alert("Favor informar um \"E-mail\" válido.");
			document.cadastro.vcND_Email.focus();
			return false;
		}		
	}
	
	//############## CAMPO EMAIL #####################
	if (Trim(document.cadastro.vcND_Email.value) == "") 
	{				
		alert("O Campo \"E-mail\" é de preenchimento obrigatório. Preencha-o corretamente.");
		document.cadastro.vcND_Email.focus();
		return false;
	}
	
	//############## CAMPO SEXO #####################	
	if (document.cadastro.chFl_Sexo.selectedIndex == 0) 
	{
		alert("Selecione o Campo \"Sexo\".");
		document.cadastro.chFl_Sexo.focus();
		return false;
	}
	
	//############## DIA NASCIMENTO #####################
	if (document.cadastro.inNR_NasDia.selectedIndex == 0) 
	{
		alert("Selecione o \"Dia de seu nascimento\".");
		document.cadastro.inNR_NasDia.focus();
		return false;
	}
	
	//############## MÊS NASCIMENTO #####################
	if (document.cadastro.inNR_NasMes.selectedIndex == 0) 
	{
		alert("Selecione o \"Mês de seu nascimento\".");
		document.cadastro.inNR_NasMes.focus();
		return false;
	}
	
	//############## ANO NASCIMENTO #####################
	if (document.cadastro.inNR_NasAno.selectedIndex == 0) 
	{
		alert("Selecione o \"Ano de seu nascimento\".");
		document.cadastro.inNR_NasAno.focus();
		return false;
	}	
	
	//############## DATA NASCIMENTO #####################
	var dataNasc = new Date( document.cadastro.inNR_NasMes.value + "/" + document.cadastro.inNR_NasDia.value + "/" + document.cadastro.inNR_NasAno.value);
	if ( dataNasc.getDate() != document.cadastro.inNR_NasDia.value )
	{
		alert("Informe uma data válida.");
		document.cadastro.inNR_NasDia.focus();
		return false;
	}
	
	//############## CAMPO DDD RESIDENCIAL #####################	
	if (Trim(document.cadastro.chND_DDD.value) == "")
	{
		 alert("O Campo \"DDD\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.chND_DDD.focus();
		 return false;
	}
	
	//############## CAMPO TEL RESIDENCIAL #####################
	if (Trim(document.cadastro.chND_Telefone.value) == "")
	{
		 alert("O Campo \"Telefone\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.chND_Telefone.focus();
		 return false;
	}
	
	//############## CAMPO ENDEREÇO #####################
	if (document.cadastro.vcND_Endereco.value == "" || document.cadastro.vcND_Endereco.value.length < 5 || Replace(document.cadastro.vcND_Endereco.value,",","") == "" || Replace(document.cadastro.vcND_Endereco.value,"'","") == "" || Replace(Replace(document.cadastro.vcND_Endereco.value,"'","")," ","") == "") 
	{
			alert("O Campo \"Endereço\" é de preenchimento obrigatório. Preencha-o corretamente.");
			document.cadastro.vcND_Endereco.focus();
			return false;
	} 	
	
	//############## CAMPO BAIRRO #####################
	if (Trim(document.cadastro.vcND_Bairro.value) == "")
	{
		 alert("O Campo \"Bairro\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.vcND_Bairro.focus();
		 return false;
	}
	
	//############## CAMPO CEP #####################
	if (Trim(document.cadastro.vcND_CEP.value) == "")
	{
		 alert("O Campo \"CEP\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.vcND_CEP.focus();
		 return false;
	}
	
	if ( !IsCEP(document.cadastro.vcND_CEP.value) )
	{
		 alert("O Campo \"CEP\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.vcND_CEP.focus();
		 return false;
	}
	
	//############## CAMPO CIDADE #####################
	if (document.cadastro.vcND_Cidade.value == "" || document.cadastro.vcND_Cidade.value.length  < 5 || Replace(document.cadastro.vcND_Cidade.value,",","") == "" || Replace(document.cadastro.vcND_Cidade.value,"'","") == "" || Replace(Replace(document.cadastro.vcND_Cidade.value,"'","")," ","") == "") 
	{
			alert("O Campo \"Cidade\" é de preenchimento obrigatório. Preencha-o corretamente.");
			document.cadastro.vcND_Cidade.focus();
			return false;
	} 
	
	//############## CAMPO ESTADO #####################
	if (document.cadastro.chND_Estado.selectedIndex == 0) 
	{
		alert("Selecione o Campo \"Estado\".");
		document.cadastro.chND_Estado.focus();
		return false;
	}
	
	//############## CAMPO LINK VIDEO 1 #####################
	if (Trim(document.cadastro.vcND_LinkVideo1.value) == "")
	{
		 alert("O Campo \"Link do Vídeo\" é de preenchimento obrigatório. Preencha-o corretamente.");
		 document.cadastro.vcND_LinkVideo1.focus();
		 return false;
	}
		
	//############## CAMPO FOTO #####################
	if (Trim(document.cadastro.uploadArquivo.value) == "") {
		alert("Favor selecionar uma foto para completar o cadastro.");
		document.cadastro.uploadArquivo.focus();
		return false;
	}
	
	return true;
 }

function checaCPF (CPF) 
{
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999" || CPF == "00000000191")
		{
		return false;
		}
	soma = 0;
	for (i=0; i < 9; i ++)
		{
		soma += parseInt(CPF.charAt(i)) * (10 - i);
		}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{
		resto = 0;
		}
	if (resto != parseInt(CPF.charAt(9)))
		{
		return false;
		}
	soma = 0;
	for (i = 0; i < 10; i ++)
		{
		soma += parseInt(CPF.charAt(i)) * (11 - i);
		}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{
		resto = 0;
		}
	if (resto != parseInt(CPF.charAt(10)))
		{
		return false;
		}
	else
		{
//        alert('Correto');
		return true;
		}
}

function fDataValida(data){
   var err=0;
   if (data.length != 10) {
      err=1;
   }
 
   dia     = data.substring(0, 2); // dia
   barra_1 = data.substring(2, 3); // '/'
   mes     = data.substring(3, 5); // mes
   barra_2 = data.substring(5, 6); // '/'
   ano     = data.substring(6, 10); // ano



	if (isNaN(dia) != false && dia != "") err = 1;
	if (isNaN(mes) != false && mes != "") err = 1;
	if (isNaN(ano) != false && ano != "") err = 1;


   //check de erros b&aacute;sicos
   if (mes<1 || mes>12) err = 1;
   if (barra_1 != '/') err = 1;
   if (dia<1 || dia>31) err = 1;
   if (barra_2 != '/') err = 1;
   if (ano<1900 || ano>2999) err = 1;
 
   // meses com 30 dias
   if (mes==4 || mes==6 || mes==9 || mes==11){
      if (dia==31) err=1;
   }
 
   // tratamento para fevereiro
   if (mes==2){
      var aux=parseInt(ano/4);
      if (isNaN(aux)) {
         err=1;
      }
 
      if (dia>29) err=1;
      if (dia==29 && ((ano/4)!=parseInt(ano/4))) err=1;
   }
 
   if (err==1){
      return false;
   } else {
      return true;
   }
}

function soNumeros(fld, e) 
{
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789,';
	
	var aux = aux2 = '';
	var whichCode = (window.Event) ? ( (e.which) ? e.which : e.keyCode ) : e.keyCode; //Esta verificação a mais é devido a um "bug" do novo ie8
	if (( whichCode != 0 )&& ( whichCode != 8 )) //Outros chars (F5, TAB, ...) e Backspace
	{
		key = String.fromCharCode(whichCode);
		if (strCheck.indexOf(key) == -1) return false;  
	}
}

function soLetras(fld, e) 
{
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = 'abcdefghijlmnopqrstuxvyzwykABCDEFGHIJLMNOPQRSTUVXZWYKÀÁÂÃáàâãÉÈÊéèêÍÌíìÔÕÓÒôõóÚÙÜúùüÇç1234567890&+-ºª°@. ';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? ( (e.which) ? e.which : e.keyCode ) : e.keyCode; //Esta verificação a mais é devido a um "bug" do novo ie8
	if (( whichCode != 0 )&& ( whichCode != 8 )) //Outros chars (F5, TAB, ...) e Backspace
	{
		key = String.fromCharCode(whichCode);
		if (strCheck.indexOf(key) == -1) return false;  
	}
}

function soLetrasNumeros(fld, e) 
{
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = 'abcdefghijlmnopqrstuxvyzwykABCDEFGHIJLMNOPQRSTUVXZWYK1234567890-';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? ( (e.which) ? e.which : e.keyCode ) : e.keyCode; //Esta verificação a mais é devido a um "bug" do novo ie8
	if (( whichCode != 0 )&& ( whichCode != 8 )) //Outros chars (F5, TAB, ...) e Backspace
	{
		key = String.fromCharCode(whichCode);
		if (strCheck.indexOf(key) == -1) return false;  
	}
}

function soData(fld, e) 
{
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '1234567890/';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? ( (e.which) ? e.which : e.keyCode ) : e.keyCode; //Esta verificação a mais é devido a um "bug" do novo ie8
	key = String.fromCharCode(whichCode);  
	if (strCheck.indexOf(key) == -1) return false;  
}

function IsCEP(strCEP)
{
	// Caso o CEP não esteja nesse formato ele é inválido!
	var objER = /^[0-9]{8}$/;

	strCEP = Trim(strCEP)
	if((strCEP.length > 0)&&( strCEP.length <= 8 ))
		{
			if(objER.test(strCEP))
			{					
				objER = /([0-9])\1{7,}/; //Validando se o CEP tem todos os chars iguais
				if ( !objER.test(strCEP) )
				{
					return true;	
				}
				else
				{
					return false	
				}
			}
			else
				return false;
		}
	else
		return false;
}

function Trim(STRING){
	STRING = LTrim(STRING);
	return RTrim(STRING);
}

function RTrim(STRING){
	while(STRING.charAt((STRING.length -1))==" "){
		STRING = STRING.substring(0,STRING.length-1);
	}
	return STRING;
}


function LTrim(STRING){
	while(STRING.charAt(0)==" "){
		STRING = STRING.replace(STRING.charAt(0),"");
	}
	return STRING;
}

function Replace(STRING,REPLACE_THIS,REPLACE_WITH){
	while(STRING.indexOf(REPLACE_THIS) > -1){
		STRING = STRING.replace(REPLACE_THIS,REPLACE_WITH);
	}
	return STRING;
}
