function addItem(form, fromSelectBox, toSelectBox) {
	var from = document.forms[form].elements[fromSelectBox];
	var to = document.forms[form].elements[toSelectBox];
	if (from.selectedIndex != -1) {
		isDuplicate = false;
		for (i = 0; i < to.length; i++) {
			if (to.options[i].value == from.options[from.selectedIndex].value) {
				isDuplicate = true;
			}
		}
		
		if (!isDuplicate) {
			var sel = from.selectedIndex;
			var newItem = new Option(from.options[from.selectedIndex].text, from.options[from.selectedIndex].value);
			to.options[to.length] = newItem;
			from.options[from.selectedIndex] = null;

			if ( sel > from.options.length-1 )
				from.selectedIndex = from.options.length-1 ;
			else
				 from.selectedIndex = sel;

		}
	}
	iSortSelect ( to );
}

function removeItem(form, fromSelectBox, toSelectBox) {
	var from = document.forms[form].elements[fromSelectBox];
	var to = document.forms[form].elements[toSelectBox];
	if (to.selectedIndex != -1) {
		var sel = to.selectedIndex;
		var newItem = new Option(to.options[to.selectedIndex].text, to.options[to.selectedIndex].value);
		from.options[from.length] = newItem;
		to.options[to.selectedIndex] = null;
		if ( sel > to.options.length-1 )
			to.selectedIndex = to.options.length-1 ;
		else
			 to.selectedIndex = sel;
		 iSortSelect ( from );
	}
}

function prepareMultiSelects( form ) {
	var e = document.forms[form].elements;
	for (i = 0; i < e.length; i++) {
		if (e[i].name && e[i].name.substring(0, 9) == 'selected_') {
			var key = e[i].name.substring(9, e[i].name.length);
			if (key.charAt(key.length - 2) == '.') {
				key = key.substring(0, key.length - 2);
			}
			var resultField = 'multi_' + key;
			var selectedValues = document.forms[form].elements[resultField].value;
			for (j = 0; j < e[i].options.length; j++) {
				selectedValues = selectedValues + '|';
				selectedValues = selectedValues + e[i].options[j].value;
			}
			if (selectedValues.charAt(0) == '|') {
				selectedValues = selectedValues.substring(1, selectedValues.length);
			}
			document.forms[form].elements[resultField].value = selectedValues;
		}
	}
}

function initMultiSelects( form ) {
	var e = document.forms[form].elements;
	for (i = 0; i < e.length; i++) {
		if (e[i].name && e[i].name.substring(0, 9) == 'selected_') {
			var key = e[i].name.substring(9, e[i].name.length) + '_selector';
			var selector = document.forms[form].elements[key];
			var selected = e[i];
			var value = -1;
			for (j = 0; j < selected.options.length; j++) {
				value = selected.options[j].value;
				for (k = 0; k < selector.options.length; k++) {
					if ( selector.options[k].value == value ) {
						selector.options[k] = null;
					}
				}
			}
		}
	}
}

var multiSelects = new Array();

function initMultiSelect( name ) {
	multiSelects[multiSelects.length] = name;
	
	var from = document.getElementById ( name+"_from[]" )
	var to   = document.getElementById ( name+"[]" )

    if ( !from ) {
        return;
    }
    
	for (var i=(from.options.length-1); i>=0; i--) {
		if ( hasOption ( to, from.options[i].value ) )
			from.options[i].selected = true;
	}
	
	removeSelectedOptions ( from );

	iSortSelect ( from );
	iSortSelect ( to );
}

function addItems( name ) {
	var from = document.getElementById ( name+"_from[]" );
	var to   = document.getElementById ( name+"[]" );
	moveSelectedOptions ( from, to );
	iSortSelect ( to );
}

function removeItems( name ) {
	var from = document.getElementById ( name+"_from[]" );
	var to   = document.getElementById ( name+"[]" );
	moveSelectedOptions ( to, from );
	iSortSelect ( from );
}

function submitMultiSelects( ) {
	for (var i=(multiSelects.length-1); i>=0; i--) {
		selectAllOptions ( document.getElementById ( multiSelects[i]+"[]" ) );
	}
	
}

