I have a listbox and a textbox on an asp page. I can search for items in the listbox by typing into the textbox and the listbox will scroll down to the item that I am searching for. The found item is highlighted in the listbox.

However, when I click on the button to add the highlighted item to the parent page, I am finding that the highlighted item is not actually "selected".

What is wrong? This only happens when I use the search functionality. If I click on the items in the listbox everything works great.

HTML
Code:
<tr><td>Search: <input name='txtSearch' id='txtSearch' onKeyUp='return SearchList();' type='text' xmlns:asp='#unknown' /></td>

<input class= "button" type="button" name="cancel" value="Add To Floorplan" onclick="onSubmit();">
Javascript
Code:
function onSubmit()
{
if(formButtons.locationcode.value == "")
{
 //alert('You must choose a location.');
 top.frames.mainFrame.onAdd('NONE');
 return;
}
top.frames.mainFrame.onAdd(formButtons.locationcode.value);
}

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

function onAdd(sLocation)
{
  top.opener.formFloorPlan.timekeeperIDs.value = getSelectedItems();
alert("TK: " + getSelectedItems());
  top.opener.formFloorPlan.action.value = "addemployees";
  top.opener.formFloorPlan.location.value = sLocation;
  top.opener.showStatus('Adding Employee...');  
  top.opener.formFloorPlan.submit();
  top.close();
  
}

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

function getSelectedItems()
{
  aItems = new Array();
  for(x=0;x<aSelectedItems.length;x++)
  {
    if(aSelectedItems[x] != "") aItems.push(aSelectedItems[x]);
  }
  return aItems;
}

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

function SearchList() {
    var tb = document.getElementById('txtSearch');
    var patt = new RegExp("^" + tb.value, "i");
    for (var i = 0; i < document.getElementById("lstEmpList").options.length; i++) {
        if (document.getElementById("lstEmpList")[i].text.toLowerCase().match(tb.value.toLowerCase())) {
            document.getElementById("lstEmpList").options[i].selected = true;
            document.getElementById("lstEmpList").options[i].selected = false;
            document.getElementById("lstEmpList").options[i].selected = true;
            return false;
        }
        else {
            ClearSelection();
        }
    }
}