var SelectTools = new Object();

SelectTools.clear = function(select){
	select = this._find(select);
	while(select.options.length)
		select.remove(0);
}

SelectTools._find = function(select){
	if(typeof(select) == 'string')
		return document.getElementById(select);
	else
		return select;
}

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

	oOption.text = optionText;
	oOption.value = optionValue;
	selectElement.options.add(oOption);

	oOption.selected = selected;

	return oOption;
}

SelectTools.clearAdd = function(select, value, text){
	select = this._find(select);
	
	this.clear(select);
	this.addOption(select, value, text, true);
}

SelectTools.fill = function(select, elements, selectedElement){
	select = this._find(select);
	for(var i in elements){
		SelectTools.addOption(select, elements[i][0], elements[i][1], selectedElement == elements[i][0]);
	}
}

SelectTools.makeData = function(data, value, text){
	var out = new Array();
	if(typeof(value) == 'function' && typeof(text) == 'function'){
		for(var i in data){
			out.push([value(data[i]), text(data[i])]);
		}
	}
	else if(typeof(value) == 'function'){
		for(var i in data){
			out.push([value(data[i]), data[i][text]]);
		}
	}
	else if(typeof(text) == 'function'){
		for(var i in data){
			out.push([data[i][value], text(data[i])]);
		}
	}
	else{
		for(var i in data){
			out.push([data[i][value], data[i][text]]);
		}
	}
	
	return out;
}