//Defines functions used by the Booking Right Hand Panel page. 
//The functions that are used by other pages too can be found in common_booking.js

// this is the Main function called on entry to the page
function setUp(obDepartCity, obArriveCity, obCabin, ibDepartCity, ibArriveCity, ibCabin)
{
  removeUnavailableRoutes();
  
  //check whether the Outbound and Inbound departure cities are in the dropdown list or not
	//if one of them are not available, set default departure cities as Gateway City and LON
	if(checkDepartCities(obDepartCity,ibDepartCity, null) || !isDepartCity(obDepartCity)){
		obDepartCity = gatewayCityCode;
		defaultArrivalCity = 'LON';
		if(gatewayCityCode=='LON'){//if Gateway Country is UK, set arrival city as NYC
			defaultArrivalCity = 'NYC';
		}
		obArriveCity = defaultArrivalCity;
		ibDepartCity = defaultArrivalCity;
		ibArriveCity = gatewayCityCode;
	}
	//If gateway city is not in the list, set defult to LON and NYC
	if(checkDepartCities(obDepartCity,ibDepartCity, null)){
		obDepartCity = 'LON';
		obArriveCity = 'NYC';
		ibDepartCity = 'NYC';
		ibArriveCity = 'LON';
	}

	// do the outbound stuff
	doOutboundDeparture(obDepartCity);
	doOutboundArrival(obDepartCity, obArriveCity);
}

// Populates the outbound departure city drop down
// Loops through all the cities, adding the ones that have the canDepart flag set to true 
function doOutboundDeparture(departCity)
{	
	var selected = 0;
	var optionIndex = 0;

	document.main.departure.options.length = 0;
	for (var i = 0; i < cityNames.length; i++)
	{
		var city = cityNames[i];
		
		if (city.canDepart) { 
			document.main.departure.options.length++;
			document.main.departure.options[optionIndex++] = new Option(city.name, city.code);
			
			if (city.code == departCity){
				selected = optionIndex - 1;
			}
		}
	}
	// set the selected one
	document.main.departure.options[selected].selected = true;
}

// Populates the outbound arrival city drop down
function doOutboundArrival(departCity, arrivalCity){
	var arrivalDropdown = document.main.arrival;

	// clear down the select box
	clearOptions(arrivalDropdown);

	var optionIndex = 0;
	var cabinList = 0;
	var selected = 0;
	for (var i = 0; i < ourRoutes.length; i++)
	{
		var route = ourRoutes[i];
		
		// if this route departs from our departcity then add it
		if ((route.departCity == departCity))
		{
			var cityName = getCityName(route.arriveCity)
			arrivalDropdown.options[optionIndex++] = new Option(cityName, route.arriveCity);
			// check for the default
			if (route.arriveCity == arrivalCity)
			{
				selected = optionIndex - 1;
			}
		}
	}
	// set the selected one
	arrivalDropdown.options[selected].selected = true;
}

/**
	Enables or disables the controls for the return flight depending on whether round trip or one way was selected.  	
 **/
function doRoundTrip(isRoundTrip){
	document.main.returnday.disabled = !isRoundTrip;
	document.main.returnmonth.disabled = !isRoundTrip;
}

//------ Event Handlers ----------------------------------------------

// Outbound Departure drop down changed
function obDepChanged(){
	// do outbound arrival cities
	var departCity = document.main.departure.options[document.main.departure.selectedIndex].value;
	doOutboundArrival(departCity, getDefaultArrival(departCity));
}
//------ END Event Handlers ----------------------------------------------



