
/** 
* Allows moving option element within two select elements and keeps 
* a list of chosen elements in a hidden input
* 
* @param leftSelectionId	Leftmost selection-element id
* @param rightSelectionId	Rightmost selection-element id
* @param resultFieldId		Hidden field id
*/
function OnestaSelectTool(leftSelectionId, rightSelectionId, resultFieldId){
	/**
	* @param id	String containing the wanted element id
	* @returns reference to the wanted element
	*/
	var $ = function (id){return document.getElementById(id);}
	
	
	/** Should the selections be unselected after operation? */
	this.removeSelection=false;
	/** What's the data separator? */
	this.dataSeparator=';';
	/** Should the option-elements be sorted automatically? */
	this.autoSort=true;
	/** Should the select-element widths be frozen on first move? */
	this.fixedWidths=true;
	
	
	var leftWidth=null;
	var rightWidth=null;
	this.resultField = resultFieldId;
	var me=this;
	
	
	
	/**
	* Move chosen items from the left select to right
	*/
	this.add=function(){
		move($(leftSelectionId),$(rightSelectionId));
	}
	
	/**
	* Move chosen items from the right select to left
	*/
	this.del=function(){
		move($(rightSelectionId),$(leftSelectionId));
	}
	
	/**
	* Internal function for adding and deleting
	* @param fromSel	Move from this select-element
	* @param toSel		Move to this select-element
	*/
	function move(fromSel,toSel){
		if(leftWidth==null){leftWidth=$(leftSelectionId).offsetWidth;}
		if(rightWidth==null){rightWidth=$(rightSelectionId).offsetWidth;}
		
		if (fromSel.selectedIndex >= 0) {
			var selected=[];
			for (i = fromSel.length - 1; i>=0; i--) {
			
				if (fromSel.options[i].selected) {
					selected.push(fromSel.options[i])
				}
			}
			
			for (i=selected.length-1;i>=0;i--) {
				toSel.appendChild(selected[i])
			}
			
			
			if(me.removeSelection){
    			fromSel.selectedIndex = -1;
    			toSel.selectedIndex = -1;
			}
		}
		me.rewriteResultField();
		if(me.autoSort){
			me.sort();
		}
		if(me.fixedWidths){
			$(leftSelectionId).style.width=leftWidth+"px";
			$(rightSelectionId).style.width=rightWidth+"px";
		}
	}
	
	/**
	* Write all the ids from right select-element to the hidden field
	*/
	this.rewriteResultField = function(){
		var result=[];
		var to = $(rightSelectionId);
		for (i=0;i<to.length;i++) {
			result.push(to[i].value);
		}
		$(me.resultField).value=result.join(this.dataSeparator);
	}
	
	/**
	* Function for sorting both select-elements
	*/
	this.sort = function(){
		sortSelect($(leftSelectionId));
		sortSelect($(rightSelectionId));
	}
	
	/**
	* Internal function for sorting a chosen select-element
	* @param sel	Chosen select-element
	*/
	function sortSelect(sel){
		var o=[];
		if(sel==null||sel.options==null){return false;}
		for(var i=0;i<sel.options.length;i++){
			o.push(new Option(sel.options[i].text,sel.options[i].value,sel.options[i].defaultSelected,sel.options[i].selected))
		}
		o=o.sort(
				function(a,b){
					if ((a.text+"") < (b.text+"")) { return -1; }
					if ((a.text+"") > (b.text+"")) { return 1; }
					return 0;
				}
			);
		for(var i=0;i<sel.options.length;i++){
			sel.options[i]=o[i];
		}
	}
	
}