// JavaScript Document

	var entrada      = '';
	var noches       = '';
	var habitaciones = '';
	var fechahoy     = new Date();
	var fechaentrada = '';
	var diaentrada   = '';
	var mesentrada   = '';
	var anyoentrada  = '';

	window.onload = function DoLoad() {
						hoy		= AnadeCero(fechahoy.getDate())+'/'+AnadeCero(fechahoy.getMonth()+1)+'/'+fechahoy.getFullYear();
						manana	= "";
						
						document.getElementById("entrada").value = hoy;
						document.getElementById("salida").value = CalculaSalida(hoy, 1);
					}



	function CambiaFechas(pElemento)
		{		
		entrada	= document.getElementById("entrada").value;
		noches	= parseInt(document.getElementById("noches").value);
		salida	= document.getElementById("salida").value;

		if (pElemento == 'noches') { document.getElementById("salida").value = CalculaSalida(entrada, noches); }
		if (pElemento == 'entrada') { document.getElementById("salida").value = CalculaSalida(entrada, noches); }
		if (pElemento == 'salida') { document.getElementById("salida").value = CalculaEntrada(salida, noches); }
		}

	function Enviar() 
		{
		entrada      = document.getElementById("entrada").value;
		noches       = parseInt(document.getElementById("noches").value);
		habitaciones = document.getElementById("habs").value;

		if ( (entrada != "") && (noches > 0) && (habitaciones > 0) )
			{
			if (ComprobarValores())
				{
				document.getElementById("fechafin").value = CalculaSalida(entrada,noches);
				document.getElementById("form-quick").submit();
				}
			else
				{ alert('Valores de busqueda incorrectos.'); }
			}
		else
			{ alert('Debe indicar la fecha de entrada.'); }
		}
		
	
	function ComprobarValores() 
		{
		comprobacion = false;
		
		// Fecha entrada mayor o igual que hoy		
		if ( CompruebaEntrada() ) 
			{
			// Numero de noches entre 1 y 30
			if ( CompruebaNoches() ) 
				{
				// Numero de habitaciones entre 1 y 5	
				if ( CompruebaHabitaciones() ) 
					{ comprobacion = true; }
				}
			}
		
		return comprobacion;
		}
	
	
	function CompruebaEntrada() 
		{
		comprobacion = false;
		
		diaentrada   = entrada.split("/")[0];
		mesentrada   = entrada.split("/")[1];
		anyoentrada  = entrada.split("/")[2];
		
		fechaentrada = new Date(anyoentrada, mesentrada-1, diaentrada);
		
		if ( (fechaentrada = fechahoy) || (fechaentrada > fechahoy) )
			{ comprobacion = true; }
		else
			{ comprobacion = false; }
		
		return comprobacion;
		}
	
	function CompruebaNoches() 
		{ 
		comprobacion = false;
		
		if ( (noches>0) && (noches<=30) ) { comprobacion = true; }
		
		return comprobacion; 
		}
	
	function CompruebaHabitaciones() 
		{ 
		comprobacion = false;
		
		if ( (habitaciones>0) && (habitaciones<=5)  ) { comprobacion = true; }
		
		return comprobacion; 
		}

	function CalculaEntrada(psalida, pnoches)
		{
		pnoches = parseInt(pnoches);
		diasalida = psalida.split("/")[0];
		messalida = psalida.split("/")[1];
		anyosalida = psalida.split("/")[2];

		fechasalida = new Date(anyosalida, messalida-1, diasalida,0,0,0);

		entrada = new Date(fechasalida.getFullYear(), fechasalida.getMonth(), fechasalida.getDate()-pnoches,0,0,0);

		fechaentrada = AnadeCero(entrada.getDate())+'/'+AnadeCero(entrada.getMonth()+1)+'/'+entrada.getFullYear();

		return fechaentrada;
		}
	
	function CalculaSalida(pentrada, pnoches)
		{
		pnoches = parseInt(pnoches);
		diaentrada = pentrada.split("/")[0];
		mesentrada = pentrada.split("/")[1];
		anyoentrada = pentrada.split("/")[2];

			
		fechaentrada = new Date(anyoentrada, mesentrada-1, diaentrada,0,0,0);
		
		salida = new Date(fechaentrada.getFullYear(), fechaentrada.getMonth(), fechaentrada.getDate()+pnoches,0,0,0);
		
		fechasalida = AnadeCero(salida.getDate())+'/'+AnadeCero(salida.getMonth()+1)+'/'+salida.getFullYear();
		
			
		return fechasalida;
		}
	
	function AnadeCero(string)
		{
		string = String(string);
		if ( string.length < 2) { string='0'+string; }
		return string;
		}