/**
 * @author wayne?
 * 
 * Use ExtJS SimpleStore Objects for standard form dropdowns
 * ...isn't that awesome, mate!?
 * 
 * Data in SimpleStore should be divided in "value" and "name"
 * 
 */

/**
 * Requires ExtJS SimpleStore input
 * @param {SimpleStore} simplestore Data source
 * @param {String} target Target dropdown element id
 * @param {Boolean} clear Whether to clear old content
 */
function fillDropDownWithStore(simplestore, target, clear){
	if(clear){
		clearDropDown(target);
	}
	
	simplestore.each(function(e){
		addDropDownItem(target, e.data.name, e.data.value);
	});
}

/**
 * @param {String} element Target dropdown element id
 * @param {String} text Dropdown option text
 * @param {String} value Dropdown option submit value
 */
function addDropDownItem(element, text, value){
	var el = document.getElementById(element);
	var option = document.createElement("option");
	option.text = text;
	option.value = value;
	el.options.add(option);
}

/**
 * @param {String} element Target dropdown element id
 */
function clearDropDown(element){
	var el = document.getElementById(element);
	el.length = 0;
}

/**
 * @param {String} element Target dropdown element id
 * @param {String} value Value, which should be selected
 */
function selectDropDownItemByValue(element, val){
	var el = document.getElementById(element);
	for(i=0; i < el.options.length; i++){
		if(el.options[i].value == val){
			el.options[i].selected = true;
		}
	}
}


/**
 * @param {String} element Target dropdown element id
 * @return {String} Returns the selected value as a string
 */
function getSelectedDropDownValue(element){
	var el = document.getElementById(element);
	return el.options[el.selectedIndex].value;
}



