function initialise()
{
	// Populate level 1 list
	populateLevel1()
}


function findElement(objList, strTextToFind)
{

	var blnResult = false;
	var intLength = objList.options.length;

	if(intLength > 0)
	{
		for (i = 0; i < intLength; i++)
		{
			var strElementText = objList.options[i].value;
			
			if (strElementText == strTextToFind)
			{
				blnResult = true;
				break;
			}
			
		}
	}

	return blnResult;
}


function addElement(targetObjectName, text, value, parent)
{

	objTargetList = document.getElementById(targetObjectName);

	strValue = value;
	strText = text;

	if (!(findElement(objTargetList, strValue)))
	{
		if (parent)
		{
			objOption = window.parent.document.createElement("OPTION");
		}
		else
		{
			objOption = document.createElement("OPTION");
		}
		
		objOption.innerHTML = strText;
		objOption.value = strValue;
		objTargetList.appendChild(objOption);
	}
}


function populateLevel1()
{
	
	var arrOption = new Array();
	
	for (var i = 0; i < arrLevel1.length; i++)
	{
		arrOption = arrLevel1[i];
		
		addElement("lstLevel1", arrOption[2], arrOption[0], parent)
		
	}
	
}


function populateLevel(objectList, level)
{

	unHighlightObject("lstLevel" + eval("level-1"));
	
	var objList = document.getElementById(objectList);

	if (objList.length > 0) {		
		var intParentID = objList.options[objList.selectedIndex].value;
		var arrOption = new Array();
		var arrSource = eval("arrLevel" + level);
		var objTargetList = document.getElementById("lstLevel"+level);
		var objNextTargetList = document.getElementById("lstLevel"+ eval("level+1"));

		if (objNextTargetList != undefined) { removeAllElements(objNextTargetList.name); }
		
		removeAllElements(objTargetList.name);
		
		for (var i = 0; i < arrSource.length; i++)
		{
			arrOption = arrSource[i];
			
			if(arrOption[1] == intParentID || arrOption[0] == "X") {
				addElement(objTargetList.name, arrOption[2], arrOption[0], parent)
			}
			
		}

		// Error object properties: contructor, description, message, name, number, prototype
		if (objTargetList.length == 1) {
			try {
				objTargetList.remove(0);
			} catch(e) {
				return true;
			}
			
		} else {
			highlightObject("lstLevel" + level);
		}

	}
	
}


function refreshList(objectList, objectButton)
{
	var intItemCount = objectList.options.length;

	if (intItemCount > 0) {
		objectList.size = intItemCount;
// 		if (objectList.style.display != 'inline') {
// 			objectList.style.display='inline';
// 			objectButton.style.display='inline';
// 		}
		
		objectList.focus();
		objectList.selectedIndex = (intItemCount - 1);
// 	} else {
// 		objectList.style.display='none';
// 		objectButton.style.display='none';
	}

// 	if(objectList.options.length > 0)
// 	{
// 		var intLen = objectList.options.length;
// 		
// 		objectList.size = (intLen < 5 ? intLen : 5);
// 		
// 		try {
// 			objectList.selectedIndex = (intLen - 1);
// 		} catch(e) {
// 			return true;
// 		}
// 		
// 		//objectList.style.display='inline';
// 		//objectButton.style.display='inline';
// 	}
// 	else
// 	{
// 		//objectList.style.display='none';
// 		//objectButton.style.display='none';
// 	}
}


function removeElement(objectList, objectButton)
{
	objList = document.getElementById(objectList);
	objButton = document.getElementById(objectButton);

	intCount = objList.options.length;
	intSelected = objList.selectedIndex;
	
	if (intCount > 0 && intSelected >= 0)
	{
		var nullElement = objList.removeChild(objList.options[objList.selectedIndex]);
		refreshList(objList, objButton);
	}
}


function selectAllElements(objectName)
{
	objSelect = document.getElementById(objectName);
	intCount = objSelect.options.length;
	
	for (var i = 0; i < intCount; i++) {
		objSelect.options[i].selected = true;
	}
	
}


function removeAllElements(objectName)
{
	unHighlightObject(objectName);

	objSelect = document.getElementById(objectName);
	intCount = objSelect.options.length;
	
	if (intCount > 0) {
		objSelect.options[0].selected = true;
		
		for (var i = 0; i < intCount; i++) {
			objSelect.remove(0);
		}
		
	}
	
}


function copyElement(sourceObjectID, targetObjectID, targetButtonID, parent)
{

	objSourceList = document.getElementById(sourceObjectID);
	objTargetList = document.getElementById(targetObjectID);
	objTargetButton = document.getElementById(targetButtonID);

	if (objSourceList.length > 0) {
		strValue = objSourceList.options[objSourceList.selectedIndex].value;
		strText = objSourceList.options[objSourceList.selectedIndex].text;
	
		if (strValue != "X" && !(findElement(objTargetList, strValue)))
		{
			if (parent)
			{
				objOption = window.parent.document.createElement("OPTION");
			}
			else
			{
				objOption = document.createElement("OPTION");
			}
			
			objOption.innerHTML = strText;
			objOption.value = strValue;
			objTargetList.appendChild(objOption);
			
			objSourceList.selectedIndex = 0;
			
			unHighlightObject(sourceObjectID);

			refreshList(objTargetList, objTargetButton);
		}
	}
}


function highlightObject(objectID)
{
	var objElement = document.getElementById(objectID);
	
	if (objElement.tagName == "SELECT") {
		objElement.className = "HiliteList";
	}
	
}


function unHighlightObject(objectID)
{
	
	var objElement = document.getElementById(objectID);
	
	if (objElement.tagName == "SELECT") {
		objElement.className = "UnHiliteList";
	}
	
}
