// -- SEARCH FUNCTIONS
// Set initial menus
var selected;
var tcName;

function loadTownCity(a,suburb,townCity,selectedID)
{
    tcName = townCity;
    sbName = suburb;
    // If changed then kill suburbs
    emptyList(suburb);
    var subs = document.getElementById(suburb);
    subs.disabled = true;
    fillList(townCity,rg[a],tc,selectedID);
}

function loadSuburb(a,suburb,selectedID)
{
    sbName = suburb;
    fillList(suburb,tc_su[a],su,selectedID);
}

// -- HELPER FUNCTIONS --
// Fill List
function fillList(selectBox,arrayC,arrayP, selectedID)
{
	// Empty options
	emptyList(selectBox);
	// Check for data
	var sbx = document.getElementById(selectBox);
	if (arrayC == null)
	{
		sbx.disabled = true; 
	}
	else
	{
	    sbx.disabled = false;
	    var i;
		for (i=0;i<arrayC.length;i++)
		{
			addOption(selectBox,arrayP[arrayC[i]],arrayC[i], selectedID);
		}
	}
}
// Add a single option
function addOption(selectBox,text,value, selected)
{
    var sbx = document.getElementById(selectBox);
	var i = sbx.length;
	sbx.options[i] = new Option(text,value);
	if (value == selected)
	{
	    sbx.options[i].selected = true;
	    if (selectBox == tcName)
	    {
	        loadSuburb(sbx.options[i].value,sbName);
	    }
	}
}
// Empty List (except for 0)
function emptyList(selectBox)
{
    var sbx = document.getElementById(selectBox);
    if (sbx != null)
    {
	    var i;
	    for (i=sbx.options.length-1;i>1;i--) // i>1 to leave a spacer option
	    {
		    sbx.remove(i);
	    }
	}
}
