function comprueba_disponibilidad(idioma){
var xmlHttp;
var date_in = document.getElementById("date_in").value;
var date_out = document.getElementById("date_out").value;
try{
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
}catch (e){
  // Internet Explorer
  try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }catch (e){
    try{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }catch (e){
      alert("Your browser does not support AJAX!");
      return false;
     }
   }
 }
  xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
	  document.getElementById("habitaciones_disponibles").innerHTML = xmlHttp.responseText;
	  
	  switch(idioma){
		case "EN":
		
		document.getElementById("notificacion_habitaciones").innerHTML = "<div class='notificacion_habitaciones'>We have rooms available theese days. Choose how many rooms you want in your reservation</div>";
	  
	  document.getElementById("label_num_habitaciones").innerHTML = "<label for='num_hab'>Num. of rooms</label>";
		
		break;
		default:
		
		document.getElementById("notificacion_habitaciones").innerHTML = "<div class='notificacion_habitaciones'>Tenemos habitaciones disponibles en esas fechas. Elije a continuaci&oacute;n el n&uacute;mero de habitaciones que deseas reservar</div>";
	  
	  document.getElementById("label_num_habitaciones").innerHTML = "<label for='num_hab'>Numero de habitaciones</label>";
		
		break;
	  }	  
	  document.getElementById("datos_personales").style.display = "none";
    }
  }
  if(comprueba_fechas()){
	  xmlHttp.open("GET","disponibilidad.php?date_in="+date_in+"&date_out="+date_out+"&idioma=" + idioma,true);
	  xmlHttp.send(null);
  }
  else{
	   switch(idioma){
		case "EN":
	  alert("Selecciona unas fechas validas!");
	  break;
	  default:
	  alert("Wrong dates!");
	  break;
	   }
	  document.getElementById("habitaciones_disponibles").innerHTML = "";
	  document.getElementById("notificacion_habitaciones").innerHTML = "";
	  document.getElementById("label_num_habitaciones").innerHTML = "";
	  document.getElementById("configurar_habitaciones").innerHTML = "";
  }
}

function comprueba_fechas(){
	var date_in = document.getElementById("date_in").value;
	var date_out = document.getElementById("date_out").value;
	
	if(date_in == "" || date_out == "") return false;
	
	var array_fecha_in =date_in.split("/");
	var array_fecha_out =date_out.split("/");
	
	if(array_fecha_out[2] > array_fecha_in[2]){ return true; }
	else if (array_fecha_out[2] == array_fecha_in[2]){ 
			if(array_fecha_out[1] > array_fecha_in[1]){ return true; }
			else if (array_fecha_out[1] == array_fecha_in[1]){ 
				if(array_fecha_out[0] > array_fecha_in[0]){ return true; }
				else{ 
					return false; 
				} 
			}
			else return false;
		}
	else return false;
	
	
}

function calcular_precio(num_hab,idioma){
var xmlHttp;
var date_in = document.getElementById("date_in").value;
var date_out = document.getElementById("date_out").value;
if(num_hab >= 0){
	var tipo;
	var pension;
	var cadena_get = "num_hab=" + num_hab + "&date_in="+date_in+"&date_out="+date_out;
	var personas = 0;
	var tipo_nom = "";
	var total_personas = 0;
	
	for(var i = 1; i<= num_hab; i++){		
		tipo = document.getElementById("tipo" + i).value;
		pension = document.getElementById("pension" + i).value;
		adultos = document.getElementById("adultos" + i).value;
		ninos = document.getElementById("ninos" + i).value;
		bebes = document.getElementById("bebes" + i).value;
		cadena_get = cadena_get + "&tipo" + i + "=" + tipo + "&pension" + i + "=" + pension + "&adultos" + i + "=" + adultos + "&ninos" + i + "=" + ninos + "&bebes" + i + "=" + bebes;	
		
		
		switch(tipo){
			case "I":
				personas = 1;	
				switch(idioma){
				case "EN":
					tipo_nom = "single";
				break;
				default:
					tipo_nom = "individual";
				break;
				}
				break;
			case "D":
				personas = 2;
				switch(idioma){
				case "EN":
					tipo_nom = "double";
				break;
				default:
					tipo_nom = "doble";
				break;
				}
				break;
			case "T":
				personas = 3;
				case "EN":
					tipo_nom = "triple";
				break;
				default:
					tipo_nom = "triple";
				break;
				break;
		}
		total_personas = parseInt(adultos) + parseInt(ninos);
		if( total_personas > personas || total_personas < personas ){
			switch(idioma){
				case "EN":
					alert("Check the number of people on room " + i + ". You selected the room as " + tipo_nom + " and you selected " + total_personas + " people counting adults and children. Remember babies do not influence on this calculation.");
				break;
				default:
					alert("Revise el numero de personas en la habitacion " + i + ". Ha seleccionado la habitacion como " + tipo_nom + " y ha seleccionado " + total_personas + " personas entre adultos y ninos. Recuerde que los bebes no entran en este computo.");
				break;
			}
			
			return false;
		}

	}
	//alert(cadena_get);

	try{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}catch (e){
	  // Internet Explorer
	  try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }catch (e){
		try{
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
		  alert("Your browser does not support AJAX!");
		  return false;
		 }
	   }
	 }
	  xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			
			switch(idioma){
				case "EN":
					document.getElementById("precio").innerHTML = '<div style="border-bottom:1px solid #999;padding-bottom:5px;margin-bottom:5px;">Next you can see the see the price of your reservation in detail. Check it and proceed to fill up the next form with your personal data. If you want to make any modification on your reservation, do it now and re-compute the price by clicking on "<b>Compute Price</b>" button</div>' + xmlHttp.responseText;
				break;
				default:
					document.getElementById("precio").innerHTML = '<div style="border-bottom:1px solid #999;padding-bottom:5px;margin-bottom:5px;">Le presentamos a continuaci&oacute;n el detalle del precio de su reserva. Compru&eacute;belo y pase a completar el formulario de solicitud proporcion&aacute;ndonos sus datos personales. Si quiere realizar alguna modificaci&oacute;n en los detalles de su reserva, h&aacute;galo y recalcule el precio mediante el bot&oacute;n "<b>Calcular precio</b>"</div>' + xmlHttp.responseText;
				break;
				}
			
		  document.getElementById("datos_personales").style.display = "block";

		}
	  }
	  xmlHttp.open("GET","calcular_precio.php?" + cadena_get,true);
	  xmlHttp.send(null);
}
else {
	
	switch(idioma){
				case "EN":
					alert("You have to choose a number of rooms!");
				break;
				default:
					alert("Tienes que seleccionar un número de habitaciones!");
				break;
			}
}
}

function cambia_tipo_habitacion(numero){
	var tipo = document.getElementById("tipo" + numero).value;
	var cuantas = 0;
	if( tipo == "I") cuantas = 1;
	else if( tipo == "D") cuantas = 2;
	else if( tipo == "T") cuantas = 3;
	var option = "";
	
	var the_select_adultos = document.getElementById("adultos" + numero);
	var the_select_ninos = document.getElementById("ninos" + numero);
	
	//for (var j=0; j< the_select.options.length )
	document.getElementById("adultos" + numero).innerHTML = "";
	document.getElementById("ninos" + numero).innerHTML = "";
	
	//the_select_adultos.options = null;
	//the_select_ninos.options = null;
	
	for(var i = 0; i <= cuantas; i++){
		
			the_select_adultos.options[i] = new Option(i,i);
			the_select_ninos.options[i] = new Option(i,i);

			//option = option + "<option value='" + i.toString() + "'>" + i.toString() + "</option>";
	}
	//document.getElementById("adultos" + numero).innerHTML = option;
	//document.getElementById("ninos" + numero).innerHTML = option;
}

function muestra_habitaciones(cuantas_habitaciones,idioma){
	//var cuantas_habitaciones = document.getElementById("num_hab").value;
	var cadena = "";
	
	var text = "";
			switch(idioma){
				case "EN":
					text = "Compute Price";
				break;
				default:
					text = "Calcular Precio";
				break;
			}
	
	for(var i = 1; i <= cuantas_habitaciones; i++){
		switch(idioma){
				case "EN":
							cadena = cadena + "<table><tr><td colspan='2'><b style='color:#4D2612;'>Room " + i + "</b></td></tr><tr><td><label for='type" + i + "'>Kind of room</label></td><td><select id='tipo" + i + "' name='tipo" + i + "' onchange='cambia_tipo_habitacion(" + i + ");'><option value='I'>Single</option><option value='D'>Double</option><option value='T'>Triple</option></select></td></tr><tr><td><label for='pension" + i + "'>Accommodation</label></td><td><select id='pension" + i + "' name='pension" + i + "'><option value='HA'>Room only</option><option value='HD'>Bed & Breakfast</option><option value='HMP'>Half board</option><option value='HPC'>Full board</option></select></td></tr><tr><td><label for='adultos" + i + "'>Number of adults</label></td><td><select id='adultos" + i + "' name='adultos" + i + "'><option value='0'>0</option><option value='1'>1</option></select></td></tr><tr><td><label for='ninos" + i + "'>Number of children</label></td><td><select id='ninos" + i + "' name='ninos" + i + "'><option value='0'>0</option><option value='1'>1</option></select></td></tr><tr><td><label for='bebes" + i + "'>Number of babies</label></td><td><select id='bebes" + i + "' name='bebes" + i + "'><option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></td></tr></table>";	
				break;
				default:
							cadena = cadena + "<table><tr><td colspan='2'><b style='color:#4D2612;'>Habitacion " + i + "</b></td></tr><tr><td><label for='type" + i + "'>Tipo de habitacion</label></td><td><select id='tipo" + i + "' name='tipo" + i + "' onchange='cambia_tipo_habitacion(" + i + ");'><option value='I'>Individual</option><option value='D'>Doble</option><option value='T'>Triple</option></select></td></tr><tr><td><label for='pension" + i + "'>Tipo de alojamiento</label></td><td><select id='pension" + i + "' name='pension" + i + "'><option value='HA'>Solo alojamiento</option><option value='HD'>Alojamiento y desayuno</option><option value='HMP'>Media Pension</option><option value='HPC'>Pension Completa</option></select></td></tr><tr><td><label for='adultos" + i + "'>N&uacute;mero de adultos</label></td><td><select id='adultos" + i + "' name='adultos" + i + "'><option value='0'>0</option><option value='1'>1</option></select></td></tr><tr><td><label for='ninos" + i + "'>N&uacute;mero de ni&ntilde;os</label></td><td><select id='ninos" + i + "' name='ninos" + i + "'><option value='0'>0</option><option value='1'>1</option></select></td></tr><tr><td><label for='bebes" + i + "'>N&uacute;mero de beb&eacute;s</label></td><td><select id='bebes" + i + "' name='bebes" + i + "'><option value='0'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></td></tr></table>";	
				break;
			}

	}
	
	cadena = cadena + '<div align="right"><table cellpadding="0" cellspacing="0"><tr><td><img src="images/btnLeft.gif" border="0" width="4" height="18"></td><td background="images/btnFons.gif"><a href="#" onclick="calcular_precio(document.getElementById(\'num_hab\').options[document.getElementById(\'num_hab\').selectedIndex].value,\'' + idioma + '\');"><span style="padding-right: 2px; padding-left: 2px; color: rgb(255, 255, 255);">' + text + '</span></a></td><td><img src="images/btnRight.gif" border="0" width="4" height="18"></td></tr></table></div>';
	
	switch(idioma){
				case "EN":
					cadena = '<br/><div style="border-bottom:1px solid #999;padding-bottom:5px;margin-bottom:5px;">Specify next the details on every room. To compute the price of your booking, press button "<b>Compute Price</b>"</div>' + cadena;
				break;
				default:
					cadena = '<br/><div style="border-bottom:1px solid #999;padding-bottom:5px;margin-bottom:5px;">Especifique a continuaci&oacute;n los detalles de cada habitaci&oacute;n en su reserva. Para ver el desglose del precio de su reserva, presione el bot&oacute;n "<b>Calcular Precio</b>"</div>' + cadena;
				break;
			}
	
	document.getElementById("configurar_habitaciones").innerHTML = cadena;
	//document.getElementById("configurar_habitaciones").style.display = "block";
	
}




