var SmartListbox = {
	baseUrl: 'api/smartlist.php',
	request: function(oListbox, area, action, key, lastkey, tip) {
		var url = encodeURI(SmartListbox.baseUrl + '?area=' +  area + '&action=' + action + '&key=' + key +'&lastkey=' + lastkey);
		var request = new Json.Remote(url, {
			onComplete: function(jsonObj) {
				SmartListbox.process(oListbox,jsonObj,tip);
			}
		}).send();
	},
	
	process: function(oListbox, content, tip) {
		oListbox.disabled = 'disabled';
		SmartListbox.disableDependence(oListbox);
		//oListbox.empty();
		SmartListbox.clearListbox(oListbox);
		SmartListbox.addOption(oListbox, '请选择'+ tip, '');
		content.each(function(item) {
			SmartListbox.addOption(oListbox, item, item);
		});
		oListbox.disabled = '';
	},
	
	clearListbox: function(oListbox) {
		for(var i=oListbox.options.length-1; i >= 0; i--) {
			//alert(i);
			oListbox.empty(i);
		}	
	},
	
	addOption: function(oListbox, sName, sValue) {
		var oOption = document.createElement("option");
		oOption.appendChild(document.createTextNode(sName));
		if(arguments.length == 3) {
			oOption.setAttribute("value",sValue);
			
		}
		oListbox.appendChild(oOption);		
	},
	
	disableDependence: function(oListbox)
	{
		if(!oListbox.dependence)
			return;
		SmartListbox.disableDependence(oListbox.dependence);
		oListbox.dependence.selectedIndex = 0;
		oListbox.dependence.disabled = "disabled";
	}
};
