function LoadRegions(){
	jQuery("#region_id").empty();
	jQuery("<option></option>").html('Loading...').appendTo(jQuery("#region_id"));
	jQuery("#city_id").empty();
	jQuery("<option></option>").html('Loading...').appendTo(jQuery("#city_id"));
	loadGeoData('regions', jQuery("#country_id").val(), createSelectRegions)
}
function createSelectRegions(data){
	var sel = jQuery("#region_id");
	sel.empty();
	jQuery.each(data, function(i,item){
		jQuery("<option></option>")
			.attr("value", i)
			.html(item)
			.appendTo(sel);
	});
	sel.get(0).selectedIndex = 0;
	LoadRegion();
}
function LoadRegion(){
	jQuery("#city_id").empty();
	jQuery("<option></option>").html('Loading...').appendTo(jQuery("#city_id"));
	loadGeoData('cities', jQuery("#region_id").val(), createSelectCities)
}
function loadGeoData(a,id,f){
	jQuery.getJSON('/navapp.php?do=ajax:Regions',{ "action":a, "id":id },f);
}
function createSelectCities(data){
	var sel = jQuery("#city_id");
	sel.empty();
	jQuery.each(data, function(i,item){
		jQuery("<option></option>")
			.attr("value", item.city_id)
			.html(item.city_name)
			.appendTo(sel);
		codes[item.city_id] = item.city_code;
	});
	sel.get(0).selectedIndex = 0;
}
function calc_code(){
	if(10 > jQuery("#phone").val().length){
		jQuery("#phone").attr("value", '+' + country_codes[jQuery("#country_id").val()] + '(' + codes[jQuery("#city_id").val()] + ')'+jQuery("#phone").val());
	}
}
function initRegions(c){
	if(typeof(jQuery) == 'function'){
		jQuery("#country_id").bind("change", LoadRegions );
		jQuery("#region_id").bind("change", LoadRegion );
		jQuery("#phone").bind("focus", calc_code );
	}else{
		if(c--)
			setTimeout('initRegions(' + c + ')', 500);
	}
}
