/*
  This file contains the routines used for shuttlebox manipulation.

  Copyright 2004, Version 1 Software Ltd.
  All rights reserved.

  $Log: shuttlebox.js,v $
  Revision 1.2  2004/10/11 11:02:53  stephen.fensom
  sync with office

  Revision 1.5  2004/10/07 16:32:42  stevensg
  fixed bug 1151 to keep shuttlebox options in right-hand box

  Revision 1.4  2004/08/27 10:08:47  kearneyd
  Updated sb_moveall, to insert an empty option in the select box once << is selected

  Revision 1.3  2004/07/05 13:21:19  kearneyd
  Added workaround for the empty box having a blank entry initially

  Revision 1.2  2004/03/24 17:20:33  fensoms
  bug fixes

  Revision 1.1  2004/03/05 14:16:51  fensoms
  initial import

  Revision 1.2  2004/02/18 10:50:47  fensoms
  Restructure of general / update

  Revision 1.1  2004/02/16 16:30:45  royc
  new repos structure

  Revision 1.1  2004/02/16 14:56:25  royc
  initial import

  Revision 1.4  2004/01/30 10:07:06  mckennab
  Made a change to the routines which change the list contents to
  ensure that all elements get examined.

  Revision 1.3  2004/01/29 17:36:02  mckennab
  Fixed a bug in the select method that had meant that we only
  copied every other row....

  Revision 1.2  2004/01/28 16:50:08  mckennab
  Added method to ensure that right-hand select box items get
  selected so that the server will see them on submit.

  Revision 1.1  2004/01/28 09:40:41  mckennab
  Initial check-in of JavaScript code used to manipulate shuttleboxes.


  $Id: shuttlebox.js,v 1.2 2004/10/11 11:02:53 stephen.fensom Exp $
*/

function trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function


function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function




/*
  This function moves all selected items from the left-hand select box
  to the right-hand one.
*/
function sb_move(lhname, rhname) {
  var lsb;  // Left-hand select
  var rsb;  // Right-hand select
  var oopt; // Old option (in left-hand box)
  var nopt; // New option (in right-hand box)

  lsb = document.getElementById(lhname);
  rsb = document.getElementById(rhname);

  if (lsb.selectedIndex == -1) {
     // Means nothing was selected to be moved.
     return;
  }

  for (var idx=0; idx < lsb.options.length; idx++) {
     if (lsb.options[idx].selected) {
         oopt = lsb.options[idx];
         nopt = new Option(oopt.text,oopt.value,false,false);
         // workaround for the empty box having a blank entry initially
         if ((rsb.options.length==1) && (rsb.options[0].value=='')) {
           rsb.options[0] = nopt;
         } else {
           rsb.options[rsb.options.length] = nopt;
         }
         lsb.options[idx] = null;
         idx--;
     }
  }

  return;
}

/*
  Function to move -all- of the items from the left hand list to the
  right-hand one.
*/
function sb_moveall(lhname, rhname) {
  var lsb;  // Left hand one
  var rsb;  // Right hand one
  var copt; // Current (left-hand) option
  var nopt; // New (right-hand) option

  lsb = document.getElementById(lhname);
  rsb = document.getElementById(rhname);

  // When there is nothing in the lsb, there is really a blank entry,
  // nothing to do.
  if (lsb.options.length==0 || lsb.options[0].value=='') return;

  // On load, if the right hand select box is empty a
  // default blank option exists <option value=""/>.
  // Set this to null if the first value is empty
  if (rsb.options.length > 0)  {
    var rsbFirstValue = rsb.options[0].value;
    if (rsbFirstValue == '') {
      rsb.options[0] = null;
    }
  }

  for (var idx=0; idx < lsb.options.length; idx++) {
    copt = lsb.options[idx];
    nopt = new Option(copt.text,copt.value,false,false);
    rsb.options[rsb.options.length] = nopt;
    lsb.options[idx] = null;
    idx--;
  }
  // leave an empty option here, so select all works i.e.
  // select all know that there is now nothing in the list
  lsb.options[0] = new Option("","",true);

  return;
}

/*
  Function to move the selected items from the right-hand list to the
  left-hand one.  Just calls sb_move() with swapped parameters.
*/
function sb_remove(lhname, rhname) {
  sb_move(rhname,lhname);
  return;
}

/*
  Function to move all of the items on the right-hand side to the left-hand
  side.  As with sb_remove(), it calls its corresponding inverse function
  with the parameters swapped.
*/
function sb_removeall(lhname, rhname) {
  sb_moveall(rhname,lhname);
  return;
}

/*
  Function to ensure that all of the items (if any) in the right-hand
  select box in the shuttlebox 'control' are selected prior to the form's
  being submitted.  This is necessary for the server to actually receive
  the values involved.
*/
function sb_selectall(rhname) {
  var rsb;  // Right-hand select box

  rsb = document.getElementById(rhname);
  if (rsb != null) {
    for (var idx=0; idx < rsb.options.length; idx++) {
      rsb.options[idx].selected = true;
    }
  }
  return;
}

/*
  Function to load the right-hand list from a string of values.
*/
function sb_loadRsbFromString( elementName, string, delimiter ) {
  if( null == string || string == '' || string.length == 0 ) {
    // Means nothing was selected to be loaded.
    return;
  }

  var lsb = document.getElementById( elementName + '_lhs' );  //  Left-hand select box
  var rsb = document.getElementById( elementName );           //  Right-hand select box

  // If no delimiter, assume only one value provided.
  if( null == delimiter ) {
    loadRsbItem( lsb, rsb, string );
    // Return even if we didn't find it.
    return;
  }

  //  If delimiter provided, assume more than one value provided.
  var delimiterIndex = 0;
  while( delimiterIndex > -1 ) {
    delimiterIndex = string.indexOf( delimiter );
    var valueString = string.substring( 0, delimiterIndex );
    loadRsbItem( lsb, rsb, valueString );
    string = string.substring( delimiterIndex + 1, string.length );
  }
  //  Account for last item not having a delimiter after it.
  if( string.length > 0 ) {
    loadRsbItem( lsb, rsb, string );
  }

  return;
}

/*
  Function to load a value into the right-hand list.
*/
function loadRsbItem( lsb, rsb, string ) {
  var num = new Number( string );
  var lsbIdx = getLsbIndex( lsb, num );
  if( null != num && null != lsbIdx ) {
    var lsOpt = lsb.options[lsbIdx];
    var rsOpt = new Option( lsOpt.text, lsOpt.value, false, false );
    //  Workaround for the empty box having a blank entry initially
    if( ( rsb.options.length == 1 ) && ( rsb.options[0].value == '' ) ) {
      rsb.options[0] = rsOpt;
    } else {
      rsb.options[rsb.options.length] = rsOpt;
    }
    lsb.options[lsbIdx] = null;
  }
}

/*
  Function to get the index for a value in the left-hand list.
*/
function getLsbIndex( lsb, num ) {
  for( var i = 0; i < lsb.options.length; i++ ) {
    if( lsb.options[i].value == num ) {
      return i;
    }
  }
}

/*
  Function called when onkeyup occurs - to control via keyboard
 */
function sb_keyup(isLeft, lsb, rsb) {
  var pressedKey;
  if (document.all) {
    e = window.event;
    pressedKey = e.keyCode;
  } else if (document.layers) {
    pressedKey = e.which;
  } else {
    return;
  }
  if (pressedKey == 13) {
    if (isLeft) sb_move(lsb, rsb);
    else sb_remove(lsb, rsb);
  }
  return;
}

/*
  ADD ENTRY TO LIST
*/
function sb_addItem(lhname, rhname) {

  var msgStr = "";
  var current = "";

  fromList = document.getElementById(lhname);
  toList = document.getElementById(rhname);


  if (toList.options.length > 0 && toList.options[0].value == 'temp') {
    toList.options.length = 0;
  }
  var current = fromList.value;

  // Remove any blank options
  for(i=0; i<toList.options.length; i++) {
    if(trim(toList.options[i].value) == '') {
        toList.options[i] = null;
    }
  }

  for(i=0; i<fromList.options.length; i++) {
      if(fromList.options[i].value == trim(current)) {
        txt = fromList.options[i].name;
      }
  }


  if (trim(current) == '') {
    msgStr = "You cannot add a blank item.";
    alert (msgStr);
    return;
  }
  else {
    var elementExists = false;
    for(i=0; i<toList.options.length; i++) {
      if(toList.options[i].value == trim(current)) {
        elementExists = true;
      }
    }
    if(!elementExists) {
      val = current;
      toList.options[toList.length] = new Option(txt,val);
      fromList.value = '';
    }
    else {
      msgStr = "The item already exists in the list.";
      alert(msgStr);
      return;
    }
  }
}

/*
  ADD ENTRY TO LIST
*/
function sb_addItemText(lhname, rhname) {

  var msgStr = "";
  var txt = "";

  fromList = document.getElementById(lhname);
  toList = document.getElementById(rhname);

  var currentFullString = fromList.value;

  // Remove any blank options
  for(i=0; i<toList.options.length; i++) {
    if(trim(toList.options[i].value) == '') {
        toList.options[i] = null;
    }
  }
  
  if ((currentFullString=='')||(trim(currentFullString) == '')) {
    msgStr = "You cannot add a blank item.";
    alert (msgStr);
    return;
  }
  else {
    var currentArray = currentFullString.split(",");
    for (var currentIndex = 0; currentIndex < currentArray.length; currentIndex++){
      var current = currentArray[currentIndex];
      var elementExists = false;
      if (toList.options!=null)
      {
         for(i=0; i<toList.options.length; i++) {
           if(toList.options[i].value == trim(current)) {
             elementExists = true;
           }
         }
      }
      if(!elementExists) {
        txt = current;
        val = current;
        toList.options[toList.length] = new Option(txt,val);
        fromList.value = '';
      }
      else {
        msgStr = "The item [" + current + "] already exists in the list.";
        alert(msgStr);
      }
    }
  }
}

/*
  REMOVE ENTRY FROM LIST
*/
function sb_removeItem(rhname) {

  var i=0;
  var msgStr = "";

  toList = document.getElementById(rhname);

  if(toList.options.length > 0) {
    do{
      if(toList.options[i].selected == true) {
        toList.options[i] = null;
        i=-1;
      }
      i++;
    } while (toList.options.selectedIndex >= 0)
  }
  else {
    msgStr = "There are no more items to remove.";
    alert(msgStr);
    return false;
  }
}

/*
  REMOVE ALL ENTRIES FROM LIST
*/
function sb_removeAllItems(rhname) {

  var i=0;

  toList = document.getElementById(rhname);

  toList.options.length = 0;
}

/*
 * Function to swap two items in a list.
 * This can be used to move items up or down in a list.
 * TMcA 20090622
 */
function sb_swapItem(itemList, index1, index2) {
  
  var item1Text;
  var item1Value;
  
  item1Text = itemList.options[index1].text;
  item1Value = itemList.options[index1].value;
  
  itemList.options[index1].text = itemList.options[index2].text;
  itemList.options[index1].value = itemList.options[index2].value;
  
  itemList.options[index2].text = item1Text;
  itemList.options[index2].value = item1Value;
  
  itemList.options[index2].selected = true;
  itemList.options[index1].selected = false;
}

function sb_moveItemUp(listName) {
   
  var itemList = document.getElementById(listName);
  for (var i = 0; i < itemList.length; i++) {
    if (itemList.options[i].selected) {
      if (i > 0){
        sb_swapItem(itemList, i, i - 1);
        i = itemList.length;
      }
    }
  }
}

function sb_moveItemDown(listName) {
  
  var itemList = document.getElementById(listName);
  
  for (var i = 0; i < itemList.length; i++) {
    if (itemList.options[i].selected) {
      if (i < (itemList.length - 1)){
        sb_swapItem(itemList, i, i + 1);
        i = itemList.length;
      }
    }
  }
}
