
/* SELECT BOX MANAGING */
// move a line from a selectbox to an other (origine => destination)
	function setSelected(orig,dest) {
		var origine=$(orig);
		var destination=$(dest);
		
		var origineLenght=origine.options.length;
		var destinationLenght=destination.options.length;
		
		for (var i = 0; i < origine.options.length; i++) {
			// line is selected
			if (origine.options[i].selected) {
				var isNew=true;
				// check if line is already selected (=> isNew=true)
				for (var j = 0; j<destinationLenght; j++) {
					if (destination.options[j].value==origine.options[i].value) {
						isNew=false;
					}
				}
				// line is not in the dest select
				if (isNew) {
					destination.options.length++;
					destination.options[destination.options.length-1].value=origine.options[i].value;
					destination.options[destination.options.length-1].text =origine.options[i].text ;
					origine.options[i].selected=false;
					origine.options[i].selected="";
					origineLenght--;
					for (var j=i; j<origineLenght; j++) {
						origine.options[j].value=origine.options[j+1].value;
						origine.options[j].text=origine.options[j+1].text;
						if (origine.options[j+1].selected=="selected" || origine.options[j+1].selected) {
							origine.options[j].selected="selected";
							origine.options[j].selected=true;
							origine.options[j+1].selected="";
							origine.options[j+1].selected=false;
						}
					}
					origine.options.length--;
					i--;
				}
			}
  		}
	}
	
	// delete a line from a selectbox
	function setUnselected(SelectName) {
		var destination=$(SelectName);
		var destinationLenght=destination.options.length;
		
		for (var i = 0; i < destinationLenght; i++) {
			if (destination.options[i].selected) {
				for (var j = i; j < destinationLenght-1; j++){
					destination.options[j].text =		destination.options[j+1].text;
					destination.options[j].value =		destination.options[j+1].value;
					destination.options[j].className =	destination.options[j+1].className;
				}
				destination.options.length--;
				destinationLenght--;
			}
		}
	}
	
	//move up a line in a selectbox
	function SwitchUp(elmnt) {
		return IntervertLine(-1,0,elmnt);
	}
	
	//move down a line in a selectbox
	function SwitchDn(elmnt) {
		return IntervertLine(1,$(elmnt).options.length,elmnt);
	}

	// intervertis deux ligne dans une selectBox
	function IntervertLine(inc,returnValue,elmnt) {
		var list=$(elmnt);
		var selectedIndex=list.selectedIndex;
		if (selectedIndex==-1 || selectedIndex==returnValue) {
			return;
		}

		var other=selectedIndex+inc;

		var text=list.options[selectedIndex].text;
		var value=list.options[selectedIndex].value;
		//var class=list.options[selectedIndex].className;

		list.options[selectedIndex].text=list.options[other].text;
		list.options[selectedIndex].value=list.options[other].value;
//		list.options[selectedIndex].className=list.options[other].className;		
		
		list.options[other].text=text;
		list.options[other].value=value;
	//	list.options[other].className=class;
		
		list.options[selectedIndex].selected='';
		list.options[other].selected='selected';
	}
	
	// selectionne deux ligne d'une checkbox
	function selectAll(SelectName) {
		var list=$(SelectName);
		var listLenght=list.options.length;
		
		for(var i = 0; i < listLenght; i++) {
			list.options[i].selected=true;
			list.options[i].selected = "selected";
		}
	}
	
	/**
	 * déselectionnnes Toutes les lignes d'un selectBox
	 */
	function unSelectAll(SelectID) {
		var list=$(SelectID);
		var listLenght=list.options.length;
		
		for(var i = 0; i < listLenght; i++) {
			list.options[i].selected=false;
			list.options[i].selected = "";
		}
	}
	
	/**
	 * Selectionne dans un selectBox
	 */
	function setSelectedByValue(SelectId, aValue, onlyThese) {
		var ValueLength = aValue.length;
		
		var SelectLength = $(SelectId).options.length;
		
		if (!SelectLength) {
			return;
		}
		
		if (onlyThese == true) {
			unSelectAll(SelectId);
		}
		
		for (i=0; i< SelectLength; i++) {
			Line = $(SelectId).options[i];
			for (j=0; j < ValueLength; j++) {
				if (aValue[j] == Line.value) {
					Line.selected = true;
					Line.selected = "selected";
				}
			}
		}
	}
/* / SELECT BOX MANAGING */
