// JavaScript Document
// RETIRA DO PRIMEIRO PARAMETRO TODOS OS CARACTERES QUE N?O CONSTAM NO SEGUNDO PARAMETRO
// EX: retornavalidos('999.888.777-66', '0123456789') RETORNA '99988877766'
function retornavalidos(string, strcheck) { 
	var len = string.length;
	var aux = '';

	for (i = 0; i < len; i++) 
	{ if (strcheck.indexOf(string.charAt(i)) != -1) aux += string.charAt(i); }

	return aux;
}


// RETORNA FALSE CASO O CPF SEJA INV?LIDO
function critcpf(cpf) {
  var cpf = retornavalidos(cpf, '0123456789');
  var n1 = cpf.substring(0,1);
  var n2 = cpf.substring(1,2);
  var n3 = cpf.substring(2,3);
  var n4 = cpf.substring(3,4);
  var n5 = cpf.substring(4,5);
  var n6 = cpf.substring(5,6);
  var n7 = cpf.substring(6,7);
  var n8 = cpf.substring(7,8);
  var n9 = cpf.substring(8,9);
  var n10 = cpf.substring(9,10);
  var n11 = cpf.substring(10,11);
  
  if ((n1 == n2) && (n2 == n3) && (n3 == n4) && (n4 == n5) && (n5 == n6) && (n6 == n7) && (n7 == n8) && (n8 == n9)) {
    return false;
  }
  
  dv1 = 11 - (((n1*10) + (n2*9) + (n3*8) + (n4*7) + (n5*6) + (n6*5) + (n7*4) + (n8*3) + (n9*2)) % 11);
  if (dv1 >= 10) { dv1 = 0; }
    
  dv2 = 11 - (((n1*11) + (n2*10) + (n3*9) + (n4*8) + (n5*7) + (n6*6) + (n7*5) + (n8*4) + (n9*3) + (dv1*2)) % 11);
  if (dv2 >= 10) { dv2 = 0; }
 
  if ((dv1 == n10) && (dv2 == n11)) { return true } else { return false }
}

// BLOQUEAR A INSERCAO DE VALORES NAO NUMERICOS
function soNumeros(evento){
	var strCheck = '0123456789';
	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var key = String.fromCharCode(tecla);

	if ((tecla != 0) && (tecla != 8) && (strCheck.indexOf(key) == -1)) return false;
}

// BLOQUEAR A INSERCAO DE VALORES NAO ALFANUMERICOS
function soLetras(evento){
	var strCheck = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var key = String.fromCharCode(tecla);

	if ((tecla != 0) && (tecla != 8) && (strCheck.indexOf(key) == -1)) return false;
}

// CRIA MASCARAS DE INSERCAO EX. ###.###.###-##
function formataCampo(object,evento,mascara){
	var tecla = (window.Event) ? evento.which : evento.keyCode;
	var valor, temp, temp2;
	valor = object.value;
	temp = "";
	temp2 = "";

	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
	{
		if (tecla==8) { valor = valor.substring(0,valor.length); }

		for (i=0;i<valor.length;i++)
		{
			if (valor.substring(i,i+1) != "." && valor.substring(i,i+1) != "," && valor.substring(i,i+1) != "-" && valor.substring(i,i+1) != "/" && valor.substring(i,i+1) != "(" && valor.substring(i,i+1) != ")" && valor.substring(i,i+1) != ":")
			temp = temp + valor.substring(i,i+1);
		}

		j = 0;
		temp2 = "";
		for (i=0; i<temp.length;i++)
		{
			if (mascara.substring(j,j+1)=="#")
			{
				temp2 = temp2 + temp.substring(i,i+1);
				j = j + 1;
			}
			else
			{
				temp2 = temp2 + mascara.substring(j,j+1) + temp.substring(i,i+1);
				j = j + 2;
			}
		}

		object.value = temp2;
	}
}

//VALIDACAO DE CAMPOS DE DATA
function validadata(dia,mes,ano) {
  if ((!(dia > 0)) || (!(mes > 0)) || (!(ano > 0))) { return false; }
  if ((dia < 01) || (dia < 01 || dia > 30) && (mes == 04 || mes == 06 || mes == 09 || mes == 11) || dia > 31) { return false; }
  if (mes < 01 || mes > 12 ) { return false; }
  if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { return false; }
  return true;
}

//USADO PARA FIXAR O TAMANHO EM CAMPOS TEXTAREA
function tamanhoValido(e,obj,tam)
{
	if ((e.keyCode == 8) || (e.keyCode == 46)) return true;
	if ((e.keyCode >= 33) && (e.keyCode <= 40)) return true;
	if (obj.value.length >= tam) { return false; }
}

//O CAMPO DO FORMULARIO ACEITA SOMENTE NUMEROS
function aceitarSomenteNumero(campo) {
   var valor = "";
   var teste = campo.value.toUpperCase();
   for (var i = 1;i<=teste.length;i++) {
      if(!isNaN(teste.charAt(i-1))) {
         valor += teste.charAt(i-1);
	  }
   }
   campo.value=valor;
}

		
		
		function dropTipo(){
			definirLegend(document.getElementById("tipo").options[document.getElementById("tipo").selectedIndex].text);
			if(document.getElementById("tipo").value=="acidentes"){
				controleDisplay("none","block","none");
				definirLegend(document.getElementById("tipo").options[0].text);
			}
			if(document.getElementById("tipo").value=="infracoes"){
				controleDisplay("none","none","block");
				definirLegend(document.getElementById("tipo").options[1].text);
			}
			if(document.getElementById("tipo").value=="criminalidade"){
				controleDisplay("block","none","none");
			}
		}
		function controleDisplay(divConsulta, selectAcidente,selectInfracao){
			document.getElementById("criterioConsulta").style.display = divConsulta;
			document.getElementById("acidente").style.display = selectAcidente;
			document.getElementById("infracao").style.display = selectInfracao;
		}
		
		function processStart(){
			document.getElementById("start").style.display = "block";
		}
		
		function processEnd(){
			document.getElementById("start").style.display = "none";
		}
		
		function controleDisplayCamposConsulta(selectUf, selectMes, selectAno){
		    document.getElementById("criterioConsulta").style.display = "block";
			document.getElementById("ufLabel").style.display = selectUf;
			document.getElementById("uf").style.display = selectUf;
			document.getElementById("mesLabel").style.display = selectMes;
			document.getElementById("mes").style.display = selectMes;
			document.getElementById("anoLabel").style.display = selectAno;
			document.getElementById("ano").style.display = selectAno;
		}
		
		function definirCriminalidadeAction(){
			var formObj = document.formRelatorio;
			if(document.getElementById("tipo").value=="criminalidade"){
				formObj.action = 'result_rod_geral.jsp';
				controleDisplayCamposConsulta("block","block","block");
			} else {
				formObj.action = '';
			}
		}
		
		function definirLegend(descricao){
			document.getElementById("legendaRelatorio").innerHTML = "CONSULTA RELAT&Oacute;RIO DE " + descricao;
		}
		
		function definirInfracaoAction(){
			var formObj = document.formRelatorio;
			definirLegend(document.getElementById("tipo").options[document.getElementById("tipo").selectedIndex].text + " X " + document.getElementById("infracao").options[document.getElementById("infracao").selectedIndex].text);
			if(document.getElementById("tipo").value=="infracoes"){
				if(document.getElementById("infracao").value=="infracaoGeral"){
					formObj.action = 'result_infracoes_geral.jsp';
					controleDisplayCamposConsulta("block","none","block");
				}
				if(document.getElementById("infracao").value=="infracaoTipo"){
					formObj.action = 'result_infracoes_tipo.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
			} else {
				formObj.action = '';
			}
		}
		
		function definirAcidenteAction(){
			var formObj = document.formRelatorio;
			definirLegend(document.getElementById("tipo").options[document.getElementById("tipo").selectedIndex].text + " X " + document.getElementById("acidente").options[document.getElementById("acidente").selectedIndex].text);
			if(document.getElementById("tipo").value=="acidentes"){
				if(document.getElementById("acidente").value=="acidenteGeral"){
					formObj.action = 'result_acidentes_geral.jsp';
					controleDisplayCamposConsulta("block","none","block");
				}
				if(document.getElementById("acidente").value=="acidenteAno"){
					formObj.action = 'result_acidentes_ano.jsp';
					controleDisplayCamposConsulta("block","none","none");
				}
				if(document.getElementById("acidente").value=="acidenteTipo"){
					formObj.action = 'result_acidentes_tipo.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
				if(document.getElementById("acidente").value=="acidenteBR"){
					formObj.action = 'result_acidentes_br.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
				if(document.getElementById("acidente").value=="acidenteCausa"){
					formObj.action = 'result_acidentes_causa.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
				if(document.getElementById("acidente").value=="acidenteTipoVeiculo"){
					formObj.action = 'result_acidentes_tipo_veiculo.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
				if(document.getElementById("acidente").value=="acidenteFaseDia"){
					formObj.action = 'result_acidentes_fase.jsp';
					controleDisplayCamposConsulta("block","block","block");
				}
			} else {
				formObj.action = '';
			}
		}
		
		function imprimirRelatorio(){
			if(confirm("Imprimir relatório?")){
				window.print();
			}
		}
		
		function fecharJanela(){
			if(confirm("Fechar a janela?")){
				window.close();
				opener.location.reload();
			}
		}
		
		function verificarPopup(){
			var formObj = document.formRelatorio;
				formObj.target = 'frameEstatistica';
			if(document.getElementById("popup").checked){
				alert('O processamento podera levar alguns minutos!');
				formObj.target = '_blank';
			}else{
				processStart();
			}
		}