//Defines the functions that are used by all pages that contain the booking panel or parts of it.

//Function to check whether the cities are in the allowed routes
function checkDepartCities(outBoundDepart, inBoundDepart, searchType){
	
	if(!checkDepartCity(outBoundDepart, searchType)){
		return true;
	}	
	
	if(!checkDepartCity(inBoundDepart, searchType)){
		return true;
	}	
	return false;
}

//function to check city is in the departures list
function checkDepartCity(departureCityName, searchType){
	
	for (var i = 0; i < ourRoutes.length; i++)
	{
		var route = ourRoutes[i];
		// if this route departs from our departcity then true
		if (qualifiesForSearchType(route, searchType) && (route.departCity == departureCityName))
		{
			return true;
		}
	}
	return false;
}

//function to return whether city is in the list of can depart cities
function isDepartCity(cityToDepart)
{	
	for (var i = 0; i < cityNames.length; i++)
	{
		var city = cityNames[i];
		if(city.code == cityToDepart){
			if (city.canDepart) return true;
		}
	}
	return false;
}

/*
  Determines whether the given route is allowed for the given search type.
*/
function qualifiesForSearchType(route, searchType){
  if (searchType == "milesPlusMoney"){
    return route.allowsMPM;
  }
  else if (searchType == "redeemMiles"){
    return route.allowsRedemption;
  }
  else{
    return true;
  }
}

// gets the city name from the city code
function getCityName(cityCode)
{
	var city;
	// loop through the cities till we find this one
	for (var i=0; i < cityNames.length; i++)
	{
		city = cityNames[i];
		if (city.code == cityCode) break;
	}
	return city.name;
}

// gets the default arrival city for this departure city
// DODGY HARD CODED CITY CODES - NICE...
function getDefaultArrival(departCity){
	var arrivalCity;
	if (departCity == 'LON')
	{
		arrivalCity = 'NYC';
	}
	else if (departCity == 'MAN')
	{
		arrivalCity = 'ORL';
	}
	else
	{
		arrivalCity = 'LON';
	}
	return arrivalCity;
}

// Clears the select box
function clearOptions(theSelect){
	if (theSelect.type=='select-one')
	    while(theSelect.length>0)
	        theSelect.options[0]=null;
}

//---------------- JS types -----------------------------------

// The CabinDetails class represents info on a cabin such as cabin ID 
// (for submission to the backend) and a friendly name for display.
function CabinDetails(id, fn, flx, nei, sk)
{
    this.myId=id;
    this.myFriendlyName=fn;
	this.flexible = flx;
	this.neither = nei;
	this.sortKey = sk;
}

// represents a city for the drop downs. This is not used by the Multiple Destinations page.
function City(cd, nm, cdp)
{
	this.code = cd;
	this.name = nm;
	this.canDepart = cdp;
}

// The Route class represents an individiual route
function Route(dc, ac, cl, ms, allowsRedemption, allowsMPM)
{
    this.departCity = dc;
	this.arriveCity = ac;
	this.cabinList = cl;
	this.multiSector = ms;
	this.allowsRedemption = allowsRedemption;
	this.allowsMPM = allowsMPM;
}
//---------------- END JS types -----------------------------------