$(function() {
	   
	$("#dialog").dialog({
		autoOpen: false,
		resizable: false,
		width:450,
		height:220,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Cerrar: function() {
				$(this).dialog('close');
			}
		}

	});

	$("#datepicker").datepicker({
		beforeShowDay: function(date) { 
			var fechaActual=new Date();
			var anyoActual=fechaActual.getFullYear();
			var mesActual=parseInt(fechaActual.getMonth() + 1);
			var diaActual=parseInt(fechaActual.getDate());
			
			var fecha=new Date(date);
			var anyo=fecha.getFullYear();
			var mes=parseInt(fecha.getMonth() + 1);
			var dia=parseInt(fecha.getDate());
			
			
			if (anyo==anyoActual){
				if(mes>mesActual){
					return [true,'',''];
				}else{
					if(mes==mesActual){
						if(dia>=diaActual){
							return [true,'',''];
						}else{
							return [false,'',''];	
						}	
					}else{
						return [false,'',''];	
					}	
				}
			}else{
				if(anyo>anyoActual){
					return [true,'',''];
				}else{
					return [false,'',''];
				}
			}
		},
		showOn: 'button', 
		buttonImage: 'calend.png', 
		buttonImageOnly: true,
		firstDay:1,
		dateFormat:'dd/mm/yy',
		dayNames: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'],
		dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
		dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],
		monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
		monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic']
		
	});
	
	$("#datepicker2").datepicker({
		beforeShowDay: function(date) { 
			var fechaActual=new Date();
			var anyoActual=fechaActual.getFullYear();
			var mesActual=parseInt(fechaActual.getMonth() + 1);
			var diaActual=parseInt(fechaActual.getDate());
			
			var fecha=new Date(date);
			var anyo=fecha.getFullYear();
			var mes=parseInt(fecha.getMonth() + 1);
			var dia=parseInt(fecha.getDate());
			
			
			if (anyo==anyoActual){
				if(mes>mesActual){
					return [true,'',''];
				}else{
					if(mes==mesActual){
						if(dia>=diaActual){
							return [true,'',''];
						}else{
							return [false,'',''];	
						}	
					}else{
						return [false,'',''];	
					}	
				}
			}else{
				if(anyo>anyoActual){
					return [true,'',''];
				}else{
					return [false,'',''];
				}
			}
		},						 
		showOn: 'button', 
		buttonImage: 'calend.png', 
		buttonImageOnly: true,
		firstDay:1,
		dateFormat:'dd/mm/yy',
		dayNames: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'],
		dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
		dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],
		monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
		monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic']
	});
	
});

function reservar(){
	var texto="";
	var txtHabita="";
	if (document.getElementById("formularioReservas").fecha_llegada.value==""){
		texto+="* Debe indicar la fecha de llegada.<br>"	
	}else{
		var cadenaFecha=document.getElementById("formularioReservas").fecha_llegada.value;
		var vectorFecha=cadenaFecha.split("/");
		var tamVector=vectorFecha.length;
		if(tamVector<3 || tamVector >3){
			texto+="* Formato de fecha de llegada incorrecto ( dd/mm/aaaa ).<br>"	
		}else{
			var auxAnyo=vectorFecha[2];
			if (auxAnyo==""){
				auxAnyo=0;
			}else{
				auxAnyo=parseInt(auxAnyo,10);	
			}
			
			var auxMes=parseInt(vectorFecha[1],10);
				
			
						
			if (vectorFecha[0]=="08"){
				var auxDia=8;
			}else{
				if(vectorFecha[0]=="09"){
					var auxDia=9;
				}else{
					var auxDia=parseInt(vectorFecha[0],10);
				}	
			}	
			
			if(auxMes<1 || auxMes >12 || auxDia <1 || auxDia >31 || auxAnyo < 2010 ){
				//alert( auxMes + " - " + auxDia + " - " + auxAnyo)
				texto+="* Formato de fecha de llegada incorrecto ( dd/mm/aaaa ).<br>"
			}
		}
	}
	
	if (document.getElementById("formularioReservas").fecha_salida.value==""){
		texto+="* Debe indicar la fecha de salida.<br>"	
	}else{
		var cadenaFecha=document.getElementById("formularioReservas").fecha_salida.value;
		var vectorFecha=cadenaFecha.split("/");
		var tamVector=vectorFecha.length;
		if(tamVector<3 || tamVector >3){
			texto+="* Formato de fecha de salida incorrecto ( dd/mm/aaaa ).<br>"	
		}else{
			var auxAnyo=vectorFecha[2];
			if (auxAnyo==""){
				auxAnyo=0;
			}else{
				auxAnyo=parseInt(auxAnyo,10);	
			}
			var auxMes=parseInt(vectorFecha[1],10);
			if (vectorFecha[0]=="08"){
				var auxDia=8;
			}else{
				if(vectorFecha[0]=="09"){
					var auxDia=9;
				}else{
					var auxDia=parseInt(vectorFecha[0],10);
				}	
			}	
			if(auxMes<1 || auxMes >12 || auxDia <1 || auxDia >31 || auxAnyo < 2010 ){
				texto+="* Formato de fecha de salida incorrecto ( dd/mm/aaaa ).<br>"
			}
		}	
	}
	
	if(document.getElementById("formularioReservas").fecha_llegada.value!="" && document.getElementById("formularioReservas").fecha_salida.value!=""){
		if(document.getElementById("formularioReservas").fecha_llegada.value==document.getElementById("formularioReservas").fecha_salida.value){
			texto+="* La fecha de llegada y salida no pueden ser la misma<br>"
		}else{
			//if(document.getElementById("formularioReservas").fecha_llegada.value>document.getElementById("formularioReservas").fecha_salida.value){
			//	texto+="* La fecha de llegada es superior a la fecha de salida<br>"
			//}
		}	
	}	
	
	if (document.getElementById("formularioReservas").habitaciones.value==""){
		texto+="* Debe indicar el numero de habitaciones.<br>"	
	}
	
	if (document.getElementById("formularioReservas").adultos.value==""){
		texto+="* Debe indicar el numero de adultos.<br>"	
	}else{
		if (document.getElementById("formularioReservas").habitaciones.value!=""){
			//comprobamos que el numero de adultos y las habitaciones sean las correctas.
			var numAdultos=document.getElementById("formularioReservas").adultos.value;
			//var numNinos=document.getElementById("formularioReservas").ninos.value;
			//if (numNinos==""){
				numNinos=0;
			//}
			var numHabitaciones=document.getElementById("formularioReservas").habitaciones.value;
			var numTotalPersonas=eval(numAdultos) + eval(numNinos);
			if (numNinos>0){
				var cociente=parseInt(numTotalPersonas/numHabitaciones);
				var resto=numTotalPersonas%numHabitaciones;
				if(cociente==3){
					if (resto==1){
						//texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos y 1 niño."
						texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos.";
						txtHabita="Habitaciones insuficientes"
					}
				}else{
					if (cociente>3){
						//texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos y 1 niño.";
						texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos.";
						txtHabita="Habitaciones insuficientes"
					}
				}
			}else{
				var cociente=parseInt(numAdultos/numHabitaciones);
				var resto=numAdultos%numHabitaciones;
				if(cociente==2){
					if (resto==1){
						//texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos y 1 niño.";
						texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos";
						txtHabita="Habitaciones insuficientes"
					}
				}else{
					if (cociente>2){
						//texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos y 1 niño."
						texto+="* Debe seleccionar más habitaciones.<br>* Nuestras habitaciones puede albergar como máximo 2 adultos.";
						txtHabita="Habitaciones insuficientes"
					}
				}
			}
		}
	}
	
	
	if(texto!=""){
		//alert(texto);	
		
		if(txtHabita!=""){
			$('#dialog').dialog('option', 'title', 'Habitaciones insuficientes');
		}
		$('#dialog').dialog('open');
		document.getElementById("capaAlertaReserva").innerHTML=texto;
	}else{
		//alert(texto)
		document.getElementById('over').style.display='block';
		document.getElementById('fade').style.display='block';
		//document.getElementById("formularioReservas").submit();
		url = "comprobar-disponibilidad.asp?fecha_llegada=" + document.getElementById("formularioReservas").fecha_llegada.value + "&fecha_salida=" + document.getElementById("formularioReservas").fecha_salida.value + "&habitaciones=" + document.getElementById("formularioReservas").habitaciones.value;
		http.open("GET", url , true);
		http.onreadystatechange = handleHttpResponseComprobarReserva;
		http.send(null);
	}
}

function handleHttpResponseComprobarReserva() {
	if (http.readyState == 4) {    
		var respuesta=http.responseText;
		if(respuesta=="si"){
			document.getElementById("formularioReservas").submit();
		}else{
			//document.getElementById('over').style.display='block';
			//document.getElementById('fade').style.display='block';
			document.getElementById('over').innerHTML=respuesta;
		}	
	}    
}




function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)){
		return false;
	}else{
		return true;
	}
}

function validarReserva(){
	var texto=""
	//var numninos=document.getElementById("formulario").numninos.value;
	
	/*for(i=1;i<=numninos;i++){
		if(document.getElementById("nino_" + i).value==0){
			texto+="* Debe indicar la edad del niño " + i + ".<br>"	
		}	
	}*/
	
	if(document.getElementById("formulario").nombre.value==""){
		texto+="* Debe indicar su nombre.<br>"	
	}
	
	if(document.getElementById("formulario").apellidos.value==""){
		texto+="* Debe indicar sus apellidos.<br>"	
	}
	
	if(document.getElementById("formulario").telefono.value==""){
		texto+="* Debe indicar su telefono.<br>"	
	}
	
	if(document.getElementById("formulario").email.value==""){
		texto+="* Debe indicar su email.<br>"	
	}
	
	if(document.getElementById("formulario").condiciones.checked==false){
		texto+="* Debe aceptar las condiciones.<br>"	
	}
	
	if(texto!=""){
		//alert(texto)	
		$('#dialog').dialog('open');
		document.getElementById("capaAlertaReserva").innerHTML=texto;
	}else{
		document.getElementById("formulario").submit();	
	}
	
}

function validarOferta(){
	var texto=""
	
	if(document.getElementById("formulario").nombre.value==""){
		texto+="* Debe indicar su nombre.<br>"	
	}
	
	if(document.getElementById("formulario").apellidos.value==""){
		texto+="* Debe indicar sus apellidos.<br>"	
	}
	
	if(document.getElementById("formulario").telefono.value==""){
		texto+="* Debe indicar su telefono.<br>"	
	}
	
	if(document.getElementById("formulario").email.value==""){
		texto+="* Debe indicar su email.<br>"	
	}
	
	if(document.getElementById("formulario").condiciones.checked==false){
		texto+="* Debe aceptar las condiciones.<br>"	
	}
	
	if(document.getElementById("formulario").plazas.value==""){
		texto+="* Debe indicar el numero de plazas.<br>"
	}	
	
	
	if(texto!=""){
		//alert(texto)	
		$('#dialog').dialog('open');
		document.getElementById("capaAlertaReserva").innerHTML=texto;
	}else{
		url = "comprobar-plazas.asp?id=" + document.getElementById("formulario").id.value + "&plazas=" + document.getElementById("formulario").plazas.value;
		http.open("GET", url , true);
		http.onreadystatechange = handleHttpResponseComprobarPlazas;
		http.send(null);
	}
	
}

function handleHttpResponseComprobarPlazas() {
	if (http.readyState == 4) {    
		var respuesta=http.responseText;
		if(respuesta=="si"){
			document.getElementById("formulario").submit();
		}else{
			$('#dialog').dialog('open');
			document.getElementById("capaAlertaReserva").innerHTML="No quedan plazas suficientes";
		}	
	}    
}

function noReservas(texto){
	document.getElementById("capaAlertaReserva").innerHTML="dsfds";
	$('#dialog').dialog('open');	
}

function cargarImagenes(){
	document.getElementById("capaOfertas").style.display="block"
	$("#ofertas").jCarouselLite({
		visible:1,
		auto: 4000,
		speed: 3000

	});
	
}

function cerrarAviso(){
	var contenido=""
	
	contenido+="<div style='padding-top:14px;'>"
	contenido+="<img src='ftp/logo/logo.jpg'><br><br><br><br>"
	contenido+="Comprobando disponibilidad...<br><br>"
	contenido+="<img src='wait.gif'>"
	contenido+="</div>"

	document.getElementById('over').style.display='none';
	document.getElementById('fade').style.display='none';
	document.getElementById('over').innerHTML=contenido;
}
