/*
 * Commented out code sets the options back to their defaults.  This
 * ensures that the options are correctly retained when the page is
 * entered via the back button.  Unfortunately, this has the undesirable 
 * effect of allowing a user to select an invalid country.  Since the latter
 * is more undesirable than the former the code has been commented out.
 */

/*
var COUNTRIES;
var COUNTRIES_INDEX;
*/

var ADJOINED_COUNTRIES= new Array();
ADJOINED_COUNTRIES["Austria"]=
  new Array("Germany", "Hungary", "Italy", "Croatia/Slovenia", "Switzerland",
            "Czech Republic");
ADJOINED_COUNTRIES["Benelux"]=
  new Array("France", "Germany", "Ireland");
ADJOINED_COUNTRIES["Denmark"]=
  new Array("Germany", "Norway", "Sweden");
ADJOINED_COUNTRIES["Finland"]=
  new Array("Germany", "Sweden");
ADJOINED_COUNTRIES["France"]=
  new Array("Benelux", "Germany", "Ireland", "Italy", "Spain", "Switzerland");
ADJOINED_COUNTRIES["Germany"]=
  new Array("Austria", "Benelux", "Denmark", "Finland",
            "France", "Sweden", "Switzerland", "Czech Republic");
ADJOINED_COUNTRIES["Greece"]=
  new Array("Italy", "Bulgaria");
ADJOINED_COUNTRIES["Hungary"]=
  new Array("Austria", "Romania", "Croatia/Slovenia", "Bulgaria", "Montenegro/Serbia");
ADJOINED_COUNTRIES["Ireland"]=
  new Array("Benelux", "France");
ADJOINED_COUNTRIES["Italy"]=
  new Array("Austria", "France", "Greece", "Croatia/Slovenia", "Spain", "Switzerland");
ADJOINED_COUNTRIES["Norway"]=
  new Array("Denmark", "Sweden");
ADJOINED_COUNTRIES["Portugal"]=
  new Array("Spain");
ADJOINED_COUNTRIES["Spain"]=
  new Array("France", "Italy", "Portugal");
ADJOINED_COUNTRIES["Sweden"]=
  new Array("Denmark", "Finland", "Germany", "Norway");
ADJOINED_COUNTRIES["Switzerland"]=
  new Array("Austria", "France", "Germany", "Italy");
ADJOINED_COUNTRIES["Romania"]=
  new Array("Hungary", "Bulgaria", "Montenegro/Serbia");
ADJOINED_COUNTRIES["Croatia/Slovenia"]=
  new Array("Austria", "Hungary", "Italy", "Bulgaria", "Montenegro/Serbia");
ADJOINED_COUNTRIES["Bulgaria"]=
  new Array("Greece", "Romania", "Montenegro/Serbia");
ADJOINED_COUNTRIES["Montenegro/Serbia"]=
  new Array("Hungary", "Romania", "Croatia/Slovenia", "Bulgaria");
ADJOINED_COUNTRIES["Czech Republic"]=
  new Array("Austria", "Germany");

/* Given the index [1..N_SELECT_COUNTRIES] of the current selection,
 * set the next country selection to a list of legal countries and all
 * remaining country selections to an empty set of countries.
 */
function setSelects(form, currentSelect) {
  var N_SELECT_COUNTRIES;

  if (form.elements["nCountries"] == undefined) {
    N_SELECT_COUNTRIES = 5;
  }
  else {
    N_SELECT_COUNTRIES = form.elements["nCountries"].value;
  }
  if( currentSelect < N_SELECT_COUNTRIES)
  {
    var selectToFill= currentSelect + 1;

    for (i= selectToFill; i <= N_SELECT_COUNTRIES; i++) { //empty out options.
      form.elements["selCountry" + i].options.length= 2;
      form.elements["selCountry" + i].options[0].selected= true;
    }
    var countries= getCountries(form, selectToFill - 1);
    if (countries.length > 0) fillBox(form, selectToFill, countries);
  }

  /*
  var select= form.elements["selCountry" + currentSelect];
  var selectCountry= select.options[select.selectedIndex].value;
  select.options.length= 2;
  for (var i= 0; i < COUNTRIES.length; i++) {
    var c= COUNTRIES[i];
    select.options[select.options.length]= new Option(c, c);
  }
  var selectedIndex= COUNTRIES_INDEX[selectCountry] + 2;
  for (var i= 0; i < select.options.length; i++) {
    select.options[i].selected= (i == selectedIndex);
  }
  */
}

/* Return the union of the countries adjacent to the selected countries
 * with indexes in [1..lastSelectIndex] minus the selected countries.
 */
function getCountries(form, lastSelectIndex) {

  //var form= document.forms[0];
  var selectedCountries= new Array();
  for (var i= 1; i <= lastSelectIndex; i++) {
    var select= form.elements["selCountry" + i];
    selectedCountries[select.options[select.selectedIndex].value]= 1;
  }
  var countries= new Array();
  for (var i= 1; i <= lastSelectIndex; i++) {
    var select= form.elements["selCountry" + i];

    if (select.selectedIndex < 2) select.selectedIndex = 2;
    var selectCountry= select.options[select.selectedIndex].value;
    var adjCountries= ADJOINED_COUNTRIES[selectCountry];
    for (var j= 0; j < adjCountries.length; j++) {
      var c= adjCountries[j];
      if (!selectedCountries[c]) {
	var found= 0;
	for (var k= 0; k < countries.length; k++) {
	  if (countries[k] == c) { found= 1; break; }
	}
	if (!found) countries[countries.length]= c;
      }
    }
  }
  return countries.sort();
}

function fillBox(form, selectToFill, countries) {
  var select= form.elements["selCountry" + selectToFill];
  select.onchange = function(){ return setSelects(this.form, selectToFill );}
  for (var i= 0; i < countries.length; i++) {
    select.options[select.options.length]=
      new Option(countries[i], countries[i]);
  }
}

function onLoadSetup(formName) {
  /*
  COUNTRIES= new Array();
  for (var c in ADJOINED_COUNTRIES) {
    COUNTRIES[COUNTRIES.length]= c;
  }
  COUNTRIES.sort();
  COUNTRIES_INDEX= new Array();
  for (var i= 0; i < COUNTRIES.length; i++) {
    COUNTRIES_INDEX[COUNTRIES[i]]= i;
  }
  */
  if(formName == '') {
	  formName = 'ProductForm';
  }

  setSelects(document[formName], 1);

  var form = document[formName];
  var nCountries;
//  alert(' con ' + form.elements["nCountries"].value);
  if (form.elements["nCountries"] == undefined) {
    nCountries = 5;
  }
  else {
    nCountries = form.elements["nCountries"].value;
  }

 
  if (nCountries == 3) {
    form.selCountry4.disabled = true;
    form.selCountry5.disabled = true;
  }
  if (nCountries == 4) {
	  form.selCountry4.disabled = false;
    form.selCountry5.disabled = true;
  }

   if (nCountries == 5) {
	  form.selCountry4.disabled = false;
    form.selCountry5.disabled = false;
  }

}

