function ChangeDictionaryItem(controlID, childElementIds, parentElementValues, childElementDummyTexts, childElementValues, childElementNames, childElementOptions)
{
	var parentControl = document.getElementById(controlID);
	var parentControlValueIndex = -1;
	for (var pvi = 0; pvi < parentElementValues.length; pvi++)
	{
		if (parentElementValues[pvi] == parentControl.value)
		{
			parentControlValueIndex = pvi;
			break;
		}
	}
	for (var childControlIndex = 0; childControlIndex < childElementIds.length; childControlIndex++)
	{
		switch(childElementOptions[childControlIndex][0])
		{
			case 0:
				ResetSingleSelectionChildControl(
					childElementIds[childControlIndex], childElementDummyTexts[childControlIndex],
					childElementOptions[childControlIndex], parentControlValueIndex, childControlIndex, 
					parentElementValues, childElementValues, childElementNames);
				break;
			case 1:
				ResetMultiSelectionChildControl(
					childElementIds[childControlIndex], childElementOptions[childControlIndex], 
					parentControlValueIndex, childControlIndex, 
					parentElementValues, childElementValues, childElementNames)
				break;
		}
	}
}

function UpdateItemValue(controlID)
{
	if (!loadPageMode)
	{
		var control = document.getElementById(controlID);
		var controlValue = document.getElementById(controlID + '_value');
		controlValue.value = control.value;
	}
}

function ResetSingleSelectionChildControl(childControlID, dummyText, options, parentControlValueIndex, childControlIndex, parentElementValues, childElementValues, childElementNames)
{
	var childControl = document.getElementById(childControlID);
	if (childControl == null)
		return;
	
	while (childControl.options.length > 0)
		childControl.remove(0);

	if (dummyText != null)
	{
		var oOption = new Option(dummyText, -1);
		childControl.options.add(oOption);
	}				
	PopulateOptionsList(childControl, options, parentControlValueIndex, childControlIndex, parentElementValues, childElementValues, childElementNames);

/*
	if (dummyText == null)
	{
	    childControl.selectedIndex = -1;
	}
*/	
	if (loadPageMode)
	{
		SetDropDownValue(childControl, document.getElementById(childControlID + '_value').value, dummyText != null);
	}
	if (childControl.onchange != null)
	{
		childControl.onchange();
	}
}

function ResetMultiSelectionChildControl(childControlID, options, parentControlValueIndex, childControlIndex, parentElementValues, childElementValues, childElementNames)
{
	var childControlAllItems = document.getElementById(childControlID + '_available');
	var childControlSelectedItems = document.getElementById(childControlID + '_selected');
	if (childControlAllItems == null || childControlSelectedItems == null)
		return;
	
	while (childControlAllItems.options.length > 0)
		childControlAllItems.remove(0);
	while (childControlSelectedItems.options.length > 0)
		childControlSelectedItems.remove(0);

	PopulateOptionsList(childControlAllItems, options, parentControlValueIndex, childControlIndex, parentElementValues, childElementValues, childElementNames);
	
	if (loadPageMode)
	{
		LoadSelectedValues(childControlID);
	}
	else
	{
		SaveSelectedValues(childControlID + '_selected',childControlID + '_values');
        if (childControlSelectedItems.options.length == 0)
        {
            AddDummyItem(childControlSelectedItems);
        }	
	}
}

function PopulateOptionsList(childControl, options, parentControlValueIndex, childControlIndex, parentElementValues, childElementValues, childElementNames)
{
	var elementsArray = new Array();
	if (parentControlValueIndex >= 0)
	{
		AddOptions(elementsArray, 
			childElementValues[parentControlValueIndex][childControlIndex],
			childElementNames[parentControlValueIndex][childControlIndex]);
	}
	else if (options[1])
	{
		for (var pvi = 0; pvi < parentElementValues.length; pvi++)
		{
			AddOptions(elementsArray, 
				childElementValues[pvi][childControlIndex],
				childElementNames[pvi][childControlIndex]);
		}
		elementsArray.sort(CompareAscending);
	}
	for (var i = 0; i < elementsArray.length; i++)
	{
		childControl.options.add(elementsArray[i]);
	}
	if (childControl.options.length > 0) 
	{
	    childControl.selectedIndex = 0;
	}
}

function AddOptions(elementsArray, values, names)
{
	if (values == null)
		return;
		
	for (var i = 0; i < values.length; i++)
	{
		elementsArray[elementsArray.length] = new Option(names[i], values[i]);
	}
}

function SetDropDownValue(dropDownControl, value, hasDummyText)
{
	for (var i = 0; i < dropDownControl.options.length; i++)
	{
		if (dropDownControl.options[i].value == value)
		{
			dropDownControl.selectedIndex = i;
			return;
		}
	}
	if (hasDummyText)
	{
		dropDownControl.selectedIndex = 0;
	}
	else
	{
		dropDownControl.selectedIndex = -1;
	}
}

function CompareAscending(a, b)
{
	if (typeof(a.text.localeCompare) != 'function')
	{
		if (a.text.toLowerCase() < b.text.toLowerCase())
			return -1;
		else if (a.text.toLowerCase() > b.text.toLowerCase())
			return 1;
		else
			return 0;
	}
	else
	{
		return a.text.toLowerCase().localeCompare(b.text.toLowerCase());
	}
}