﻿function callback_cd(r)
{
	var tE = document.getElementById('txtEntrada').value;
	var tS = document.getElementById('txtSalida').value;
	var tH = document.getElementById('txtHabitaciones').value;
	var tP = document.getElementById('txtPersonas').value;
	var tN = document.getElementById('txtNinos').value;
	//var tR = document.getElementById('selRegimen').value;
	var tA = document.getElementById('selTipoHab').value;
	
	if (tH == '') tH = 1;
	if (tP == '') tP = 1;
	if (tN == '') tN = 0;

	if (r.error != null)
	{
alert(r.error);r
		input_box = confirm('Lo sentimos pero no es posible determinar si actualmente tenemos plazas para las fechas dadas.\n¿Desea continuar y enviarnos sus datos para ponernos en contacto con usted?');
		if (input_box)
		{
			window.location = 'reservas/reservas.aspx?e=' + tE + '&s=' + tS + '&h=' + tH + '&p=' + tP + '&n=' + tN + '&a=' + tA;
		}
	}
	else
	{	
		var bR = r.value;

		if (bR < tH)
		{
			alert('Lo sentimos pero no hay disponibilidad de plazas en las fechas indicadas.\nPor favor seleccione otras fechas o pongase en contacto con nosotros en el Tel: +34 926 26 13 66');
		}
		else if (bR > 0) 
		{
			input_box = confirm('Hay disponibilidad de plazas para las fechas indicadas.\n¿Desea realizar la reserva?');
			if (input_box)
			{
				window.location = 'reservas/reservas.aspx?e=' + tE + '&s=' + tS + '&h=' + tH + '&p=' + tP + '&n=' + tN + '&t=' + tA;
			}
		}
		else if (bR == 0)
		{
			alert('Lo sentimos pero no hay disponibilidad de plazas en las fechas indicadas.\nPor favor seleccione otras fechas o pongase en contacto con nosotros en el Tel: +34 926 26 13 66');
		}
	}
}

function cd()
{
	var tE = document.getElementById('txtEntrada').value;
	var tS = document.getElementById('txtSalida').value;
	var tH = document.getElementById('txtHabitaciones').value;
	var tP = document.getElementById('txtPersonas').value;
	var tN = document.getElementById('txtNinos').value;
	var tA = document.getElementById('selTipoHab').value;
	
	if (tH == '') tH = 1;
	if (tP == '') tP = 1;
	if (tN == '') tN = 0;

	if (isNaN(tH) || isNaN(tP) || isNaN(tN) || tH <= 0 || tP <= 0)						
	{
		alert('Por favor introduzca el numero de habitaciones, personas y camas supletorias correcto.');
		return false;
	}
	if(parseInt(tN) > parseInt(tH))
	{
		alert('El numero de camas supletorias no puede ser mayor que el numero de habitaciones.');
		return false;
	}	
	if (calcDias(tE,tS) > 3)
	{
		alert('Si desea realizar una reserva de mas de 3 noches \npongase en contacto con nosotros en el telefono +34 926 26 13 66.');
		return false;
	}
	if (parseInt(tH) > 2 && (tA == "S" || tA == "B"))
	{
		alert('Lo sentimos pero solo disponemos de dos habitaciones del tipo seleccionado');
		return false;
	}
	if (parseInt(tH) > 4 && tA == "E")
	{
		alert('Lo sentimos pero solo disponemos de cuatro habitaciones Estándar');
		return false;
	}	
	
	comun.ACompDisp(tE, tS, tH, tP, tA, callback_cd);
}

// Devuelve los dias comprendidos entre 2 fechas
function calcDias(sEntrada, sSalida)
{
  sEntrada = sEntrada.split("/");
  sSalida = sSalida.split("/");
  
  var fEntrada = new Date(sEntrada[1]+"/"+sEntrada[0]+"/"+sEntrada[2]);
  var fSalida = new Date(sSalida[1]+"/"+sSalida[0]+"/"+sSalida[2]);
  var dias = Math.abs(Math.round((fEntrada-fSalida)/86400000));
  return dias;
}
