// JavaScript Document
// 28 Aug 2006
<!--
//
// INITIALISE FORM
// ---------------
var packageFM;
function initPackageForm() {
	packageFM = document.forms["packageFrm"];
	villageDD = packageFM.village_name;
	cityDD = packageFM.departure_city;
	submitDD = packageFM.get_rates_btn;
	//
	// Disable booking form if XMl feedback an error
	if (availableCityArr != undefined && cityArr != undefined) {
		selectVillage();
	}
	villageDD.disabled = false;
	submitDD.disabled = false;	
}
//
// MAKE VILLAGE SELECTION
// ----------------------
function selectVillage() { //called when user change village selection
	if (villageDD.value != 0) {
		populateCity(); //rebuild city drop down
	} else {
		if (villageDD.options.length > 0) {
			villageDD.selectedIndex = 1;
			selectVillage();
		}
	}
}
//
// populate city
function populateCity() { //execute when user change village selection
	if (cityDD.selectedIndex != -1) { //record selection by user
		var selectedCity = cityDD.options[cityDD.selectedIndex].value;
	} else {
		var selectedCity = "";
	}
	
	cityDD.options.length = 1;
	/*
	cityDD.options[0].text = cityArr[0]["label"];
	cityDD.options[0].value = "";
	cityDD.selectedIndex = 0;
	*/
	var tmp_arr = new Array();
	var k = 0;
	for (var i = 0; i < availableCityArr.length; i++) {
		var theData = availableCityArr[i];		
		if (theData["vid"] == villageDD.value) {
			tmp_arr[k] = theData["cid"];
			k += 1;
		}
	}
	//
	if (tmp_arr.length > 0) {
		for (j = 0; j < cityArr.length; j++) { // loop through cityArr (for checking and sorting purpose)
		var theCode = cityArr[j];
			
			for (var i = 0; i < tmp_arr.length; i++) {
				if (theCode["code"] == tmp_arr[i] && theCode["code"] != "") {
					var addCity = true;
					for (var h = 0; h < cityDD.options.length; h++) { // check if the city already been added into cityDD
						if (cityDD.options[h].value == tmp_arr[i])
							addCity = false;
					}
					//
					if (addCity) {
						cityDD.options.length += 1;
						var theOption = cityDD.options[cityDD.options.length - 1];
						//
						theOption.text = theCode["label"];
						theOption.value = theCode["code"];
						//
						if (selectedCity == theOption.value) { //reapply selection by user
							cityDD.selectedIndex = theOption.index;
							selectedCity = undefined;
						}
					}
							
				}
			}
		}
		cityDD.disabled = false;
		submitDD.disabled = false;
	} else {
		cityDD.disabled = true;
		submitDD.disabled = true;
	}
}
//-->