function selectState()
{
    if ($("#state").val().length == 0) return;
	$.post('select.php', {'state':$("#state").val()}, function(c, s) {populateCounty(c);}, 'json');
}

function selectCounty()
{
    if ($("#county").val().length == 0 || $("#state").val().length == 0) return;
	$.post('select.php', {'county':$("#county").val(), 'state':$("#state").val()}, function(c, s) {populateCity(c);}, 'json');
}

function selectCity()
{
    if ($("#city").val().length == 0) return;
	selectLocation($("#city").val());
}

function populateCounty(c)
{
	if (c.length == 0) return;
	/*
	if (c.length == 1 && c[0].id.length > 0)
	{
		selectLocation(c[0].id);
		return;
	}
	*/
    $("#county").html('<option value="">--Select--</option>');
    for (var i = 0; i < c.length; i++)
    {
    	var item = c[i];
    	$("#county").append('<option value="'+item.name+'">'+item.name+'</option>');
    }
    $("#countyselect").show();
}

function populateCity(c)
{
	if (c.length == 0) return;
	if (c.length == 1)
	{
		selectLocation(c[0].id);
		return;
	}
    $("#city").html('<option value="">--Select--</option>');
    for (var i = 0; i < c.length; i++)
    {
    	var item = c[i];
    	$("#city").append('<option value="'+item.id+'">'+item.name+'</option>');
    }
    $("#cityselect").show();
}

function selectLocation(id)
{
   $("#locID").val(id);
   $("#locationForm").submit();
}	