country_name = false;
state_name = false;
city_name = false;
	
function get_states(form) {
	var country = form.find("select[name=country]");
	var states = form.find("select[name=state]");
	states.html("<option>Loading...</option>").prop("disabled", true);
	$.post("/lookup.php", { country: country.val() }, function(data) {
		var opt = $("<option>").val(0).text("-- " + country.find("option:selected").text() + " --");
		states.html("").append(opt);
		for (i in data) {
			var opt = $("<option>").val(i).text(data[i]);
			states.append(opt);
			if (state_name == data[i]) {
				opt.prop("selected", true);
				get_cities(form);
			}
		}
		states.prop("disabled", false);
	}, "json");
}

function get_cities(form) {
	var country = form.find("select[name=country]");
	var state = form.find("select[name=state]");
	var cities = form.find("select[name=city]");
	cities.html("<option>Loading...</option>").prop("disabled", true);
	$.post("/lookup.php", { state: state.val() }, function(data) {
		var opt1 = $("<option>").val(0).text("-- " + country.find("option:selected").text() + " --");
		var opt2 = $("<option>").val(0).text("-- " + state.find("option:selected").text() + " --");
		cities.html("").append(opt1).append(opt2);
		for (i in data) {
			var opt = $("<option>").val(i).text(data[i]);
			cities.append(opt);
			if (city_name == data[i] && state_name == state.find("option:selected").text()) {
				opt.prop("selected", true);
			}
		}
		cities.prop("disabled", false);
	}, "json");
}

$(function() {
	$("form select[name=country]").live("change", function() {
		var form = $(this).closest("form");
		form.find("select[name=city],select[name=state]").html("").prop("disabled", true);
		get_states(form);
	});
	
	$("form select[name=state]").live("change", function() {
		var form = $(this).closest("form");
		$(this).find("select[name=city]").html("").prop("disabled", true);
		get_cities(form);
	});
});

