﻿// JavaScript Document

// VARIAVEIS PARA SUBMENU
// FUNÇÕES QUE PRECISAM DO DOCUMENTO PRONTO
$(document).ready(function(){
});

$(function(){
	// MÁSCARAS
	jQuery(function($){
		$(".uf").mask("aa");
		$(".ddd").mask("99");
		$(".fone").mask("99999999");
		$(".telefone").mask("(99) 9999.9999");
		$(".data").mask("99/99/9999");
		$(".hora").mask("99:99");
		$(".cpf").mask("999.999.999-99");
	});
	
	// ABRIR LINKS COM REL BLANK
	$("a[rel=_blank]").click(function(){
		window.open($(this).attr('href'));
		return false;
	})
	
	// RETIRA CONTEUDO INICIAL DOS INPUTS
	$("#formBusca input[type='text'], #formNewsletter input[type='text']").each(function(){
		if($(this).attr("iniVal") == undefined){
			$(this).attr("iniVal", $(this).val());
		}
		$(this).focus(function(){
			if($(this).val() == $(this).attr("iniVal")){
				$(this).val("");
			}
		});
		$(this).blur(function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("iniVal"));
			}
		});
	});
	
	$("#formReserva").validate({
		rules: {
			nome: {required: true},
			email: {required: true, email:true},
			telefone: {required: true},
			observacoes: {required: true}
		},
		messages: {
			nome: {required: 'Informe o nome'},
			email: {required: 'Informe o e-mail', email:'Informe um e-mail válido'},
			telefone: {required: 'Informe o telefone'},
			observacoes: {required: 'Escreva mais detalhes sobre a encomenda'}
		},
		submitHandler: function(form){
			$('#retornoReserva').html('Processando informações...');
			
			// DESABILITA O BOTÃO PARA EVITAR DUPLO CLIQUE E EXIBE MENSAGEM
			$('#formReserva .btEnvia').attr('disabled','disabled');
			
			// ENVIA O FORMULARIO
			$.post('produto/reservar', $(form).serializeArray(), getRetorno, "json");
			return false;
		}
	});

	$("#formDepoimento").validate({
		rules: {
			nome: {required: true},
			email: {required: true, email:true},
			texto: {required: true}
		},
		messages: {
			nome: {required: 'Informe seu nome'},
			email: {required: 'Informe seu e-mail', email:'Informe um e-mail válido'},
			texto: {required: 'Informe o depoimento'}
		},
		submitHandler: function(form){
			// DESABILITA O BOTÃO PARA EVITAR DUPLO CLIQUE E EXIBE MENSAGEM
			$('#formDepoimento .btEnvia').attr('disable','disable');
			$('#retornoDepoimento').html('Aguarde, processando...');
			
			// ENVIA O FORMULARIO
			$.post('depoimentos/gravar', $(form).serializeArray(), getRetorno, "json");
			return false;
		}
	});

	$("#formContato").validate({
		rules: {
			nome: {required: true},
			email: {required: true, email:true},
			telefone: {required: true},
			texto: {required: true}
		},
		messages: {
			nome: {required: 'Informe seu nome'},
			email: {required: 'Informe seu e-mail', email:'Informe um e-mail válido'},
			telefone: {required: 'Informe o telefone'},
			texto: {required: 'Informe a mensagem'}
		},
		submitHandler: function(form){
			// DESABILITA O BOTÃO PARA EVITAR DUPLO CLIQUE E EXIBE MENSAGEM
			$('#formContato .btEnvia').attr('disable','disable');
			$('#retornoContato').html('Aguarde, processando...');
			
			// ENVIA O FORMULARIO
			$.post('contato/enviar', $(form).serializeArray(), getRetorno, "json");
			return false;
		}
	});
});


function getRetorno(data){
	
	var msg = '';
	
	if(data.tipo == 'reserva'){
		if(data.rs == 'erro_post' || data.rs == 'erro_obrigatorio'){
			msg = 'Informe os campos obrigatórios (com *)';
		}
		if(data.rs == 'erro_envio_email'){
			msg = 'Erro ao enviar e-mail';
		}
		if(data.rs == 'erro_db'){
			msg = 'Erro ao gravar e-mail, por favor tente mais tarde';
		}
		if(data.rs == 'ok'){
			msg = 'Reserva realizada com sucesso.';
			$("#formReserva input[type='text'], #formReserva textarea").val('');
		}
		
		$('#retornoReserva').html(msg);
		$('#formReserva .btEnvia').removeAttr("disabled");
	}
	
	if(data.tipo=='depoimento'){
		if(data.rs == 'erro_post' || data.rs == 'erro_obrigatorio'){
			msg = 'Todos os campos são obrigatórios';
		}
		if(data.rs == 'erro_envio_email'){
			msg = 'Erro ao enviar e-mail';
		}
		if(data.rs == 'erro_db'){
			msg = 'Erro ao gravar depoimento, por favor tente mais tarde';
		}
		if(data.rs == 'ok'){
			msg = 'Depoimento enviado com sucesso.';
			$("#formDepoimento input[type='text'], #formDepoimento textarea").val('');
		}
		
		$('#retornoDepoimento').html(msg);
		$('#formDepoimento .btEnvia').removeAttr("disabled");
	}
	
	if(data.tipo == 'contato'){
		if(data.rs == 'erro_post' || data.rs == 'erro_obrigatorio'){
			msg = 'Todos os campos são obrigatórios';
		}
		if(data.rs == 'erro_envio_email'){
			msg = 'Erro ao enviar e-mail, por favor tente mais tarde';
		}
		if(data.rs == 'ok'){
			msg = 'Mensagem enviada com sucesso.';
			$('#formContato input, #formContato textarea').val('');
			
			// AGUARDA UM TEMPO E FECHA A CAIXA
			setTimeout(function(){ $('#retornoContato').html(''); }, 2000);
		}
		
		$('#retornoContato').html(msg);
		$('#formContato .btEnvia').removeAttr("disabled");
	}
	
}




