/* ================================================================
= by Thomas Balthazar - v0.2 - 2005/09/07		                  =
= 	changelog : when values pass from a <select> to another,	  =
= 	they are selected (.selected=true)							  =
=                                                                 =
= by Thomas Balthazar - v0.1 - 2005/09/02                         =
=                                                                 =
= This script allows you to pass values                           =
= between 2 multiselect.                                          =
=                                                                 =
= Usage :                                                         =
= 1. include this .js file in your .html page                     =
= 2. create 2 mutli-select, giving them id's                      =
=		e.g. <select id="from"> and <select id="to">              =
= 3. create 2 links  :                                            =
= 		>> : onclick="javascript:addSelectedValues('from','to')"  =
= 		<< : onclick="javascript:addSelectedValues('to','from')"  =
= 4. that's it                                                    =
=                                                                 =
================================================================ */


// ******************
// *** START PRIVATE
// ******************

// *** return an array containing the <option> of a multi <select>
// *** if aBlnSelectedOnly is true, only return selelcted <option>
function getOptions(aObjSelect,aBlnSelectedOnly) {
	var nbSelectedOptions = 0 ;
	var arrSelected = new Array();
	
	// *** get the nb of available option in the <select>
	nbSelectedOptions = aObjSelect.options.length ;
	
	// *** for each option, test if selected
	for(i=0;i<nbSelectedOptions;i++) {
		if(aBlnSelectedOnly) {
			if(aObjSelect.options[i].selected) {
				value = aObjSelect.options[i].value ;
				label = aObjSelect.options[i].text ;
				arrSelected[value] = label ;
			}
		}
		else {
			value = aObjSelect.options[i].value ;
			label = aObjSelect.options[i].text ;
			arrSelected[value] = label ;
		}
	}
	
	return arrSelected ;
}


// *** add an array of <option> to a <select> tag
function addOptionsTo(aArrValues,aArrAlready,aObjSelect) {
	for(key in aArrValues) {
		// *** if not already in
		if(aArrAlready[key]==undefined) {
			nextId = aObjSelect.options.length ; 
			aObjSelect.options[nextId] = new Option(aArrValues[key],key) ; 
			aObjSelect.options[nextId].selected=true;
		}
	}
}


// *** remove an array of <option> from a <select> tag
function removeOptionsFrom(aArrOptions,aObjSelect) {
	var length 	= aObjSelect.options.length ;
	for(i=0;i<length;i++) {
		optionValue = aObjSelect.options[i].value  ;
		// *** if <option> is in the array -> remove it
		if(aArrOptions[optionValue]!=undefined) {
			aObjSelect.options[i] = null ;
			i-- ;
			length-- ;
		}
	}
}

// ******************
// *** END PRIVATE
// ******************



// ******************
// *** START PUBLIC
// ******************

// *** this is the main function to call
function	addSelectedValues(aFromId,aToId, blnAll, blnOnlySelected) {
	var objFrom 			= document.getElementById(aFromId) ;
	var objTo 				= document.getElementById(aToId) ;
	// var blnAll 				= false ;
	// var blnOnlySelected 	= true ;
	var blnAll = (blnAll == null) ? false : blnAll;
	var blnOnlySelected = (blnOnlySelected == null) ? true : blnOnlySelected;
	var arrSelected, arrAlready ;
	
	// *** 1. get the selected <option> in the source <select>
	arrSelected	= getOptions(objFrom,blnOnlySelected) ;
	
	// *** 2. get all the <option> in the destination <select> 
	arrAlready = getOptions(objTo,blnAll) ;
	
	// *** 3. add the selected <option> in the destination <select>
	addOptionsTo(arrSelected,arrAlready,objTo) ;
	
	// *** 4. remove selected <option> from the source <select>
	removeOptionsFrom(arrSelected,objFrom) ;

} 

// *** this function allows you to add/remove all values
function addAllValues(aFromId, aToId) {
	addSelectedValues(aFromId,aToId, true, false);
}

// ******************
// *** END PUBLIC
// ******************
