
var vector = new Array();
$j(document).ready(function() {

		var country_select = $j('select#country_list');
		if(country_select.val()!=0 && country_select.val()!=null){
			country = country_select.val();
			if(!vector[country]){
				populate();
			}else{
				populatefromarray();
			}
		}
		country_select.change(function(){
			country = $j('select#country_list').val();
			if(!vector[country]){
				populate();
			}else{
				populatefromarray();
			}
		})
});
function populate(){
	var i=1;
	var country = $j('select#country_list').val();
	var region_selected_id = document.getElementById('region_selected_id').value;
	if(country!=0){
		$j("div#error_for_country_list").hide();
		jQuery.getJSON('/region-list/'+country, function(data) {
			with($j("select#region_list")) {
				empty();
				append("<option value=\"0\">"+scriptFeeds_regionList_select+"</option>");
			}

			vector[country] = new Array();
			jQuery.each(data, function(idx, region) {
				if (region["current_owner_id"]!=country && region["current_owner_id"]!=''){
					current_owner = " (annexed by "+region["current_owner_name"]+")";
				}
				else{
					current_owner = "";
				}
				if (region["id"] == region_selected_id){
					$j("select#region_list").append("<option value=\""+region["id"]+"\"SELECTED >"+region["name"]+current_owner+"</option>");
				} else {
					$j("select#region_list").append("<option value=\""+region["id"]+"\">"+region["name"]+current_owner+"</option>");
				}
				vector[country][i]="<option value=\""+region["id"]+"\">"+region["name"]+current_owner+"</option>";
				i++;
			});
			vector[country][0] = i;
			$j("div.region_list").fadeIn();
		});
	}
	else{
		$j("div#error_for_country_list").show();
		$j("div.region_list").fadeOut();
	}
};

function populatefromarray(){
	$j("div#error_for_country_list").hide();
	with($j("select#region_list")) {
		empty();
		append("<option value=\"0\">"+scriptFeeds_regionList_select+"</option>");
	}
	for(var k=1; k<vector[country][0]; k++){
		$j("select#region_list").append(vector[country][k]);
	}
	$j("div.region_list").fadeIn();
};