function swapOptions(list, i1, i2) {
	var t1 = list.options[i1].text;
	var v1 = list.options[i1].value;

	var t2 = list.options[i2].text;
	var v2 = list.options[i2].value;

	list.options[i1].text = t2;
	list.options[i1].value = v2;

	list.options[i2].text = t1;
	list.options[i2].value = v1;

	if (list.form.elements['O'+v1] != null) {
		list.form.elements['O'+v1].value = i2;
		list.form.elements['O'+v2].value = i1;
	}
}

function sortOptions(list) {
	for (var i = 0; i < list.options.length - 1; i++)  {
		for (var j = i + 1; j < list.options.length; j++)  {
			if (list.options[i].text > list.options[j].text)  {
				swapOptions(list, i, j);
			}
		}
	}
}

function moveOption(l1, l2) {
	for (var i = l1.length - 1; i >= 0; i--) {
		if (l1.options[i].selected) {
			var text = l1.options[i].text;
			var value = l1.options[i].value;

			l2.options[l2.options.length] = new Option(text, value);
			l2.options[l2.options.length - 1].selected = true;

			for (j = i; j < l1.length - 1; j++) {
				l1.options[j].text = l1.options[j + 1].text;
				l1.options[j].value = l1.options[j + 1].value;

				if (j == i)
					l1.options[j].selected = true;

			}

			l1.length--;

			if (l1.form.elements['L'+value] != null)
				l1.form.elements['L'+value].value = l2.name;
		}
	}
}

function optionUp(list) {
	if (list.selectedIndex > 0) {
		swapOptions(list, list.selectedIndex, list.selectedIndex - 1);
		list.selectedIndex--;
	}
}

function optionDown(list) {
	if (list.selectedIndex >= 0 && list.selectedIndex < list.length - 1) {
		swapOptions(list, list.selectedIndex, list.selectedIndex + 1);
		list.selectedIndex++;
	}
}

function fillSelect(l1, l2, separator) {
	if (separator == null)
		separator = ',';

	var current = l1.options[l1.options.selectedIndex];
	var hidden = l1.form.elements[l2.name+current.value];

	if (hidden != null) {
		var array = hidden.value.split(separator);
		var value;

		l2.options.length = 0;

		for (var i = 0; i < array.length; i++) {
			if ((i & 1) == 1)
				l2.options[l2.options.length] = new Option(array[i], value);
			else
				value = array[i];
		}
	}
}
