



// 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]);
	}
}

var SCR_MSG_CSEARCH_ESCRIBA_DIRECCION = 'Please enter in address';
var SCR_MSG_CSEARCH_ESCRIBA_PUNTO_INTERES = 'Please enter point of interest';

function changeSearchMethod(stype, showLayer) {
	var f = document.searchForm;
	f.stype.value = stype;
	if (showLayer === true || showLayer === undefined) searchLayerGroup.showSelectedLayer(stype - 1);
}

function loadAll(country, city, hotel, stype) {
	if (isEmpty(country) || country == '0') return;

	var f = document.searchForm;
	if (stype == 1) {
		preloadCCH(f.country1, f.city1, f.hotel1, country, city, hotel, 'All cities', 'All hotels', jsCities);
	} else if (stype == 2) {
		preloadCC(f.country2, f.city2, country, city, 'Select a city', jsCitiesSearch);
	} else if (stype == 3) {
		preloadCC(f.country3, f.city3, country, city, 'Select a city', jsCitiesSearch);
	}
}

// Init Calendar
function initCalendarData() {
	var localReferenceCal = top.frames.calendarFrame;
	if (localReferenceCal.cal == null) {return;} 
	if (document.searchForm.fini.value != null && document.searchForm.fini.value != '') {
		localReferenceCal.cal.initDates(document.searchForm.fini.value, document.searchForm.fout.value);
		localReferenceCal.loadNumberNights();
		localReferenceCal.cal.load(true, false);
	}
}

function submitSearchForm(cptp) {

	var f = document.searchForm;

	var stype = f.stype.value;
	var stdate = f.stdate.value;
	var enddate = f.enddate.value;
	
	if((stdate != "dd-mm-yyyy" && enddate == "dd-mm-yyyy") || (stdate == "dd-mm-yyyy" && enddate != "dd-mm-yyyy"))
	{
		alert('Please select an arrival and departure date');
		return false;
	}
	
	if(stype == 1) {
		if (f.stext1 && f.stext1.value != SCR_MSG_CSEARCH_ESCRIBA_CIUDAD_HOTEL && f.stext1.value != '' && !auto.isTextEligible()) {
			alert('Your search has not produced any matches');
			f.stext1.focus();
			return false;
		}
	}

	if(stype == 2) {
		if ((f.country2.value == 0) || (isEmpty(f.city2.value) || f.city2.value == 0)) {
			alert('Please select a country and city');
			return false;
		}
		if ((isEmpty(f.stext2.value) || f.stext2.value == 'Please enter in address')) {
			alert('Please enter an address');
			f.stext2.focus();
			return false;
		}
	}

	if (stype == 3) {
		if ((f.country3.value == 0) || (isEmpty(f.city3.value) || f.city3.value == 0)) {
			alert('Please select a country and city');
			return false;
		}
		if ((isEmpty(f.stext3.value) || f.stext3.value == 'Please enter point of interest')) {
		     alert('Please enter a point of interest');
			f.stext3.focus();
			return false;
		}
	}

	if ((cptp == true ) && (f.fini.value == "" || f.fout.value == "")) {
		alert('Please select an arrival and departure date');
		return false;
	}
	
	if ((cptp == true ) && (f.partyId.value == "")) {
		alert('Please select a company');
		return false;
	}
	if ((cptp == true ) && (f.branchId.value == "")) {
		alert('Please select a branch');
		return false;
	}

    if (f.searchButton.className == "processing") { return false; }
    f.searchButton.className = "processing";
    f.searchButton.value = 'Searching...';

	var url = "/nh/en/search.html?action=search";
	if (stype == 1) {
		if (!isEmpty(f.country1.value) && f.country1.value != 0) {
			url = "/nh/en/hotels/";
			if (url.charAt(url.length - 1) == '/') url = url.substr(0, url.length - 1);
			url += "/" + preparsedCountries[f.country1.value + ""];
			if (!isEmpty(f.city1.value) && f.city1.value != 0) {
				url += "/" + preparsedCities[f.city1.value + ""];
			}
			url += ".html?action=search";
		}
		url = url + "&event=" + 1;
		url = url + "&stype=" + stype;
		url = url + "&country=" + f.country1[f.country1.selectedIndex].value;
		url = url + "&city=" + f.city1[f.city1.selectedIndex].value;
		url = url + "&hotel=" + f.hotel1[f.hotel1.selectedIndex].value;
		if (f.stext1) {
			url = url + "&stext=" + (f.stext1.value == 'City, hotel or point of interest' ? '' : f.stext1.value);
		}
	} else if (stype == 2) {
		url = url + "&event=" + 2;
		url = url + "&stype=" + stype;
		url = url + "&country=" + f.country2[f.country2.selectedIndex].value;
		url = url + "&city=" + f.city2[f.city2.selectedIndex].value;
		url = url + "&stext=" + f.stext2.value;
	} else if (stype == 3) {
		url = url + "&event=" + 3;
		url = url + "&stype=" + stype;
		url = url + "&country=" + f.country3[f.country3.selectedIndex].value;
		url = url + "&city=" + f.city3[f.city3.selectedIndex].value;
		url = url + "&stext=" + f.stext3.value;
	}
	url = url + "&nrooms=" + f.nrooms.value;
	url = url + "&nadults=" + f.nadults.value;
	url = url + "&nchilds=" + f.nchilds.value;
	// disponibilidad alternativa
	if (f.rate!=null) url = url+"&rate=" + f.rate.value;
	else url = url + "&webrate=" + f.webrate.value;
	// **************************
	url = url + "&fini=" + f.fini.value;
	url = url + "&fout=" + f.fout.value;
	if (f.priceRange != null) url = url + "&priceRange=" + getRadioValue(f.priceRange);
	if (cptp) {
		if (f.partyId != null) url = url + "&partyId=" + f.partyId.value;
		if (f.branchId != null) url = url + "&branchId=" + f.branchId.value;
		if (f.clientPartyId != null && f.clientPartyId != '') { 
			url = url + "&clientPartyId=" + f.clientPartyId.value;
			url = url + "&clientBranchId=" + getClientBranchId(f.partyId.value, f.clientPartyId.value);
		}
	}
	url = url + "&searchEngine=" + f.searchEngine.value;
	return url;
}

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));
    }
*/
}
