// by Lidianne Albernaz

function verifica(){
	nome = document.insc.nome.value;
	cpf = document.insc.cpf.value;
	curso = document.insc.curso.value;
	nasc = document.insc.nasc.value;
	dia = document.insc.dia.value;
	horario = document.insc.horario.value;
	end = document.insc.end.value;
	num = document.insc.num.value;
	complemento = document.insc.complemento.value;
	cep = document.insc.cep.value;
	bairro = document.insc.bairro.value;
	cidade = document.insc.cidade.value;
	estado = document.insc.estado.value;
	res = document.insc.res.value;
	cel = document.insc.cel.value;
	mail = document.insc.mail.value;
	confirmacao = document.insc.confirmacao.value;
	
	if(nome == "" || mail == "" || confirmacao == "" || curso== "")
	{
		alert("Por favor, preencha todos os campos obrigatórios(*).");
		document.getElementById('nome').style.backgroundColor = "#F1F2F3";
		document.getElementById('curso').style.backgroundColor = "#F1F2F3";
		document.getElementById('mail').style.backgroundColor = "#FFFF99";
		document.getElementById('confirmacao').style.backgroundColor = "#FFFF99";
		return false;
	}
	
		
	if(curso=="Informática no Escritório" || curso=="" || curso=="Planejamento e Gestão com TI"){
		alert("Atenção! O valor informado no campo CURSO, não é uma opção válida. Selecione um dos cursos em preto. Obrigado.");
		document.getElementById('curso').style.backgroundColor = "#F1F2F3";
		return false;
	}
	
	
	if(mail!=confirmacao){
		alert("Informações referentes a e-mail não conferem.");
		document.getElementById('mail').style.backgroundColor = "#FFFF99";
		document.getElementById('confirmacao').style.backgroundColor = "#FFFF99";
		return false;
	}
	
	if(mail.indexOf('@')==-1) 
	{
		alert("Digite um e-mail válido!");
		document.getElementById('mail').style.backgroundColor = "#FFFF99";
		document.getElementById('confirmacao').style.backgroundColor = "#FFFF99";
		return false;
	}

// começa a validação de CPF

	var CPF = document.insc.cpf.value; // Recebe o valor digitado no campo

	// Aqui começa a checagem do CPF
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado
	
	// Desemembra o número do CPF na array DIGITO
	for (I=0; I<=8; I++) {
	  DIGITO[I] = CPF.substr( I, 1);
	}
	
	// Calcula o valor do 10º dígito da verificação
	POSICAO = 10;
	SOMA = 0;
	   for (I=0; I<=8; I++) {
		  SOMA = SOMA + DIGITO[I] * POSICAO;
		  POSICAO = POSICAO - 1;
	   }
	DIGITO[9] = SOMA % 11;
	   if (DIGITO[9] < 2) {
			DIGITO[9] = 0;
	}
	   else{
		   DIGITO[9] = 11 - DIGITO[9];
	}
	
	// Calcula o valor do 11º dígito da verificação
	POSICAO = 11;
	SOMA = 0;
	   for (I=0; I<=9; I++) {
		  SOMA = SOMA + DIGITO[I] * POSICAO;
		  POSICAO = POSICAO - 1;
	   }
	DIGITO[10] = SOMA % 11;
	   if (DIGITO[10] < 2) {
			DIGITO[10] = 0;
	   }
	   else {
			DIGITO[10] = 11 - DIGITO[10];
	   }
	
	// Verifica se os valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];
	   if (DV != DV_INFORMADO) {
		  alert('CPF inválido');
		  document.getElementById('cpf').style.backgroundColor = "#DDDDFF";
		  document.insc.cpf.value = '';
		  document.insc.cpf.focus();
		  return false;
	   }
//fim da validação de cpf
	document.insc.submit();
}

// Apenas_numeros
function soNumero(e) {
if(window.event) {
// for IE, e.keyCode or window.event.keyCode can be used
key = e.keyCode;
}
else if(e.which) {
// netscape
key = e.which;
}
if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8));
{
    return true;
  }
}

function checa_data(){
	dia = document.getElementById('formulario').dia.value;
	mes = document.getElementById('formulario').mes.value;
	ano = document.getElementById('formulario').ano.value;
	
		if(ano%4 != 0 && mes == "02" && dia >= 29){
			alert("A data de expiração não existe.");
		}else if (ano%4 == 0 && mes == "02" && dia >= 30){
			alert("A data de expiração não existe.");
		}else if (mes == "04" && dia == 31){
			alert("A data de expiração não existe.");
		}else if (mes == "06" && dia == 31){
			alert("A data de expiração não existe.");
		}else if (mes == "09" && dia == 31){
			alert("A data de expiração não existe.");
		}else if (mes == "11" && dia == 31){
			alert("A data de expiração não existe.");
		}else{
			document.getElementById('formulario').submit();
		}
}

// colocar no evento onKeyUp passando o objeto como parametro
function formata(val)
{
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		// charAt -> retorna o caractere posicionado no índice especificado
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0){
		   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
		   // ocorrência de expreg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
		   // instStr.search(expreg);
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}else if(i==1){
			   
			   if(lchar.search(expr) != 0){
				  // substring(indice1,indice2)
				  // indice1, indice2 -> será usado para delimitar a string
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if(pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}
