function LZ(x) {return(x<0||x>9?"":"0")+x}


function addMoreApartments(){

	iForm = document.forms["idForm"];
	iNumRooms = iForm.elements["num_apartments"].value


	if (iNumRooms==1){
		document.idForm.num_rooms_1.value = 0;
		document.idForm.num_rooms_2.value = 0;

		document.getElementById("apartment_3").style.display = "none";
		document.getElementById("apartment_2").style.display = "none";
	}

	if (iNumRooms==2){
		document.idForm.num_rooms_1.value = 1;
		document.idForm.num_rooms_2.value = 0;
		document.getElementById("apartment_2").style.display = "block";
		document.getElementById("apartment_3").style.display = "none";
	}

	if (iNumRooms==3){
		document.idForm.num_rooms_1.value = 1;
		document.idForm.num_rooms_2.value = 1;

		document.getElementById("apartment_2").style.display = "block";
		document.getElementById("apartment_3").style.display = "block";
	}

}



function validateApartmentCombos(){

	iForm = document.forms["idForm"];
	iNumC1 = iForm.elements["num_children"].value

	if (iNumC1==0){

		document.getElementById("edades1_1").style.display = "none";
		document.getElementById("edades2_1").style.display = "none";
		document.getElementById("edades3_1").style.display = "none";
		document.getElementById("edades4_1").style.display = "none";
		document.getElementById("edades5_1").style.display = "none";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==1){

		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "none";
		document.getElementById("edades3_1").style.display = "none";
		document.getElementById("edades4_1").style.display = "none";
		document.getElementById("edades5_1").style.display = "none";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==2){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "none";
		document.getElementById("edades4_1").style.display = "none";
		document.getElementById("edades5_1").style.display = "none";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==3){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "none";
		document.getElementById("edades5_1").style.display = "none";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==4){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "block";
		document.getElementById("edades5_1").style.display = "none";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==5){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "block";
		document.getElementById("edades5_1").style.display = "block";
		document.getElementById("edades6_1").style.display = "none";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";
	}

	if (iNumC1==6){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "block";
		document.getElementById("edades5_1").style.display = "block";
		document.getElementById("edades6_1").style.display = "block";
		document.getElementById("edades7_1").style.display = "none";
		document.getElementById("edades8_1").style.display = "none";

	}

	if (iNumC1==7){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "block";
		document.getElementById("edades5_1").style.display = "block";
		document.getElementById("edades6_1").style.display = "block";
		document.getElementById("edades7_1").style.display = "block";
		document.getElementById("edades8_1").style.display = "none";

	}

	if (iNumC1==8){
		document.getElementById("edades1_1").style.display = "block";
		document.getElementById("edades2_1").style.display = "block";
		document.getElementById("edades3_1").style.display = "block";
		document.getElementById("edades4_1").style.display = "block";
		document.getElementById("edades5_1").style.display = "block";
		document.getElementById("edades6_1").style.display = "block";
		document.getElementById("edades7_1").style.display = "block";

		document.getElementById("edades8_1").style.display = "block";
	}




}


function validateApartmentCombos1(){

	iForm = document.forms["idForm"];
	iNumC1 = iForm.elements["num_children_1"].value

	if (iNumC1==0){

		document.getElementById("edades1_2").style.display = "none";
		document.getElementById("edades2_2").style.display = "none";
		document.getElementById("edades3_2").style.display = "none";
		document.getElementById("edades4_2").style.display = "none";
		document.getElementById("edades5_2").style.display = "none";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==1){

		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "none";
		document.getElementById("edades3_2").style.display = "none";
		document.getElementById("edades4_2").style.display = "none";
		document.getElementById("edades5_2").style.display = "none";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==2){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "none";
		document.getElementById("edades4_2").style.display = "none";
		document.getElementById("edades5_2").style.display = "none";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==3){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "none";
		document.getElementById("edades5_2").style.display = "none";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==4){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "block";
		document.getElementById("edades5_2").style.display = "none";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==5){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "block";
		document.getElementById("edades5_2").style.display = "block";
		document.getElementById("edades6_2").style.display = "none";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";
	}

	if (iNumC1==6){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "block";
		document.getElementById("edades5_2").style.display = "block";
		document.getElementById("edades6_2").style.display = "block";
		document.getElementById("edades7_2").style.display = "none";
		document.getElementById("edades8_2").style.display = "none";

	}

	if (iNumC1==7){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "block";
		document.getElementById("edades5_2").style.display = "block";
		document.getElementById("edades6_2").style.display = "block";
		document.getElementById("edades7_2").style.display = "block";
		document.getElementById("edades8_2").style.display = "none";

	}

	if (iNumC1==8){
		document.getElementById("edades1_2").style.display = "block";
		document.getElementById("edades2_2").style.display = "block";
		document.getElementById("edades3_2").style.display = "block";
		document.getElementById("edades4_2").style.display = "block";
		document.getElementById("edades5_2").style.display = "block";
		document.getElementById("edades6_2").style.display = "block";
		document.getElementById("edades7_2").style.display = "block";
		document.getElementById("edades8_2").style.display = "block";
	}




}//end validateapartment combos 1

function validateApartmentCombos2(){

	iForm = document.forms["idForm"];
	iNumC1 = iForm.elements["num_children_2"].value

	if (iNumC1==0){

		document.getElementById("edades1_3").style.display = "none";
		document.getElementById("edades2_3").style.display = "none";
		document.getElementById("edades3_3").style.display = "none";
		document.getElementById("edades4_3").style.display = "none";
		document.getElementById("edades5_3").style.display = "none";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==1){

		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "none";
		document.getElementById("edades3_3").style.display = "none";
		document.getElementById("edades4_3").style.display = "none";
		document.getElementById("edades5_3").style.display = "none";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==2){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "none";
		document.getElementById("edades4_3").style.display = "none";
		document.getElementById("edades5_3").style.display = "none";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==3){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "none";
		document.getElementById("edades5_3").style.display = "none";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==4){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "block";
		document.getElementById("edades5_3").style.display = "none";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==5){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "block";
		document.getElementById("edades5_3").style.display = "block";
		document.getElementById("edades6_3").style.display = "none";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";
	}

	if (iNumC1==6){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "block";
		document.getElementById("edades5_3").style.display = "block";
		document.getElementById("edades6_3").style.display = "block";
		document.getElementById("edades7_3").style.display = "none";
		document.getElementById("edades8_3").style.display = "none";

	}

	if (iNumC1==7){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "block";
		document.getElementById("edades5_3").style.display = "block";
		document.getElementById("edades6_3").style.display = "block";
		document.getElementById("edades7_3").style.display = "block";
		document.getElementById("edades8_3").style.display = "none";

	}

	if (iNumC1==8){
		document.getElementById("edades1_3").style.display = "block";
		document.getElementById("edades2_3").style.display = "block";
		document.getElementById("edades3_3").style.display = "block";
		document.getElementById("edades4_3").style.display = "block";
		document.getElementById("edades5_3").style.display = "block";
		document.getElementById("edades6_3").style.display = "block";
		document.getElementById("edades7_3").style.display = "block";
		document.getElementById("edades8_3").style.display = "block";
	}




}// end validatecombos2



function setDate(){

	var today_date = getTodayDate();
	var checkin_date = getCheckinDate();

	// Only reset the dates if the checkin date is before today
	// This means the default value given by php is not correct (because of the cache)
	if (calculateDifference(checkin_date, today_date)<0) {
		// Reset the checkin date to today
		var today = LZ((today_date.getDate()+1))+"-"+LZ((today_date.getMonth()+1))+"-"+today_date.getYear();
		document.idForm.checkin_date.value = today;

		if (typeof(document.idForm.checkout_date) != "undefined" ){
			// Reset the checkout date to tomorrow
			var tomorrow_date = new Date();
			tomorrow_date.setTime(today_date.getTime() + 86400000);
			var tomorrow = LZ((tomorrow_date.getDate()+1))+"-"+LZ((tomorrow_date.getMonth()+1))+"-"+tomorrow_date.getYear();
			document.idForm.checkout_date.value = tomorrow;
		}
	}

}



function resetDate(){

	var today_date = getTodayDate();
	var checkin_date = getCheckinDate();

	// Only reset the dates if the checkin date is before today
	// This means the default value given by php is not correct (because of the cache)
	if (calculateDifference(checkin_date, today_date)<0) {
		// Reset the checkin date to today
		var day = today_date.getDate();
		var month = today_date.getMonth();
		var year = today_date.getYear();
		document.idForm.checkin_day.selectedIndex = (day - 1);
		document.idForm.checkin_month.selectedIndex = month;
		var obj = document.idForm.checkin_year;
		for(var i = 0; i < obj.options.length; i++){
			if(obj.options[i].value == year){
				obj.selectedIndex = i;
				break;
			}
		}

		// Reset the checkout date to tomorrow
		if (typeof(document.idForm.checkout_day) != "undefined" ){
			var tomorrow_date = new Date();
			tomorrow_date.setTime(today_date.getTime() + 86400000);
			var t_day = tomorrow_date.getDate();
			var t_month = tomorrow_date.getMonth();
			var t_year = tomorrow_date.getYear();
			document.idForm.checkout_day.selectedIndex = (t_day - 1);
			document.idForm.checkout_month.selectedIndex = t_month;
			var obj = document.idForm.checkout_year;
			for(var i = 0; i < obj.options.length; i++){
				if(obj.options[i].value == t_year){
					obj.selectedIndex = i;
					break;
				}
			}
		}
	}

}

// does all the validations



function validateAll(){
	var Error = new Array(3)
	Error[0] = 0;
	Error = validateDate(); //Valida la Fecha.
	if (Error[0]==0){ //Valida las Habitaciones.
		Error = validateRooms();
	}
	if (Error[0]==0){
		Error = validateTown(); //Valida Pais y localidad.
	}
	if (Error[0]==0){
		//Valida Si en caso de que remite = interno ha introducido el id_gestor.
		Error = validateGestor();
	}
	return Error;
}


function validateSmall(){

	var submitcount = 0;
	var Error = new Array(3)
	Error[0] = 0;
	Error = validateDate(); //Valida la Fecha.

	if (Error[0]==0){ //Valida las Habitaciones.
		if (typeof(document.idForm.apartments) == "undefined" )
		{
			if (typeof(document.idForm.form_type) != "undefined")
			{
				if (document.idForm.form_type.value == "multi")
				{
					Error = validateRoomsSmall(); // original
				} else {
					Error = validateAjaxRoom();
				}
			} else {
				Error = validateAjaxRoom();
			}
		}
	}

	if (Error[0]==0){
		Error = validateTown(); //Valida Pais y localidad.
	}


	if (Error[0]==0){ //Se envia el Formulario.
		if (submitcount == 0){
			document.body.style.cursor = "wait";
			submitcount++;
			document.idForm.submit();
		}
	}

	return Error;
}

/**Valida Si la fecha entrada y salida son correctas**/
function validateDate(){

	Error= new Array(3);
	Error[0] = 0;
	var current_date = getTodayDate();
	var earlierdate = getCheckinDate();
	var laterdate = getCheckoutDate();


	if (earlierdate==0){
		// indicates input date is invalid
		Error[0] = 1222;
	}else{

		if (laterdate==0){
			// indicates input date is invalid
			Error[0] = 1525;
		}
		if (document.idForm.num_nights.value<1){
			Error[0] = 427;
		}

		if (Error[0]==0){
			daysDifference = calculateDifference(earlierdate, current_date);
			if (daysDifference<0){	// Minimo fecha Actual.
				Error[0] = 421;
			}

			if (daysDifference>334){	// Minimo fecha Actual.
				Error[0] = 1205;
			}
		}

		datesDifference = calculateDifference(laterdate, earlierdate);
		if (datesDifference < 0)
		Error[0] = 428;

	}// endif invalid input date
	return Error;
}

/**Funciï¿½n que valida las habitaciones**/
function validateRoomsSmall(){

	formulario = document.idForm;
	Error= new Array(3);
	Error[0] = 0;

	// Se comprueba que como minimo haya seleccionado 1 habitacion.
	if (formulario.num_rooms.value<1){
		Error[0] = 48;

	}

	if (Error[0]==0){
		rooms= new Array();
		//Se mete en un array

		rooms["num_rooms"] = eval('formulario.num_rooms.value');
		rooms["room_code"] = eval('formulario.habitacion_1.value');
		if (typeof(eval('formulario.age_child1')) != "undefined")
		{
			rooms["age_child1"] = eval('formulario.age_child1.value');
			rooms["age_child2"] = eval('formulario.age_child2.value');
			has_tag_age_children = 1;
		}else{
			rooms["age_child1"] = 0;
			rooms["age_child2"] = 0;
			has_tag_age_children = 0;
		}

		//validate if has not chosen room type with children but has put in childrens ages
		/*
		if ((rooms["room_code"]==1 ||rooms["room_code"]==2 ||rooms["room_code"]==6)
		&& (rooms["age_child1"] != 0 || rooms["age_child2"] != 0)){
		tipo = "";
		Error[0] = 435;
		Error[1] = tipo;

		}
		*/
		//validate if has chosen room type with one child but has put in two childrens ages

		if ((rooms["room_code"]==3 || rooms["room_code"]==6) && (rooms["age_child2"] != 0))
		{
			tipo = "";
			Error[0] = 439;
			Error[1] = tipo;

		}
	}
	if (Error[0]==0){
		// validate if has chosen a room type with one child, but put the age in the column of the second child
		if ((rooms["room_code"]==3 ||rooms["room_code"]==4 ||rooms["room_code"]==7 )&& (rooms["age_child1"]==0 && rooms["age_child2"] != 0 && has_tag_age_children==1)){
			tipo = "";
			Error[0] = 436;
			Error[1] = tipo;
		}
	}

	if (Error[0]==0){

		// validate if has chosen a room type with one child but not put in any childrens age
		if ((rooms["room_code"]==3 ||rooms["room_code"]==7 )&&(rooms["age_child1"]<1 &&  has_tag_age_children==1)){
			tipo = "";
			Error[0] = 1152;
			Error[1] = tipo;

		}
		// validate if has chosen a room type with two children but not put in any childrens age in one or the other
		if ((rooms["room_code"]==5) &&(has_tag_age_children==1 && (rooms["age_child1"]<1 || rooms["age_child2"]<1))){
			Error[0] = 1152;
		}

	}

	return Error;
}





function getCheckinDate(){

	if (typeof(document.idForm.checkin_date) != "undefined" ) {
		year = document.idForm.checkin_date.value.substring(6,10);
		month 	= document.idForm.checkin_date.value.substring(3,5);
		day 	= document.idForm.checkin_date.value.substring(0,2);
		checkin_date = new Date(year,month-1,day);
	}else{
		dia_inicio = document.idForm.checkin_day.value;
		if (typeof(document.idForm.checkin_month) != "undefined" ) {
			mes_inicio = document.idForm.checkin_month.value;
			anno_inicio = document.idForm.checkin_year.value;
		}else{
			mes_inicio = document.idForm.check_in_month_year.value.substring(0,2);
			anno_inicio = document.idForm.check_in_month_year.value.substring(3,7);

		}
		//El mes de la clase empieza desde 0, por eso tengo q restarle 1.
		checkin_date = new Date(anno_inicio, mes_inicio-1, dia_inicio);
		if (checkin_date.getDate() != dia_inicio)
		{
			//indicate this is an invalid date by setting to zero
			checkin_date = 0;
		}
	}

	return checkin_date;
}

function getCheckoutDate(){
	if (typeof(document.idForm.checkout_date) != "undefined" ) {
		year = document.idForm.checkout_date.value.substring(6,10);
		month 	= document.idForm.checkout_date.value.substring(3,5);
		day 	= document.idForm.checkout_date.value.substring(0,2);
		checkout_date = new Date(year,month-1,day);
	}else{
		dia_fin = document.idForm.checkout_day.value;
		if (typeof(document.idForm.checkout_month) != "undefined" ) {
			mes_fin = document.idForm.checkout_month.value;
			anno_fin = document.idForm.checkout_year.value;
		}else{
			mes_fin = document.idForm.check_out_month_year.value.substring(0,2);
			anno_fin = document.idForm.check_out_month_year.value.substring(3,7);

		}
		//El mes de la clase empieza desde 0, por eso tengo q restarle 1.
		checkout_date = new Date(anno_fin, mes_fin-1, dia_fin);
	}

	if (checkout_date.getDate() != dia_fin){
		return 0;
	}else{
		return checkout_date;
	}

}

function getTodayDate(){
	current_date = new Date(); //Fecha Actual.
	// Remove time of the day
	current_date.setHours(0);
	current_date.setMinutes(0);
	current_date.setSeconds(0);
	return current_date;
}


function validateTown(){
	var Error = new Array(3) ;
	Error[0] = 0;

	if ( typeof(document.idForm.coast_code) != "undefined" ) {
		if (document.idForm.coast_code.value == "") {
			Error[0] = 582;
		}
	}

	// now check for ski areas when ski areas chosen
	if (typeof(document.idForm.coast_code) == "undefined" ) {
		if (document.idForm.country_code.value=="-"){
			if (document.idForm.town.value == "" && document.idForm.location_code.value == "") {
				Error[0] = 582;
			}
		}
	}

	if (typeof(document.idForm.zone_code) != "undefined" ) {

		if (typeof(document.idForm.location_code) != "undefined" ) {
			if (document.idForm.location_code.value == ""){
				Error[0] = 443;
			}
		}
	}

	if (typeof(document.idForm.apartments) != "undefined" ) {
		if (document.idForm.location_code.value == ""){
			Error[0] = 443;
		}
	}

	var iForm, iTown;
	if((iForm = document.forms["idForm"]) && (iTown = iForm.elements["town"])) {

		if (typeof(document.idForm.location_code) != "undefined" ) {
			if(iTown.value == "" && document.idForm.location_code == ""){
				Error[0] = 426;
			}
		}else{
			// its undefined, only look for town
			if(iTown.value == "" ){
				Error[0] = 426;
			}

		}
	}


	if (document.idForm.zone_code.value >0){
		if (document.idForm.location_code.value == ""){
			Error[0] = 443;
		}
	}
	return Error;
}


/**Funciï¿½n que valida las habitaciones**/
function validateRooms(){
	var Error = new Array(3) ;
	Error[0] = 0;
	formulario = document.idForm;
	rooms= new Array();
	//Se mete en un array
	for (x = 0;x<3;x++){
		cadena = "";
		if (x>0){
			cadena = "_"+x;
		}
		type_rooms = new Array();
		if (eval('formulario.num_rooms'+cadena)){
			type_rooms["num_rooms"] = eval('formulario.num_rooms'+cadena+'.value');
			type_rooms["room_code"] = eval('formulario.room_code'+cadena+'.value');
			type_rooms["age_child1"] = eval('formulario.age_child1'+cadena+'.value');
			type_rooms["age_child2"] = eval('formulario.age_child2'+cadena+'.value');
		}
		rooms[x] = type_rooms;
	}

	//1ï¿½ Comprobamos que las habitaciones sean contiguas.

	encontrado = true;
	for (x = 0;x<rooms.length;x++){
		if (rooms[x]["num_rooms"] != 0 || rooms[x]["age_child1"] != 0 || rooms[x]["age_child2"] != 0){
			if (encontrado==false){
				tipo = x+1;
				Error[0] = 429;
				Error[1] = tipo;

			}
			encontrado = true;
		}else{
			encontrado = false;
		}

	}

	//2ï¿½ Se comprueba que como minimo haya seleccionado 1 habitacion.
	if (Error[0]==0){
		if (rooms[0]["num_rooms"]==0){
			Error[0] = 430;

		}
	}

	//3ï¿½ Comprobamos que los tipos de habitaciones sean diferentes.
	if (Error[0]==0){
		for (x = 0;x<rooms.length-1;x++){

			if (rooms[x]["num_rooms"] != 0)
			{
				for (y = x+1;y<rooms.length;y++){
					if (rooms[y]["num_rooms"]>0 && rooms[x]["room_code"]==rooms[y]["room_code"]  && rooms[x]["age_child1"]==rooms[y]["age_child1"] && rooms[x]["age_child2"]==rooms[y]["age_child2"])
					{
						tipo1 = x+1;
						tipo2 = y+1;
						Error[0] = 431;
						Error[1] = tipo1;
						Error[2] = tipo2;
					}
				}
			}
		}
	}
	//4ï¿½ Comprobamos que los datos estan bien metidos.
	if (Error[0]==0){
		for (x = 0;x<rooms.length;x++){
			//validate if has not chosen room type but has put number of rooms
			if (rooms[x]["num_rooms"] != 0 && rooms[x]["room_code"]==0 ){
				tipo = x+1;
				Error[0] = 438;
				Error[1] = tipo;

			}
			// ESTO NO SE NECESITA EN EL NUEVO BUSCADOR DE AJAX
			//validate if has not chosen room type with children but has put in childrens ages
			/*
			if ((rooms[x]["room_code"]==1 ||rooms[x]["room_code"]==2 ||rooms[x]["room_code"]==6)
			&& (rooms[x]["age_child1"] != 0 || rooms[x]["age_child2"] != 0)){
			tipo = x+1;
			Error[0] = 435;
			Error[1] = tipo;
			datosOk = false;
			}

			// validate if has chosen a room type with one child, but put the age in the column of the second child
			if ((rooms[x]["room_code"]==3 ||rooms[x]["room_code"]==4 ||rooms[x]["room_code"]==7 )&& rooms[x]["age_child1"]==0 && rooms[x]["age_child2"] != 0){
			tipo = x+1;

			Error[0] = 436;
			Error[1] = tipo;
			}
			*/
			// validate if has chosen a room type with one child but not put in any childrens age
			if ((rooms[x]["room_code"]==3 ||rooms[x]["room_code"]==7 ) && (rooms[x]["age_child1"]<1)){
				tipo = x+1;
				Error[0] = 1152;
				Error[1] = tipo;
			}

			// validate if has chosen a room type with two children but not put in any childrens age in one or the other
			if ((rooms[x]["room_code"]==5) && (rooms[x]["age_child1"]<1 || rooms[x]["age_child2"]<1)){
				tipo = x+1;
				Error[0] = 1152;
				Error[1] = tipo;
			}
		}
	}

	return Error;
}

/**Valida en caso de que la reserva la vaya a realizar un gestor si la casilla del gestor ha introiducido el id del gestor**/
function validateGestor(){
	var Error = new Array(3) ;
	Error[0] = 0;
	if (document.idForm.gestor_id != null){
		gestor_id = document.idForm.gestor_id.value;
		if (gestor_id==""){
			Error[0] = 1;
			datosOk = false;
		}
	}


	return Error;
}

/**Calcula los dias de diferencia entre 2 fechas.
Laterdate==>fecha inicio.
earlierdate==>fecha fin.
**/
function calculateDifference(laterdate, earlierdate) {
	var difference = laterdate.getTime() - earlierdate.getTime();
	var milliseconds_in_day = 1000*60*60*24;
	var daysDifference = Math.round(difference/milliseconds_in_day);
	return daysDifference;
}

// rewrote changeNumNights - sylwia 8/may/2007
function changeNumNights(){
	var iForm, oMonth, oDay, oYear, iMonth, iDay, iYear, iNights;
	if ((iForm = document.forms["idForm"]) && (oSelect1 = iForm.elements["check_out_month_year"]) && (oSelect2 = iForm.elements["checkout_day"]) && (iSelect1 = iForm.elements["check_in_month_year"]) && (iSelect2 = iForm.elements["checkin_day"]) && (iNights = iForm.elements["num_nights"])) {
		oMonth = oSelect1.value.substr(0,2);
		oDay = oSelect2.value;
		oYear = oSelect1.value.substr(3,6);
		checkout_date = new Date(oMonth+"/"+oDay+"/"+oYear);
		iMonth = iSelect1.value.substr(0,2);
		iDay = iSelect2.value;
		iYear = iSelect1.value.substr(3,6);
		checkin_date = new Date(iMonth+"/"+iDay+"/"+iYear);
		num_nights = calculateDifference(checkout_date,checkin_date);
		iNights.value = num_nights;
	}
}


function changeCheckOutDate(){

	num_nights = document.idForm.num_nights.value;
	checkout_date = new Date(document.idForm.check_in_month_year.value.substr(0,2)+"/"+document.idForm.checkin_day.value+"/"+document.idForm.check_in_month_year.value.substr(3,6)+" 01:00");
	checkout_date.setTime(checkout_date.getTime()+num_nights*1000*60*60*24)
	checkoutday = checkout_date.getDate();
	checkout = checkoutday.toString();
	lenday = checkout.length;
	if (lenday==1)
	{
		checkout = "0"+checkout;
	}
	document.idForm.checkout_day.value = checkout;
	checkoutmonth= checkout_date.getMonth();
	checkoutmonth++;
	checkmonth = checkoutmonth.toString();
	if (checkmonth.length==1)
	{
		checkmonth = "0"+checkmonth;
	}

	checkoutyear =checkout_date.getFullYear();
	document.idForm.check_out_month_year.value = checkmonth + '/' + checkoutyear;


}


function getDaysInMonth(aDate){
	// returns the last day of a given month
	var m = new Number(aDate.getMonth());
	var y = new Number(aDate.getYear());

	var tmpDate = new Date(y, m, 28);
	var checkMonth = tmpDate.getMonth();
	var lastDay = 27;

	while(lastDay <= 31){
		temp = tmpDate.setDate(lastDay + 1);
		if(checkMonth != tmpDate.getMonth())
		break;
		lastDay++
	}
	return lastDay;
}

function upToDate (){

	var d = new Date();
	var booking_release = 1;
	var day     = d.getDate();
	var month   = d.getMonth();
	var year    = d.getFullYear();
	d.setTime(d.getTime()+1*1000*60*60*24)
	checkinday = d.getDate();
	checkin = checkinday.toString();
	lenday = checkin.length;
	if (lenday==1)
	{
		checkin = "0"+checkin;
	}
	document.idForm.checkin_day.value = checkin;
	checkinmonth= d.getMonth();
	checkinmonth++;
	checkmonth = checkinmonth.toString();
	if (checkmonth.length==1)	{
		checkmonth = "0"+checkmonth;
	}

	checkinyear =d.getFullYear();
	document.idForm.check_in_month_year.value = checkmonth + '/' + checkinyear;

	if (typeof(document.idForm.checkout_day) != "undefined" ){
		checkout_date = new Date();
		checkout_date.setTime(checkout_date.getTime()+2*1000*60*60*24)
		checkoutday = checkout_date.getDate();
		checkout = checkoutday.toString();
		lenday = checkout.length;
		if (lenday==1)
		{
			checkout = "0"+checkout;
		}
		document.idForm.checkout_day.value = checkout;
		checkoutmonth= checkout_date.getMonth();
		checkoutmonth++;
		checkmonth = checkoutmonth.toString();
		if (checkmonth.length==1)	{
			checkmonth = "0"+checkmonth;
		}

		checkoutyear =checkout_date.getFullYear();
		document.idForm.check_out_month_year.value = checkmonth + '/' + checkoutyear;


	}
}


function upToDateApartments (){
	var d = new Date();
	var booking_release = 1;
	var day     = d.getDate();
	var month   = d.getMonth();
	var year    = d.getFullYear();
	document.idForm.num_nights.value = "7";
	d.setTime(d.getTime()+1*1000*60*60*24)
	checkinday = d.getDate();
	checkin = checkinday.toString();
	lenday = checkin.length;
	if (lenday==1)
	{
		checkin = "0"+checkin;
	}
	document.idForm.checkin_day.value = checkin;
	checkinmonth= d.getMonth();
	checkinmonth++;
	checkmonth = checkinmonth.toString();
	if (checkmonth.length==1)	{
		checkmonth = "0"+checkmonth;
	}

	checkinyear =d.getFullYear();
	document.idForm.check_in_month_year.value = checkmonth + '/' + checkinyear;

	if (typeof(document.idForm.checkout_day) != "undefined" ){
		checkout_date = new Date();
		checkout_date.setTime(checkout_date.getTime()+7*1000*60*60*24)
		checkoutday = checkout_date.getDate();
		checkout = checkoutday.toString();
		lenday = checkout.length;
		if (lenday==1)
		{
			checkout = "0"+checkout;
		}
		document.idForm.checkout_day.value = checkout;
		checkoutmonth= checkout_date.getMonth();
		checkoutmonth++;
		checkmonth = checkoutmonth.toString();
		if (checkmonth.length==1)	{
			checkmonth = "0"+checkmonth;
		}

		checkoutyear =checkout_date.getFullYear();
		document.idForm.check_out_month_year.value = checkmonth + '/' + checkoutyear;


	}

}


// ************************************************************************************
// (rewritten 28/may/07) funciones de validacion - sylwia
// ************************************************************************************

function checkErrorRoomCode()
{
	var Error = new Array();
	Error[0] = 0;
	edadMinima = 2;

	if (Error[0]==0) {
		iForm = document.forms["idForm"];
		iHab1 = iForm.elements["habitacion_1"]; // room 1
		iHab2 = iForm.elements["habitacion_2"]; //room 2
		iHab3 = iForm.elements["habitacion_3"]; //room 3
		iRooms = iForm.elements["valor"]; // how many rooms

		if(iRooms.value == 1) {
			if(iHab1.value == 3 || iHab1.value == 4 || iHab1.value == 7) {
				iAge1 = iForm.elements["edad_1_1"];
				if(iAge1.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab1.value == 5) {
				iAge1 = iForm.elements["edad_1_1"];
				iAge2 = iForm.elements["edad_2_1"];
				if(iAge1.value < edadMinima || iAge2.value < edadMinima) {
					Error[0] = 1152;
				}
			}
		}


		if(iRooms.value == 2) {
			if(iHab1.value == 3 || iHab1.value == 4 || iHab1.value == 7) {
				iAge1 = iForm.elements["edad_1_1"];
				if(iAge1.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab1.value == 5) {
				iAge1 = iForm.elements["edad_1_1"];
				iAge2 = iForm.elements["edad_2_1"];
				if(iAge1.value < edadMinima || iAge2.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab2.value == 3 || iHab2.value == 4 || iHab2.value == 7) {
				iAge3 = iForm.elements["edad_1_2"];
				if(iAge3.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab2.value == 5) {
				iAge3 = iForm.elements["edad_1_2"];
				iAge4 = iForm.elements["edad_2_2"];
				if(iAge3.value < edadMinima || iAge4.value < edadMinima) {
					Error[0] = 1152;
				}
			}
		}

		if(iRooms.value == 3) {

			if(iHab1.value == 3 || iHab1.value == 4 || iHab1.value == 7) {
				iAge1 = iForm.elements["edad_1_1"];
				if(iAge1.value < edadMinima) {
					Error[0] = 1152;
				}
			}



			if(iHab1.value == 5) {
				iAge1 = iForm.elements["edad_1_1"];
				iAge2 = iForm.elements["edad_2_1"];
				if(iAge1.value < edadMinima || iAge2.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab2.value == 3 || iHab2.value == 4 || iHab2.value == 5 || iHab2.value == 7) {
				iAge3 = iForm.elements["edad_1_2"];
				if(iAge3.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab2.value == 5) {
				iAge3 = iForm.elements["edad_1_2"];
				iAge4 = iForm.elements["edad_2_2"];
				if(iAge3.value < edadMinima || iAge4.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab3.value == 3 || iHab3.value == 4 || iHab3.value == 7) {
				iAge5 = iForm.elements["edad_1_3"];
				if(iAge5.value < edadMinima) {
					Error[0] = 1152;
				}
			}

			if(iHab3.value == 5) {
				iAge5 = iForm.elements["edad_1_3"];
				iAge6 = iForm.elements["edad_2_3"];
				if(iAge5.value < edadMinima || iAge6.value < edadMinima) {
					Error[0] = 1152;
				}
			}
		}
	}
	return Error;
} // final funcion checkErrorRoomCode




// FunciÃ³n de prueba
function validateAjaxRoom()
{
	var Error = Array();
	var age_child = Array();
	Error[0] = 0;
	Error[1] = 0;

	if (Error[0]==0){
		Error = checkErrorRoomCode();
	}
	return Error;
}


function ninyos(num) {
	// si la habitacion incluye ninos
	if((iForm = document.forms["idForm"]) && (oSelect = iForm.elements["habitacion_"+num])) {
		var str = oSelect.value;
		if (str == 3 || str == 5 || str == 7 || str == 4) {
			document.getElementById("etiqueta_ninyos").style.visibility = 'visible';
			document.getElementById("edades_"+num).style.display = 'inline';
			if(str == 5) {
				document.getElementById("etiqueta_ninyos").style.visibility = 'visible';
				document.getElementById("edad_1_"+num).style.visibility = 'visible';
				document.getElementById("edad_2_"+num).style.visibility = 'visible';
			} else {
				document.getElementById("etiqueta_ninyos").style.visibility = 'visible';
				document.getElementById("edad_1_"+num).style.visibility = 'visible';
				document.getElementById("edad_2_"+num).style.visibility = 'hidden';
				//document.getElementById("edad_2_"+num).value = 0;
			}
		} else {
			document.getElementById("etiqueta_ninyos").style.visibility = 'hidden';
			document.getElementById("edades_"+num).style.display = 'none'
			document.getElementById("edad_1_"+num).style.visibility = 'hidden';
			document.getElementById("edad_2_"+num).style.visibility = 'hidden';
			//document.getElementById("edad_1_"+num).value = 0;
			//document.getElementById("edad_2_"+num).value = 0;

		}
	}
}


function validateCombos() {

	iForm = document.forms["idForm"];

	//iRoomNums = eval(iForm.elements["num_rooms"].value) + eval(iForm.elements["num_rooms_1"].value) + eval(iForm.elements["num_rooms_2"].value);
	//alert ("the iroomsnums is "+iRoomsNums);
	//habitaciones
	iHab1 = iForm.elements["habitacion_1"].value
	iHab2 = iForm.elements["habitacion_2"].value
	iHab3 = iForm.elements["habitacion_3"].value
	//maneja el tamaño del iframe (si existe)
	try {
		if (window.top.document.getElementById('Buscadores')){
			height = 275;//tamaño original del iframe
			if(iForm.elements["valor"].value == 1) {

			}
			if(iForm.elements["valor"].value == 2) {
				height += 35;
			}
			if(iForm.elements["valor"].value == 3) {
				height += 70;
			}
			if(document.getElementById('adicionales').style.display == "block"){
				height += 200; //capa 'adicionales'
			}
			window.top.document.getElementById('Buscadores').height = height;
		}
	} catch (e) {
	}

	if(iForm.elements["valor"].value == 1) {

		iForm.elements["num_rooms"].value = 1;
		iForm.elements["num_rooms_1"].value = 0;
		iForm.elements["num_rooms_2"].value = 0;

		iForm.elements["num_adults_1"].value = 0;
		iForm.elements["num_children_1"].value = 0;
		iForm.elements["age_child1_1"].value = 0;
		iForm.elements["age_child2_1"].value = 0;

		iForm.elements["num_adults_2"].value = 0;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		iHab2 = 0;
		iHab3 = 0;

		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "none";
		document.getElementById("habitacion_repeat_3").style.display = "none";

		document.getElementById("edades_1").style.display = "none";
		document.getElementById("edades_2").style.display = "none";
		document.getElementById("edades_3").style.display = "none";

		document.getElementById("edad_1_2").style.visibility = "hidden";
		document.getElementById("edad_2_2").style.visibility = "hidden";
		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}


	if(iForm.elements["valor"].value == 2) {
		iForm.elements["num_rooms"].value = 1;
		iForm.elements["num_rooms_1"].value = 1;
		iForm.elements["num_rooms_2"].value = 0;

		iForm.elements["num_adults_2"].value = 0;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "block";
		document.getElementById("habitacion_repeat_3").style.display = "none";

		document.getElementById("edades_3").style.display = "none";
		document.getElementById("edades_2").style.display = "none";

		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";

		iHab3 = 0;
	}

	if(iForm.elements["valor"].value == 3) {
		iForm.elements["num_rooms"].value = 1;
		iForm.elements["num_rooms_1"].value = 1;
		iForm.elements["num_rooms_2"].value = 1;

		iForm.elements["num_adults_2"].value = 0;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "none";
		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "block";
		document.getElementById("habitacion_repeat_3").style.display = "block";

	}

	if(iForm.elements["num_rooms_1"].value == 0) {
		iForm.elements["num_adults_1"].value = 0;
		iForm.elements["num_children_1"].value = 0;
		iForm.elements["age_child1_1"].value = 0;
		iForm.elements["age_child2_1"].value = 0;

		document.getElementById("edades_2").style.visibility = "hidden";
		document.getElementById("edad_1_2").style.visibility = "hidden";
		document.getElementById("edad_2_2").style.visibility = "hidden";
	}

	if(iHab2 == 1) {
		iForm.elements["num_adults_1"].value = 2;
		iForm.elements["num_children_1"].value = 0;
		iForm.elements["age_child1_1"].value = 0;
		iForm.elements["age_child2_1"].value = 0;
		document.getElementById("edad_1_2").style.visibility = "hidden";
		document.getElementById("edad_2_2").style.visibility = "hidden";
	}
	if(iHab2 == 2) {
		iForm.elements["num_adults_1"].value = 1;
		iForm.elements["num_children_1"].value = 0;
		iForm.elements["age_child1_1"].value = 0;
		iForm.elements["age_child2_1"].value = 0;
		document.getElementById("edad_1_2").style.visibility = "hidden";
		document.getElementById("edad_2_2").style.visibility = "hidden";

	}
	if(iHab2 == 3) {
		iForm.elements["num_adults_1"].value = 2;
		iForm.elements["num_children_1"].value = 1;
		iForm.elements["age_child1_1"].value = iForm.elements["edad_1_2"].value;
		iForm.elements["age_child2_1"].value = 0;

		document.getElementById("edades_2").style.display = "block";
		document.getElementById("edades_2").style.visiblity = "visible";
		document.getElementById("edad_1_2").style.visibility = "visible";
		document.getElementById("edad_2_2").style.visibility = "hidden";

	}

	if(iHab2 == 4) {
		iForm.elements["num_adults_1"].value = 1;
		iForm.elements["num_children_1"].value = 1;
		iForm.elements["age_child1_1"].value = iForm.elements["edad_1_2"].value;
		iForm.elements["age_child2_1"].value = 0;

		document.getElementById("edades_2").style.display = "block";
		document.getElementById("edad_1_2").style.visibility = "visible";
		document.getElementById("edad_2_2").style.visibility = "hidden";
	}

	if(iHab2 == 5) {
		iForm.elements["num_adults_1"].value = 2;
		iForm.elements["num_children_1"].value = 2;
		iForm.elements["age_child1_1"].value = iForm.elements["edad_1_2"].value;
		iForm.elements["age_child2_1"].value = iForm.elements["edad_2_2"].value;

		document.getElementById("edades_2").style.display = "block";
		document.getElementById("edad_1_2").style.visibility = "visible";
		document.getElementById("edad_2_2").style.visibility = "visible";
	}

	if(iHab2 == 6) {
		iForm.elements["num_adults_1"].value = 3;
		iForm.elements["num_children_1"].value = 0;
		iForm.elements["age_child1_1"].value = 0;
		iForm.elements["age_child2_1"].value = 0;
		document.getElementById("edades_2").style.display = "none";
		document.getElementById("edad_1_2").style.visibility = "hidden";
		document.getElementById("edad_2_2").style.visibility = "hidden";
	}

	if(iHab2 == 7) {
		iForm.elements["num_adults_1"].value = 3;
		iForm.elements["num_children_1"].value = 1;
		iForm.elements["age_child1_1"].value = iForm.elements["edad_1_2"].value;
		iForm.elements["age_child2_1"].value = 0;

		document.getElementById("edades_2").style.display = "block";
		document.getElementById("edad_1_2").style.visibility = "visible";
		document.getElementById("edad_2_2").style.visibility = "hidden";
	}

	if(iHab3 == 1) {
		iForm.elements["num_adults_2"].value = 2;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "none";
		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab3 == 2) {
		iForm.elements["num_adults_2"].value = 1;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "none";
		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab3 == 3) {
		iForm.elements["num_adults_2"].value = 2;
		iForm.elements["num_children_2"].value = 1;
		iForm.elements["age_child1_2"].value = iForm.elements["edad_1_3"].value;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "block";
		document.getElementById("edad_1_3").style.visibility = "visible";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab3 == 4) {
		iForm.elements["num_adults_2"].value = 1;
		iForm.elements["num_children_2"].value = 1;
		iForm.elements["age_child1_2"].value = iForm.elements["edad_1_3"].value;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "block";
		document.getElementById("edad_1_3").style.visibility = "visible";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab3 == 5) {
		iForm.elements["num_adults_2"].value = 2;
		iForm.elements["num_children_2"].value = 2;
		iForm.elements["age_child1_2"].value = iForm.elements["edad_1_3"].value;
		iForm.elements["age_child2_2"].value = iForm.elements["edad_2_3"].value;

		document.getElementById("edades_3").style.display = "block";
		document.getElementById("edad_1_3").style.visibility = "visible";
		document.getElementById("edad_2_3").style.visibility = "visible";
	}

	if(iHab3 == 6) {
		iForm.elements["num_adults_2"].value = 3;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "block";
		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab3 == 7) {
		iForm.elements["num_adults_2"].value = 3;
		iForm.elements["num_children_2"].value = 1;
		iForm.elements["age_child1_2"].value = iForm.elements["edad_1_3"].value;
		iForm.elements["age_child2_2"].value = 0;

		document.getElementById("edades_3").style.display = "block";
		document.getElementById("edad_1_3").style.visibility = "visible";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iForm.elements["num_rooms_2"].value == 0) {
		iForm.elements["num_adults_2"].value = 0;
		iForm.elements["num_children_2"].value = 0;
		iForm.elements["age_child1_2"].value = 0;
		iForm.elements["age_child2_2"].value = 0;
		document.getElementById("edades_3").style.display = "none";
		document.getElementById("edad_1_3").style.visibility = "hidden";
		document.getElementById("edad_2_3").style.visibility = "hidden";
	}

	if(iHab1 == 1) {
		iForm.elements["num_adults"].value = 2;
		iForm.elements["num_children"].value = 0;
		iForm.elements["age_child1"].value = 0;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "none";
		document.getElementById("edad_1_1").style.visibility = "hidden";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}

	if(iHab1 == 2) {
		iForm.elements["num_adults"].value = 1;
		iForm.elements["num_children"].value = 0;
		iForm.elements["age_child1"].value = 0;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "none";
		document.getElementById("edad_1_1").style.visibility = "hidden";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}

	if(iHab1 == 3) {
		iForm.elements["num_adults"].value = 2;
		iForm.elements["num_children"].value = 1;
		iForm.elements["age_child1"].value = iForm.elements["edad_1_1"].value;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "block";
		document.getElementById("edad_1_1").style.visibility = "visible";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}

	if(iHab1 == 4) {
		iForm.elements["num_adults"].value = 1;
		iForm.elements["num_children"].value = 1;
		iForm.elements["age_child1"].value = iForm.elements["edad_1_1"].value;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "block";
		document.getElementById("edad_1_1").style.visibility = "visible";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}

	if(iHab1 == 5) {
		iForm.elements["num_adults"].value = 2;
		iForm.elements["num_children"].value = 2;
		iForm.elements["age_child1"].value = iForm.elements["edad_1_1"].value;
		iForm.elements["age_child2"].value = iForm.elements["edad_2_1"].value;

		document.getElementById("edades_1").style.display = "block";
		document.getElementById("edad_1_1").style.visibility = "visible";
		document.getElementById("edad_2_1").style.visibility = "visible";

	}

	if(iHab1 == 6) {
		iForm.elements["num_adults"].value = 3;
		iForm.elements["num_children"].value = 0;
		iForm.elements["age_child1"].value = 0;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "none";
		document.getElementById("edad_1_1").style.visibility = "hidden";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}

	if(iHab1 == 7) {
		iForm.elements["num_adults"].value = 3;
		iForm.elements["num_children"].value = 1;
		iForm.elements["age_child1"].value = iForm.elements["edad_1_1"].value;
		iForm.elements["age_child2"].value = 0;
		document.getElementById("edades_1").style.display = "block";
		document.getElementById("edad_1_1").style.visibility = "visible";
		document.getElementById("edad_2_1").style.visibility = "hidden";
	}



}




function updateCombos() {
	iForm = document.forms["idForm"];
	iRoomNums = eval(iForm.elements["num_rooms"].value) + eval(iForm.elements["num_rooms_1"].value) + eval(iForm.elements["num_rooms_2"].value);

	iCategory = iForm.elements["category"].value;

	//adults

	iAdults = iForm.elements["num_adults"].value;
	iAdults1 = iForm.elements["num_adults_1"].value;
	iAdults2 = iForm.elements["num_adults_2"].value;
	//kids
	iKids = iForm.elements["num_children"].value;
	iKids1 = iForm.elements["num_children_1"].value;
	iKids2 = iForm.elements["num_children_2"].value;

	//kids age
	kidAge_1_1 = iForm.elements["age_child1"].value;
	kidAge_2_1 = iForm.elements["age_child2"].value;

	kidAge_1_2 = iForm.elements["age_child1_1"].value;
	kidAge_2_2 = iForm.elements["age_child2_1"].value;

	kidAge_1_3 = iForm.elements["age_child1_2"].value;
	kidAge_2_3 = iForm.elements["age_child2_2"].value;


	iForm.elements["valor"].value = iRoomNums;
	// first room
	if(iAdults==1) {
		if((iKids == "") || (iKids == 0)) {
			iForm.elements["habitacion_1"].value = 2;
			document.getElementById("edades_1").style.display = "none";
			document.getElementById("edad_1_1").style.visibility = "hidden";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		} else if(iKids == 1) {

			iForm.elements["habitacion_1"].value = 4;
			iForm.elements["edad_1_1"].value = kidAge_1_1;
			document.getElementById("edades_1").style.display = "block";
			document.getElementById("edad_1_1").style.visibility = "visible";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		}
	}

	if(iAdults == 2) {
		if((iKids == "") || (iKids == 0)) {
			iForm.elements["habitacion_1"].value = 1;
			document.getElementById("edades_1").style.display = "none";
			document.getElementById("edad_1_1").style.visibility = "hidden";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		} else if(iKids == 1) {
			iForm.elements["habitacion_1"].value = 3;
			iForm.elements["edad_1_1"].value = kidAge_1_1;
			document.getElementById("edades_1").style.display = "block";
			document.getElementById("edad_1_1").style.visibility = "visible";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		} else if(iKids == 2) {
			iForm.elements["habitacion_1"].value = 5;
			iForm.elements["edad_1_1"].value = kidAge_1_1;
			iForm.elements["edad_2_1"].value = kidAge_2_1;
			document.getElementById("edades_1").style.display = "block";
			document.getElementById("edad_1_1").style.visibility = "visible";
			document.getElementById("edad_2_1").style.visibility = "visible";
		}
	}

	if(iAdults==3) {
		if((iKids == "") || (iKids == 0)) {
			iForm.elements["habitacion_1"].value = 6;
			document.getElementById("edades_1").style.display = "none";
			document.getElementById("edad_1_1").style.visibility = "hidden";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		} else {
			iForm.elements["habitacion_1"].value = 7;
			iForm.elements["edad_1_1"].value = kidAge_1_1;
			document.getElementById("edades_1").style.display = "block";
			document.getElementById("edad_1_1").style.visibility = "visible";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		}
	}
	// second room
	if(iAdults1==1) {
		if((iKids1 == "") || (iKids1 == 0)) {
			iForm.elements["habitacion_2"].value = 2;
			document.getElementById("edades_2").style.display = "none";
			document.getElementById("edad_1_1").style.visibility = "hidden";
			document.getElementById("edad_2_1").style.visibility = "hidden";
		} else {
			iForm.elements["habitacion_2"].value = 4;
			iForm.elements["edad_1_2"].value = kidAge_1_2;
			document.getElementById("edades_2").style.display = "block";
			document.getElementById("edad_1_2").style.visibility = "visible";
			document.getElementById("edad_2_2").style.visibility = "hidden";
		}
	}

	if(iAdults1 == 2) {
		if((iKids1 == "") || (iKids1 == 0)) {
			iForm.elements["habitacion_2"].value = 1;
			document.getElementById("edades_2").style.display = "none";
			document.getElementById("edad_1_2").style.visibility = "hidden";
			document.getElementById("edad_2_2").style.visibility = "hidden";
		} else if(iKids1 == 1) {
			iForm.elements["habitacion_2"].value = 3;
			iForm.elements["edad_1_2"].value = kidAge_1_2;
			document.getElementById("edades_2").style.display = "block";
			document.getElementById("edad_1_2").style.visibility = "visible";
			document.getElementById("edad_2_2").style.visibility = "hidden";
		} else if(iKids1 == 2) {
			iForm.elements["habitacion_2"].value = 5;
			iForm.elements["edad_1_2"].value = kidAge_1_2;
			iForm.elements["edad_2_2"].value = kidAge_2_2;
			document.getElementById("edades_2").style.display = "block";
			document.getElementById("edad_1_2").style.visibility = "visible";
			document.getElementById("edad_2_2").style.visibility = "visible";
		}
	}

	if(iAdults1==3) {
		if((iKids1 == "") || (iKids1 == 0)) {
			iForm.elements["habitacion_2"].value = 6;
			document.getElementById("edades_2").style.display = "none";
			document.getElementById("edad_1_2").style.visibility = "hidden";
			document.getElementById("edad_2_2").style.visibility = "hidden";
		} else {
			iForm.elements["habitacion_2"].value = 7;
			iForm.elements["edad_1_2"].value = kidAge_1_2;
			document.getElementById("edades_2").style.display = "block";
			document.getElementById("edad_1_2").style.visibility = "visible";
			document.getElementById("edad_2_2").style.visibility = "hidden";
		}
	}

	//third room
	if(iAdults2==1) {
		if((iKids2 == "") || (iKids2 == 0)) {
			iForm.elements["habitacion_3"].value = 2;
			document.getElementById("edades_3").style.display = "none";
			document.getElementById("edad_1_3").style.visibility = "hidden";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		} else {
			iForm.elements["habitacion_3"].value = 4;
			iForm.elements["edad_1_3"].value = kidAge_1_3;
			document.getElementById("edades_3").style.display = "block";
			document.getElementById("edad_1_3").style.visibility = "visible";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		}
	}

	if(iAdults2 == 2) {
		if((iKids2 == "") || (iKids2 == 0)) {
			iForm.elements["habitacion_3"].value = 1;
			document.getElementById("edades_3").style.display = "none";
			document.getElementById("edad_1_3").style.visibility = "hidden";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		} else if(iKids2 == 1) {
			iForm.elements["habitacion_3"].value = 3;
			iForm.elements["edad_1_3"].value = kidAge_1_3;
			document.getElementById("edades_3").style.display = "block";
			document.getElementById("edad_1_3").style.visibility = "visible";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		} else if (iKids2 == 2){
			iForm.elements["habitacion_3"].value = 5;
			iForm.elements["edad_1_3"].value = kidAge_1_3;
			iForm.elements["edad_2_3"].value = kidAge_2_3;
			document.getElementById("edades_3").style.display = "block";
			document.getElementById("edad_1_3").style.visibility = "visible";
			document.getElementById("edad_2_3").style.visibility = "visible";
		}
	}

	if(iAdults2==3) {
		if((iKids2 == "") || (iKids2 == 0)) {
			iForm.elements["habitacion_3"].value = 6;
			document.getElementById("edades_3").style.display = "none";
			document.getElementById("edad_1_3").style.visibility = "hidden";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		} else {
			iForm.elements["habitacion_3"].value = 7;
			iForm.elements["edad_1_3"].value = kidAge_1_3;
			document.getElementById("edades_3").style.display = "block";
			document.getElementById("edad_1_3").style.visibility = "visible";
			document.getElementById("edad_2_3").style.visibility = "hidden";
		}
	}

	if(iForm.elements["valor"].value == 1) {
		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "none";
		document.getElementById("habitacion_repeat_3").style.display = "none";
	}

	if(iForm.elements["valor"].value == 2) {
		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "block";
		document.getElementById("habitacion_repeat_3").style.display = "none";
	}

	if(iForm.elements["valor"].value == 3) {
		document.getElementById("habitacion_repeat_1").style.display = "block";
		document.getElementById("habitacion_repeat_2").style.display = "block";
		document.getElementById("habitacion_repeat_3").style.display = "block";
	}
}

// this function is a port of InterfaceManager::translate_room_codes()
function translateRoomCode(code)
{
	r = new Array();
	r['adults'] = 0;
	r['children'] = 0;
	
	switch (code)
	{
		// double
		case '1':
			r['adults'] = 2;
			r['children'] = 0;
			break;
		// single
		case '2':
			r['adults'] = 1;
			r['children'] = 0;
			break;
		// double + 1
		case '3':
			r['adults'] = 2;
			r['children'] = 1;
			break;
		// single + 1
		case '4':
			r['adults'] = 1;
			r['children'] = 1;
			break;
		// double + 2
		case '5':
			r['adults'] = 2;
			r['children'] = 2;
			break;
		// triple
		case '6':
			r['adults'] = 3;
			r['children'] = 0;
			break;
		// triple + 1
		case '7':
			r['adults'] = 3;
			r['children'] = 1;
			break;
		// triple + 2
		case '8':
			r['adults'] = 3;
			r['children'] = 2;
			break;
	}
	return r;
}

// function that combines all rooms of same type into one variable upon submission
// of the ajax form
function processCombosOnSubmit() {
	iForm = document.forms["idForm"];
	num_diff_rooms = iForm.elements["valor"].value;
	
	if(num_diff_rooms == '1' && typeof(iForm.elements["room_code"])!="undefined")
	{
		iForm.elements['room_code'].value = iForm.elements["habitacion_1"].value;
	}
	
	iHab1 = translateRoomCode(iForm.elements["habitacion_1"].value);
	iHab2 = translateRoomCode(iForm.elements["habitacion_2"].value);
	iHab3 = translateRoomCode(iForm.elements["habitacion_3"].value);

	iForm.elements["num_adults"].value = iHab1['adults'];
	iForm.elements["num_adults_1"].value = iHab2['adults'];
	iForm.elements["num_adults_2"].value = iHab3['adults'];

	iForm.elements["num_children"].value = iHab1['children'];
	iForm.elements["num_children_1"].value = iHab2['children'];
	iForm.elements["num_children_2"].value = iHab3['children'];

	iForm.elements["age_child1"].value = iForm.elements["edad_1_1"].value;
	iForm.elements["age_child2"].value = iForm.elements["edad_2_1"].value;
	iForm.elements["age_child1_1"].value = iForm.elements["edad_1_2"].value;
	iForm.elements["age_child2_1"].value = iForm.elements["edad_2_2"].value;
	iForm.elements["age_child1_2"].value = iForm.elements["edad_1_3"].value;
	iForm.elements["age_child2_2"].value = iForm.elements["edad_2_3"].value;
}

// HK: esta funcion no valida nada, le he cambiado el nombre y dejado una 
// referencia para scripts que usen el nombre antiguo
validateCombosOnSubmit = processCombosOnSubmit;
