/**
 * DynamicDropDown class
 * ---------------------
 * TAKEN FROM dynamic_dropdown.js just chopped out the COUNTRIES part
 * Allows users to select from a DATA drop down and then
 * populates the dependendt airport drop downs depending
 * on the values of the 1st and the lookup file stored in the DATA JSON array
 */
var DynamicDropDown = { 

  // The ids of our drop downs
  citySelectId: 'selectCity',
  airportSelectId: 'selectAirport',
		
  // The lookup of text to display as default select options
  TITLES:{
    fr:{
      city:new Option('S\351lectionnez une ville'),
      air:new Option('S\351lectionnez un a\351roport')
    },
    en:{
      city:new Option('Select a city'),
      air:new Option('Select an airport')
    }
  },

  DATA: {}, // The main DATA object

  CITIES:[],    // All our cities (need this when we reset the dropdowns)
  AIRPORTS:[],  // All our airports
  
  // set the city and airport drop downs
  setDropDowns : function(frm, sel, langCode) {

    var citySel = document.getElementById(this.citySelectId);        
    var airSel = document.getElementById(this.airportSelectId);

    // Get the selected loc_id from main form (and check if it is in our list (DATA)
    var locID = frm[sel].options[frm[sel].selectedIndex].value;
    if (this.DATA[locID]) {

      //reset city and airport selects
      
    	
      airSel.options.length = 0;
      airSel.options[0] = this.TITLES[langCode].air;

      // airports
      var newAirports = this.DATA[locID] ? this.DATA[locID].airports : null;
      if (newAirports) {
    	var air_opts = [];
        for (i=0; i<newAirports.length; i++) {
          air_opts[air_opts.length] = new Option(newAirports[i].name[langCode], newAirports[i].id);
        }
        air_opts.sort(this.compareOpts);
        for (i=0; i<air_opts.length; i++) airSel.options[airSel.length] = air_opts[i];
      }     
    }else { // no locID - reset the drop downs (ensuring that we set the 0th element to title stuff)
      citySel.options.length = 0;
      citySel.options[0] = this.TITLES[langCode].city;
      for (var i=0; i<this.CITIES.length; i++) {
        citySel.options[citySel.length] = this.CITIES[i];
      }
      airSel.options.length = 0;
      airSel.options[0] = this.TITLES[langCode].air;
      for (i=0; i<this.AIRPORTS.length; i++) {
        airSel.options[airSel.length] = this.AIRPORTS[i];
      }
    }    
  },
  
  // Load the initial data - set the city and airport dropdowns from the supplied data
  load : function(data) {

    this.DATA = data.cities;
    var lang = data.lang;
    
    var citySel = document.getElementById(this.citySelectId);
    var airSel = document.getElementById(this.airportSelectId);

    // Initialise the CITIES and AIRPORTS arrays with the default language text
    citySel.options[0] = this.TITLES[lang].city;
    airSel.options[0] = this.TITLES[lang].air;

  	// Load up our data
  	for (var loc_id in this.DATA) {
  	  var city = this.DATA[loc_id];
  	  this.CITIES[this.CITIES.length] = new Option(city.name[lang], loc_id)
  	  for (var air in city.airports) {
  	    var a = city.airports[air];
  	    if (a) {
  	  	    var a_opt = new Option(a.name[lang], a.id);
  	  	    this.AIRPORTS[this.AIRPORTS.length] = a_opt;
  	    }
  	  }
  	}
  	
  	// Now sort them
  	this.CITIES.sort(this.compareOpts);
  	this.AIRPORTS.sort(this.compareOpts);
  	
  	// And load the sorted values into our drop downs
  	for (var i=0; i<this.CITIES.length; i++) citySel.options[citySel.length] = this.CITIES[i];  	  
  	for (i=0; i<this.AIRPORTS.length; i++) airSel.options[airSel.length] = this.AIRPORTS[i];  	  
  	
  },
  // navigate to the specified page
  goPage : function(frm, sel, fx) {
  	var idx = frm[sel].selectedIndex;
  	// ignore the default - 1st item
  	if (idx > 0) {
      var goID = frm[sel].options[idx].value;
	  if (goID != null && goID != '') {
	    var goURL = 'index.htm?fx=' + fx + '&loc_id='+ goID;
	    document.location.href = goURL;
	  }
  	}
  },
  // comparator for Options
  compareOpts : function(opt1, opt2) {
  	if (opt1.text > opt2.text) {
  		return 1;
  	} else if (opt1.text < opt2.text) {
  		return -1;
  	} else {
  		return 0;
  	}
  }  
};
