/**
 * Passa de um passo para o outro 
 * 
 */
function navegaPassos(passoAtual, acao){
	
	numPasso = passoAtual.replace(".html", "");
	numPasso = numPasso.replace("passo", "");
	numPasso = numPasso * 1;
	numProximo = numPasso + 1;
	numAnterior = numPasso - 1;
	passoAnterior = "passo"+numAnterior+".html";
	passoProximo = "passo"+numProximo+".html";
	
	switch ( acao ){
		case 1: 
			$("#cadastroPassos").load(passoAnterior);
			break;
		case 2:
			$("#cadastroPassos").load(passoProximo);
			break;
	}
	
} 

/**
 * Mostra os detalhes do plano 
 */

function mostraDetalhes ( campo ){
	campof = "." + campo + "f";
	//verifica se esta aberto ou fechado o item
		if($(campof).css("display")=='none'){
			//se o submenu estiver fechado
			$('.cfilho').addClass("campofnone");
			$(campof).removeClass("campofnone");
		}else{
			//se o submenu estiver aberto
			$(campof).addClass("campofnone");
		}
}
/**
 *  
 *  Exibe dados de um determinado plano 
 * @param String - nomePlano
 */

function exibirDadosPlanos( nomePlano, tipoPagamento){
	
     $.ajax({
             type: "GET",
             url: "_xml/planosGuiaRecreio.xml",
             dataType: "xml",
             success: function(xml) {
				
                $(xml).find( nomePlano ).each(function(){
			 	
					var pagamentos = $(this).children("pagamentos");
					var recursos = $(this).children("recursos");

					var htmlPagamentos = "";
					htmlPagamentos += "<h3 class='tit-comparativo'>Selecione a periodicidade de pagamento</h3>" ;
					htmlPagamentos += "<table summary='Selecione a periodicidade de pagamento' class='comparativo'>" ;
					htmlPagamentos += " <tr>" ;
					htmlPagamentos += 		"<td colspan='5' class='grupo'>" ;
					htmlPagamentos += 			"<strong>Modalidades de pagamento</strong>" ;
					htmlPagamentos += 		" </td>" ;
					htmlPagamentos += "</tr>" ;
					
					// Para cada tipo de pagamento
					pagamentos.children().each(function(){

						var nome = this.tagName;
						var desc = $(this).attr('desc');
						var valor = $(this).attr('valor');
						
						if (valor != "") {
							
							var selecionado;							
							if (tipoPagamento == null && (nome == "anual" || nome == "valorgratuito" )){
								selecionado = "checked='checked'";
							}else if( tipoPagamento == nome ){
								selecionado = "checked='checked'";
							}
							
							htmlPagamentos += "<tr>";
							htmlPagamentos += "<td class='td-pagamento'>";
							htmlPagamentos += "<input type='radio' id='" + nome + "' name='pagamento' value='" + nome + "' " + selecionado  + " /> " + desc;
							htmlPagamentos += "</td>";
							htmlPagamentos += "<td>";
							if( nomePlano != "gratuito"){
								htmlPagamentos += "<label for='" + nome + "''>" + "R$ " + valor.replace(".", ",") + "</label>";
							}else{
								htmlPagamentos += "<label for='" + nome + "''>" + valor + "</label>";
							}
							htmlPagamentos += "</td>";
							htmlPagamentos += "</tr>";
						}
					});
					
					htmlPagamentos += "</table>" ;					
					
					var htmlRecursos = "";
					htmlRecursos += "<h3 class='tit-comparativo'>Caracteristicas do plano escolhido</h3>" ;
					htmlRecursos += "<table summary='Recursos do plano' class='comparativo'>" ;
					htmlRecursos += "<col width='128' /><col style='text-align: center;' width='95' />" ;
					htmlRecursos += " <tbody>" ;
					htmlRecursos += " <tr>" ;
					htmlRecursos += 		"<td colspan='2' class='grupo'>" ;
					htmlRecursos += 			"<strong>Recursos do plano</strong>" ;
					htmlRecursos += 		" </td>" ;
					htmlRecursos += "</tr>" ;					
					
					var i = 1 ;
					
					// Para cada "categoria" dos recursos
					recursos.children().each(function(){

					/*		var desc = $(this).attr('desc');			
	
						htmlRecursos +=  "<tr>";
						htmlRecursos += 		"<td colspan='2' class='grupo' >";
						htmlRecursos += 			"<strong>" + desc + "</strong>";	
						htmlRecursos +=  		"</td>";						
						htmlRecursos +=  "</tr>";
					*/
						// Para cada recurso de uma "categoria"
						$(this).children().each(function(){
							var desc = $(this).attr('desc');
							var valor =$(this).text();

						htmlRecursos +=  "<tr class='cfilho campofnone campo"+i+"f'>";							
						htmlRecursos +=  		"<td>" + desc + "</td>";
						
						if( desc == "Desconto" ){
							htmlRecursos +=  		"<td>" + valor + "%</td>";		
						}else{
							if (valor == "sim") {
								htmlRecursos += "<td><img src='_img/visto.png' alt='ok' width='16' height='16' /></td>";
							} else if ( valor == "não" ) {
								htmlRecursos += "<td><img src='_img/ico-erro.png' alt='não possui' width='16' height='16' /></td>";							
							} else {
								htmlRecursos += "<td>" + valor + "</td>";								
							}
						}
						
						htmlRecursos +=  "</tr>";												
							
						});
						
						i++;
						
					});
					
					htmlRecursos += "</tbody>" ;	
					htmlRecursos += "</table>" ;	
					
					$("#div-atencao").show();			
					$("#bt-prosseguir").show();	
					
					// Exibimos os dados de pagamento e recursos		
					//if (nomePlano != "gratuito") {
					$("#modalidadePagamentos").html(htmlPagamentos);
					$("#modalidadePagamentos").show();
						
					/*}else{
						$("#modalidadePagamentos").hide();
					}*/
					$("#recursosPlano").html(htmlRecursos);
					$("#recursosPlano").show();
					
							
                 }); //close each(
                 
             }
         }); //close $.ajax(	
	
}

/**
 * Muda a cor dos campos quando estiverem com focus
 */
/*
$(document).ready( function(){
	$('select').focus(function(){
		$(this).addClass("inputFocus");  
	});

	$('select').blur(function(){
		$(this).removeClass("inputFocus");  
	});
	
	$('textarea').focus(function(){
		$(this).addClass("inputFocus");  
	});

	$('textarea').blur(function(){
		$(this).removeClass("inputFocus");  
	});
	
	$('input').focus(function(){
		$(this).addClass("inputFocus");  
	});

	$('input').blur(function(){
		$(this).removeClass("inputFocus");  
	});

});*/
/**
 * 
 * Função para corrigir bug, na alteração de cor do input, provocado pelo autoTab,
 * função adicionada no onblur do input, pois o jquery não reconhece o .blur
 * quando o input esta com autoTab ativado nele.
 * Esta função corrige o bug tirando a classe colocada no onfucus
 *
 */
function removeClasse(input){
	input.className = "";
}

/**
 * Exibe campos específicos conforme o tipo de pessoa física ou jurídica.
 *
 * @param string - tipoPessoa
 */
function montarCamposPessoa( tipoPessoa ){
	
	if( tipoPessoa == 'fisica' ){
		
		$('#divCamposEspecificosPessoaFisica').show();
		$('#divCamposEspecificosPessoaJuridica').hide();
		
		// Mudamos os título para que sejam obrigatórios só os campos específico de cada tipo de pessoa
		// O * no título indica que um campo é obrigatório.
		document.getElementById('lblEstadoCivil').innerHTML = '* Estado Civil';
		document.getElementById('estadoCivil').title='* Estado Civil';
		document.getElementById('rg').title='* RG';
		document.getElementById('cpf').title='* CPF';
		document.getElementById('nome').title='* Nome';
		document.getElementById('contato').title='Contato';
		//document.getElementById('emailContato').title='E-mail contato';
		
		document.getElementById('lblContato').innerHTML='Contato';
		//document.getElementById('lblEmailContato').innerHTML='E-mail contato';
		
		document.getElementById( 'CPFMsg2' ).innerHTML = '';
		document.getElementById('CNPJMsg').innerHTML = '';	
		document.getElementById('razaoSocial').value='';
		document.getElementById('razaoSocial').title='';
		document.getElementById('cnpj').value='';
		document.getElementById('cnpj').title='';
		document.getElementById('representanteLegal').value='';
		document.getElementById('representanteLegal').title='';
		document.getElementById('cpfRepresentante').value='';
		document.getElementById('cpfRepresentante').title='';
		document.getElementById('rgRepresentante').value='';
		document.getElementById('rgRepresentante').title='';
		
	} else if( tipoPessoa == 'juridica' ){ 
		
		$('#divCamposEspecificosPessoaFisica').hide();
		$('#divCamposEspecificosPessoaJuridica').show();		
		
		// Mudamos os título para que sejam obrigatórios só os campos específico de cada tipo de pessoa
		// O * no título indica que um campo é obrigatório.
		document.getElementById('razaoSocial').title='* Razão Social';
		document.getElementById('cnpj').title='* CNPJ';
		document.getElementById('representanteLegal').title='* Representate Legal';
		document.getElementById('cpfRepresentante').title='* CPF Representante';		
		document.getElementById('rgRepresentante').title='* RG Representate Legal';
		document.getElementById('contato').title='* Contato';
		//document.getElementById('emailContato').title='*E-mail contato';
		
		document.getElementById('lblContato').innerHTML='* Contato';
		//document.getElementById('lblEmailContato').innerHTML='E-mail contato*';		
		
		document.getElementById('CPFMsg').innerHTML = '';
		document.getElementById('estadoCivil').value='';
		document.getElementById('estadoCivil').title='Estado Civil';
		document.getElementById('lblEstadoCivil').innerHTML = 'Estado Civil';
		document.getElementById('rg').value='';		
		document.getElementById('rg').title='RG';
		document.getElementById('cpf').value='';		
		document.getElementById('cpf').title='CPF';
		document.getElementById('nome').value='';		
		document.getElementById('nome').title='Nome';					
		
	}
	//alert( ' cpfRepresentante -> ' + document.getElementById('cpfRepresentante').title );
	
}
/**
 *	função para validar telefone,
 *	tipos 1 = residencial , 2 = celular, 
 */
function validaTelefone(tipo, telefone){
	if( tipo == 1){
		//verifica se o telefone começa com 2 ou 3
		var erTel = /^[23]([0-9]){3}-[0-9]{4}$/;
		return erTel.test(telefone);
				
	}else if(tipo == 2){
		//verifica se o telefone começa com 8,9 ou 7
		var erCel = /^[897]([0-9]){3}-[0-9]{4}$/;
		return erCel.test(telefone);
		
	}else if (tipo == 3) {
		
		var erTel = /^[23]([0-9]){3}-[0-9]{4}$/;
		var tel = erTel.test(telefone);
		
		var erCel = /^[897]([0-9]){3}-[0-9]{4}$/;
		var cel = erCel.test(telefone);
		
		if( tel || cel ){
			return true;
		}else{
			return false;
		}
		
	}else {
		return false;
	}
}


/**
 *  fução que verifica o tipo de telefone e valida
 */

//tipos de telefones 
	//1 = telefone residencial
	//2 = celular
	//3 = fax
	//4 = telefone representante
	function verificaTelefone(tipo, telefone, id){
		
		if(tipo == 1){
			if (telefone.value != '____-____' && telefone.value.length > 0) {
				if (validaTelefone(1, telefone.value)) {
					$('#msgTelefone').html("");
					$('#telefone').removeClass("campo-erro");
					telefoneOk = true;
				}
				else {
					$('#telefone').addClass("campo-erro");
					$('#msgTelefone').html("<p class='msgTextoErro'>Telefone Inválido</p>");
					telefoneOk = false;
				}
			}else{
				$('#msgTelefone').html("");
				$('#telefone').removeClass("campo-erro");
				telefoneOk = true;
			}
		}else if(tipo == 2){
			if (telefone.value != '____-____' && telefone.value.length > 0) {
				if (validaTelefone(2, telefone.value)) {
					$('#msgCelular').html("");
					$('#celular').removeClass("campo-erro");
					celularOk = true;
				}
				else {
					$('#celular').addClass("campo-erro");
					$('#msgCelular').html("<p class='msgTextoErro'>Celular Inválido</p>");
					celularOk = false;
				}
			}else{
				$('#msgCelular').html("");
				$('#celular').removeClass("campo-erro");
				celularOk = true;
			}
		}else if(tipo == 3){
			if (telefone.value != '____-____' && telefone.value.length > 0) {
				if (validaTelefone(3, telefone.value)) {
					$('#telefone'+id).removeClass("campo-erro");
					$('#msgTelefone'+id).html("");
					switch (id){
						case 'I': telefoneI = true;
							break;
						case 'II': telefoneII = true;
							break;
						case 'III': telefoneIII = true;
							break;
					}
				}
				else {
					$('#telefone'+id).addClass("campo-erro");
					$('#msgTelefone'+id).html("<p class='msgTextoErro'>Telefone Inválido</p>");
					switch (id){
						case 'I': telefoneI = false;
							break;
						case 'II': telefoneII = false;
							break;
						case 'III': telefoneIII = false;
							break;
					}
				}
			}else{
				$('#telefone'+id).removeClass("campo-erro");
				$('#msgTelefone'+id).html("");
				switch (id){
						case 'I': telefoneI = true;
							break;
						case 'II': telefoneII = true;
							break;
						case 'III': telefoneIII = true;
							break;
					}
			}
		}	
	
	} 
 
