//javascript

//----Variaveis Globais-----//
  var corCerta = "#006699";
  var corErrada = "#0099FF";
  var historico=[]
  var atual=1
  
// ----------------------- //

function carregarDiv(id,arquivo,controle,metodo){
//	if(arquivo !=
	guardarHistorico(id+","+arquivo);
	//verAtual();
	ajaxHTML(id,arquivo,controle,metodo);
}


function guardarHistorico(dados){
	historico[atual] = dados;
	atual++;
}

function verAtual(){
	//tam = atual-1;
	//alert(atual);
	//alert(historico[tam]+" ->"+tam);
}

function voltarHistorico(){
	if(atual > 2){
		//verAtual();
		atual = atual -1;
		tam = atual -1;
		dado = historico[tam].split(",");
		if(dado[0] == "miolo")
			ajaxHTML(dado[0],dado[1],2,"GET");
		else
			ajaxHTML(dado[0],dado[1],0,"GET");
		
	}else{
		window.location = "index.php";
	}
}

function voltarHistoricoPerguntasFrequentes(){
	if(atual > 2){
		//verAtual();
		atual = atual -1;
		tam = atual -1;
		dado = historico[tam].split(",");
		if(dado[0] == "dicas")
			ajaxHTML(dado[0],dado[1],2,"GET");
		else
			ajaxHTML(dado[0],dado[1],0,"GET");
		
	}else{
		window.location = "index.php";
	}
}


function habilitar(id){
	var input = document.getElementById(id);
	if(input.disabled == true)
		input.disabled = false;
	else{
		input.disabled = true;
		input.value = "";
	}
}

function mostrar(id){
	var x = document.getElementById(id);
	if(x.style.display == "none"){
		x.style.display = "block";
		var y = document.getElementById("formEndosso");
		for(i=0;i<y.length;i++){
			if(y[i].id.indexOf("veiculo_") != -1){
				y[i].disabled = false;
			}
		}
	}else
		x.style.display = "none";
}

function mudarBG(id){
	var i;
	var x = document.getElementsByName("botaoMenu");
	for(i=0;i<x.length;i++){
		x[i].style.backgroundPosition = "left top";
	}
	document.getElementById(id).style.backgroundPosition = "left bottom";
}

function validarContato(id){
	var x = document.getElementById(id);
	for(i=0;i<x.length;i++){
		
		if( x[i].value == "" && x[i].name != "ramal" && x[i].name != "observacoes" && x[i].name != "comentario" ){
			alert("Preencha todos os campos!");
			x[i].style.backgroundColor = corErrada;
			x[i].focus();
			return false;
		}else
			x[i].style.backgroundColor = corCerta;
		
		if(x[i].name == "email"){
			if(x[i].value.indexOf("@") == -1){
				alert("Digite um email valido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
				x[i].style.backgroundColor = corCerta;
		
		if(x[i].name == "telefone" || x[i].name == "celular"){
			if(isNaN(x[i].value) == true || x[i].value <= 0){	
				alert("Só são permitidos números!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
			x[i].style.backgroundColor = corCerta;
	}
	x.submit();
	/*
	setarCampos(id);
	carregarDiv('respostaContato','include/formContato.php',1,'POST');*/
}

function validarNews(){
	var x = document.formNews;
	for(i = 0, erros = 0; i< x.length; i++){
		if(erros == 0){
			if(x.elements[i].value == "" || x.elements[i].value == "Seu Nome" || x.elements[i].value == "Seu E-mail"){
				alert("Preencha todos os campos!");
				x.elements[i].focus();
				erros++;
			}// == ""
			if(x.elements[i].name == "email" && erros == 0){
				if(x.elements[i].value.indexOf("@") == -1){
					alert("Digite um email valido!");
					x.elements[i].focus();
					erros++;
				}//if indexof
			}//if email
		}//if erros
	}//for
	if(erros == 0)
		x.submit();

}



function validarNews_novo (id){
var x = document.getElementById(id);
for(i = 0; i< x.length; i++){

if( x[i].value == "" || x.elements[i].value == "Seu Nome" || x.elements[i].value == "Seu E-mail" ){
alert("Preencha todos os campos!");
x[i].focus();
return false;
	}
	
	if( x[i].name == "email" ){
	if(x[i].value.indexOf("@") == -1){
	alert("Digite um email valido!");
	x[i].focus();
	return false;
	}
		}
	
}

x.submit();
}


function validarTrabalheConosco(id){
	var x = document.getElementById(id);
	for(i=0;i<x.length;i++){
		
		if( x[i].name != "ramal" && x[i].type != "file" && x[i].name != "observacoes" ){
			if(x[i].value == ""){
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].type == "file"){
			if(x[i].value == ""){
				alert("Selecione seu curriculo!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "email"){
			if(x[i].value.indexOf("@") == -1){
				alert("Digite um email invalido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "cep"){
			
			if(x[i].value.indexOf("-") == -1){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
			
			if(x[i].value.length != 9){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
			
			if(isNaN(x[i].value.substr(0,5)) == true  || isNaN(x[i].value.substr(6,3)) == true){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
		}//if cep
		
		if(x[i].name == "telefone"){
			if(isNaN(x[i].value) == true || x[i].value <= 0){	
				alert("Só são permitidos números!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
			x[i].style.backgroundColor = corCerta;
	
	}//for
	x.submit();
}

function validarOrcamento(id){
	var x = document.getElementById(id);
	var y = document.getElementById("tipo");
	for(i=0;i<x.length;i++){
		
		if(x[i].name != "email" && x[i].name != "fax" && x[i].name != "ramal" && x[i].name != "observacoes"  ){
			if(x[i].value == ""){
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "email" && y.value == "email"){
			if(x[i].value.indexOf("@") == -1){
				alert("Digite um email valido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
			
			if(x[i].value == ""){
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "fax" && y.value == "fax"){
			if(x[i].value == ""){
				alert("Digite o número do fax!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "cep"){
			
			if(x[i].value.indexOf("-") == -1){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
			
			if(x[i].value.length != 9){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
			
			if(isNaN(x[i].value.substr(0,5)) == true  || isNaN(x[i].value.substr(6,3)) == true){
				alert("Digite um CEP válido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
		}//if cep.
		
		if(x[i].name == "telefone"){
			if(isNaN(x[i].value) == true || x[i].value <= 0){	
				alert("Só são permitidos números!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
			x[i].style.backgroundColor = corCerta;
		
	}//for
	/*
	setarCampos(id);
	carregarDiv('respostaOrcamento','include/formOrcamento.php',1,'POST');*/
}

function validarOrcamentoViagem(id){
	var x = document.getElementById(id);
	
	for(i=0;i<x.length;i++){
		
		if(x[i].name != "email" && x[i].disabled != true){
			if(x[i].value == ""){
				alert(x[i].name);
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "email"){
			if(x[i].value == ""){
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
			
			if(x[i].value.indexOf("@") == -1){
				alert("Digite um email valido!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "date1" || x[i].name == "date2"){
			if(x[i].value.indexOf("/") == -1){
				alert("Digite uma data válida!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
			x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "telefone" || x[i].name == "idade"){
			if(isNaN(x[i].value) == true || x[i].value <= 0){	
				alert("Só são permitidos números!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
			x[i].style.backgroundColor = corCerta;
		
	}
	x.submit();
	/*setarCampos(id);
	carregarDiv('respostaOrcamento','include/formOrcamentoProdutos.php',1,'POST');*/
}

function carregarPessoas(valor){
	valor = Number(valor);
	var x = document.getElementById('informacoes');
	if(valor != 0){
		x.style.display = "";
		x.value = "";
		for(i=0;i<valor;i++){
			if(i == 0)
				x.value = x.value+"-------------------------------\nNome:\nIdade:\n-------------------------------\n";
			else
				x.value = x.value+"Nome:\nIdade:\n-------------------------------\n";
		}
	}else{
		x.style.display = "none";
	}
}

function esconderDivs(){
	document.getElementById('quantidade').style.display = 'none';
	document.getElementById('informacoes').style.display = 'none';
}

function mostrarDivs(){
	document.getElementById('quantidade').style.display = '';
}

function validarOrcamentoSeguroAuto(id){
	var x = document.getElementById(id);
	for(i=0;i < x.length;i++){
		if(x[i].name.indexOf("dadosSeguro") == -1){
			if(x[i].value == "" && x[i].disabled == false){
				alert("Preencha todos os campos!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}else
				x[i].style.backgroundColor = corCerta;
		}
		
		if(x[i].name == "seguradoFoneResidencial" || x[i].name == "seguradoFoneComercial" || x[i].name == "seguradoCelular" || x[i].name == "idade"){
			if(isNaN(x[i].value) == true || x[i].value <= 0){	
				alert("Só são permitidos números!");
				x[i].style.backgroundColor = corErrada;
				x[i].focus();
				return false;
			}
		}else
			x[i].style.backgroundColor = corCerta;
	}
	
	//setarCampos(id);
	//carregarDiv('respostaOrcamento','include/formOrcamentoSeguroAuto.php',1,'POST');
	x.submit();
}

function renovacao(mostrar){
	opcao = "none";
	if(mostrar == 1)
		opcao = "";
	x = document.getElementsByTagName("tr");
	for(i=0;i<x.length;i++){
		if(x[i].id.indexOf("renovacao") != -1)
			x[i].style.display = opcao;
	}
}

/*
function marcarCampo(id){
	var x = document.getElementById(id);
	if(x != null){
		if(x.disabled == false){
			x.disabled = true;
		}else{
			x.disabled = false;
		}
	}
}
*/

function buscarDicas(busca){
	carregarDiv('respostaPerguntas','include/dicionarioSeguros.php?acao=result_peguntas_freq&letra='+busca,1,'GET')
}

/*
function formatarTelefone(id,valor,event){
	var telefone = "";
	if(valor != ""){
		if(valor.length == 8)
			telefone = valor+"-";
		else if(valor.length == 3)
			telefone = valor+")";
		else if(valor.length == 1)
			telefone = "("+valor;
						 
		if(event.keyCode == "8"){
			telefone = valor;
			telefone[valor.lenght-1] = "";
		}
			
		if(telefone != "")
			document.getElementById(id).value = telefone;
	}	
}
*/

function formatarCep(event,id,valor){
	var aux = "";
	if(valor != ""){
		if(valor.length == 5)
			aux = valor+"-";
		
		if(event.keyCode == "8"){
			aux = valor;
			aux[valor.lenght-1] = "";
		}
		
		if(aux != "")
			document.getElementById(id).value = aux;	
	}
}

function formatarData(event,id,valor){
	var aux = "";
	if(valor != ""){
		if(valor.length == 2)
			aux = valor+"/";
		
		if(valor.length == 5)
			aux = valor+"/";
		
		if(event.keyCode == "8"){
			aux = valor;
			aux[valor.lenght-1] = "";
		}
		
		if(aux != "")
			document.getElementById(id).value = aux;	
	}
}

function formatarCNPJ(event,id,valor){
	var aux = "";
	if(valor != ""){
		if(valor.length == 2)
			aux = valor+".";
		if(valor.length == 6)
			aux = valor+".";
		if(valor.length == 10)
			aux = valor+"/";
		if(valor.length == 14)
			aux = valor+"-";
		
		if(event.keyCode == "8"){
			aux = valor;
			aux[valor.lenght-1] = "";
		}
		
		if(aux != "")
			document.getElementById(id).value = aux;	
	}
}

function formatarCPF(event,id,valor){
	var aux = "";
	if(valor != ""){
		if(valor.length == 3)
			aux = valor+".";
		if(valor.length == 7)
			aux = valor+".";
		if(valor.length == 11)
			aux = valor+"-";
		
		if(event.keyCode == "8"){
			aux = valor;
			aux[valor.lenght-1] = "";
		}
		
		if(aux != "")
			document.getElementById(id).value = aux;	
	}
}

function mudarPessoa(tipo){
	switch(tipo){
		case 0:
			document.getElementById("pessoaTipoFisica").style.display = "block";
			document.getElementById("pessoaTipoJuridica").style.display = "none";
			break;
		case 1:
			document.getElementById("pessoaTipoFisica").style.display = "none";
			document.getElementById("pessoaTipoJuridica").style.display = "block";
			break;
	}	
}

//valor = 2; Valor é o numero de pessoas, caso seja -1 esconde todos e deixa disabled para não ir valor no POST
function aparecerPessoas(valor){
	var j;
	if(valor == -1){
		for(i=0; document.getElementById("pessoa"+i) != null; i++){
			document.getElementById("pessoa"+i).style.display = "none";
			j = i;
			mudarCamposPessoas(j,1);
		}
		return false;
	}
	
	for(i=0; i<valor; i++){
		document.getElementById("pessoa"+i).style.display = "";
		j = i;
		mudarCamposPessoas(j,0);
	}
	for(j=0; document.getElementById("pessoa"+i) != null; i++){
		document.getElementById("pessoa"+i).style.display = "none";
		j = i;
		mudarCamposPessoas(j,1);
	}
}

//par = 1 (deixa disabled); par = 0 (deixa normal); tipo = 1(puxa do form Auto), tipo = 2 (puxa do form Viagem);
function mudarCamposPessoas(numero,par,tipo){
	switch(tipo){
		case 2:
			if(par == 1){
				document.getElementById("dirigir"+numero+"Nome").disabled = true;
				document.getElementById("dirigir"+numero+"EstadoCivil").disabled = true;
				document.getElementById("dirigir"+numero+"Nascimento").disabled = true;
				document.getElementById("dirigir"+numero+"Relacao").disabled = true;
			}else{
				document.getElementById("dirigir"+numero+"Nome").disabled = false;
				document.getElementById("dirigir"+numero+"Nome").value = "";
				document.getElementById("dirigir"+numero+"EstadoCivil").disabled = false;
				document.getElementById("dirigir"+numero+"EstadoCivil").value = "";
				document.getElementById("dirigir"+numero+"Nascimento").disabled = false;
				document.getElementById("dirigir"+numero+"Nascimento").value = "";
				document.getElementById("dirigir"+numero+"Relacao").disabled = false;
				document.getElementById("dirigir"+numero+"Relacao").value = "";
			}
		break;
		case 1:
		if(par == 1){
				document.getElementById("pessoa"+numero+"Nome").disabled = true;
				document.getElementById("pessoa"+numero+"Idade").disabled = true;
			}else{
				document.getElementById("pessoa"+numero+"Nome").disabled = false;
				document.getElementById("pessoa"+numero+"Nome").value = "";
				document.getElementById("pessoa"+numero+"Idade").disabled = false;
				document.getElementById("pessoa"+numero+"Idade").value = "";
			}
		break;
	}
}

//valor = 2; Valor é o numero de pessoas, caso seja -1 esconde todos e deixa disabled para não ir valor no POST
function aparecerPessoas(valor,tipo){
	var j;
	
	if(valor == -1){
		for(i=0; document.getElementById("pessoa"+i) != null; i++){
			document.getElementById("pessoa"+i).style.display = "none";
			j = i;
			if(tipo == 1)
				mudarCamposPessoas(j,1,1);
			else
				mudarCamposPessoas(j,1,2);
		}
		return false;
	}
	
	for(i=0; i<valor; i++){
		document.getElementById("pessoa"+i).style.display = "";
		j = i;
		if(tipo == 1)
			mudarCamposPessoas(j,0,1);
		else
			mudarCamposPessoas(j,0,2);
		
	}
	for(j=0; document.getElementById("pessoa"+i) != null; i++){
		document.getElementById("pessoa"+i).style.display = "none";
		j = i;
		if(tipo == 1)
			mudarCamposPessoas(j,1,1);
		else
			mudarCamposPessoas(j,1,2);
	}
	
}






function validarContato_endosso(id){
	
	 x = document.formEndosso;
	 
	for(i = 0, erros = 0; i< x.length; i++){
		
if( x.elements[i].name=="ramo2" || x.elements[i].name=="num_apolice2" || x.elements[i].name=="segurado2" || x.elements[i].name=="data2" || x.elements[i].name=="endereco2" || x.elements[i].name=="cpf2" ){
	
 if( x.elements[i].value == "" ){
 alert("Preencha todos os campos!");
 x.elements[i].focus();
 erros++;
 break;
 }
 }	
 }
 
 if( isNaN(x.num_apolice2.value) == true ){
    erros++;
	alert("Digite um valor númerico no campo -Apólice número");
	x.num_apolice2.focus();	 
 }
	
	if( x.item22.checked && ( isNaN(x.import_seg2.value)==true ) ){	
	erros++;
	alert("Digite um valor numérico no campo -Importância Segurada para:");
	x.import_seg2.focus();
	}
	
	if( x.item52.checked && ( isNaN(x.inclu_franq2.value)==true || x.inclu_franq2.value=="" ) ){	
	erros++;
	alert("Digite um valor numérico no campo -Inclusão da Franquia de:");
	x.inclu_franq2.focus();
	}
	
	if( x.item62.checked && ( isNaN(x.exclu_franq2.value)==true || x.exclu_franq2.value=="" ) ){	
	erros++;
	alert("Digite um valor numérico no campo -Exclusão da Franquia de :");
	x.exclu_franq2.focus();
	}
	
	
	
if( erros == 0 )	
x.submit();
	/*setarCampos(id);
	carregarDiv('respostaContato','include/trata_form_endosso.php',1,'POST');*/
}// fim do validar_contatoEndosso

function validarContato_sinistro(id){
	var x = document.trata_form_sinistro;
	
	for( i = 0, erros = 0; i< x.length; i++ ){
	
		if( (x.elements[i].value=="" || x.elements[i].value=="Selecione o estado" ) && x.elements[i].name!="profissao" ){
		 	alert("Preencha todos os campos!");
		 	x.elements[i].style.backgroundColor = corErrada;
         x.elements[i].focus();
         erros++;
         break;
		}else
			x.elements[i].style.backgroundColor = corCerta;
	}
		
	if( isNaN(x.idade.value) == true ){
		erros++;
		alert("Digite um valor numérico no campo Idade");
		x.elements[i].style.backgroundColor = corErrada;
		x.idade.focus();
	}else
		x.elements[i].style.backgroundColor = corCerta;
	
	if( isNaN(x.ano2.value) == true ){
		erros++;
		alert("Digite um valor numérico no campo Ano");
		x.elements[i].style.backgroundColor = corErrada;
		x.ano2.focus();
	}else
		x.elements[i].style.backgroundColor = corCerta;
	
	if( isNaN(x.ano_modelo.value) == true ){
		erros++;
		alert("Digite um valor numérico no campo Ano/modelo.");
		x.elements[i].style.backgroundColor = corErrada;
		x.ano_modelo.focus();
	}else
		x.elements[i].style.backgroundColor = corCerta;
	
	if( isNaN(x.num_habilitacao.value) == true ){
		erros++;
		alert("Digite apenas números no campo -Habilitacao Nª");
		x.elements[i].style.backgroundColor = corErrada;
		x.num_habilitacao.focus();
	}else
		x.elements[i].style.backgroundColor = corCerta;
	
	
	if( erros == 0 )
		x.submit();
	
	/*setarCampos(id);
	carregarDiv('respostaContato','include/trata_form_sinistro.php',1,'POST');*/
}// fim do validarContato_sinistro

