var elementSelectorDragObj;
var currentHoverElement = null;
var sListElementDropEffect = "none";

var arrListElements = new Array();
var arrListContentAreas = new Array();
var iListTypeId;
var bElementHover = false;

function DragDropInit() {
	if(document.getElementById("treecontainer")!=null) {
		return;
	}
	
	var oElementSelector = document.getElementById("ElementSelector");
	var oElementSelectorItem;
	if(oElementSelector!=null) {
		for(var i=0; i<oElementSelector.childNodes.length; i++) {
			oElementSelectorItem = oElementSelector.childNodes[i];
			// enable drag and drop
			oEventManager.QueueEvent(oElementSelectorItem, "mousedown", swElementSelectorMouseDown, false);
			oEventManager.QueueEvent(oElementSelectorItem, "dragstart", swElementSelectorDragStart, false);
			oEventManager.QueueEvent(oElementSelectorItem, "dragend", swElementSelectorDragEnd, false);
			oEventManager.QueueEvent(oElementSelectorItem, "drag", swElementSelectorDrag, false);

			// Hack: put x.gif over the link to enable dragging on the link (text) (this also prevents selecting the text!!)
			// - first remove any previously placed drag and drop "handle" images (placed below....)
			for(var j=0; j<oElementSelectorItem.childNodes.length; j++) {
				if(oElementSelectorItem.childNodes[j].DragDropHandle==true) {
					oElementSelectorItem.removeChild(oElementSelectorItem.childNodes[j]);
				}
			}
			
			// - then apply a new drag and drop "handle" image
			var oWH = Element.getDimensions(oElementSelectorItem.firstChild);
			var dragImg = document.createElement("IMG");
			dragImg.src = "graphics/x.gif";
			dragImg.width = oWH.width;
			dragImg.height = oWH.height;
			dragImg.DragDropHandle = true;
			
			// add and position
			oElementSelectorItem.insertBefore(dragImg, oElementSelectorItem.firstChild);
			Position.absolutize(dragImg);
		}
	}

	var enableDragAndDrop = (oElementSelector!=null);
	var oFormContainer = document.getElementById("FormContainer");
	
	if(enableDragAndDrop) {
		oFormContainer.ondragenter = new Function("FormContainerDragEnter(this)");
	}
	
	InitializeLists(oFormContainer, enableDragAndDrop);

    AddMouseDownHandler("ListOnMouseDownHandler();", true);
}

swElementSelectorMouseDown = function(e) {
	if ( e == null ) { e = event; }
	ElementSelectorMouseDown(swFindParentMatchingClassFromElement(e.srcElement, "ContentSelectorItem"), e);
}

swElementSelectorDragStart = function(e) {
	if ( e == null ) { e = event; }
	ElementSelectorDragStart(swFindParentMatchingClassFromElement(e.srcElement, "ContentSelectorItem"), e);
}

swElementSelectorDragEnd = function(e) {
	if ( e == null ) { e = event; }
	ElementSelectorDragEnd(swFindParentMatchingClassFromElement(e.srcElement, "ContentSelectorItem"), e);
}

swElementSelectorDrag = function(e) {
	if ( e == null ) { e = event; }
	ElementSelectorDrag(e);
}

function ListOnMouseDownHandler() {
	ListDisableAllListTools();
}

function QueueListElementDragEvents(element) {
	oEventManager.QueueEvent(element, "drop", swListElementDrop, false);
	oEventManager.QueueEvent(element, "dragover", swListElementDragOver, false);
	oEventManager.QueueEvent(element, "dragenter", swListElementDragEnter, false);
	oEventManager.QueueEvent(element, "dragleave", swListElementDragLeave, false);
	/*
	oEventManager.QueueEvent(element, "dragstart", swListElementDragStart, false);
	oEventManager.QueueEvent(element, "dragend", swListElementDragEnd, false);
	oEventManager.QueueEvent(element, "drag", swListElementDrag, false);
	*/
	
}

AddRowDragImage = function(row) { // CORE-1610
	// make drag-image
	var oWH = Element.getDimensions(row);
	var dragImg = document.createElement("IMG");
	dragImg.src = "graphics/x.gif";
	dragImg.width = oWH.width;
	dragImg.height = oWH.height;
	
	// add and position
	row.firstChild.insertBefore(dragImg, row.firstChild.firstChild);
	Position.absolutize(dragImg);
}

function InitializeRowDrag(row) {
	QueueListElementDragEvents(row);

	if ( row.className.indexOf("EmptyRow") == -1 ) { // empty rows should not be draggable
		var dragImg = row.firstChild.firstChild;
		oEventManager.QueueEvent(dragImg, "dragstart", swListElementDragStart, false);
		oEventManager.QueueEvent(dragImg, "dragend", swListElementDragEnd, false);
		oEventManager.QueueEvent(dragImg, "drag", swListElementDrag, false);
	}
}

function InitializeLists(oElement, bEnableDragAndDrop) {
	var oChildNode;
	var bListContentAreaPushed = false;
	var tables = oElement.getElementsByTagName("TABLE");
	
	for ( var j = 0; j < tables.length; j++ ) {
		var thisTable = tables[j];
		
		// TreeViewList is initialized elsewhere
//		if ( thisTable.ListType != "TreeView" ) {
			var targetTable = swFindParentWithClassFromElement(thisTable, "List");
			
			if ( thisTable.ListContent == "true" ) {
				arrListContentAreas.push(targetTable);
				
				var colCount = thisTable.getElementsByTagName("COL").length;
				var tbody = thisTable.getElementsByTagName("TBODY")[0];
				
				// any reference to tbody.getElementsByTagName("TR") will grow automatically
				// when tbody.insertBefore is used to insert a row. To prevent looping endlessly, 
				// the array is copied entry by entry
				var tmprows = tbody.getElementsByTagName("TR");
				var rows = [];
				for ( var i = 0; i < tmprows.length; i++ ) {
					rows.push(tmprows[i]);
				}
				
				var containedTypeIds = [];
				var bTypeIdsAssigned = false;
				var k = 0;
				while ( !bTypeIdsAssigned && k < arrGuibElementContainedTypeIds.length ) {
					if ( arrGuibElementContainedTypeIds[k].ElementName == targetTable.Name ) {
						containedTypeIds = arrGuibElementContainedTypeIds[k].TypeIds;
						bTypeIdsAssigned = true;
					}
					k++;
				}
				
				for ( var i = 0; i < rows.length; i++ ) {
					var thisRow = rows[i];
					
					// This causes the tooltip on the TD to fail...
					// AddRowDragImage(thisRow); 
					
					if ( thisRow.ElementContainer != null ) {
						if ( targetTable.Enabled == "true" ) {
							
							// assign type-ids
							thisRow.ContainedTypeIds = containedTypeIds;
							
							// drag and drop events
							if ( bEnableDragAndDrop ) {
								
								AddRowDragImage(thisRow); 
								InitializeRowDrag(thisRow);
								
								// seperator-row above current row
								var sepTR = document.createElement("TR");
								tbody.insertBefore(sepTR, thisRow);
								
								sepTR.setAttribute("className", thisRow.className + " RowSeparator");
								sepTR.ContainedTypeIds = containedTypeIds;
								
								// CORE-3251 first separator used as spacer
								if ( i == 0 ) { 
									sepTR.style.lineHeight = "8px"; 
								}
								
								sepTR.RowSeparator = 1;
								// duplicate attributes from "real" row
								with ( thisRow ) {
									sepTR.NodeId = NodeId;
									sepTR.TypeId = TypeId;
									sepTR.CustomId = CustomId;
								}
								
								var sepTD = document.createElement("TD");
								sepTR.appendChild(sepTD);
								sepTD.setAttribute("colspan", colCount);
								sepTD.innerHTML = "&nbsp;";
								
								QueueListElementDragEvents(sepTR);
							}
						}
					}
				}
				
				if ( bEnableDragAndDrop ) {
					// if drag and drop is enabled, a dummy drop target is provided as the last
					// entry in the list. This is necessary to facilitate dropping below last element
					var dummyTR = document.createElement("TR");
					dummyTR.setAttribute("className", thisRow.className + " RowSeparator");
					tbody.appendChild(dummyTR);
					
					QueueListElementDragEvents(dummyTR);
					
					dummyTR.ContainedTypeIds = containedTypeIds;
					dummyTR.RowSeparator = 1;
					dummyTR.style.lineHeight = "8px";
					
					// duplicate attributes from "real" row
					with ( targetTable ) {
						dummyTR.NodeId = NodeId;
						dummyTR.TypeId = TypeId;
						dummyTR.CustomId = CustomId;
					}
					
					var dummyTD = document.createElement("TD");
					dummyTD.setAttribute("colspan", colCount);
					dummyTD.innerHTML = "&nbsp;";
					dummyTR.appendChild(dummyTD);
					
					// events on content area (necessary...?)
					oEventManager.QueueEvent(targetTable, "dragenter", swListContentDragEnter, false);
					oEventManager.QueueEvent(targetTable, "dragleave", swListContentDragLeave, false);
				}
			}
//		}
	}
}

swSortHeaderOnClick = function(e) {
	e = (e) ? e : event;
	
	var header = e.srcElement;
	if ( header.className.indexOf("SortHeader") == -1 ) {
		header = swFindParentWithClassFromElement(e.srcElement, "SortHeader");
	}
	SortedListSort(header);
}

swListContentDragEnter = function(e) {
	e = (e) ? e : event;
}

swListContentDragLeave = function(e) {
	e = (e) ? e : event;
}

swShowGuibActions = function(e) {
	e = (e) ? e : event;
	ShowGuibActions(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementOnMouseDown = function(e) {
	e = (e) ? e : event;
	ListElementOnMouseDown(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swPerformDefaultGuibAction = function(e) {
	e = (e) ? e : event;
	PerformDefaultGuibAction(swFindParentWithClassFromElement(e.srcElement, "ListRow"));
}

swListElementDrop = function(e) {
	e = (e) ? e : event;
	ListElementDrop(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementDragOver = function(e) {
	e = (e) ? e : event;
	ListElementDragOver(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementDragEnter = function(e) {
	e = (e) ? e : event;
}

swListElementDragLeave = function(e) {
	e = (e) ? e : event;
	ListElementDragLeave(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementDragStart = function(e) {
	e = (e) ? e : event;
	ListElementDragStart(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementDragEnd = function(e) {
	e = (e) ? e : event;
	ListElementDragEnd(swFindParentWithClassFromElement(e.srcElement, "ListRow"), e);
}

swListElementDrag = function(e) {
	e = (e) ? e : event;
	ListElementDrag(e);
}

function ListElementOnMouseDown(oElement, e) {
	GetPane().SetFocus();
	GetPane().HideActions();
	parent.Selection.Clear();
	parent.Selection.SelectNode(oElement, e, iPaneId);
	
	
	if(oElement.Empty!="true") {
		ListEnableTools(oElement.GuibElementName, oElement.ActionGroupId, true);
	}

	e.returnValue = true;
	e.cancelBubble = true;
}

function ElementSelectorMouseDown(oElement, e) {
	e = (e) ? e : event;
	GetPane().SetFocus();
	GetPane().HideActions();
	parent.Selection.Clear();
	parent.Selection.SelectNode(oElement, e, iPaneId);
	e.returnValue = true;
	e.cancelBubble = true;
}

function ElementSelectorDragStart(oElement, e) {
	e = (e) ? e : event;
//	parent.Selection.SelectNode(oElement, event, iPaneId);
	iListTypeId = oElement.TypeId;
	// create drag object DIV
	elementSelectorDragObj = document.createElement("DIV");
	elementSelectorDragObj.style.position = "absolute";
	elementSelectorDragObj.style.visibility = "visible";
	elementSelectorDragObj.style.filter = "alpha(opacity=50)";
	elementSelectorDragObj.style.zIndex = 10000;

	// save typeid, fake nodeid and fake customid
	elementSelectorDragObj.TypeId = oElement.TypeId;
	elementSelectorDragObj.GuibElementName = oElement.parentNode.GuibElementName;
	
	// get what is being dragged:
	var srcObj;
	// append selection to drag object DIV
	srcObj = document.createElement("DIV");
	srcObj.innerHTML = oElement.innerHTML;
	elementSelectorDragObj.appendChild(srcObj);

	document.body.appendChild(elementSelectorDragObj);
	// set drag object at mouse cursor
	elementSelectorDragObj.style.left = (e.screenX-window.screenLeft)+"px";
	elementSelectorDragObj.style.top = (e.screenY-window.screenTop)+"px";

    // get the dataTransfer object
	var dragData = e.dataTransfer;
	// set allowed drop effect; all drop effects must be allowed so we can be able to switch between "copy", "move" and "none"
	dragData.effectAllowed = "all";
}

function ElementSelectorDragEnd(oElement, e) {
	document.body.removeChild(elementSelectorDragObj);
	elementSelectorDragObj = null;
}

function ElementSelectorDrag(e) {
	e = (e) ? e : event;
	elementSelectorDragObj.style.left = (e.screenX-window.screenLeft)+"px";
	elementSelectorDragObj.style.top = (e.screenY-window.screenTop)+"px";
}

function ListElementDrop(oElement, e) {
	e = (e) ? e : event;

	var oDestNode;
	var iAction, iLastAction;
	var iSourceTypeId, iSourceNodeId, iSourceCustomId;
	var sSourceGuibElementName;
	var oTableNode = oElement.parentNode.parentNode;

	ClearDropMarkers(oElement.parentElement);

	sSourceGuibElementName = oElement.GuibElementName;	
	iLastAction = ActionIdMove;
	var oCopyNode = new Object();
	var oPasteNode = new Object();
	
	var table = swFindParentWithClassFromElement(oElement, "List");
	
	oDestNode = oElement;
	if ( table.Empty == "true" ) {
		// the list is empty - insert into
		// -> the list element defines typeid, nodeid and customid
		iAction = ActionIdPasteInto;
	}
	else if ( oElement.nextSibling == null ) {
		// last separator in list - insert below last (not empty) element in the list
		// -> the previous list tr item defines typeid, nodeid and customid
		oDestNode = oElement.previousSibling;
		iAction = ActionIdPasteBelow;
	}
	else {
		// one of the (not empty) elements "selected" - insert above this element
		iAction = ActionIdPasteAbove;
	}
	
	// inserting a new item from element selector?
	if(elementSelectorDragObj!=null) {
		iSourceTypeId = elementSelectorDragObj.TypeId;
		iSourceNodeId = -1;
		iSourceCustomId = -1;
		iLastAction = ActionIdNew;
		sSourceGuibElementName = elementSelectorDragObj.GuibElementName;
	}
	else {
		iSourceTypeId = parent.Selection.Nodes[0].TypeId;
		iSourceNodeId = parent.Selection.Nodes[0].NodeId;
		iSourceCustomId = parent.Selection.Nodes[0].CustomId;
		
		// creating a new element from an element selector?
		if(parent.Selection.LastPaneId==iPaneId && e.ctrlKey==false) {
		}
		// copying an element from another pane?
		else {
			iLastAction = ActionIdCopy;
		}
	}
	
	oCopyNode.ActionId = iLastAction;
	oCopyNode.ActionGroupId = iSourceTypeId;
	oCopyNode.TypeId = iSourceTypeId;
	oCopyNode.NodeId = iSourceNodeId;
	oCopyNode.CustomId = iSourceCustomId;
	oCopyNode.GuibElementName = oElement.GuibElementName;

	oPasteNode.ActionId = iAction;
	oPasteNode.TypeId = oDestNode.TypeId;
	oPasteNode.NodeId = oDestNode.NodeId;
	oPasteNode.CustomId = oDestNode.CustomId;
	oPasteNode.GuibElementName = oDestNode.GuibElementName;

	// creating a document or a module from an element selector?
	if(elementSelectorDragObj!=null && 
		(oCopyNode.TypeId == TypeIdDocument || IsModuleType(oCopyNode.TypeId))) {
		GuibActionNewNode(oCopyNode, oPasteNode);
	}
	else {
		// not creating a new document or module -> perform copy/move->paste
		GuibActionCopy(oCopyNode);
		GuibActionPaste(oPasteNode);
	}
}

ClearDropMarkers = function(thisBody) {
	var rows = thisBody.getElementsByTagName("TR");
	
	for ( var i = 0; i < rows.length; i++ ) {
		RemoveClassFromElement(rows[i], "RowDropHighlight");
	}
	
	currentHoverElement = null;
}

function ListElementDragOver(oElement, e) {
	e = (e) ? e : event;
	e.returnValue = false;

	if ( oElement == currentHoverElement ) {
		// just set drop effect (to the same as previous effect)
		
		// make ctrl-key react when not moving the mouse...
		if ( elementSelectorDragObj == null && parent.Selection.LastPaneId == iPaneId ) {
			if ( e.ctrlKey && sListElementDropEffect == "move" ) {
				sListElementDropEffect = "copy";
			}
			else {
				if ( !e.ctrlKey && sListElementDropEffect == "copy" ) {
					sListElementDropEffect = "move";
				}
			}
		}
		
		SetListElementDropEffect(sListElementDropEffect,e);
		return;
	}
	
	dropEffect = "none";
	
	ClearDropMarkers(oElement.parentElement);
	currentHoverElement = oElement;
	var draggedElement = parent.Selection.Nodes[0];
	
	// one cannot drop if:
	// No elements are selected and we are not inserting a new item (elementSelectorDragObj==null)
	if ( ! ((parent.Selection.Size() == 0 && elementSelectorDragObj == null) ||
		// - The selected element is an element selector item from another pane
			(draggedElement.parentNode.id == "ElementSelector" && parent.Selection.LastPaneId != iPaneId ) ||
		// - More than one element is selected (multiselect is not supported here)
			(parent.Selection.Size() > 1)) ) {
		
		if ( ListDraggedElementIsAllowed(oElement) ) {
			if ( parent.Selection.LastPaneId==iPaneId && e.ctrlKey==false && elementSelectorDragObj == null) {
				// if inside same pane, no mod-key and not dragging from elementselector
				dropEffect = "move";
			}
			else {
				dropEffect = "copy";
			}
			
			if ( oElement.RowSeparator == 1 ) {
				AddClassToElement(oElement, "RowDropHighlight");
			}
			else {
				AddClassToElement(oElement.previousSibling, "RowDropHighlight");
			}
		}
	}
	
	SetListElementDropEffect(dropEffect, e);
}

function SortedListElementDragOver(oElement) {
	var oDraggedElement = parent.Selection.Nodes[0];
	if(oDraggedElement.GuibElementName==oElement.GuibElementName) {
		SetListElementDropEffect("none");
		event.returnValue = false;
		return;
	}
	if(ListDraggedElementIsAllowed(oElement)==false) {
		SetListElementDropEffect("none");
		event.returnValue = false;
		return;
	}
	// show "move" dropEffect if we are moving inside the pane, and not inserting a new element (elementSelectorDragObj==null)
	if(parent.Selection.LastPaneId==iPaneId && event.ctrlKey==false) {
		SetListElementDropEffect("move");
	}
	// show "copy" dropEffect if we are inserting a new element (elementSelectorDragObj!=null)
	// or dragging an element from another pane
	else {
		SetListElementDropEffect("copy");
	}
    // tell onOverDrag handler not to do anything:
    event.returnValue = false;
}

function ListElementDragEnter(oElement) {
	// TODO: necessary?
	return;

	bElementHover = true;
	if(oElement.className=="ListTrContentSelected") {
		return;
	}
	for(var i=0; i<arrListElements.length; i++) {
		if(arrListElements[i].className!="ListTrContentSelected") {
			arrListElements[i].className="ListTrContent";
		}
	}
    event.returnValue = true;
}


function ListElementDragLeave(oElement, e) {
	e = (e) ? e : event;
	
	ClearDropMarkers(oElement.parentElement);
	e.returnValue = true; // why true?
}

function ListElementDragStart(oElement, e) {
	
	// construct ghostimage
	
	var tableElement = oElement.parentElement.parentElement; // tr -> tbody -> table

	dragObj = document.createElement("DIV");
	var oDivWH = Element.getDimensions(tableElement.parentElement);
	
	// table to hold rows
	var dragTable = document.createElement("TABLE");
	dragTable.style.width = oDivWH.width;
	dragObj.appendChild(dragTable);
	
	// duplicate column group
	var cols = tableElement.getElementsByTagName("COLGROUP");
	dragTable.appendChild(cols[0].cloneNode(true));
	
	// create tbody for dragtable and append clone of row being dragged
	var dragTBody = document.createElement("TBODY");
	dragTable.appendChild(dragTBody);
	
	var cloneTR = oElement.cloneNode(true);
	cloneTR.className = "ListRow";
	dragTBody.appendChild(cloneTR);
	
	parent.AppendDragObjectToDocumentByHTML(dragObj.innerHTML);
	
	dragObj = null;
	
	// register dragdata
	e.dataTransfer.effectAllowed = "all";
	
	// position ghostimage
	ListElementDrag(e);	
}

function ListElementDragEnd(oElement, e) {
/*
	parent.DragEnd(oElement, event);
	ListEnableTools(oElement.GuibElementName, oElement.ActionGroupId, true);
	parent.Selection.Clear();
*/
	// what is this eventhandler for?
	
	parent.DragEnd(oElement,e);
}

function ListElementDrag(e) {
	// position ghostimage
	parent.Drag(e);
}

function SetListElementDropEffect(sDropEffect, e) {
	e = (e) ? e : event;
	sListElementDropEffect = sDropEffect;
	e.dataTransfer.dropEffect = sDropEffect;
}

var bListHover = false;
function ListContentDragEnter(oList) {
	bListHover = true;
	
	event.dataTransfer.dropEffect = sListElementDropEffect;
    event.returnValue = false;
}

function ListContentDragLeave(oList) {
	if(bListHover==false) {
		bElementHover = false;
	}
	else {
		bListHover = false;
	}
    event.returnValue = true;
}

function FormContainerDragEnter(oFormContainer) {
	if(bElementHover==false) {
//		for(var i=0; i<arrListContentAreas.length; i++) {
//			if(arrListContentAreas[i].className!="ListTable") {
//				arrListContentAreas[i].className = "ListTable";
//			}
//		}
	}
	else if(bListHover==true) {
		bListHover = false;
	}
	else {
		bElementHover = false;
	}
	event.dataTransfer.dropEffect = sListElementDropEffect;

	event.returnValue = false;
}

function ListElementAction(iActionId, oListTool) {
	if(oListTool.className == "ListThToolDisabled") {
		return;
	}
	
	if(oListTool.AlwaysEnabled=="true" || ListToolCanApply(oListTool)) {
		var oElement;
		if(oListTool.AlwaysEnabled=="true") {
			oElement = oListTool.parentNode.previousSibling;
		}
		else {
			oElement = parent.Selection.Nodes[0];
		}
		
		var oAction = new Object();
		oAction.TypeId = oElement.TypeId;
		oAction.NodeId = oElement.NodeId;
		oAction.CustomId = oElement.CustomId;
		oAction.ActionId = iActionId;
		oAction.Forced = false;
		oAction.GuibElementName = oElement.GuibElementName;
		oAction.ActionGroupId = oElement.ActionGroupId;
		PerformGuibAction(oAction);
	}
	else {
//		debug("No action to perform");
	}
	event.cancelBubble = true;
}

function ListToolCanApply(oListTool) {
	// find corresponding tbody
	var tbody = document.getElementById(oListTool.parentElement.ListName+"_ContentTableBody");
	return (parent.Selection.Size()==1 && tbody.contains(parent.Selection.Nodes[0]));
}

function ListDraggedElementIsAllowed(oElement) {
	var iSourceTypeId;
	// inserting a new item?
	if(elementSelectorDragObj!=null) {
		iSourceTypeId = elementSelectorDragObj.TypeId;
	}
	else {
		iSourceTypeId = parent.Selection.Nodes[0].TypeId;
	}
	
	if ( elementSelectorDragObj == null ) { // list elements only
		var draggedElement = parent.Selection.Nodes[0];
		// dragged element over self
		if ( draggedElement.TypeId == oElement.TypeId && draggedElement.NodeId == oElement.NodeId ) {
			return false;
		}
			
		// CORE-3328 only perform list-specific checks on list rows
		if ( draggedElement.className.indexOf("ListRow") != -1 ) {
			// dragged element over following sibling
			var nextSibling = draggedElement.nextSibling.nextSibling; // element -> separator -> element
			if ( nextSibling != null && nextSibling.TypeId == oElement.TypeId && nextSibling.NodeId == oElement.NodeId ) {
				return false;
			}
		}
	}
		
	var arrAllowedTypeIds = oElement.ContainedTypeIds;
	for(var i=0; i<arrAllowedTypeIds.length; i++) {
		if(arrAllowedTypeIds[i]==iSourceTypeId) {
			return true;
		}
	}
	
	return false;	
}

function ListEnableTools(listName, actionGroupId, bEnabled) {
	ListDisableAllListTools();
	var oToolContainer = document.getElementById(listName+"_tools");

	if ( oToolContainer != null ) {
		for(var i=0; i<oToolContainer.childNodes.length; i++) {
			if(oToolContainer.childNodes[i].className=="ListThTool" && !bEnabled) {
				oToolContainer.childNodes[i].className = "ListThToolDisabled";
			}
			if(oToolContainer.childNodes[i].className=="ListThToolDisabled" && bEnabled) {
				// enable list tool
				// - verify that the list tool action exists in the guib action group for the element type
				var actionId = oToolContainer.childNodes[i].ActionId;
				var containsAction = false;
				if(arrGuibActions[actionGroupId]!=null) {				
					var j = 0;
					while ( !containsAction && j < arrGuibActions[actionGroupId].length ) {
						if(arrGuibActions[actionGroupId][j].ActionId == actionId) {
							containsAction = true;
						}
						j++;
					}
				}
				// only enable the list tool if the action group contains the action id of the list tool
				if(containsAction) {
					oToolContainer.childNodes[i].className = "ListThTool";
				}	
			}
		}
	}	
}

function ListDisableAllListTools() {
	var oListHeader;
	for(var i=0; i<arrListContentAreas.length; i++) {
		oListElement = swFindParentWithClassFromElement(arrListContentAreas[i], "List");
		var oToolContainer = document.getElementById(oListElement.Name+"_tools");
		
		if ( oToolContainer != null ) {
			for(var j=0; j<oToolContainer.childNodes.length; j++) {
				if(oToolContainer.childNodes[j].className=="ListThTool" && oToolContainer.childNodes[j].AlwaysEnabled!="true") {
					oToolContainer.childNodes[j].className = "ListThToolDisabled";
				}
			}
		}
	}
}


