



// Functions to manage countries, cities and hotels lists. jsCities and jsHotels variables required

function createOption(text, value) {
	var op = document.createElement("option");
	op.setAttribute("value", value);
	op.appendChild(document.createTextNode(text));
	return op;
}

function cleanCities(city, cityLabel) {
	while(city.childNodes.length > 0) {
		city.removeChild(city.childNodes[0]);
	}

	// first element
	city.appendChild(createOption(cityLabel, 0));
}


function cleanHotels(hotel, hotelLabel) {
	while(hotel.childNodes.length > 0) {
		hotel.removeChild(hotel.childNodes[0]);
	}

	// first element
	hotel.appendChild(createOption(hotelLabel, 0));
}

var arraysDeclared = false;

// if hotel is null, there is not hotel combo
function changeCountry(country, city, hotel, labelCity, labelHotel, citiesArrayParam) {
	if (!arraysDeclared) { country.selectedIndex = 0; return;}
	if (citiesArrayParam != null) { citiesArray = citiesArrayParam; }
	else { citiesArray = jsCities; }

	// clean cities
	cleanCities(city, labelCity);

	// fill cities
	for (n=0 ; n < citiesArray.length; n++) {
		var c = citiesArray[n].split(":",3);
		if (c[1] == country.options[country.selectedIndex].value) {
			city.appendChild(createOption(c[2], parseInt(c[0])));
		}
	}

	// if there is only one city, select it
	if (city.childNodes.length == 2) {
		city.removeChild(city.childNodes[0]);
		if (hotel != null) changeCity(city, hotel, labelHotel);
	} else {
		// clean hotels
		if (hotel != null) cleanHotels(hotel, labelHotel);
	}
}

function changeCity(city, hotel, labelHotel) {
	// clean hotels
	cleanHotels(hotel, labelHotel);

	// fill hotels
	for (n=0 ; n < jsHotels.length; n++) {
		var c = jsHotels[n].split(":",3);
		if (c[1] == city.options[city.selectedIndex].value) {
			hotel.appendChild(createOption(c[2], parseInt(c[0])));
		}
	}

	// if there is only one hotel, select ir
	if (hotel.childNodes.length == 2) {
		hotel.removeChild(hotel.childNodes[0]);
	}
}

function preloadCCH(countryObject, cityObject, hotelObject, countryId, cityId, hotelId, cityLabel, hotelLabel, citiesArrayParam) {
	if (citiesArrayParam != null) { citiesArray = citiesArrayParam; }
	else { citiesArray = jsCities; }

	preloadCC(countryObject, cityObject, countryId, cityId, cityLabel, citiesArray);

	cleanHotels(hotelObject, hotelLabel);

	if (cityId == 0) return; // no city selected -> hotels can not be loaded

	for (n=0 ; n < jsHotels.length; n++) {
		var c = jsHotels[n].split(":",3);
		if (c[1] == cityId) {
			var opt = createOption(c[2], parseInt(c[0]));
			if (c[0] == hotelId) opt.setAttribute("selected", "selected");
			hotelObject.appendChild(opt);
		}
	}

	if (hotelObject.childNodes.length == 2) {
		hotelObject.removeChild(hotelObject.childNodes[0]);
	}
}

function preloadCC(countryObject, cityObject, countryId, cityId, cityLabel, citiesArray) {
	for (i=0; i < countryObject.options.length; i++) {
		if (countryObject.options[i].value == countryId) {
			countryObject.selectedIndex = i;
			break;
		}
	}

	cleanCities(cityObject, cityLabel);

	for (n=0 ; n < citiesArray.length; n++) {
		var c = citiesArray[n].split(":",3);
		if (c[1] == countryId) {
			var opt = createOption(c[2], parseInt(c[0]));
			if (c[0] == cityId) opt.setAttribute("selected", "selected");
			cityObject.appendChild(opt);
		}
	}

	if (cityObject.childNodes.length == 2) {
		cityObject.removeChild(cityObject.childNodes[0]);
	}
}


// Init Calendar
function initCalendarData(f) {
	if (f == null) f = document.createEvent;
	if (f == null) return;
	var localReferenceCal = top.frames.calendarFrame;
	if (localReferenceCal.cal == null) {return;} 
	if (f.fini.value != null && f.fini.value != '') {
		localReferenceCal.cal.initDates(f.fini.value, f.fout.value);
		localReferenceCal.loadNumberNights();
		localReferenceCal.cal.load(true, false);
	}
}


var maxPersonByRoom = 3;
var maxChildsCombo = 5;

function checkMaxNChilds() {
/*
    var f = document.searchForm;
    var maxNChilds = (maxPersonByRoom * (parseInt(f.nrooms.value))) - parseInt(f.nadults.value);
    f.nchilds.innerHTML = "";
    f.nchilds.appendChild(createOption(0, 0));
    for (var i = 1; i <= maxNChilds && i <= maxChildsCombo; i++) {
        f.nchilds.appendChild(createOption(i, i));
    }
*/
}
