////////////////////////////////////////////////////////////////////
// Country class
Region.CLASS_NAME = 'Region';

// Static vars/objs
Region.htObj = new Hashtable();
Region.htRegionCountry = new Hashtable();

// XML constructor
function Region(id, name, continentId, numConids) {
  this.id = id;
  this.name = name;
  this.continentId = continentId;
  this.numConids = numConids;

  Region.htObj.put(this.id, this);
  Continent.addRegion(this.continentId, this.id);
}

// Static method to map countries to regions
Region.addCountry = function(regionId, countryId) {
  var htCountry = Region.htRegionCountry.get(regionId);
  if (htCountry == undefined) {
    htCountry = new Hashtable();
    Region.htRegionCountry.put(regionId, htCountry);
  }
  htCountry.put(countryId, countryId);
}

// Static method to hide the countries of this region
Region.hideCountries = function(regionId) {
  var htCountry = Region.htRegionCountry.get(regionId);
  if (htCountry == undefined) {
    return;
  }

  var tdClk = $('click'+regionId);

  // If the link was already to show countries, then they were not expanded
  var oldTdClk = tdClk.getAttribute("href");
  if (oldTdClk.match("Region.showCountries")) {
    return;
  }

  // Change the onclick event to show the countries
  tdClk.setAttribute("href", "javascript:Region.showCountries('" + regionId +"')");
  var areaSelectTbl = $('areaSelectTbl');

  var tblRow = $('Region'+regionId);
  var rowIndex = tblRow.rowIndex+1;
  for (var k in htCountry.hash) {
    areaSelectTbl.deleteRow(rowIndex);
  }
}

// Static method to show the countries of this region
Region.showCountries = function(regionId) {
  var htCountry = Region.htRegionCountry.get(regionId);
  if (htCountry == undefined) {
    return; 
  } 
    
  // Change the onclick event to hide the countries
  var tdClk = $('click'+regionId);
  tdClk.setAttribute("href", "javascript:Region.hideCountries('" + regionId +"')");
    
  var areaSelectTbl = $('areaSelectTbl');
    
  var tblRow = $('Region'+regionId);
  var rowIndex = tblRow.rowIndex+1;
  var i=0; 
  for (var k in htCountry.hash) {
    var c = Country.htObj.get(k);
    var row = areaSelectTbl.insertRow(rowIndex+i);
    row.setAttribute("id", "Country"+k);
    row.setAttribute("class", "white");
    row.setAttribute("className", "white");
  
    var cell = row.insertCell(0);
    cell.innerHTML = "&nbsp;";
    cell = row.insertCell(1);
    cell.innerHTML = '&nbsp;';
    cell = row.insertCell(2);
    cell.innerHTML = c.name;
    cell = row.insertCell(3);
    cell.innerHTML = c.numConids;
    cell.setAttribute("align", "right");
    cell = row.insertCell(4);
    cell.innerHTML = '<input type="checkbox">';

    i++;
  }
}


