// JavaScript Document
jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};
$(document).ready(function() {
	
	//Scroller
	var scrollProps = {
		showArrows:true,
		reinitialiseOnImageLoad:true,
		scrollbarWidth:12,
		arrowSize:12,
		animateTo:false
	}
	$('#content .body').jScrollPane(scrollProps);
	
	//Valla
	$('.gallery-nav').hover(
		function()
		{
			$(this).fadeTo(200,1);
		},
		function()
		{
			$(this).fadeTo(200,0.5);
		}
	).fadeTo(200,0.5);
	$('#header .gallery').cycle(
		{
			next:	'#next', 
   			prev:	'#prev'
		}
	);
	$('#play').click(
		function()
		{
			$('#header .gallery').cycle('toggle');
			$(this).toggleClass('stop');
			return false;
		}
	);
	
	//Send Reserva
	$("#phoneC,#movilC").ForceNumericOnly();
	$("#submitcontact").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;

		var emailFromVal = $("#emailFromC").val();
		if(emailFromVal == '') {
			$("#emailFromC").before('<span class="error">Ingrese su email.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			$("#emailFromC").before('<span class="error">Ingrese un email valido.</span>');
			hasError = true;
		}

		var nameVal = $("#nameC").val();
		if(nameVal == '') {
			$("#nameC").before('<span class="error">Ingrese su nombre.</span>');
			hasError = true;
		}
		
		var telVal = $("#phoneC").val();
		
		var movilVal = $("#movilC").val();
		if(movilVal == '' || movilVal.length < 10) {
			$("#movilC").before('<span class="error">Ingrese un n&uacute;mero telef&oacute;nico valido (m&iacute;nimo 10 caracteres).</span>');
			hasError = true;
		}
		
		var numpersonVal = $("#numpersonC").val();
		if(numpersonVal == '') {
			$("#numpersonC").before('<span class="error">Ingrese la cantidad de personas a asistir.</span>');
			hasError = true;
		}

		var fechaVal = $("#fechaC").val();
		if(fechaVal == '') {
			$("#fechaC").before('<span class="error">Ingrese la fecha de la reserva.</span>');
			hasError = true;
		}

		var horaVal = $("#horaC").val();
		if(horaVal == '') {
			$("#horaC").before('<span class="error">Ingrese la hora de la reserva.</span>');
			hasError = true;
		}

		var messageVal = $("#messageC").val();
		
		if(hasError)
		{
			$('#content .body').jScrollPane(scrollProps);
		}
		
		if(hasError == false) {
			$(this).hide();
			/**/
			$('#tellafriend_form').slideUp(400,function(){ $('#content .body').jScrollPane(scrollProps); });
			/**/
			$("#contactenos").append('<img src="images/loader.gif" alt="Enviando..." id="cargando" />');
			
			$.post("js/contactMail.php",
   				{ name: nameVal, emailFrom: emailFromVal, phone: telVal, movil: movilVal, numperson: numpersonVal, fecha: fechaVal, hora: horaVal, message: messageVal },
				function(data){
						if(data == 'true')
						{
							$("#contactenos").before('<div class="msgSent"><h3>Tu mensaje se envi&oacute; satisfactoriamente</h3><p>Gracias por contactarnos.</p></div>'); 
						}
						else
						{
							$("#contactenos").before('<div class="msgSent error"><h3>Hay un error enviando su mensaje</h3><p>Disculpe los incomvenientes, por favor intente de nuevo mas tarde.</p></div>');
						}
						//$("#contactenos").before('<div class="msgSent"><p>'+data+'</p></div>');
						$('#cargando').fadeOut('fast',function()
						{
							$('.msgSent').fadeIn();
						});
			});
		}
		
		return false;
	});
	
	//Send Contact
	$("#qf-tel").ForceNumericOnly();
	$("#qf-submit").click(function(){
		$(".error").hide();
		var hasError = false;
		var msgError = 'Se encontraron los siguientes errores:\n\n';
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;

		var nameVal = $("#qf-name").val();
		if(nameVal == '' || nameVal == 'Nombre y Apellido*') {
			msgError += '- Ingrese su nombre.\n';
			hasError = true;
		}

		var emailFromVal = $("#qf-email").val();
		if(emailFromVal == '' || emailFromVal == 'Correo Electronico*') {
			msgError += '- Ingrese su email.\n';
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			msgError += '- Ingrese un email valido.\n';
			hasError = true;
		}
		
		var telVal = $("#qf-tel").val();
		if(telVal == '' || telVal.length < 7 || telVal == 'Telefono') {
			msgError += '- Ingrese un telefono valido (minimo 7 caracteres).\n';
			hasError = true;
		}

		var messageVal = $("#qf-comment").val();
		if(messageVal == '' || messageVal == 'Comentarios') {
			msgError += '- Ingrese un mensaje.\n';
			hasError = true;
		}
		
		if(hasError)
		{
			alert(msgError);
		}
		
		if(hasError == false) {
			$(this).hide();
			/**/
			$('#quickForm').slideUp();
			/**/
			$(".contactFormShort").append('<img src="images/loader.gif" alt="Enviando..." id="cargando2" />');
			
			$.post("js/contactMail2.php",
   				{ name: nameVal, emailFrom: emailFromVal, phone: telVal, message: messageVal },
				function(data){
						if(data == 'true')
						{
							$(".contactFormShort").before('<div class="msgSent msgShort"><h3>Tu mensaje se envi&oacute; satisfactoriamente</h3><p>Gracias por contactarnos.</p></div>'); 
						}
						else
						{
							$(".contactFormShort").before('<div class="msgSent msgShort error"><h3>Hay un error enviando su mensaje</h3><p>Disculpe los incomvenientes, por favor intente de nuevo mas tarde.</p></div>');
						}
						//$("#contactenos").before('<div class="msgSent"><p>'+data+'</p></div>');
						$('#cargando2').fadeOut('fast',function()
						{
							$('.msgShort').fadeIn();
						});
			});
		}
		
		return false;
	});

});
