// this variable is populated when the page is loaded
// it is used to determin the difference between a user 
// clicking the map and the map-manipulation DURING pageload
var cfmLoaded;

// This function does the briliant job of adding code to the 
// onload event without overwriting existing code
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
	    oldonload();
      }
      func();
    }
  }
}
// This ads some code to the onload event with the help of the function above
addLoadEvent(function() {
  // set the globally declared variable to 'complete' so that it can be determined later
  cfmLoaded = 'complete';
});


function changeContinent(dirid)
{
 var a = getElement('CountrySelector');
 var id = 0;
 for(var id=0; id<countries.length; id++)
	if(countries[id].id==dirid)
		break;
		
 for(var i=0; i<countries[id].countries.length; i++)
 {
  //var ai = a.selectedIndex;
  a.length = countries[id].countries.length;
  a[i].text = countries[id].countries[i].title; 
  a[i].value = countries[id].countries[i].id;
  //a[i].setAttribute('onclick', 'javascript:changeCountry()'); 
 }
 a.selectedIndex = 0;
 getElement('WorldMap').src = 'im/WorldMap_' + id + '.png';
 getElement('ContinentSelector').selectedIndex = id;

  if (cfmLoaded == 'complete') 
  {
    document.getElementById('CountrySelector').selectedIndex++;
    countryChanged();
  }
}

function continentChanged()
{
 var b = getElement('ContinentSelector');
 var bi = b.selectedIndex;
 b.options[bi].value
 var a = getElement('CountrySelector');
 for(var i=0; i<countries[bi].countries.length; i++)
 {
  //var ai = a.selectedIndex;
  a.length = countries[bi].countries.length;
  a[i].text = countries[bi].countries[i].title; 
  a[i].value = countries[bi].countries[i].id; 
  //a[i].setAttribute('onclick', 'javascript:changeCountry()'); 
 }
 a.selectedIndex = 0;
 getElement('WorldMap').src = 'im/WorldMap_' + bi + '.png';
}

function countryChanged()
{
 var b = getElement('ContinentSelector');
 var bi = b.selectedIndex;
 var a = getElement('CountrySelector');
 var ai = a.selectedIndex;
 var baseURL = '';
 if(aid != null) baseURL += 'aid=' + aid;
 if(did != null) {if(baseURL.length > 0) baseURL +='&'; baseURL += 'did=' + did;}
 if(bi>=0 && ai>0)
  document.location = '?' + baseURL + '&country=' + a.options[ai].value;
}
