function loadSelections(parameterName)
{
	sourceOptions = getElement("excluded_" + parameterName).options;
	sort(getElement("excluded_" + parameterName));
	includedOptions = getElement(parameterName).value;

	if (includedOptions != "")
	{
		var values = includedOptions.split("|");

		for (var i = 0; i < values.length; i++)
		{
			for (var x=sourceOptions.length - 1; x >= 0; x--)
			{
				if (sourceOptions[x].value == values[i])
				{
					sourceOptions[x].selected = true;
				}
			}
		}

		moveSelection(parameterName);
	}
}

function moveAll(parameterName, reverse)
{
	moveSelection(parameterName, reverse, true);
}

function moveSelection(parameterName, reverse, all)
{
	var sourceName = "";
	var destinationName = "";

	if (reverse)
	{
		sourceName = "included_" + parameterName;
        destinationName = "excluded_" + parameterName;
	}
	else
	{
		sourceName = "excluded_" + parameterName;
		destinationName = "included_" + parameterName;
	}

	sourceSelectList = getElement(sourceName);
	sourceOptions = sourceSelectList.options;
	destinationSelectList = getElement(destinationName);
	destinationOptions = destinationSelectList.options;
	parameter = getElement(parameterName);

	for (var i = sourceOptions.length - 1; i >= 0; i--)
	{
		if (all || sourceOptions[i].selected)
		{
			insertNewOptionToList(destinationSelectList, sourceOptions[i].text, sourceOptions[i].value);
			sourceOptions[i] = null;
		}
	}

	sort(sourceSelectList);
	sort(destinationSelectList);

	if (reverse)
	{
		includedOptions = sourceOptions;
	}
	else
	{
		includedOptions = destinationOptions;
	}

	var value = "";

	for (var i = 0; i < includedOptions.length; i++)
	{
		if (i == 0)
		{
			value = includedOptions[i].value;
		}
		else
		{
			value = value + "|" + includedOptions[i].value;
		}
	}

	parameter.value = value;
}

function insertNewOptionToList(list, text, value)
{
	var newOption = document.createElement('option');
    newOption.text = text;
    newOption.value = value;

    try
	{
    	list.add(newOption, null);
    }
	catch (exception)
	{
		list.add(newOption);
	}
}

function sort(list)
{
	var options = list.options;
	var allOptions = [];

	for (var i = 0; i < options.length; i++)
	{
		allOptions[i] =
		{
			text:options[i].text,
			value:options[i].value
		};
	}

	allOptions.sort(alphabeticalSort);
	clearList(list);

	for (var i = 0; i < allOptions.length; i++)
	{
		insertNewOptionToList(list, allOptions[i].text, allOptions[i].value);
	}
}

function alphabeticalSort(thisText, thatText)
{
	var thisDescription = thisText.text.toLowerCase();
	var thatDescription = thatText.text.toLowerCase();

	if (thisDescription < thatDescription)
	{
		 return -1;
	}
	else if (thisDescription > thatDescription)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function clearList(list)
{
	while (list.options.length > 0)
	{
 	   list.options[0] = null;
	}
}
