// JavaScript Document

	var entrada      = '';
	var noches       = '';
	var habitaciones = '';
	var fechahoy     = new Date();
	var fechaentrada = '';
	var diaentrada   = '';
	var mesentrada   = '';
	var anyoentrada  = '';
	
	function Enviar() {

            entrada     = document.getElementById("entrada").value;

            noches      = parseInt(document.getElementById("noches").value);

            habitaciones= document.getElementById("habitaciones").value;

            adultos     = parseInt(document.getElementById("sel_adultos").value);

            ninos       = parseInt(document.getElementById("sel_ninos").value);

 

            // Sumamos los adultos y niņos y rellenamos el campo "adultos"

            document.getElementById("adultos").value = adultos+ninos;

 

            if (ComprobarValores()) {

                document.getElementById("fechafin").value = CalculaSalida(entrada,noches);          

                document.getElementById("form-quick").submit();

            }

            else { 

                alert('Valores de busqueda incorrectos'); 

            }

        } 

		
	
	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 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;
		}
		
		
		
		