/*********************************************
  JavaScript Functions for ComboBox Management
*/
//---------------------------------------------------------

/**
  * Move ComboBox Selection to a given value
  * @param objCombo HTML <SELECT> Object
  * @param idValue Value to find inside ComboBox
*/
function setCombo (objCombo, idValue) {
  var opt = objCombo.options;
  var len = opt.length;

  for (var i=0;i<len;i++)
    if (opt[i].value == idValue || opt[i].text == idValue) {
	opt.selectedIndex = i;
	break;
    } // fi()
} // setCombo

//---------------------------------------------------------

/**
  * Get Index of a value inside a ComboBox
  * @param objCombo HTML <SELECT> Object
  * @param idValue Value to find inside ComboBox
  * return Index of value inside COmboBox or -1 if value was not found
*/
function comboIndexOf (objCombo, idValue) {
  var opt = objCombo.options;
  var len = opt.length;
  var idx = -1;

  for (var i=0;i<len;i++)
    if (opt[i].value == idValue || opt[i].text == idValue) {
      idx = i;
      break;
    } // fi()
  return idx;
} // comboIndexOf

//---------------------------------------------------------

/**
  * Add a value to a ComboBox
*/

function comboPush (objCombo, txValue, idValue, defSel, curSel) {
  var opt = new Option(txValue, idValue, defSel, curSel);
  objCombo.options[objCombo.options.length] = opt;
}

//---------------------------------------------------------

/**
  * Get Selected Value for a ComboBox
  * @param objCombo HTML <SELECT> Object
  * @return Value for selected option or
  *         null if no option is selected
*/

function getCombo (objCombo) {
  var opt = objCombo.options;

  if (-1==opt.selectedIndex)
    return null;
  else
    return opt[opt.selectedIndex].value;
}

//---------------------------------------------------------

/**
  * Get Selected Text for a ComboBox
  * @param objCombo HTML <SELECT> Object
  * @return Text for selected option or
  *         null if no option is selected
*/

function getComboText (objCombo) {
  var opt = objCombo.options;

  if (-1==opt.selectedIndex)
    return null;
  else
    return opt[opt.selectedIndex].text;
}

//---------------------------------------------------------

/**
  * Clear all options for a ComboBox
*/
function clearCombo (objCombo) {
  var opt = objCombo.options;

  for (var i=opt.length-1; i>=0; i--)
    opt.remove(i);
}

//---------------------------------------------------------

/**
  * Sort ComboBox texts
*/
function sortCombo (objCombo) {
   var copyOption = new Array();
   var optCount = objCombo.options.length;

   for (var i=0; i<optCount; i++)
     copyOption[i] = new Array(objCombo[i].value, objCombo[i].text);

   copyOption.sort(function(a,b) { if (isNaN(a[1]) || isNaN(a[2])) return (a[1]>b[1] ? 1 : (a[1]<b[1] ? -1 : 0)); else return (a[1]-b[1]); });

   clearCombo (objCombo);

   for (var i=0; i<copyOption.length; i++)
     comboPush (objCombo, copyOption[i][1], copyOption[i][0], false, false)
} // sortCombo

//---------------------------------------------------------

