/** 
emptyList removes the options from the select control passed to it
ctl = the select control 
*/
function emptyList(ctl){
	while (ctl.options.length > 0){
		ctl.options[0] = null;
	}
}

/**
selectAllInList selects each item in the list
*/
function selectAllInList(ctl){
	for (var i=0; i < ctl.options.length; i++){
         ctl.options[i].selected = true;
	}
}

/**
getIdsFromList returns all the values / ID's from the list control
Not the same as the text
*/
function getValuesFromList(ctl){
	var arr = new Array()
	for (var i=0; i < ctl.options.length; i++){
		arr.push(ctl.options[i].value);
	}
	return arr;
}

/**
getSelectedValue returns the selected items value from the list control
*/
function getSelectedValue(ctl){
	return ctl.options[ctl.selectedIndex].value;
}	
/**
getSelectedId returns the selected items id from the list control
*/
function getSelectedId(ctl){
	return ctl.options[ctl.selectedIndex].id;
}
/**
getSelectedText returns the selected items text from the list control
*/
function getSelectedText(ctl){
	return ctl.options[ctl.selectedIndex].text;
}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}
	
	
// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}	