// Set up the selection box (with id selectid) options from the contents of arr.
// Parameters:
//  - selectid: if of the select box
//  - arr: array of data to be used for the select box options
//    arr can be an array or object (i.e an object used as an associative array) 
//    Example/ It can be of the form Array(1,2,3,4,5) or {'1','2'...} or {'1':'one','2':'two'} or {'1':{...},'2':{...}}
//  - setToHighestVal (true,false): Select the highest value of arr
//  - selectValue: select the selectValue

function setSelectOptionsFromArray(selectId,arr,setToHighestVal,selectValue)
{
  var selectObject = document.getElementById(selectId);
  var option = 0;
  var highestVal = 0;
  var highestOption = 0;
  var setOption = -1;
  selectObject.options.length=0;//empty

  if (arr != null)
  {
    for (var i in arr) 
    {
      if (arr[i] == "" || (typeof(arr[i]) == 'object'))
      {
        selectObject.options[option] = new Option(i,i);
        if (selectValue != "" && selectValue == i)
          setOption = option;
        if (setOption == -1 && setToHighestVal && i > highestVal)
        {
          highestVal = i;
          highestOption = option;
        }
      }
      else
      {
        selectObject.options[option] = new Option(arr[i],i);
        if (selectValue != "" && selectValue == i)
          setOption = option;
        if (setOption == -1 && setToHighestVal && arr[i] > highestVal)
        {
          highestVal = arr[i];
          highestOption = option;
        }
      }
      option++;
    }
  }
  else
    selectObject.options[0] = new Option("no data","nodata");
  if (setOption != -1)
     selectObject.selectedIndex = setOption;
  else
  {
    if (highestOption > 0)
      selectObject.selectedIndex = highestOption;
  }
}

function setSelectOptionNoData(selectId)
{
  var selectObject = document.getElementById(selectId);
  selectObject.options.length=0;//empty
  selectObject.options[0] = new Option("no data","nodata");
}

// Get the current selected index from the select box with a specified id
function getSelectedValue(id)
{
  var targetElt = document.getElementById(id);
  var selected = targetElt[targetElt.selectedIndex].value;
  return selected;
}

// Set the current selected index
function setSelectedValue(selectId,value)
{
  var selectObject = document.getElementById(selectId);
  for(var index = 0; index < selectObject.length; index++) 
  {
    if(selectObject[index].value == value)
      selectObject.selectedIndex = index;
  }
}


