function updateLocationsList() {
	cleanForm();
	$.getJSON(
		'modules/_distribution.php', 
		{id_province:  $('#id_province').val() },
		function(json) {
			select = $('#location').get(0);
			select.length = 1;
			for (i = 0; i < json.length; i++) {
				select.options[i + 1] = new Option(json[i], json[i]);
			}
			if (json.length == 1) {
				select.selectedIndex = 1;
				select.disabled = false;
				updateShopsList();
			} else if (json.length > 0) {
				select.disabled = false;
			}
		}
	);
}

function updateShopsList() {
	cleanForm('location');
	$.getJSON(
		'modules/_distribution.php', 
		{location:  $('#location').val() },
		function(json) {
			select = $('#id_shop').get(0);
			select.length = 1;
			for (i = 0; i < json.length; i++) {
				select.options[i + 1] = new Option( json[i].distributor, json[i].id);
			}
			if (json.length == 1) {
				select.selectedIndex = 1;
				select.disabled = false;
				updateShop();
			} else if (json.length > 0) {
				select.disabled = false;
			}
		}
	);
}

function updateShop() {
	if ($('#id_shop').val() == 0) {
		cleanForm('shops');
		return;
	}
	$('#shops').load(
		'modules/_distribution.php', 
		{lang: idLanguage, id_shop:  $('#id_shop').val() },
		function() { $('#shops .shop').fadeIn('slow') }
	);
}

function cleanForm(level) {
	$('#shops .shop').hide();
	if (level == 'shops') {
		return true;
	}
	select = $('#id_shop').get(0);
	select.length = 1;
	select.disabled = "disabled";
	if (level == 'location') {
		return true;
	}
	select = $('#location').get(0);
	select.length = 1;
	select.disabled = "disabled";
}
