var SelectedLM = new Array() ;

// Function for updating the item count in the search panel.
function SetItemCount(LMCount)
{
  if (typeof(window.parent.ONTopMenu) != 'undefined')
    if (typeof(window.parent.ONTopMenu.window.SetNewItemCount) != 'undefined')
      window.parent.ONTopMenu.window.SetNewItemCount(LMCount) ;
}

// Generic function for applying changes to the shopping cart. Constructs a
// list of changes that is suitable for the backend functions for updating and
// if this list isn't empty, redirects the browser to an update page by setting
// the action of a passed form and submitting this.
//
// This function relies on the CheckLM to have been called when the user
// checks/unchecks the 'in cart' checkbox displayed to the user (usually in
// a list).
// ForceCall: false = no changes results in no action
//            true  = the destination page is called regardless of changes
//            2     = if no changes, then the user is asked whether to change or not (requires QuestText to be set as well).
function ApplyCartChanges(FormName, DestPage, ForceCall, ServletName, QuestText)
{
  var UpdAction ;

  UpdAction = ConstructUpdString() ;
  if (UpdAction != '')
  {
    var evalString ;
    evalString =
      'document.' + FormName + '.action = \''+ ServletName + 'call?htmltemplate=' + DestPage + '&' ;
    UpdAction = 'UpdateCart=' + UpdAction.substring(0, UpdAction.length - 1) ;
    evalString += UpdAction + '\'' ;
    eval(evalString) ;
    evalString = 'document.' + FormName + '.submit()' ;
    eval(evalString) ;
  }
  else if (ForceCall == true)
  {
    var evalString ;
    evalString =
      'document.' + FormName + '.action = \''+ ServletName + 'call?htmltemplate=' + DestPage + '\'' ;
    eval(evalString) ;
    evalString = 'document.' + FormName + '.submit()' ;
    eval(evalString) ;
  }
  else if (ForceCall == 2 && typeof(QuestText) != 'undefined')
  {
    if (confirm(QuestText) == true)
    {
      var evalString ;
      evalString =
        'document.' + FormName + '.action = \''+ ServletName + 'call?htmltemplate=' + DestPage + '\'' ;
      eval(evalString) ;
      evalString = 'document.' + FormName + '.submit()' ;
      eval(evalString) ;
    }
  }
}

// Generic function for maintaining the SelectedLM array. Can be called when the
// user clicks a LM checkbox to keep the SelectedLM array updated.
function CheckLM(fld, LMID, WM_ENCP_ID, SL_ID, UOT_ID)
{
  var g ;
  for (g = 0 ; g < SelectedLM.length ; g++)
  {
    if (SelectedLM[g].GetFld() == fld)
    {
      SelectedLM[g].UpdData(LMID, fld.checked, WM_ENCP_ID, UOT_ID) ;
      break ;
    }
  }

  if (g == SelectedLM.length)
    SelectedLM[SelectedLM.length] = new Material(fld, LMID, fld.checked, WM_ENCP_ID, SL_ID, UOT_ID) ;
}

// Simple version of CheckLM. Intended to be used when the caller doesn't allow changes to WM_ENCP_ID
// In this case, only the checked/unchecked property can be changed, and the previous state can be
// determined by negating the current state.... this means, that instantiation of an Material object for the
// current (passed) LMID can be postponed until changes occur (which is *not* the case when the user is allowed
// to change the 3 other properties) thus making the administration simpler, by only calling SimpleCheckLM()
// when changes actually occur...:-)
// Note that WM_ENCP_ID must be passed and must be valid....
// This is the function that will be used be the search result functions, as they use default values for the 3
// IDs, making this administration simpler.
// See UpdCheckedState() for an even simpler checkmark function...
function SimpleCheckLM(fld, LMID, WM_ENCP_ID, SL_ID, UOT_ID)
{
  var g ;
  for (g = 0 ; g < SelectedLM.length ; g++)
  {
    if (SelectedLM[g].GetFld() == fld)
    {
      SelectedLM[g].UpdData(LMID, fld.checked, WM_ENCP_ID, UOT_ID) ;
      break ;
    }
  }

  if (g == SelectedLM.length)
  {
    // Create new Material object for this LM_ID, initiate with original values (= !fld.checked) and immediately
    // update with the new status...
    var newMaterial = new Material(fld, LMID, !fld.checked, WM_ENCP_ID, SL_ID, UOT_ID) ;
    newMaterial.UpdData(LMID, fld.checked, WM_ENCP_ID, UOT_ID) ;
    SelectedLM[SelectedLM.length] = newMaterial ;
  }
}

// Simple function for just flipping the state of an already initialized checkbox - i.e. for use if the
// WM_ENCP_ID isn't known for the row, but the caller has ensured that the ID has been set previously, at
// initialization for instance, and now the user has just changed the checkbox state....
// This function will not create an entry in the list of "known" items... as we don't have the WM_ENCP_ID for
// which to create the entry....
function UpdCheckedState(fld, LM_ID, SL_ID)
{
  var g ;
  for (g = 0 ; g < SelectedLM.length ; g++)
  {
    if (SelectedLM[g].GetFld() == fld)
    {
      SelectedLM[g].UpdChkState(fld.checked) ;
      break ;
    }
  }
}

// Function for constructing a string of LM id's that has been updated
// by the user. This string can be passed to the backend
// (to the UPDATESHOPCART function) for updating of the shopping cart.
function ConstructUpdString()
{
  var g ;
  var UpdAction = new String('') ;

  for (g = 0 ; g < SelectedLM.length ; g++)
    if (SelectedLM[g].HasChanged())
    {
      UpdAction += SelectedLM[g].GetLMID() + ':' +
                   SelectedLM[g].GetSLID() +  ':' +
                   SelectedLM[g].GetOrigWMENCPID() + ':' +
                   SelectedLM[g].GetWMENCPID() + ':' +
                   SelectedLM[g].GetOrigUOTID() + ':' +
                   SelectedLM[g].GetUOTID() ;


      if (SelectedLM[g].IsChecked())
        UpdAction += ':1,' ;
      else
        UpdAction += ':0,' ;
    }

  return (UpdAction) ;
}

// Adds a LM to the cart - for use when the caller doesn't use a checkbox to keep the in-box state.
// (e.g. clicking a picture puts the LM in the cart)
function AddLMToCart(fld, LMID, WM_ENCP_ID, SL_ID, UOT_ID, newStatus)
{
  var g ;
  for (g = 0 ; g < SelectedLM.length ; g++)
  {
    if (SelectedLM[g].GetFld() == fld)
    {
      SelectedLM[g].UpdData(LMID, newStatus, WM_ENCP_ID, UOT_ID) ;
      break ;
    }
  }

  if (g == SelectedLM.length)
  {
    // Create new Material object for this LM_ID, initiate with original values (= !fld.checked) and immediately
    // update with the new status...
    var newMaterial = new Material(fld, LMID, !newStatus, WM_ENCP_ID, SL_ID, UOT_ID) ;
    newMaterial.UpdData(LMID, newStatus, WM_ENCP_ID, UOT_ID) ;
    SelectedLM[SelectedLM.length] = newMaterial ;
  }
}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
function Material(fld, LMID, isChecked, WM_ENCP_ID, SL_ID, UOT_ID)
{
  if (typeof(MaterialIsDefined) == 'undefined')
    protoMaterial() ;

  this.Init(fld, LMID, isChecked, WM_ENCP_ID, SL_ID, UOT_ID) ;
}
function protoMaterial()
{
  MaterialIsDefined = true ;
  Material.prototype.Init             = _MInit ;
  Material.prototype.UpdData          = _MUpdData ;
  Material.prototype.IsChecked        = _MIsChecked ;
  Material.prototype.GetFld           = _MGetFld ;
  Material.prototype.GetLMID          = _MGetLMID ;
  Material.prototype.GetWMENCPID      = _MGetWMENCPID ;
  Material.prototype.GetOrigWMENCPID  = _MGetOrigWMENCPID ;
  Material.prototype.GetSLID          = _MGetSLID ;
  Material.prototype.GetOrigSLID      = _MGetOrigSLID ;
  Material.prototype.GetUOTID         = _MGetUOTID ;
  Material.prototype.GetOrigUOTID     = _MGetOrigUOTID ;
  Material.prototype.HasChanged       = _MHasChanged ;
  Material.prototype.UpdChkState      = _MUpdChkState ;
}
function _MInit(fld, LMID, isChecked, WM_ENCP_ID, SL_ID, UOT_ID)
{
  this.fld            = fld ;
  this.LMID           = LMID ;
  this.isChecked      = isChecked ;
  this.WM_ENCP_ID     = WM_ENCP_ID ;
  this.UOT_ID         = UOT_ID ;
  this.SL_ID          = SL_ID ;

  this.origChecked    = this.isChecked ;
  this.origParentLM   = this.LMID ;
  this.origLMID       = this.LMID ;
  this.origWM_ENCP_ID = this.WM_ENCP_ID ;
  this.origUOT_ID     = this.UOT_ID ;
  this.origSL_ID      = this.SL_ID ;
}
function _MUpdData(LMID, isChecked, WM_ENCP_ID, UOT_ID)
{
  this.LMID       = LMID ;
  this.isChecked  = isChecked ;
  this.WM_ENCP_ID = WM_ENCP_ID ;
  this.UOT_ID     = UOT_ID ;
}
function _MUpdChkState(isChecked)
{
  this.isChecked = isChecked ;
}
function _MIsChecked()
{
  return(this.isChecked) ;
}
function _MGetFld()
{
  return(this.fld) ;
}
function _MGetLMID()
{
  return(this.LMID) ;
}
function _MGetWMENCPID()
{
  return(this.WM_ENCP_ID) ;
}
function _MGetSLID()
{
  return(this.SL_ID) ;
}
function _MGetUOTID()
{
  return(this.UOT_ID) ;
}
function _MGetOrigSLID()
{
  return(this.origSL_ID) ;
}
function _MGetOrigWMENCPID()
{
  return(this.origWM_ENCP_ID) ;
}
function _MGetOrigUOTID()
{
  return(this.origUOT_ID) ;
}
function _MHasChanged()
{

  if (this.isChecked != this.origChecked ||
      this.LMID != this.origLMID ||
      this.WM_ENCP_ID != this.origWM_ENCP_ID ||
      this.SL_ID != this.origSL_ID ||
      this.UOT_ID != this.origUOT_ID)
    return (true) ;
  else
    return (false) ;
}