// 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 a specific value from the select
function removeOptionFromSelect(selectObj, value)
{
  for (index = 0; index < selectObj.options.length; index++) {
    if (selectObj.options[index].value == value) {
      selectObj.options[index] = null;

      // Since we just deleted one item from the array, we need to decrease the count
      // due to the array key shift
      index--;
    }
  }

  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;
      }
    }
  }
}

/**************************************************************************************

The below code provides the ability to turn any element on a page into a select option.
Essentially you have created a hidden form with a clean display.

Ex. You have an html table display records in the database. Each TR is a record with a
unique id. When you click on a row, the row highlighting is toggled and its id is
added/removing from a hidden select. Ctrl+click will allow the toggling and
adding/removing of multiple rows. Basically you have just turned the entire table into
a big select box.

The highlighting/unhighlighting css can be customized by overriding the class name.

Sample Script:

<link rel="stylesheet" type="text/css" href="{LINK_ROOT}/css/default.css">
<script type="text/javascript" src="{LINK_ROOT}/js/form_library_adv.js"></script>
<script type="text/javascript">
  var new_select = new customSelect();

  //override default css
  new_select.highlightClassName = 'hi';
  new_select.unhighlightClassName = 'low';
</script>

<style type="text/css">
//custom css
.hi { color : red; font-weight: bold; background-color: black;}
.low { color: black; font-weight: normal; background-color: white; }

</style>

<!-- The select object can be visible or hidden, if it is visible it should be disabled -->
<select name="ids[]" id="ids[]" multiple disabled></select>

<table width="100%">
   <tr id="1" onclick="new_select.customToggleSelect(this.id, event, 'ids[]');"><td> 1 </td></tr>
   <tr id="2" onclick="new_select.customToggleSelect(this.id, event, 'ids[]');"><td> 2 </td></tr>
   <tr id="3" onclick="new_select.customToggleSelect(this.id, event, 'ids[]');"><td> 3 </td></tr>
</table>

This insanity was created by Brandon, Sabrina and Sean with love from Alex Roger

**************************************************************************************/

//Constructor for custom select object
function customSelect()
{
  //initialize default values
  this.highlightClassName         = 'highlight';
  this.unhighlightClassName       = 'unhighight';
  this.current_id                 = '';
  this.customToggleSelect         = customToggleSelect;
  this.addOptionToSelect          = addOptionToSelect;
  this.removeAllOptionsFromSelect = removeAllOptionsFromSelect;
}

/*
 * toggle element highlighting and selection
 *
 * @param string the element id
 * @param object the captured event
 * @param string the select object name
*/
function customToggleSelect(id, e, select_name)
{
  //get the current class name for the element
  class_name = document.getElementById(id).className;
  //get the select object
  select_obj = document.getElementById(select_name);

  //capture the event based on the browser
  //Reference: http://www.thescripts.com/forum/post1883743-4.html
  e = e || window.event;

  //we are holding the ctrl key to perform multiple selections
  if (e.ctrlKey) {
    //check to see if the the element is already selected
    if (document.getElementById(id).className == this.highlightClassName) {
      //unhighlight the element
      document.getElementById(id).className = this.unhighlightClassName;

      //check to see if the select object has options
      if (select_obj.options.length) {
        //loop through the options
        for (x = 0; x < select_obj.options.length; x++) {
          //if the option is the same as the id then remove it
          if (select_obj.options[x].value == id) {
            select_obj.options[x] = null;
            return;
          }
        }
      }
      return;
    }

    //check to see if the object was not already selected
    if (document.getElementById(id).className == this.unhighlightClassName || document.getElementById(id).className == '') {
      //highlight the new selection, set it as the current id and add it to the select
      document.getElementById(id).className = this.highlightClassName;
      this.current_id = id;
      addOptionToSelect(select_obj, id, id, true);
      return;
    }
  }
  //we are performing single selects
  else {
    //check to see if we already have an current id
    if (this.current_id) {
      //check to see if we have options in our select obj
      if (select_obj.options.length) {
        //loop through the options of the select object
        for (x = 0; x < select_obj.options.length; x++) {
          if (select_obj.options[x].value != id) {
            //if the option is not the same as the new id then unhighlight it
            document.getElementById(select_obj.options[x].value).className = this.unhighlightClassName;
          }
        }
      }

      //clear the select obj
      removeAllOptionsFromSelect(select_obj);
    }

    //highlight the new selection, set it as the current id and add it to the select
    document.getElementById(id).className = this.highlightClassName;
    this.current_id = id;
    addOptionToSelect(select_obj, id, id, true);
    return;
  }

  return;
}