/* *******************************
Single-Select Control
******************************* */

var Index = new Object;

function InsertIntoTextSingle(txtBox, listBox) 
{     
    if (!document.getElementById) return false;
    var text    = document.getElementById(txtBox);
    var select  = document.getElementById(listBox); 

    // An IE bug means we need another event before select.selectedIndex
    // will update from -1. Since we need to swap the focus anyway we'll
    // do it before we look at what was clicked in the select box.
    text.focus();	 
    var suburb = select.options[select.selectedIndex];

    // short circuit on 'show all suburbs'
    if (select.selectedIndex == 0) 
    {
     text.value = '';
     return true;
    }

    var newvalue =  suburb.text;
    text.value = newvalue;
    return false;
}

function CompleteTextFromList (txtId,selectId) 
{
    if (!document.getElementById) return;
    var text   = document.getElementById(txtId);
    var select = document.getElementById(selectId);
    if (!Index.populated) BuildIndex(selectId);
    var suburb = text.value.match(/,*([^,]+)$/);

    if (suburb) 
    {
        var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
        for (var i = Index[name.charAt(0)]; i < select.options.length; i++) 
        {
            if (select.options[i].text.toUpperCase().indexOf(name) == 0) 
            {
                 select.selectedIndex = i;
                 break;
            }
            else 
            {
                 select.selectedIndex = -1;
            }
        }
    }
}

 function BuildIndex (selectId) 
 {
	 if (!document.getElementById) return;
	 var select = document.getElementById(selectId);
	 for (var i = select.options.length; i--;) 
	 {
	 	Index[select.options[i].text.toUpperCase().charAt(0)] = i;
	 }
	 Index.populated = true;
 }