// Adds an option to the specified select object
function addOptionToSelect(selectObj, text, value, selected)
{
  var new_Option = new Option(text, value, selected, selected);

  selectObj.options[selectObj.options.length] = new_Option;
}


// Move the selected option up one entry
function moveOptionUpInSelect(selectObj)
{
  if (!selectObj.selectedIndex) {
    // We are already at the top
    return false;
  }

  // Have to store selected index since once we move an option it gets unselected.
  var selectedIndex = selectObj.selectedIndex;
  var nextOption    = selectObj.options[selectedIndex - 1];

  selectedValue = new Number(selectObj.options[selectedIndex].value);
  nextValue     = new Number(nextOption.value);

  selectObj.options[selectedIndex - 1] = new Option(selectObj.options[selectedIndex].text, selectedValue);
  selectObj.options[selectedIndex]     = new Option(nextOption.text,nextValue);

  // Re select the option
  selectObj.selectedIndex = selectedIndex - 1;

  return true;
}


// Move the selected option down one entry
function moveOptionDownInSelect(selectObj)
{
  if (selectObj.selectedIndex == selectObj.options.length - 1) {
    // We are already at the bottom
    return false;
  }

  // Have to store selected index since once we move an option it gets unselected.
  var selectedIndex = selectObj.selectedIndex;
  var nextOption = selectObj.options[selectedIndex + 1];
  selectObj.options[selectedIndex + 1] = new Option(selectObj.options[selectedIndex].text, selectObj.options[selectedIndex].value);
  selectObj.options[selectedIndex]     = new Option(nextOption.text,nextOption.value);

  // Re select the option
  selectObj.selectedIndex = selectedIndex + 1;
  return true;
}


// Replace the selected option in a select with a new value
function replaceSelectedOptionInSelect(selectObj,text,value)
{
  if (!selectObj.selectedIndex) {
    return false;
  }

  selectObj.options[selectObj.selectedIndex] = new Option(text,value);
  return true;
}


// Remove the selected option
function removeSelectedOptionFromSelect(selectObj)
{
  if (!selectObj.selectedIndex < 0) {
    // nothing is selected so just return
    return false;
  }

  // Loop through the select
  for (count=0; count < selectObj.options.length; count++) {
    // If the value is selected, delete it
    if  (selectObj.options[count].selected) {
      selectObj.options[count] = null;
      // Since we just deleted one item from the array, we need to decrease the count
      // due to the array key shift
      count--;
    }
  }
  return true;
}


// Remove all options from the select
function removeAllOptionsFromSelect(selectObj)
{
  for (index = 0; index < selectObj.options.length; index++) {
    selectObj.options[index] = null;
    index--;
  }
  return true;
}


// Deselect all the options of the select box
function deselectAllOptions(selectObj)
{
  if (!selectObj || !selectObj.options) {
    return false;
  }

  for (count = 0; count < selectObj.options.length; count++) {
    selectObj.options[count].selected = false;
  }

  return true;
}


// Select all the options of the select box
function selectAllOptions(selectObj)
{
  if (!selectObj || !selectObj.options) {
    return false;
  }

  for (count = 0; count < selectObj.options.length; count++) {
    selectObj.options[count].selected = true;
  }

  return true;
}

// Return the texts from the select object as an array.
function getTextArrayFromSelect(selectObj)
{
  if (!selectObj || !selectObj.options) {
    return false;
  }

  result = new Array();
  for (count =0; count < selectObj.options.length; count++) {
    result[count] = selectObj.options[count].text;
  }

  return result;
}

// Return the option that is selected (only for single select) and return the object
function getSelectedOptionFromSelect(select_obj)
{
  if (!select_obj || !select_obj.options || select_obj.selectedIndex < 0) {
    return false;
  }

  return select_obj.options[select_obj.selectedIndex];
}


// Sets the selected index or the selected options (multiple) to what it was originally
function setSelectedToDefaultSelected(select_obj)
{
  if (!select_obj || !select_obj.options) {
    return false;
  }

  for (index = 0; index < select_obj.options.length; index++) {
    if (select_obj.multiple) {
      select_obj.options[index].selected = select_obj.options[index].defaultSelected;
    }
    else {
      if (select_obj.options[index].defaultSelected) {
        select_obj.selectedIndex = index;
        break;
      }
    }
  }
}


