var loadSelectPhrases = { loading : 'Подгрузка данных...', trash : 'удалено', special : 'служебн.' };

function clearSelect(select){
	while(select.options.length)
		select.remove(0);
}

function addSelectOption( selectElement, optionText, optionValue, selected )
{
	var oOption = document.createElement("OPTION");

	oOption.text = optionText;
	oOption.value = optionValue;
	oOption.selected = selected;

	selectElement.options.add(oOption);

	return oOption;
}

function loadSelect(select, url, selected, onLoad){
	if(typeof(select) == 'string'){
		select = document.getElementById(select);
	}
	
	if(!url)
		return false;
	
	var oXML = new FCKXml();
	while(select.options.length)
		select.remove(0);

	addSelectOption(select, loadSelectPhrases.loading, '');
	
	$(select).trigger('refresh');
	
	oXML.LoadUrl(url, function(xml){
			while(select.options.length)
				select.remove(0);
				
			var items = xml.SelectNodes('data/item');
			var nodeName;
			
			for(var i = 0; i < items.length; i++){
				nodeName = items[i].attributes.getNamedItem('name').value.replace(/&#34;/g, '"').replace(/&amp;/g, '&');
				if (items[i].attributes.getNamedItem('trash') && items[i].attributes.getNamedItem('trash').value == '1') {
					nodeName = nodeName + ' --' + loadSelectPhrases.trash + '--';
				}
				if (items[i].attributes.getNamedItem('special') && items[i].attributes.getNamedItem('special').value == '1') {
					nodeName = nodeName + ' --' + loadSelectPhrases.special + '--';
				}
				addSelectOption(select, nodeName, items[i].attributes.getNamedItem('value').value, selected == items[i].attributes.getNamedItem('value').value);
			}
			
			if(typeof(onLoad) == 'function')
				onLoad(select, xml);
			
			$(select).trigger('refresh');
		});
}
