//  ajaxCache stores json data 
// (used in refillSelectList())
window.ajaxCache = new Object();



// refill content of a select, like country, region or city lists....
function refillSelectList(path, selectId, preselect) {
		
		if (ajaxCache[path]) {
			
			json = ajaxCache[path];
			return buildSelect(json, selectId, preselect);
			
		} else {
		
			$.ajax({
			  url: path,
			  cache: true,
			  dataType: 'json',
			  success: function(json){
		    	
					ajaxCache[path] = json;
					return buildSelect(json, selectId, preselect);
				
			  }
			});
		}
}

function buildSelect(json, selectId, preselect) {
	
	$('#' + selectId).removeAttr('disabled');				
	var select = document.getElementById(selectId);
	select.innerHTML = null;
	
	for (var i=0; i < json.length; i++) {
			var option = document.createElement('option');
			option.value = json[i].value;
			option.innerHTML = json[i].name;
			
			if (preselect == json[i].value) {
				option.selected = 'selected';
			}	
			
			$(select).append(option);
	}
	
	if (json.length < 2) {
		$('#' + selectId).attr('disabled', 'disabled');				
		return false;
	}
	
	return true;
	
}






