﻿function insertOptionBefore( elSel, value, text )
{
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = text;
    elOptNew.value = value;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
}

function removeOptionSelected( elSel )
{
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function appendOptionLast( elSel, value, text )
{
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  elOptNew.value = value;

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
//  elSel.value = value;
}

function removeOptionLast( elSel )
{
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}

function removeAll( elSel ) {
  while (elSel.length > 0)
    elSel.remove(elSel.length - 1);
}