/**
 * DynamicDropDown class
 * ---------------------
 * Allows users to select from a DATA drop down and then
 * populates the dependendt city and 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
  countrySelectId: 'selectCountry',		
  citySelectId: 'selectCity',
  airportSelectId: 'selectAirport',
		
  // The lookup of text to display as default select options
  TITLES:{
    fr:{
	  cnt:new Option('S\351lectionnez un pays'),
      city:new Option('S\351lectionnez une ville'),
      air:new Option('S\351lectionnez un a\351roport')
    },
    en:{
      cnt:new Option('Select a country'),
      city:new Option('Select a city'),
      air:new Option('Select an airport')
    }
  },

  DATA: {}, // The main DATA object

  COUNTRIES:[], // All our countries as an array of Options
  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
      citySel.options.length = 0;
      citySel.options[0] = this.TITLES[langCode].city;
    	
      airSel.options.length = 0;
      airSel.options[0] = this.TITLES[langCode].air;

      // cities
      var newCities = this.DATA[locID] ? this.DATA[locID].cities : null;
      if (newCities) {
    	var city_options = [];   
        for (var i=0; i<newCities.length; i++) {
          city_options[city_options.length] = new Option(newCities[i].name[langCode], newCities[i].id);
        }
    	city_options.sort(this.compareOpts);
    	for (i=0; i<city_options.length; i++) citySel.options[citySel.length] = city_options[i];

      }      
      // 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 country, city and airport dropdowns from the supplied data
  load : function(data) {

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

    // Initialise the COUNTRIES, CITIES and AIRPORTS arrays with the default language text
    countrySel.options[0] = this.TITLES[lang].cnt;
    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 country = this.DATA[loc_id];
  	  this.COUNTRIES[this.COUNTRIES.length] = new Option(country.name[lang], loc_id)
  	  for (var city in country.cities) {
  	    var c = country.cities[city];
  	    if (c) {
  	  	    var c_opt = new Option(c.name[lang], c.id);
  	  	    this.CITIES[this.CITIES.length] = c_opt;
  	    }
  	  }
  	  for (var air in country.airports) {
    	var a = country.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.COUNTRIES.sort(this.compareOpts);
  	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.COUNTRIES.length; i++) countrySel.options[countrySel.length] = this.COUNTRIES[i];  	  
  	for (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;
  	}
  }  
};
