// Utility functions for select lists

// Returns the value of the selected index for the input select list
// object
function getSLValue(selectlistObj) {
  var value = null;
  if (selectlistObj != null) {
    var selectedIdx = selectlistObj.selectedIndex;
    if (selectedIdx != -1) {
      value = selectlistObj[selectedIdx].value;
    }
  }
  return value;
}

// Iterates over all options in the input select list and returns an
// array of values of all the selected options
function getSLValueArray(selectlistObj) {
  // Determine the selectlist options that are selected, if any
  var valuesArray = new Array();
  if (selectlistObj != null) {
    var valuesIdx = 0;
    for (var optionIdx = 0; optionIdx < selectlistObj.length; optionIdx++) {
      var optionObj = selectlistObj[optionIdx];
      if (optionObj.selected) {
        valuesArray[valuesIdx++] = optionObj.value;
      }
    }
  }
  return valuesArray;
}

// Selects the item in the input selectlist that has the input value
// If such an item is found and selected, returns true
// Otherwise returns false
function updateSLSelectionByValue(selectlistObj, selectlistValue) {
  var valueFound = false;
  if (selectlistObj != null) {
    for (var optionIdx = 0; optionIdx < selectlistObj.length; optionIdx++) {
      var optionObj = selectlistObj[optionIdx];
      if (optionObj.value == selectlistValue) {
        optionObj.selected = true;
        valueFound = true;
        break;
      }
    }
  }
  return valueFound;
}
