// JavaScript Document
$j(function(){
	//$j.loading({display:0});
	abrir_banner();
	Site.abas();
});
var Site = {
	popup: function(url, width, height, scroll, resize) {
		return void(open(''+url+'','','width='+width+',height='+height+',scrollbars='+scroll+', resizable='+resize+''));
	},
	
	abas: function(){
		$j('#agenda').tabs();
	}
}
var Contato = {
	validar:function(form){
		if(Form.validar(form)){
			this.enviar(form);
		}
		return false
	},
	
	enviar:function(form){
		$j('#enviando').show();
		$j.ajax({url:'acoes.php?contato&enviar',
			type:'post', postBody:Form.serialize(form),
			onComplete:function(o){
				if(o.responseText==""){
					Element.hide('enviando');
					Element.show('enviado');
				}else{
					alert('Erro. Tente novamente mais tarde.');
				}
			}
		});
	}
}
var Enquete = {
	urlacoes: '/cms/modulos/padrao/ger_enquetes/componentes/enquete.php?',
	
	habilita_botao_votar: function(form) {
		with(form) {
			form.votar.disabled = false;	
		}
	},
	
	votar: function(form, div) {
		Element.inner('alerta', 'Computando seu voto, aguarde...');
		
		with(form) {
			new ajax(this.urlacoes+'enquete&votar', {
				method:'post', postBody:Form.serialize(form), 
				onComplete: function(o) { //alert(o.responseText);
					setTimeout(function() { Element.inner('alerta-enquete', 'Voto computado com sucesso!') }, 1000);
					setTimeout(function() { Element.inner('alerta-enquete', '') }, 5000);
					Element.inner(div, o.responseText);
				}
			});
		}		
		return false;
	}
};
/* INSCRICAO */
var Inscricao={
	validar_pessoal:function(form){
		if($Form.validar(form)){
			if(form.crc.value!=""){
				this.atualiza_valor(form);
			}
			Element.show('residencial');
			Element.hide('pessoal');
		}
		return false;
	},

	validar_residencial:function(form){
		if($Form.validar(form)){
			Element.show('profissional');
			Element.hide('residencial');
		}
		return false;
	},

	situacao_profisional:function(form){

		with(form) {
			if(sit_profissao[2].checked==true){
				$j('#content_profissional').slideDown();
			}else{
				$j('#content_profissional').slideUp('normal', function(){
					nom_empresa.value="";
					num_cnpj_empresa.value="";
					num_fone_emp.value = "";
					des_email_emp.value = "";
				});
			}
		}
	},

	validar_profissional:function(form){
		if($Form.validar(form)){
			this.verifica_valor(form);
			Element.show('adicional');
			Element.show('media');
			Element.hide('profissional');
		}
		return false;
	},

	verifica_valor:function(form){
		with(form) {
			if(tipo.value=="W"){
				$j.ajax({url:'verifica_pagamento.php?id_data='+id_data.value,
					success:function(response) {
						var valores = unescape(response).split("|");
						text_pgto.value=valores[0];
						pgto.value=valores[1];
					}		 
				});
			}
		}	
	},

	/*Caso o crc seja preenchido atualiza o valor se o cliente estiver desconto*/

	atualiza_valor:function(form){

		with(form) {
			if(tipo.value=="W"){
				$j.ajax({url:'verifica_pagamento.php?id_data='+id_data.value+'&crc='+crc.value+'&nome='+nome.value+'&cpf='+cpf_cliente.value,
					success:function(response) {
						var valores = unescape(response).split("|");
						text_pgto.value=valores[0];
						pgto.value=valores[1];
						if(valores[2]=="yes"){
							$j.alert({
								type: 'success',
								html: "Parabens lhe foi concedido um desconto de 20%", timeou:5000
							});	
						}
					}		 
				});
			}
		}	
	},

	validar_adicional:function(form){
		if($Form.validar(form)){
			if(form.senha.value != form.confirma_senha.value){
				$j.alert({type:'error', html:'As senhas não conferem', timeout:5000});
			}else if(form.email.value != form.confirma_email.value){
				$j.alert({type:'error', html:'Os emails não são iguais', timeout:5000});
			}else{
				this.efetuar(form);
			}
		}
		return false;
	}, 

	validar:function(form){
		$j.alert({type:'loading', hide:false, html:'Processando dados...'});
		if(form.des_email_confirma.value == form.des_email_cand.value){
			if($j("input[@name='opcaoPagamento']:checked").val() > 0){
				if($j("input[@name='vencimento']:checked").val() > 0){
					if($Form.validar(form)){
						form.cod_cliente.value == '' ? this.inserirCliente(form) : this.salvarCliente(form);
					}else{
						$j.alert({type:'error', html:'Preencha corretamente o formulário', timeou:5000});
					}
				}else{
					$j.alert({
						type: 'alert',
						html:'Selecione o melhor dia para o vencimento', timeou:5000
					});
				}
			}else{
				$j.alert({
					type: 'alert',
					html:'Selecione a Forma de Pagamento', timeou:5000
				});
			}
		}else{
			$j.alert({
				type: 'alert',
				html:'Os e-mails não são iguais', timeou:5000
			});
		}
		return false;
	},
	
	inserirCliente:function(form){
		$j.ajax({
			url:'acoes.php?cadastrar_aluno', type:'post', data:$j(form).serialize(),
			success:function(response) {//alert(response);
				if(response.indexOf('inserido') != -1){
					var valores = unescape(response).split("|");
					$j.alert({html:'Cadastro foi realizado com sucesso.', timeou:5000});
					location = location+'&pagamento&inscricao='+valores[1];
				}else if(response=='Este e-mail j&aacute; est&aacute; cadastrado.'){
					$j.alert({type:'error', html:response, timeou:5000});
				}else{
					$j.alert({type:'error', html:'N&atilde;o foi possivel realizar o cadastro.<br />'+response, timeou:5000});
				}
			}
		});
	},
	
	salvarCliente:function(form){
		$j.ajax({
			url:'acoes.php?salvar_cliente', type:'post', data:$j(form).serialize(),
			success:function(response) {//alert(response);
				if(response=="Inscrito"){
					$j.alert({type:'alert', html:'Voc&ecirc; j&aacute; est&aacute; inscrito neste curso', timeou:5000});
				}else if(response.indexOf('inserido') != -1){
					var valores = unescape(response).split("|");
					location = location+'&pagamento&inscricao='+valores[1];
				}else{
					$j.alert({type:'error', html:'N&atilde;o foi possivel realizar o cadastro.', timeou:5000});
				}
			}
		});
	},
	
	efetuar:function(form){
		Element.show('inscrevendo');
		new ajax('acoes.php?cadastrar_aluno', {
			method:'post', postBody:Form.serialize(form), 
			onComplete: function(o) { //alert(o.responseText);

				if(o.responseText.indexOf('inserido') != -1){
					Element.hide('inscrevendo');
					Element.show('inscrito');
					Element.hide('adicional');
					Element.hide('media');
					Element.show('pessoal');					
					if((form.tipo.value=="W") && (form.pgto.value!='0,00')){
						var valores = unescape(o.responseText).split("|");
						Site.popup('../boleto/emitir.php?inscricao='+valores[1], 650, 500, 0, 0);
					}
					alert('InscriÃ§Ã£o realizada com sucesso!!!');
					form.reset();
					location='index.php';
				}else if(o.responseText== "cadastrado"){
					alert('VocÃª jÃ¡ possui cadastro! Efetue a inscriÃ§Ã£o clicando no botÃ£o "sou cadastrado".');
				}else{
					alert("Sua inscriÃ§Ã£o nÃ£o foi realizada. Tente novamente em instantes.");
					new ajax('acoes.php?reportar&local=InscriÃ§Ã£o Aluno&ref=Inscricao.efetuar&msg='+o.responseText);					
				}
			}
		});
	},	

	login:function(form){
		$j.alert({type:'loading', hide:false,  html:'Carregando dados...'});
		if($Form.validar(form)){
			$j.ajax({
				url:'acoes.php?logar', type:'post', data:$j(form).serialize(),
				success:function(response) {
					if(response == "logado"){			
						location.reload();		
					}else{
						$j.alert({type:'error', html:'Login incorreto!', timeou:5000});
					}
				}
			});
		}
		return false;
	},
	
	recuperar_senha:function(form){
		var form = document.getElementById('f_login');
		if(form.cpf_cliente.value==""){
			form.cpf_cliente.className="form-container invalid";
		}else{
			new ajax('/cms/modulos/personalizado/clientes/componentes/sig_acoes.php?recuperar_senha', {
				method:'post', postBody:Form.serialize(form), 
				onComplete: function(o) { //alert(o.responseText);
					if(o.responseText==""){
						Form.reset(form);
						$j.alert({type:'success', html:'Sua nova senha foi enviada por e-mail!', 'timeout':5000});
					} else {
						$j.alert({type:'error', html:'Cadastro nao localizado!', 'timeout':5000});
					}
				}
			});	
		}
	},
	
	insere_aluno :function(form){
		if($j('#pagseguro').val()=='s'){
			$j.ajax({
				url:'acoes.php?inscrever_cliente&tipo=G&pagseguro', type:'post', data:$j(form).serialize(),
				success:function(response) {
					if(response.indexOf('inserido') != -1){
						var valores = unescape(response).split("|");
						$j('#f_pagseguro #item_id_1').val(valores[1]);
						$j('#aviso').html('Sua IncriÃ§Ã£o foi realizada com sucesso. <br> Clique no botÃ£o abaixo para realizar o pagamento.');
						$j('#f_login,#f_inscricao, #coluna_direita radio').hide();
						$j('#bt_pagseguro').show();
						//$j('#f_pagseguro').submit();
						//alert('InscriÃ§Ã£o realizada com sucesso!!!');
						/*form.reset();
						location='index.php';*/
	
					}else if(response=="Inscrito"){
						$j.alert({
							type: 'alert',
							html: 'VocÃª jÃ¡ estÃ¡ inscrito neste Curso', timeou:5000
						});
					}else{
						$j.alert({
							type: 'error',
							html: 'NÃ£o foi PossÃ­vel realizar inscriÃ§Ã£o. Tente novamente mais tarde \n' + o.responseText, timeou:5000
						});
					}
				}
			});
		}else{
			
			$j.ajax({url:'acoes.php?inscrever_cliente',
				type:'post', data:$j(form).serialize(),
				success:function(response) {
					
					if(response.indexOf('inserido') != -1){
						alert('InscriÃ§Ã£o realizada com sucesso!!!');
						if((form.tipo.value=="W") && (form.pgto.value!='0,00')){
							var valores = unescape(o.responseText).split("|");
							Site.popup('../boleto/emitir.php?inscricao='+valores[1], 650, 500, 0, 0);
						}
						form.reset();
						location='index.php';
	
					}else if(response=="Inscrito"){
						$j.alert({
							type: 'alert',
							html: 'VocÃª jÃ¡ estÃ¡ inscrito neste Curso', timeou:5000
						});
					}else{
						$j.alert({
							type: 'error',
							html: 'NÃ£o foi PossÃ­vel realizar inscriÃ§Ã£o. Tente novamente mais tarde \n' + o.responseText, timeou:5000
						});
	
					}
	
				}
			});
		}
	}
}

var Videos={
	janela:function(titulo,video,width_v,height_v){
		Site.popup('../videos.php?video='+video+'&width='+width_v+'&height='+height_v, width_v+15, height_v+85, 0, 0);
	}
}

var Idicacao={
	validar:function(form){
		if($Form.validar(form)){
			Element.show('enviando');
			$j.ajax({url:'acoes.php?indique', 
				type:'post', data:$j(form).serialize(),
					success:function(response) {
						if(response==""){
							$j('#enviando').hide();
							$j('#enviado').show();
							form.reset();
						}else{
							alert(response);
							alert('NÃ£o foi possÃ­vel enviar sua reserva. Tente mais tarde');	
						}
					
					} 
				}					 
			);
		}
		return false;
	}	

}/* Funï¿½ï¿½o para banner flutuante*/

function fechar_banner() {
	$j('#lista').hide();
}
function abrir_banner(){
	$j('#lista').show();
}
var Conhecimento={
	validar:function(form){
		if($Form.validar(form)){
			$j('#dados').slideUp(function(){
				$j('#pergunta_1').slideDown();					  
			});
		}
	}	
}
var Banner ={
	contarClick:function(links){	
		$j.ajax({
			url: 'acoes.php?banner=' + links
		});		
	}
}
var Promo={
	validar:function(form){
		var interesse = 0;
		$j('#interesse :selected').each(function(i, selected){
			interesse ++;			
		});
		
		if(interesse > 0){
			if($Form.validar(form)){
				form.name == "f_promo" ? this.Cadastrar(form) : this.Indicar(form) ;
			}
		}else{
			$j.alert({type:'error', html:'Selecione ao menos um interesse.', timeou:5000});
		}
		return false;
	},
	
	Cadastrar:function(form){
		var interesse = "";
		var non_interesse = "";
		$j('#interesse :selected').each(function(i, selected){
			interesse += $j(selected).val()+'|';
			non_interesse += $j(selected).text()+'|';
		});
		
		$j.ajax({
			url:'acoes.php?promo=cadastrar&area_interesse='+interesse+'&non_interesse='+non_interesse, type:'post', data:$j(form).serialize(),
			success:function(response) {//alert(response);
				if(response.length < 2){
					$j.alert({html:'Cadastro foi realizado com sucesso.'});
					form.reset();
				}else if(response=='Voc&ecirc; j&aacute; est&aacute; cadastrado nesta promo&ccedil;&atilde;o.'){
					$j.alert({type:'error', html:response, timeou:5000});
				}else{
					$j.alert({type:'error', html:'N&atilde;o foi possivel realizar o cadastro.<br />'+response});
				}
			}
		});
	},
	
	Indicar:function(form){
		$j.ajax({
			url:'acoes.php?promo=indicar', type:'post', data:$j(form).serialize(),
			success:function(response) {//alert(response);
				if(response.indexOf('enviado') != -1){
					$j.alert({html:'Sua indica&ccedil;&atilde;o foi realizada com sucesso.', timeou:5000});
				}else{
					$j.alert({type:'error', html:'N&atilde;o foi poss&iacute;vel enviar sua indica&ccedil;&atilde;o.<br />'+response, timeou:5000});
				}
			}
		});
	}
}
