$(function() {
	if ($("#region_id option:first").text() != '' && $("#region_id option:first").attr('value') == '') {
		var startRegions = $("#region_id option:first").text();
	}

	$("#country_id").live('change', function() {

		var form = $(this).parents('form');

		$(form).find("#region_id option").removeAttr('selected');
		$(form).find("#city_name").val('');
		$(form).find("#city_id").val('');

		if ($(form).find("#region_id").html() != null) {

			var countryId = $(this).val();
			var url = rootPath+"admin/member_ajax_location.php";
			$.getJSON(
				url,
				{
					id : countryId,
					area : 'region'
				},
				function(listing) {
					var select = $(form).find("#region_id");
					var start = $(form).find("#region_id option:first").text();
					if (start == "Aucune") {
						select.html("<option selected='selected'>Aucune</option>");
					} else if (startRegions != null) {
						select.html("<option selected='selected'>"+startRegions+"</option>");
					} else {
						select.html('');
					}
					select.append("<option value=''>Toutes</option>");
					for(var i = 0; i < listing.length; i++)  {
						region = listing[i];
						select.append("<option value='" + region.region_id + "'>" + region.name + "</option>");
					}
				}
			);
		}
	});

	$("#region_id").live('change', function() {
		$("#city_name").val('');
		$("#city_id").val('');
	});
	
});
