// Search functions

function initSearch() {
	var f = document.search;
	
	// back browser
	if(f.country.selectedIndex > 0) {
		changeCountry(f.country, f.city, msgAllCities);
	}
	preloadCCH(f.country, f.city, country, city, hotel, msgAllCities, msgAllHotels);
}


// if hotel is null, there is not hotel combo
function changeCountry(country, city, labelCity, labelHotel) {

	// clean cities
	cleanCities(city, labelCity);

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

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

// if hotel is null, there is not hotel combo
function changeCountryAdmin(country, city, hotel, labelCity, labelHotel) {

	// clean cities
	cleanCities(city, labelCity);

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

	// if there is only one city, select it
	if (city.childNodes.length == 2) {
		city.removeChild(city.childNodes[0]);
		changeCity(city, 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, countryId, cityId, hotelId, cityLabel, hotelLabel) {
	preloadCC(countryObject, cityObject, countryId, cityId, cityLabel);

	// Not necesary to clean hotels
	/*
	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) {
	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 < jsCities.length; n++) {
		var c = jsCities[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]);
	}
}

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

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

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

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

function createOption(txt, value, selected) {
    var newOption = document.createElement("OPTION");
    newOption.value = value;
    if (selected != null) newOption.selected = ((selected)?"selected":"");
    newOption.innerHTML = txt;
    return newOption;
}
