
emmapp.selectionlist = {
		
	setSelectOptionHtml: function(/*DOM*/e, /*String*/content) {
	//console.debug("1:" + content);
	
		if ( e != null ) {		
			if ( content == null || content == "") {
				content = "<option value=\"\" selected=\"selected\"></option>";
			}
			
			try {
			
			if (dojo.isIE) {
				//alert("I am the stupid IE");
				// the first option got lost in IE, this is a dirty workaround
	 			var html = "<option value=\"\">---</option>" + content;
	 			e.innerHTML = html;	 			
				var re = new RegExp("(\)(.*?)(\)");
				//if(e.outerHTML) e.outerHTML = e.outerHTML.replace(re, "$1" + html + "$4");
				if(e.outerHTML) e.outerHTML = e.outerHTML.replace(re, "$1");
				
			} else {
				e.innerHTML = content;				
			}
			
			} catch (error) {
				console.error("emmapp.selectionlist.setSelectOptionHtml error: " + error);
			} finally {

			}

		}
	},

	removeOptionsByText: function(selectName, text) {
	  for (var i=selectName.options.length-1; i>=0; i--) {
	    if (selectName.options[i].text == text) {
	      selectName.options[i] = null;
	    }
	  }
	},

	removeOptionsByValue: function(selectName, value) {
	  for (var i=selectName.options.length-1; i>=0; i--) {
	    if (selectName.options[i].value == value) {
	      selectName.options[i] = null;
	    }
	  }
	},
	setOptionsByValue: function(selectName, value) {
		var optionsSize = parseInt(selectName.options.length)-1;
		  for (var i=optionsSize; i>=0; i--) {
		    console.log(selectName+"   value |"+selectName.options[i].value+"|");
			if (selectName.options[i].value == value) {
		    	selectName.options[i].selected = "selected";
		    	
		    }
		  }
		},
	removeAllOptions: function(selectName) {
		  for (var i=selectName.options.length-1; i>=0; i--) {
		    if (selectName.options[i].value != '') {
		      selectName.options[i] = null;
		    }
		  }
		},
	addNewOption: function(selectName, text, value) {
	      var index = selectName.options.length;
	      var optionName = new Option(text, value);
	      selectName.options[index] = optionName;
	},

	// switch option index1 and index2
	switchOptions: function(selectName, index1, index2) {
	      var size = selectName.options.length;
	      if ( index1 < 0 || index1 >= size ) return;

	      var optionName1 = new Option(selectName.options[index1].text, selectName.options[index1].value);
	      var optionName2 = new Option(selectName.options[index2].text, selectName.options[index2].value);
	      selectName.options[index1] = optionName2;
	      selectName.options[index2] = optionName1;
	},

	listOptionMoveUp: function(selectName) {
//				alert("Selection list move up - " + selectName.name);

	      var index = selectName.options.selectedIndex;
//				alert(index);

	      if (index == 0 ) return; 
	      this.switchOptions(selectName, index, index-1); 
		selectName.options(index-1).selected = true;
	},

	listOptionMoveDown: function(selectName) {

	//alert("Selection list move down - " + selectName.name);

	      var index = selectName.options.selectedIndex;
	      if (index == selectName.options.length-1 ) return; 
	      this.switchOptions(selectName, index, index+1); 

		selectName.options(index+1).selected = true;
	},

	// move the selected options in the source selection to the destination selection
	switchSelectedOptions: function(source, destination) 
	{
	//alert("switch");

	/*
	  var indx = destination.options.length;

	  for (var i=source.options.length-1; i>=0; i--) 
	  {
	    	if (source.options[i].selected) 
	  	{
			// add this option to the destination
			addNewOption(destination, source.options[i].text, source.options[i].value)
			
			// delete the option from the source
	      		source.options[i] = null;
	    	}
	  }
	*/

		this.moveListItem(source, destination);

	},

	moveListItem: function(fromListBox, toListBox)
	 {

		var i, j, maxElem;
		maxElem = fromListBox.options.length;
		j = 0;
		for (i = 0; i < maxElem; i++) 
		{
			if (fromListBox.options(j).selected)
			 {
				var objOp = document.createElement ("OPTION");
				objOp.text = fromListBox.options(j).text;
				objOp.value = fromListBox.options(j).value;
				toListBox.add (objOp);
				fromListBox.remove (j);
			} 
			else 
			{
				j++;
			}
		}

	},

	selectAllListItems: function(listBox)
	{
		// select all items in the select list, this allows the options being submitted to the server
		for (i = 0; i < listBox.options.length; i++) 
		{
			listBox.options(i).selected = true;
		}
	}		
};
