/* author: Data Access Europe B.V. */
/* Creation date: 16-07-04 */

//		Function: Menu_GetNodeIMGByID
//		Purpose: Returns the TR object of the node with the correct ID 
function Menu_GetNodeByID(iID){
	var aNodes, iLoop, oNode;
	aNodes = document.getElementsByName('MenuNode');

	for(iLoop = 0; iLoop < aNodes.length; iLoop++){
		if(aNodes[iLoop].getAttribute("NodeId") == iID){
			oNode = aNodes[iLoop];
			return oNode;
		}
	}	
	return null;
}

//		Function: Menu_GetNodeIMGByID
//		Purpose: Returns the IMG object of the node with the correct ID 
function Menu_GetNodeIMGByID(iID){
	var aIMGs, iLoop, oIMG;
	aIMGs = document.getElementsByName('MenuNodeIMG');

	for(iLoop = 0; iLoop < aIMGs.length; iLoop++){
		if(aIMGs[iLoop].getAttribute("NodeId") == iID){
			oIMG = aIMGs[iLoop];
			return oIMG;
		}
	}	
	return null;
}


//		Function: Menu_Click
//		Purpose: If extended = true i collapses the menu else it extends
function Menu_Click(iID){
	var oNode = Menu_GetNodeByID(iID);
	if(oNode.getAttribute("Extended") == "True"){
		Menu_ChangeDown(oNode.getAttribute("NodeId"), "False", "Hide");
	}else{
		Menu_ChangeAll("False", "Hide");
		Menu_ChangeUp(iID, "True", "Show");
	}
	Menu_DoSettings();
}



//		Function: Menu_ChangeAll
//		Purpose: Applies the Extended & style.display settings to al the menu items
function Menu_ChangeAll(sExtended, sDisplay){
	var aNodes, iLoop, oNode, aIMG;
	sNodes = document.getElementsByName('MenuNode');
	
	aIMG = document.getElementsByName('MenuNodeIMG');
	for(iLoop = 0; iLoop < aIMG.length; iLoop++){
		if(sExtended == "True"){
			aIMG[iLoop].src = aIMG[iLoop].getAttribute("ExtendedImage");
		}else{
			aIMG[iLoop].src = aIMG[iLoop].getAttribute("CollapsedImage");
		}
	}
	for(iLoop = 0; iLoop < sNodes.length; iLoop++){
		sNodes[iLoop].setAttribute("Extended", sExtended);
		sNodes[iLoop].setAttribute("SetTo", sDisplay);
	}
}



//		Function: Menu_Open
//		Purpose: Opens the menu so the item with iID will be displayed and opened
function Menu_Open(aIDs){
	var oNode, iNodeId;
	
	iNodeId = 1;

	for(var iLoop = 0; iLoop < aIDs.length; iLoop++){
		oNode = Menu_GetNodeByID(aIDs[iLoop]);
		if(oNode !== null){
			iNodeId = oNode.getAttribute("NodeId");			

			break;
		}
	} 


	Menu_ChangeUp(iNodeId, "True", "Show");	
	Menu_DoSettings();
}



//		Function: Menu_ChangeDown
//		Purpose: Applies the Extended & style.display settings to all children of the item with the given ID
function Menu_ChangeDown(iParentID, sExtended, sDisplay){
	var aNodes, iLoop, oChildNode;
	var oNode = Menu_GetNodeByID(iParentID);
	
	if(oNode !== null){
		
		Menu_ChangeNode(oNode.getAttribute("NodeId"), sDisplay, sExtended);
		
		sNodes = document.getElementsByName('MenuNode');
		for(iLoop = 0; iLoop < sNodes.length; iLoop++){
			if(sNodes[iLoop].getAttribute("ParentNodeId") == iParentID){
				oChildNode = sNodes[iLoop];
				Menu_ChangeNode(oChildNode.getAttribute("NodeId"), sDisplay, sExtended);
				Menu_ChangeDown(oChildNode.getAttribute("NodeId"), sExtended, sDisplay);
			}
		}
	}else{
		Menu_ChangeNode(iID, sDisplay, sExtended);
	}	
	
}



//		Function: Menu_ChangeUp
//		Purpose: Applies the Extended & style.display settings to al the parents of the fiven ID
function Menu_ChangeUp(iID, sExtended, sDisplay){
	var oNode = Menu_GetNodeByID(iID);
	if(oNode !== null){
		Menu_ChangeNode(oNode.getAttribute("NodeId"), sDisplay, sExtended);
		Menu_ChangeUp(oNode.getAttribute("ParentNodeId"), sExtended, sDisplay);
	}else{
		Menu_ChangeNode(iID, sDisplay, sExtended);
	}
		
}



//		Function: Menu_ChangeNode 
//		Purpose: Changes the SetTo setting of the direct children of the item with the given ID
function Menu_ChangeNode(iParentID, sDisplay, sExtended){
	var iLoop, aNodes, oNode, oIMG;
	
	oNode = Menu_GetNodeByID(iParentID);
	if(oNode !== null){
		oNode.setAttribute("Extended", sExtended);
		oIMG = Menu_GetNodeIMGByID(iParentID);
		if(oIMG !== null){
			if(sExtended == "True"){
				oIMG.src = oIMG.getAttribute("ExtendedImage");
			}else{
				oIMG.src = oIMG.getAttribute("CollapsedImage");
			}
		}
	}
	
	aNodes = document.getElementsByName('MenuNode');
	
	for(iLoop = 0; iLoop < aNodes.length; iLoop++){
		if(aNodes[iLoop].getAttribute("ParentNodeId") == iParentID){
			aNodes[iLoop].setAttribute("SetTo", sDisplay);
		}
	}
}


function Menu_DoSettings(){
	var iLoop, aNodes
	aNodes = document.getElementsByName('MenuNode');
	
	for(iLoop = 0; iLoop < aNodes.length; iLoop++){
		if(aNodes[iLoop].getAttribute("SetTo") == "Show"){
			aNodes[iLoop].style.display = "";
		}else{
			aNodes[iLoop].style.display = "none";
		}	
	}
}