

// BEGIN WCT LMD JS
// $Id: JSProcessor.pm,v 1.15 2004/05/27 13:31:35 stephens Exp $

function do_array_intersect(a, b) {
        if ( ! (a && b) ) {
                return new Array();
        }

        if (a.length < b.length) {
                return array_intersect(a, b);
        } else {
                return array_intersect(b, a);
        }
}


function wct_lmd_Array_indexOf(a, x)
{
  for ( var i = 0; i < a.length; ++ i ) {
    if ( a[i] == x ) return i;
  }
  return -1;
}


var _wct_lmd_in_origination_key;
var _wct_lmd_preferred_dests;
var _wct_lmd_no_preferred_dests;


function wct_lmd_prefer_dests(preferred_dests)
{
  var n, nt, diff;

  _wct_lmd_preferred_dests = preferred_dests;

  // Filter destinations.
  var new_destinations = new Object();
  n = nt = diff = 0;
  for ( de in destinations ) {
    ++ nt;
    if ( wct_lmd_Array_indexOf(preferred_dests, de) >= 0 ) {
      new_destinations[de] = destinations[de];
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " destinations");

  // Filter out cities_by_orig
  var new_cities_by_orig = new Object();
  n = nt = diff = 0;
  for ( orig in originations ) {
    ++ nt;
    var orig_c = cities_by_orig[orig];
    orig_c = do_array_intersect(orig_c, preferred_dests);

    // diff += orig_c.length - cities_by_orig[orig].length;

    // If the orig has no dests that match...
    if ( orig_c.length ) {
      new_cities_by_orig[orig] = orig_c;
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " cities_by_orig");
 
  // Filter states_by_orig.
  var new_states_by_orig = new Object();
  n = nt = diff = 0;
  for ( orig in states_by_orig ) {
    ++ nt;
    // Get the filtered destinations for the orig,
    // Use this list to filter the states_by_orig.
    var orig_c = new_cities_by_orig[orig];

    var old_orig_state = states_by_orig[orig];
    var orig_s = new Array();

    // Loop through the orig's states.
    for ( var i = 0; i < old_orig_state.length; ++ i ) {
      var state_code = old_orig_state[i];
      var state_cities = states[state_code].cities;
      state_cities = do_array_intersect(state_cities, orig_c);

      // defaultStatus = ("Orig " + orig + ": state " + state_code + " before: " + states[state_code].cities.length + " after: " + state_cities.length);
      if ( state_cities.length > 0 ) {
        // The state has at least one dest we are interested in.
        orig_s.push(state_code);
      } else {
        // The state has no matching dest cities anymore.
	// defaultStatus = ("Orig " + orig + ": deleted state " + state_code);
      }
    }

    // diff += orig_s.length - states_by_orig[orig].length;
    if ( orig_s.length ) {
      new_states_by_orig[orig] = orig_s;
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " states_by_orig");

  // Filter out originations that have no
  // destinations.
  n = nt = diff = 0;
  var new_originations = new Object();
  for ( orig in originations ) {
    ++ nt;
    var orig_c = new_cities_by_orig[orig];

    if ( orig_c && orig_c.length ) {
      new_originations[orig] = originations[orig];
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " originations[]");

  
  // If there is at least one matching, orignation
  // use the filtered data.
  if ( (_wct_lmd_no_preferred_dests = n > 0) ) {
    originations   = new_originations;
    destinations   = new_destinations;
    cities_by_orig = new_cities_by_orig;
    states_by_orig = new_states_by_orig;
  }
}


function wct_lmd_update_state(state_codes)
{
  var state = document.dest.in_destform_state;
  var i, j;

  if ( state != null ) {
    state.options.length = i = 0;
    if ( state_codes.length > 1 || ! _wct_lmd_preferred_dests ) {
      state.options[i ++] = new Option('Choose a state or region', 'all');
    }

    for ( j = 0; j < state_codes.length; j++ ) {
      var s = state_codes[j]; 
      state.options[i ++] = new Option(states[s].name, s);
    }

    // Default if only one state matches.
    if ( state_codes.length == 1 ) {
      state.selectedIndex = i - 1;
      wct_lmd_update_city(state_codes[0]);
    } else {
      wct_lmd_update_city();
    }
  }
}


function wct_lmd_update_city(this_state)
{
  var city = document.dest.in_location_key;
  var cities;
  var i, j;

  if ( this_state ) {
    var orig = document.dest.in_origination_key;
    var this_orig  = orig.options[orig.selectedIndex].value;
    cities = do_array_intersect(cities_by_orig[this_orig],
                                        states[this_state].cities);
  } else {
    cities = new Array();
  }

  city.options.length = i = 0;
  if ( (! _wct_lmd_preferred_dests) || cities.length > 1 ) {
    city.options[i ++] = new Option('Choose a city', 'all');
  }
  if ( (! _wct_lmd_preferred_dests) || cities.length == 0 ) {
    city.options[i ++] = new Option('Show all cities', 'all');
  }

  for (j = 0; j < cities.length; j++) {
    var k = cities[j];
    city.options[i ++] = new Option(destinations[k], k);
  }

  // Default if only one city matches.
  if ( cities.length == 1 ) {
    city.selectedIndex = i - 1;
  }
}


function init_destinations(o_key) {
  var state_codes = states_by_orig[o_key];
  wct_lmd_update_state(state_codes);
}



function change_state() {
  var orig = document.dest.in_origination_key;
  var state = document.dest.in_destform_state;

  var this_orig  = orig.options[orig.selectedIndex].value;
  var this_state = state.options[state.selectedIndex].value;

  wct_lmd_update_city(this_state);
}


/* 
 * takes document.dest.in_origination_key.selectedIndex as arg
 */
function change_origination(o_idx) {
  var state = document.dest.in_destform_state;
  var o_key = document.dest.in_origination_key.options[o_idx].value;
  var state_codes = states_by_orig[o_key];

  _wct_lmd_in_origination_key = o_key;

  wct_lmd_update_state(state_codes);
}


function init_originations() {
  var orig = document.dest.in_origination_key;
  var i = 0;
  var o_key = wct_lmd_in_origination_key();

  for ( o in originations ) {
    orig.options[i] = new Option(originations[o], o); 
    if ( o == o_key ) {
      orig.selectedIndex = i;
    }
    i++;
  }

  init_destinations(o_key);
}


function wct_lmd_in_origination_key() {
  if ( ! _wct_lmd_in_origination_key ) {
    if ( document.dest != null ) {
      // Try getting it from the Select widget?
      var orig = document.dest.in_origination_key;
      if ( orig.selectedIndex >= 0 ) {
        _wct_lmd_in_origination_key = orig.options[orig.selectIndex].value;
      }
    }
  }

  if ( ! _wct_lmd_in_origination_key ) {
    // Try getting it from the document URL?
    _wct_lmd_in_origination_key = document.location.href.match(/[?;&]in_origination_key=(\d+)/);
    _wct_lmd_in_origination_key = _wct_lmd_in_origination_key != null 
      ? _wct_lmd_in_origination_key[1] : 371; // "371" is magic for NYC.
  }

  return _wct_lmd_in_origination_key;
}


function wct_lmd_in_origination_key_changed()
{
  var p = document.location.href;
  var f = document.dest.in_origination_key;
  var v = f[f.selectedIndex].value;
  var r;

  if (p.indexOf('in_origination_key') >= 0) {
    r = p.replace(/in_origination_key=\d+/, 
		  'in_origination_key=' + v);
  }
  else if (p.indexOf('?') >= 0) {
    r = p + '&in_origination_key=' + v;
  }
  else {
    r = p + '?in_origination_key=' + v;
  }

  document.location = r;
}


// END WCT LMD JS


