function getCityList() {
	var state = dojo.byId("state_id");
	var country = dojo.byId("country_id");
	// console.log("getCityList");
	
	if (isIE) {
		if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
			document.location = 'add_place_info.php?prov='+state.value+"&country="+country.value;
		} else {
			var ajax = new Ajax.Request('./ajax/getCityList.php?state='+state.value, {method: 'get', onComplete: cityListHandler});
		}
	}
}

function cityListHandler(response) {
	
	var json = eval(response.responseText);
	var cities = json.cities;

	// Zero out the current list
	listNode = dojo.byId("city_id" );
	listNode.innerHTML = "" ;
	var listItem = document.createElement("option");
	listItem.value = 0;
	listItem.innerHTML = "Selecciona municipi";
	listNode.appendChild(listItem);
	for (var i=0; i<cities.length; i++) {
		var listItem = document.createElement("option");
		listItem.value = cities[i].city_id;
		listItem.innerHTML = cities[i].name;
		// console.log(cities[i].city_id+" "+cities[i].name);
		listNode.appendChild(listItem);
	}
	listNode.disabled = false;
}

function getStateList() {
	var state = dojo.byId("state_id");
	var country = dojo.byId("country_id");
	
	// limpia lista de municipios
	listNode = dojo.byId("city_id" );
	listNode.innerHTML = "" ;
	listNode.innerHTML = "" ;
	var listItem = document.createElement("option");
	listItem.value = 0;
	listItem.innerHTML = "Selecciona municipi";
	listNode.appendChild(listItem);
	
	listNode.disabled = true;
	
	if (isIE) {
		if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
			document.location = 'add_place_info.php?prov='+state.value+"&country="+country.value;
		} else {
			var ajax = new Ajax.Request('./ajax/getStateList.php?country='+country.value, {method: 'get', onComplete: stateListHandler});
		}
	}
}

function stateListHandler(response) {
	var json = eval(response.responseText);
	var states = json.states;

	// Zero out the current list
	listNode = dojo.byId("state_id" );
	listNode.innerHTML = "" ;
	var listItem = document.createElement("option");
	listItem.value = 0;
	listItem.innerHTML = "Selecciona provincia";
	listNode.appendChild(listItem);
	for (var i=0; i<states.length; i++) {
		var listItem = document.createElement("option");
		listItem.value = states[i].state_id;
		listItem.innerHTML = states[i].name;
		// console.log(states[i].city_id+" "+states[i].name);
		listNode.appendChild(listItem);
	}
	listNode.disabled = false;
}

function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
