/*!
* jQuery estudio crealia
*/

/* función para resetear el formulario */
jQuery.fn.resetform = function () {
	$(this).each (function() { this.reset(); });
}

/* mostrar ocultar y otras acciones en el formulario reservas */
jQuery(function($) {
	//$('#reservascont').hide();
	
	//miFecha = new Date($('#anno').value,$('#mes').value,$('#dia').value);
	milis = $('#cfecha').get(0).value;
	
	//$('#nombre').attr('value', milis);
	
	$('<div></div>')
		.attr({
			'id': 'reservascontload'
		})
			.hide()
			.insertAfter('#reservas');
	
	//$('#reservascontform').hide();
	//$('#reservascontform').css('opacity', 0);
	//$('#reservascont').css('opacity', 0);
	var echado = 0;
	//$('#reservascont').css('height', 500);
	
	$('.ereserva').click(function() {
		//$('#referenciacont').fadeIn('fast');
		if (echado == 0) {
			//$('#reservascont').animate({height: 'toggle'}, 300).animate({opacity: 1}, 'fast');
			//$('#reservascont').css('opacity', 1);
			echado = 1;
			//$('#reservascont').animate({top: 0}, 400);
			$('#fondo').animate({top: 355}, 400);
			$('#reservascont').animate({top: 0}, 400);
			//$('#reservascontform').animate({height: 'toggle'}, 300, function () {
																				  
				//}
			//);
			//$('#reservascont').animate({height: 'toggle'}, 300, function () {
					//$('#reservascontform').animate({opacity: 1}, 'fast');
					//$('#reservascontform').show();
				//}
			//).animate({opacity: 1}, 'fast');
		} else {
			//$('#reservascont').animate({opacity: 0}, 'fast').animate({height: 'toggle'}, 300);;
			echado = 0;
			$('#reservascont').animate({top: -355}, 400);
			$('#fondo').animate({top: 0}, 400);
			//$('#reservascont').animate({opacity: 0}, 'fast', function () {
					//$('#reservascontform').css('opacity', 0);
					//$('#reservascontform').hide();
				//}
			//).animate({height: 'toggle'}, 300);
		}

		return false;
	});
	
	// validar campo al perder el foco
	$('.formreser :input').blur(function () {
		
		if (this.id == 'nombre') {
			var elemento = $(this).parents('li:first');
			$(this).removeClass('aviso');
			$(this).prevAll('label').removeClass('aviso');
			$(this).next('span.comentario').remove();
			if (this.value == '') {
				$(this).addClass('aviso');
				$(this).prevAll('label').addClass('aviso');
			}
		}
		
		if (this.id == 'telefono') {
			$(this).removeClass('aviso');
			$(this).prevAll('label').removeClass('aviso');
			if ((!/^\d{9}$/.test(this.value) || this.value == '')) {
				$(this).addClass('aviso');
				$(this).prevAll('label').addClass('aviso');
			}
		}
		
		if (this.id == 'email') {
			var elemento = $(this).parents('li:first');
			$(this).removeClass('aviso');
			$(this).prevAll('label').removeClass('aviso');
			$(this).next('span.comentario').remove();
			//if ($('#pass').val() != '') {
				if ((!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.value) || this.value == '') && ($('#pass').val() != '' || this.value != '')) {
					$(this).addClass('aviso');
					$(this).prevAll('label').addClass('aviso');
				}
			//}
		}
									
	});
	
	// validar campo fecha al cambiar contenido
	$('.sfecha').change(function () {
		dia = $('#dia').get(0).value;
		mes = $('#mes').get(0).value - 1;
		anno = $('#anno').get(0).value;
		miFecha = new Date(anno, mes, dia);
		//miFecha.setTime(milis);
		//$('#nombre').attr('value', miFecha.getTime());
		diferencia = miFecha.getTime() - milis;
		//tipo = typeof diferencia
		//$('#telefono').attr('value', tipo);
		if (diferencia < 0) {
			//$('#telefono').attr('value', 'hola');
			$('#fechalab').addClass('aviso');
			//$('.sfecha').prevAll('label').addClass('aviso');
			$('.sfecha').addClass('aviso');
		} else {
			$('#fechalab').removeClass('aviso');
			$('.sfecha').removeClass('aviso');
		}
	});
	
	$('.formreser').submit(function () {
		$('.formreser :input').trigger('blur');
		var numAvisos = $('.aviso', this).length;
		$('#aviso-global').remove();
		if (numAvisos) {
			var listaCampos = [];
			$('label.aviso').each(function () {
				listaCampos.push($(this).attr('title'));
			});
			if (numAvisos == 1) {
				var mensajeAviso = 'El campo <strong>' + listaCampos + '</strong> no ha sido rellenado correctamente. Por favor, complételo. ';
			} else {
				var mensajeAviso = 'Los campos <strong>' + listaCampos.join('</strong>, <strong>') + '</strong> no han sido rellenados correctamente. Por favor, complételos. ';
			}
			$('<div></div>')
				.attr({
					'id': 'aviso-global',
					'class': 'aviexclreser'
				})
				.append(mensajeAviso)
				.hide()
				.insertAfter('#titreser')
				.fadeIn('fast');
			return false;
		} else {
			$('#reservas').hide();
			$('#reservascontload').show();
			
			datosenviar = $(this).serialize();
			$.ajax({
				url: "/reservar.php",
				data: datosenviar,//"mailSend=antonio@e-crealia.com",
				async:true,
				beforeSend: function(objeto){

				},
				complete: function(objeto, exito){

				},
				contentType: "application/x-www-form-urlencoded",
				dataType: "html",
				error: function(objeto, quepaso, otroobj){

				},
				success: function(datos){
					if (datos == 3) {
						var mensaje = 'Algunos datos no son correctos.';
					} else if (datos == 2) {
						var mensaje = 'Su reserva ha sido realizada. Hemos enviado un correo de confirmación con sus datos.';
					} else if (datos == 1) {
						var mensaje = 'Su reserva ha sido realizada. Hubo un error al enviar un correo de confirmación.';
					} else {
						var mensaje = 'Hubo un problema al registrar su reserva. Por favor, vuelva a intentarlo';
					}
					// mostrar mensaje.
					$('<div></div>')
					.attr({
						'id': 'aviso-global',
						'class': 'aviexclreser'
					})
						.append(mensaje)
						.hide()
						.insertAfter('#titreser')
						.fadeIn('fast');
					//$('#newsinfo').text(mensaje);
					// mostrar formulario
					//$('#mailnews').val('');
					//$buscadorLabel.fadeTo('fast', 1);
					//$("#reservas").resetform();
					$('#reservas').each (function(){
						this.reset();
					});
					$('#reservas').show();
					$('#reservascontload').hide();
					
				},
				type: "POST"
			});
			return false;
		}
	});
	
});
