//function to set the top right hand city dropdown
function doTopRight()
{ 

 doRoutes(document.main.arrival,document.main.departure.options[document.main.departure.selectedIndex].value,1);
 doBottomLeft();
}
//function to set the bottom left hand city dropdown
function doBottomLeft()
{

 if(document.main.departureReturn.type=='select-one')
 {
  doRoutes(document.main.departureReturn,document.main.departure.options[document.main.departure.selectedIndex].value,1);
  selectCity(document.main.departureReturn,document.main.arrival.options[document.main.arrival.selectedIndex].value);
  doBottomRight();
 }
}
//function to set the bottom right hand city dropdown
function doBottomRight()
{
 doRoutes(document.main.arrivalReturn,document.main.departureReturn.options[document.main.departureReturn.selectedIndex].value,0);
 selectCity(document.main.arrivalReturn,document.main.departure.options[document.main.departure.selectedIndex].value);
}
// Function to set the top left hand city dropdown
function doDefaults(departureCity,arriveCity)
{

 selectFirstCity(document.main.departure,departureCity);
 
 doTopRight();
 
 selectFirstCity(document.main.arrival,arriveCity);
 
 doBottomLeft();
  
 addClasses();
 
}

// addClasses
function addClasses()
{

 // get the classes for this route
 // find the outbound city
 var outboundDeparture = getSelectedCity(document.main.departure);
 var outboundArrival = getSelectedCity(document.main.arrival);
 
 var outboundClasses = getRouteClasses(outboundDeparture, outboundArrival);
 
 // set the outbound classes
 setClasses(document.main.classType, outboundClasses);
 
 // if we can't mix cabins, set the inbound cabin to be the 
 // same as the outbound
 if (canMixCabins(outboundDeparture, outboundArrival))
 {
  var inboundDeparture = getSelectedCity(document.main.departureReturn);
  var inboundArrival = getSelectedCity(document.main.arrivalReturn);
  var inboundClasses = getRouteClasses(inboundArrival, inboundDeparture);
  if (!isOneWay)
  {
   setClasses(document.main.classTypeReturn, inboundClasses);
  }
 }
 else
 {  
  // copy the outbound class
  copyOutboundClass();
 }
}

// copies the outbound class to the inbound drop down
function copyOutboundClass()
{ 
 if (!isOneWay)
 {
  clearOptions(document.main.classTypeReturn);
  document.main.classTypeReturn.options[0] = new Option(document.main.classType.options[document.main.classType.selectedIndex].text, 
                 document.main.classType.options[document.main.classType.selectedIndex].value);
 }
}


function getCabinRank(cab)
{
 
 // economy lowest = 11
 // economy flex = 10
 // economy = 9
 // prem economy lowest = 8
 // prem economy flex = 7
 // prem economy = 6
 // business class = 5
 // upper lowest = 4
 // upper flex = 3
 // upper = 2
 // first class = 1
 if (cab.indexOf("First") > -1) return 1;
 if (cab.indexOf("Upper") > -1)
 {
  if (cab.indexOf("Lowest") > -1)
  {
   return 4;
  }
  else if (cab.indexOf("Flex") > -1)
  {
   return 3;
  }
  else
  {
   return 2;
  }
 }
 if (cab.indexOf("Business")>-1) return 5;
 if (cab.indexOf("Premium") > -1)
 {
  if (cab.indexOf("Lowest") > -1)
  {
   return 8;
  }
  else if (cab.indexOf("Flex") > -1)
  {
   return 7;
  }
  else
  {
   return 6;
  }
 }
 else
 {
  // these are just economy flights... 
  if (cab.indexOf("Lowest") > -1)
  {
   return 11;
  }
  else if (cab.indexOf("Flex") > -1)
  {
   return 10;
  }
  else
  {
   return 9;
  }
 }
}



// set classes in the dropdown
function setClasses(selectBox, classes)
{

 clearOptions(selectBox);

 var count = 0;
 var cn="";
 var psLevel=0;
 // now add the relevant classes
 for (var i = 0; i < classes.length; i++)
 {
  for (var j = 0; j < cabins.length; j++)
  {
   if (cabins[j].id == classes[i])
   {
    cn=cabins[j].name;
    var tempOpt=new Option(cn, cabins[j].id);
    if (getCabinRank(cn) > psLevel)
    {
     psLevel = getCabinRank(cn);
     tempOpt.selected=true;
    }
    selectBox.options[count++]=tempOpt; 
    break;
   }
  }
 } 
}

// gets the route classes for this city pair
function getRouteClasses(departureCode, arrivalCode)
{
 // get the route
 var route = getRoute(departureCode, arrivalCode);
 
 // now get the classes and return them as an array
 var classes = new Array();
 
 var index = route.indexOf("|");
 var tmp = route.substring(index + 1);
 index = tmp.indexOf("|");
 tmp = tmp.substring(index + 1);;
 
 var count = 0;
 
 var isMalaysian = false;
 
 while (tmp.indexOf("-") != -1)
 {
  var cabinId = tmp.substring(0, tmp.indexOf("-"));
  
  // check to see if this was a malasian flight (see ordering the classes below)
  if (cabinId == 7 || cabinId == 8) isMalaysian = true;
  
  classes[count++] = cabinId;  
  tmp = tmp.substring(tmp.indexOf("-") + 1);
 }
 
 //We have to order these either as Economy(6), Premium Economy(4), Upper Class(2) or 
 //Economy(6), Business Class(7), First Class(8) which is either ascending or descending
 //order 
 if (isMalaysian)
 {
  classes = classes.sort(function(a, b) { return a - b; });
  
  //We have to swap around 7 and 8 if they are both here
  if (classes.length == 3)
  {
   classes[1] = 8;
   classes[2] = 7;
  }
  else if (classes.length == 2)
  {
   // check that it contains both 7 and 8 not just economy and a n other
   if (classes[0] == 7 && classes[1] == 8)
   {
    classes[0] = 8;
    classes[1] = 7;    
   }
  }
 }
 else
 {
  classes = classes.sort(function(a, b) { return b - a; });
 }
 
 return classes;
}

// reorders an array either by ascending or descending order
function reOrderArray(arr, ascending)
{
 var toReturn = new Array();
 
  
 
}

// gets the route for this city pair
function getRoute(departureCode, arrivalCode)
{
 var routeDepartureCode;
 var routeArrivalCode;
 for (var i = 0; i < ourRoutes.length; i++)
 {
  routeDepartureCode = getRouteDepartureCode(ourRoutes[i]);
  routeArrivalCode = getRouteArrivalCode(ourRoutes[i]);
  if (routeDepartureCode == departureCode && routeArrivalCode == arrivalCode)
  {
   return ourRoutes[i];
  }
 }
}

// changes the class of the returning route to departure route if the cabin CAN'T be mixed
function changeClasses() 
{
 // find the outbound city
 var outboundDeparture = getSelectedCity(document.main.departure);
 var outboundArrival = getSelectedCity(document.main.arrival);
 // if we can't mix cabins, set the inbound cabin to be the 
 // same as the outbound
 
 if (! canMixCabins(outboundDeparture, outboundArrival) && !isOneWay) {
  
  document.main.classTypeReturn.options[0] = new Option(document.main.classType.options[document.main.classType.selectedIndex].text, document.main.classType.options[document.main.classType.selectedIndex].value); 
//  document.main.classTypeReturn.options[0].selected = true;
                
 }
 
}
// tell us whether you can mix cabins on this route or not
function canMixCabins(departureCode, arrivalCode)
{
 var route = getRoute(departureCode, arrivalCode); 
 return (route.substring(route.length - 1, route.length) == 1);
}

// gets the city code selected in the list box
function getSelectedCity(selectBox)
{
 return selectBox.options[selectBox.selectedIndex].value;
}

// Searches for a match within a city drop down
function selectCity(selectVar,cityName)
{ 
 for(var loop=0;loop<selectVar.options.length;loop++)
 {
  if(selectVar.options[loop].value==cityName)
  {
   selectVar.options[loop].selected = true;
  }
 }
}

// Searches for the first match within a city drop down
function selectFirstCity(selectVar,cityName)
{ 
  for(var loop=0;loop<selectVar.options.length;loop++)
  {
    if(selectVar.options[loop].value.indexOf(cityName) > -1)
    {
      selectVar.options[loop].selected = true;
      break;
    }
  }
}

//Main routes call
function doRoutes(selectVar, cityId, isFrom)
{
 var tempCityNames = new Array();

 clearOptions(selectVar);
 var count2 = 0;
 for (var loop=0; loop < ourRoutes.length; loop++) {

  var departureCode = getRouteDepartureCode(ourRoutes[loop]);
  var arrivalCode = getRouteArrivalCode(ourRoutes[loop]);
  
  if (isFrom == 1 && departureCode == cityId)
  {
   selectVar.options[count2] = new Option(cityNames[arrivalCode],arrivalCode);
   count2++;
  }
  else if(isFrom == 0 && arrivalCode == cityId)
  {
   selectVar.options[count2] = new Option(cityNames[departureCode],departureCode);
   count2++;
  }
 }
 
 if(selectVar.selectedIndex>=selectVar.options.length||selectVar.selectedIndex<0){
  selectVar.options[0].selected = true;
 } 
}

function getRouteDepartureCode(route) {
  return route.substring(0, route.indexOf('|'));
}

function getRouteArrivalCode(route) {
  var tmpVal = route.substring(route.indexOf('|') + 1);
  return tmpVal.substring(0, tmpVal.indexOf('|'));
}

// Clears the select box
function clearOptions(theSelect)
{ 
 if (theSelect.type=='select-one')
 {
  while(theSelect.length>0)
  {
   theSelect.options[0]=null;
  }
 } 
}

// -- Classes 
// represents a cabin
function CabinDetails(id, nm)
{
 this.id = id;
 this.name = nm;
}

function debugJS()
{
 f = document.forms[0];
 outClass=f.classType.options[ f.classType.selectedIndex ];

 //alert( alert(outClass.value + " --- " + outClass.name);
}





