$(document).ready(function() {	
	// Change event for City Selection
	$('#selectCity').change(function(){
		var chosen = $('#selectCity :selected').text();
		$('#selectState option').each(function (index) {
			if (chosen.indexOf(", " + $(this).val()) > 0) {
				$('#selectState').attr('selectedIndex', index);
			}
		});
			
	});
	
	// Change event for State Selection
	$('#selectState').change(function () {
		chosen = $('#selectState option:selected').val();
		
		// First remove City List
		$('#selectCity option').each(function (index) {
			if (index > 0) { $(this).remove(); }
		});
		
		// Now rebuild City List
		for (index in cities) {
			if ( chosen == "" || cities[index].indexOf(", " + chosen) > 0 ) {
				comma = cities[index].indexOf(",");
				value = cities[index].substr(0, comma);
				$('#selectCity').append("<option value='" + value + "'>" + cities[index] + "</option>\n");
			}
		}
		
	});
});
