/* author: Data Access Europe B.V. */
/* Creation date: 16-07-04 */


function getElementsByClassName(oElm,sTagName,sClassName)
{
	var aElements=(sTagName=="*"&&document.all)?document.all:oElm.getElementsByTagName(sTagName);
	var aReturnElements=new Array();
	sClassName=sClassName.replace(/\-/g,"\\-");
	var oRegExp=new RegExp("(^|\\s)"+sClassName+"(\\s|$)");
	var oElement;

	iPos=aElements.length;
	while(iPos--)
	{
		oElement=aElements[iPos];
		if(oRegExp.test(oElement.className))
		{
			aReturnElements[aReturnElements.length]=oElement;
		}
	}
	return (aReturnElements)
};


//		Function: Menu_GetNodeIMGByID
//		Purpose: Returns the LI object of the node with the correct ID 
function Menu_GetNodeByID(iID){
    var oNode;

    iID = String(iID).replace(/MenuItem/g,'')
    iID = String(iID).replace(/MenuImage/g,'')
    
    oNode = document.getElementById("MenuItem" + iID);
    
    if (oNode) return oNode;
	//var aNodes, iLoop, oNode;
	//aNodes = getElementsByClassName(document,"*","MenuNode");

	//for(iLoop = 0; iLoop < aNodes.length; iLoop++){
		//if(aNodes[iLoop].getAttribute("id") == 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){
    iID = String(iID).replace(/MenuItem/g,'')
    iID = String(iID).replace(/MenuImage/g,'')

    oNode = document.getElementById("MenuImage" + iID);
    
    if (oNode) return oNode;

	//var aIMGS, iLoop, oIMG;
	//aIMGs = getElementsByClassName(document,"img","MenuNodeIMG");

	//for(iLoop = 0; iLoop < aIMGs.length; iLoop++){
		//if(aIMGs[iLoop].getAttribute("id") == 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("id"), "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 = getElementsByClassName(document,"*","MenuNode");
	
	aIMG = getElementsByClassName(document,"img","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, iID;
	
	iNodeId = 1;

	for(var iLoop = 0; iLoop < aIDs.length; iLoop++){
		oNode = Menu_GetNodeByID(aIDs[iLoop]);
		if(oNode !== null){
			iNodeId = oNode.getAttribute("id");			

			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;
	
	iParentID = String(iParentID).replace(/MenuItem/g,'')
    iParentID = String(iParentID).replace(/MenuImage/g,'')
                
	var oNode = Menu_GetNodeByID(iParentID);
	
	if(oNode !== null){
		
		Menu_ChangeNode(oNode.getAttribute("id"), sDisplay, sExtended);
		
		sNodes = getElementsByClassName(document,"*","MenuNode");
		for(iLoop = 0; iLoop < sNodes.length; iLoop++){
			if(sNodes[iLoop].getAttribute("ParentNodeId") == iParentID){
				oChildNode = sNodes[iLoop];
				Menu_ChangeNode(oChildNode.getAttribute("id"), sDisplay, sExtended);
				Menu_ChangeDown(oChildNode.getAttribute("id"), 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("id"), 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;
	
	iParentID = String(iParentID).replace(/MenuItem/g,'')
    iParentID = String(iParentID).replace(/MenuImage/g,'')
	
	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 = getElementsByClassName(document,"*","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 = getElementsByClassName(document,"*","MenuNode");
	
	for(iLoop = 0; iLoop < aNodes.length; iLoop++){
		if(aNodes[iLoop].getAttribute("SetTo") == "Show"){
			aNodes[iLoop].style.display = "";
		}else{
			aNodes[iLoop].style.display = "none";
		}	
	}
}
