/*
 Chooser functions for GCL classification
 Created:  2002-03-23
 Issued:   2002-03-23
 Modified: 2002-04-29
 Debugged for:
  Internet Explorer 5.5 and 6.0 for Windows
  Mozilla 0.9.3 for Windows
  Netscape Navigator 3.01 and 4.73 for Windows
  Opera 6.0 for Windows
*/
// Counter for classifications added
//////////////////////////////////////////////////////
var addIndex = 0;
// Store for overall selection status
//////////////////////////////////////////////////////
var choice = new selectionLog();
// Constructor for selection log
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// [index] = selection level
// level   = current level
//////////////////////////////////////////////////////
function selectionLog(){
  // Corrected 2002-04-29, must allow up to maxlevels.
  // The choice log is a six dimensional array!
  for(i=0; i<=maxlevels; i++){
    this[i] = new selection();
  }
  this.level = 0;
  return (this);
}
// Constructor for each selection
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// termIndex = GCL array index
// GCLRef    = GCL array reference
//////////////////////////////////////////////////////
function selection(){
  this.termIndex = 0;
  this.GCLRef = new Object();
  return (this);
}
// Base selection reference is the GCL array
//////////////////////////////////////////////////////
if(GCL){
  choice[0].GCLRef = GCL;
}
// Shorthand reference to the chooser form
//////////////////////////////////////////////////////
var chooserApp;
if((document.forms)&&(document.forms.Chooser)){
  chooserApp = document.forms.Chooser;
}
function updateSelections(levelIndex){
  // Check the level is within range and the selector exists
  if((levelIndex > 0)&&(levelIndex <= maxlevels)&&(chooserApp.elements[(levelIndex-1)])){
    if(chooserApp.elements[(levelIndex-1)].selectedIndex>0){
      // Store the current working level
      choice.level = levelIndex;
      // Store the selection index    
      choice[choice.level].termIndex = chooserApp.elements[(levelIndex-1)].selectedIndex;
      // Store the GCL reference -- concatenates GCL references
      // First extend the GCL reference to avoid implicit array creation
      var newGCLRef = choice[choice.level-1].GCLRef[choice[choice.level].termIndex];
      // Then assign it to the current index
      choice[choice.level].GCLRef = newGCLRef;
      // Check if any sub categories exist
      if(choice[choice.level].GCLRef.size > 0){
        // Check the next selector exists
        if(chooserApp.elements[levelIndex]){
          // Clear any previous options
          chooserApp.elements[levelIndex].options.length = 0;
          // Replace first option text
          chooserApp.elements[levelIndex].options[0] = new Option('Please select here',0,true);
          chooserApp.elements[levelIndex].options[0].selected = true;
          // Fill the next level selector with relevant options
          for(j=1; j<=choice[choice.level].GCLRef.size; j++){
            chooserApp.elements[choice.level].options[j] = new Option(choice[choice.level].GCLRef[j].term,j);
          }
        }
        // Reset all subsequent selectors
        for(k=(choice.level+1); k < maxlevels; k++){
          // Clear any previous options
          chooserApp.elements[k].options.length = 0;
          // Replace first option text with "Empty: Choose from the nth selector"
          if(choice.level==1){
            chooserApp.elements[k].options[0] = new Option('Empty: Choose from selector two',0,true);
          }
          if(choice.level==2){
            chooserApp.elements[k].options[0] = new Option('Empty: Choose from selector three',0,true);
          }
          if(choice.level==3){
            chooserApp.elements[k].options[0] = new Option('Empty: Choose from selector four',0,true);
          }
          chooserApp.elements[k].options[0].selected = true;
        }
      }
      else{// No further levels exist
        for(j=levelIndex; j<maxlevels; j++){
          // Clear any previous options
          chooserApp.elements[j].options.length = 0;
          // Replace first option text
          chooserApp.elements[j].options[0] = new Option('No further categories exist',0,true);
          chooserApp.elements[j].options[0].selected = true;
        }
      }
    }
    else{// Selected the first item
      // Do nothing
    }
  }
}
// Add selections to main form's Category: field
//////////////////////////////////////////////////////
function addClassification(){
  var categoryText = '';
  // Classifications are separated by a semi-colon
  if(addIndex > 0) categoryText += ';\n';
  if(choice.level>=1) categoryText += GCL[choice[1].termIndex].term;
  // GCL levels are separated by a forward slash
  if(choice.level>=2) categoryText += '/ ' + GCL[choice[1].termIndex][choice[2].termIndex].term;
  if(choice.level>=3) categoryText += '/ ' + GCL[choice[1].termIndex][choice[2].termIndex][choice[3].termIndex].term;
  if(choice.level>=4) categoryText += '/ ' + GCL[choice[1].termIndex][choice[2].termIndex][choice[3].termIndex][choice[4].termIndex].term;
  if(choice.level>=5) categoryText += '/ ' + GCL[choice[1].termIndex][choice[2].termIndex][choice[3].termIndex][choice[4].termIndex][choice[5].termIndex].term;
  if(addIndex<1){
    document.forms.Record['Category'].value = categoryText;
  }
  else{
    // Concatenate the Category field
    document.forms.Record['Category'].value += categoryText;
  }
  // Increment the classification counter
  addIndex++;
}
// Reset chooserApp and variables
//////////////////////////////////////////////////////
function clearHistory(){
  // Reset global variables
  choice.level = 0;
  // Reset secondary selectors
  for(i=1; i<maxlevels; i++){
    // Clear any previous options
    chooserApp.elements[i].options.length = 0;
    // Replace first option text and select it
    chooserApp.elements[i].options[0] = new Option('Empty: Choose from the first selector',0);
    chooserApp.elements[i].options[0].selected = true;
  }
}