function selectItem(sId, sDirection, nSort, bHandleUserDefined, sGroupColor, sGroupValue )
{
  var oSrc = null;
  var oDst = null;
  var oDstPos = null;
  var oOption = null;

  if ( sGroupColor == null )
    sGroupColor = '';
  if ( sGroupValue == null )
    sGroupValue = '';

  var bHandleGroup = (sGroupColor != '' && sGroupValue != '')


  if (sDirection == '+' || sDirection == '*') {
    oSrc = document.getElementById(sId + '_available');
    oDst = document.getElementById(sId + '_selected');
  }
  else if (sDirection == '-' || sDirection == '/') {
    oSrc = document.getElementById(sId + '_selected');
    oDst = document.getElementById(sId + '_available');
  }
  // Source & destination found
  if (oSrc && oDst) {
    // "Add all" or "Remove all" : Select all items (except required)
    if (sDirection == '*' || sDirection == '/')
      for (var nI = 0; nI < oSrc.options.length; nI++)
      {
        oSrc.options.item(nI).selected = true;
        if (sDirection == '/' && oSrc.options.item(nI).getAttribute('required') == 'true')
          oSrc.options.item(nI).selected = false;
      }
    // Loop on selected items
    while (oSrc.selectedIndex >= 0) {
      // If remove, and required, uncheck element
      if (sDirection == '-' && oSrc.options.item(oSrc.selectedIndex).getAttribute('required') == 'true')
        oSrc.options.item(oSrc.selectedIndex).selected = false;
      else
      {
        oOption = oSrc.options.item(oSrc.selectedIndex).cloneNode(true);
        oOption.text = oSrc.options.item(oSrc.selectedIndex).text;
        var sSortValue = (nSort == 0?oOption.value:oOption.text);
        var sOptGroupValue = oSrc.options.item(oSrc.selectedIndex).getAttribute('groupValue', 0);
        //alert(sOptGroupValue);
        if ((sDirection == '-' || sDirection == '/') && sOptGroupValue != null)
        {
          oOption.style.color = '';
          oOption.value = oOption.value.replace(new RegExp(sOptGroupValue + '\u00a4','i'),'');
        }
        else if (sGroupValue != '')
        {
          oOption.style.color = sGroupColor;
          oOption.value = sGroupValue + '\u00a4' + oOption.value;
          sSortValue = sGroupValue + '\u00a4' + sSortValue;
          oOption.setAttribute('groupValue', sGroupValue, 0);
        }

        // If sorted elements, find dest position
        if (nSort == 0 || nSort == 1)
        {
          if ( bHandleGroup && (sDirection == '+' || sDirection == '*') )
          {
            var sSortCompare = '';
            for (var nI = 0; nI < oDst.options.length; nI++)
            {
              sSortCompare = oDst.options.item(nI).getAttribute('groupValue', 0) + '\u00a4' +
                            (nSort == 0 ?oDst.options.item(nI).value:oDst.options.item(nI).text);
              if (sSortCompare >= sSortValue)
              {
                oDstPos = oDst.options.item(nI);
                break;
              }
            }

          }
          else
          {
            for (var nI = 0; nI < oDst.options.length; nI++)
              if ((nSort == 0 && oDst.options.item(nI).value >= sSortValue) ||
                  (nSort == 1 && oDst.options.item(nI).text  >= sSortValue))
              {
                oDstPos = oDst.options.item(nI);
                break;
              }
          }
        }
        else if (oDst.selectedIndex >= 0)
          oDstPos = oDst.options.item(oDst.selectedIndex);

        if (bHandleUserDefined == false ||
            (bHandleUserDefined == true && oSrc.options.item(oSrc.selectedIndex).getAttribute('UserDefined') == 'true') ||
            (bHandleUserDefined == true && oSrc.id == sId + '_available') )
          oDst.insertBefore(oOption, oDstPos);
        if (bHandleUserDefined == false ||
            (bHandleUserDefined == true && oSrc.options.item(oSrc.selectedIndex).getAttribute('UserDefined') == 'true') ||
            (bHandleUserDefined == true && oSrc.id == sId + '_selected') )
          oSrc.remove(oSrc.selectedIndex);
        else
          oSrc.options.item(oSrc.selectedIndex).selected = false;
      }
    }
    oSrc.style.width='';
    oDst.style.width='';
    oSrc.style.width='100%';
    oDst.style.width='100%';
  }
}
function moveItem(sId, sDirection)
{
  var oSelect = document.getElementById(sId + '_selected');
  var oSrcNode = null;
  var oDstNode = null;
  if (oSelect && oSelect.selectedIndex >= 0)
  {
    oSrcNode = oSelect.options.item(oSelect.selectedIndex);
    if (oSrcNode)
    {
      if (sDirection == '-' && oSelect.selectedIndex > 0)
        oDstNode = oSelect.options.item(oSelect.selectedIndex - 1);
      else if (sDirection == '+' && oSelect.selectedIndex < (oSelect.options.length - 1))
        oDstNode = oSelect.options.item(oSelect.selectedIndex + 1);
      if (oSrcNode && oDstNode)
      {
        if (sDirection == '-')
          oSrcNode.parentNode.insertBefore(oSrcNode, oDstNode);
        else if (sDirection == '+')
          oSrcNode.parentNode.insertBefore(oDstNode, oSrcNode);

        window.setTimeout('document.getElementById(\''+sId + '_selected\').value = \''+ oSrcNode.value.replace(/\x27/g, '\\\'') +'\';',1,'JavaScript');
      }
    }
    oSelect.style.width='';
    oSelect.style.width='100%';
  }
}
function beforeSubmit(sId)
{
  var oSelect = document.getElementById(sId + '_selected');
  if (oSelect) {
    for (var nI=0; nI < oSelect.options.length; nI++)
      oSelect.options.item(nI).selected = true;
  }
}

