/*
** Functions can also act as structured storage classes. The 'function' recRaceClasses can be
** used for storing data by creating multiple instances of it later.
*/
function recRaceClasses(strRaceName, arrClassList) {
  var strRaceName; var arrClassList;
  this.strRaceName = strRaceName; this.arrClassList = arrClassList;
}

/*
** Create the Race/Class cross reference by creating multiple instances of 'recRaceClasses' and
** arranging them in an array called arrRaceClasses.
*/
var arrRaceClasses = new Array(
  new recRaceClasses('Blood Elf', new Array('Death Knight', 'Hunter', 'Mage', 'Paladin', 'Rogue', 'Priest', 'Warlock')),
  new recRaceClasses('Draenei', new Array('Death Knight', 'Hunter', 'Mage', 'Paladin', 'Priest', 'Shaman', 'Warrior')),
  new recRaceClasses('Dwarf', new Array('Death Knight', 'Hunter', 'Paladin', 'Priest', 'Rogue', 'Warrior')),
  new recRaceClasses('Gnome', new Array('Death Knight', 'Mage', 'Rogue', 'Warlock', 'Warrior')),
  new recRaceClasses('Human', new Array('Death Knight', 'Mage', 'Paladin', 'Priest', 'Rogue', 'Warlock', 'Warrior')),
  new recRaceClasses('Night Elf', new Array('Death Knight', 'Druid', 'Hunter', 'Priest', 'Rogue', 'Warrior')),
  new recRaceClasses('Orc', new Array('Death Knight', 'Hunter', 'Rogue', 'Shaman', 'Warlock', 'Warrior')),
  new recRaceClasses('Tauren', new Array('Death Knight', 'Druid', 'Hunter', 'Shaman', 'Warrior')),
  new recRaceClasses('Forsaken', new Array('Death Knight', 'Mage', 'Priest', 'Rogue', 'Warlock', 'Warrior')),
  new recRaceClasses('Troll', new Array('Death Knight', 'Hunter', 'Mage', 'Priest', 'Rogue', 'Shaman', 'Warrior'))
);

/*
** Function fnFillRaceControl(<race html list object>)
** ---------------------------------------------------
** This function clears all the options from the list box passed by the 'lstRace' parameter
** and re-populates it with the races defined in the arrRaceClasses array.
*/
function fnFillRaceControl(lstRace) {
  /* Remove all current options from the drop-down list. */
  lstRace.options.length = 0;

  /* Loop through the arrRaceClasses array, picking out the race name and insert it as a option in the list. */
  for(var intLoop = 0; intLoop < arrRaceClasses.length; intLoop++) {
    lstRace.options[intLoop] = new Option(arrRaceClasses[intLoop].strRaceName, arrRaceClasses[intLoop].strRaceName);
  }
}

/*
** Function fnFillClassControl(<race html list object>, <class html list object>)
** ------------------------------------------------------------------------------
** This function accepts the HTML list controls for both the Race and Class drop-down lists.
** It clears the current Class options before re-populating it with the classes specified
** by the race currently selected in the Race HTML control.
*/

function fnFillClassControl(lstRaces, lstClasses) {
  /* Get the current race selected in the lstRaces control */
  var strCurrentRace = lstRaces.value;

  /* Define a variable to hold the class names for speed and ease of reading */
  var strClass = "";
  
  /* Remove all the current options in the Class html control */
  lstClasses.options.length = 0;

  /* Start searching the the arrRaceClasses array for the selected race */
  for(var intRaceLoop = 0; intRaceLoop < arrRaceClasses.length; intRaceLoop++) {
    /* Check to see if the race entry has been found in the array */
    if(arrRaceClasses[intRaceLoop].strRaceName == strCurrentRace) {
      /* Race entry found, so start looping through the races class list */
      for(var intLoop = 0; intLoop < arrRaceClasses[intRaceLoop].arrClassList.length; intLoop++) {
        /* Fetch the class name and insert it as an option in the Class list HTML control */
        strClass = arrRaceClasses[intRaceLoop].arrClassList[intLoop];
        lstClasses.options[lstClasses.options.length] = new Option(strClass, strClass);
      }
      /* Race has been processed, so set the intRaceLoop to the upper count limit to break out of the loop cleanly. */
      intRaceLoop = arrRaceClasses.length;
    }
  }
}
