// --------------------------------------------------------------------------------------------------------------
// Help routines for handling the Selection functionality.
// --------------------------------------------------------------------------------------------------------------
var selectionLMArr = new Array() ;

// --------------------------------------------------------------------------------------------------------------
// Functions for maintaining the array for selected LM.

// Add or delete a LM from the Selection array, depending on the state of a passed checkbox field.
function SelectionCheckLM(fld, LMID, WMENCPID)
{
  SelectionMaintainList(fld.checked, LMID, WMENCPID) ;
}
// Add a LM to the Selction array, regardsless of checked state of a checkbox.
function SelectionAddLM(LMID, WMENCPID) 
{
  SelectionMaintainList(true, LMID, WMENCPID) ;
}
// Delete a LM from the Selction array, regardsless of checked state of a checkbox.
function SelectionDeleteLM(LMID, WMENCPID)
{
  SelectionMaintainList(false, LMID, WMENCPID) ;
}

function SelectionMaintainList(chkState, LMID, WMENCPID) 
{
  var g ;
  var firstEmpty = -1 ;

  for (g = 0 ; g < selectionLMArr.length ; g++)
  {
    if (selectionLMArr[g] == null)
      if (firstEmpty == -1)
        firstEmpty = g ;
      else
        ;
    else if (selectionLMArr[g].GetLMID() == LMID)
      break ;
  }
  if (chkState == false)
    // Delete if found.
    if (g < selectionLMArr.length)
      selectionLMArr[g] = null ;
    else
      ;
  else
    if (firstEmpty > -1)
      selectionLMArr[firstEmpty] = new SelectionListLM(LMID, WMENCPID) ;
    else
      selectionLMArr[selectionLMArr.length] = new SelectionListLM(LMID, WMENCPID) ;
}


// --------------------------------------------------------------------------------------------------------------
// Function for adding or removing a number of marked LM to/from the user's Selection.
// The function relies on the Selection array to be maintained each time the user
// choses a LM (e.g. a onclick event on a checkbox should call a function for 
// adding the LM to the list - e.g. SelectionCheckLM() or SelectionAddLM()).
// FormName: Form to use for submit.
// DestPage: Name of template to call - ./selection/addtoselection.htm or deletefromselection.htm
// ForceCall: Call regardless of any LM marked in array.
// ServletName: Name of the servlet.
// QuestText: The text to display before redirection to page (see ForceCall).
function SelectionAction(FormName, DestPage, ForceCall, ServletName, QuestText)
{
  var selectionAction ;
  selectionAction = SelectionConstructUpdStr() ;
  if (selectionAction != '')
  {
    var evalString ;
    evalString =
      'document.' + FormName + '.action = \''+ ServletName + 'call?htmltemplate=' + DestPage + '&' ;
    selectionAction = 'selectionlist=' + selectionAction.substring(0, selectionAction.length - 1) ;
    evalString += selectionAction + '\'' ;
    eval(evalString) ;
    evalString = 'document.' + FormName + '.submit()' ;
    eval(evalString) ;
  }
}

// --------------------------------------------------------------------------------------------------------------
// Function for constructing a string of LM id's to add or remove to/from the user's selection.
function SelectionConstructUpdStr()
{
  var g ;
  var UpdAction = new String('') ;

  for (g = 0 ; g < selectionLMArr.length ; g++)
    if (selectionLMArr[g] != null)
    {
      UpdAction += selectionLMArr[g].GetLMID() + ':' +
                   selectionLMArr[g].GetWMENCPID() + ':' ;
    }

  return (UpdAction) ;
}

// --------------------------------------------------------------------------------------------------------------
// Helper object for holding the information necessary for constructing the Selection commands.
function SelectionListLM(LMID, WMENCPID)
{
  if (typeof(selectionListLMDefined) == 'undefined')
    _protoSelectionListLM() ;
  this.Init(LMID, WMENCPID) ;
}
function _protoSelectionListLM()
{
  selectionListLMDefined                  = true ;
  SelectionListLM.prototype.Init          = sllInit ;
  SelectionListLM.prototype.GetLMID       = sllGetLMID ;
  SelectionListLM.prototype.GetWMENCPID   = sllGetWMENCPID ;
}

function sllInit(LMID, WMENCPID) 
{
  this.LMID     = LMID ;
  this.WMENCPID = WMENCPID ;
}
function sllGetLMID()
{
  return (this.LMID) ;
}
function sllGetWMENCPID()
{
  return (this.WMENCPID) ;
}
