var oFirstLevelMenuOpen = "1"; 
var oFirstLevelIndicator = ""; 
var CONST_MENU_WITH_NO_CHILDREN="MenuWithNoChildren";
var oSecondLevelMenuOpen = "";
var blnKeepOneSecondLevelOpenForEachParnetOnChange = true;
var blnKeepAllLevelsOpenOnChange = true;
var sActiveChildImgSrc="";
var globalApplicationPath="";
var iTableUniqueID=0;
var guidLevelUniqueID="";
var iLastDrawnLevel=-1;
var guidHighlightedUniqueID="";
var gChannelGroup = 1;
			
function openCloseFirstLevelSub(oTD)
{
	var oTR, oNextTR;
	oTR = oTD.parentElement;
				
	oNextTR = oTR.nextSibling;
	
	//check if the there is another row.
	//if not then there are no children and nothing should happen
	//taht means that we should not open this level.
	if((typeof(oNextTR)=="undefined")||(oNextTR==null))
	{
		return;
	}
	
	if(oNextTR.style.display!="none")
	{	
		//when the next row has a class of a Top-Leveled elemnt that means that
		//this folder has no children
		if((oNextTR.childNodes[0].className=="TopChannel")||(oNextTR.childNodes[0].className=="HighlightedTopChannel")||(oNextTR.childNodes[0].className=="TopPosting")||(oNextTR.childNodes[0].className=="HighlightedTopPosting"))
		{
			//alert("No Children!!!")
			return;
		}
		
	}
	//if we want to keep all the menus opened:
	if(blnKeepAllLevelsOpenOnChange)
	{
		if (oNextTR.style.display == "")
		{
			//Check if this TR is a Seperator Roe before Closing(hiding) it:
			if(oNextTR.childNodes[0].className!="MenuSeparatorRow")
			{
				oNextTR.style.display = "none";
				//changing the image to show that the menu is open
				oTR.childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
				oTR.childNodes[0].className="TopChannel";
			}
		}
		else
		{
			oNextTR.style.display = "";
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sTopExpandedImgSrc;
			oTR.childNodes[0].className="HighlightedTopChannel";
		}
	}	
	else
	{
		//opening a first level menu
		if (oNextTR.style.display == "none")
		{
			if ((oFirstLevelMenuOpen != "")&&(oFirstLevelMenuOpen!=CONST_MENU_WITH_NO_CHILDREN))
			{
				if (oFirstLevelMenuOpen.style.display == "")
				{

					oFirstLevelMenuOpen.style.display = "none";
					//changing the image to show that the menu is open
					oFirstLevelMenuOpen.previousSibling.childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
					oFirstLevelMenuOpen.previousSibling.childNodes[0].className="TopChannel";
				}	
			}
			else if(oFirstLevelMenuOpen==CONST_MENU_WITH_NO_CHILDREN)
			{
				//changing the image to show that the menu is open
				oFirstLevelIndicator.childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
				oFirstLevelIndicator.childNodes[0].className="TopChannel";
			}
						
			oNextTR.style.display = "";
			oFirstLevelMenuOpen = oNextTR;
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sTopExpandedImgSrc;
			oTR.childNodes[0].className="HighlightedTopChannel";
		}
		else //closing a first level menu
		{
			//Check if this TR is a Seperator Roe before Closing(hiding) it:
			if(oNextTR.childNodes[0].className!="MenuSeparatorRow")
			{
				oNextTR.style.display = "none";
				oFirstLevelMenuOpen = "";
				//changing the image to show that the menu is closed
				oTR.childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
				oTR.childNodes[0].className="TopChannel";
			}
		}
	}
	
}
			
			
			
function openCloseSecondLevelSub(oTD)
{
	var oTR, oNextTR;
	var oTable;
	oTR = oTD.parentElement;
	oTable = oTR.parentElement;
	oNextTR = oTR.nextSibling;
	
	//if there are any children at all
	if(oNextTR)
	{
		if(oNextTR.style.display!="none")
		{
			if(oTR.childNodes[1].childNodes[0].src.indexOf(sCollapsdImgSrc)>-1)
			{
				//alert("No Children!!!")
				return;
			}
		}
	}
	else
	{
		return;
	}
	
	//if we want to keep all the menus opened:
	if(blnKeepAllLevelsOpenOnChange)
	{
		if (oNextTR.style.display == "")
		{
			oNextTR.style.display = "none";
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sCollapsdImgSrc;
			oTR.childNodes[0].className="Channel";
		}
		else
		{
			oNextTR.style.display = "";
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sExpandedImgSrc;
			oTR.childNodes[0].className="HighlightedChannel";
		}
	}	
	else
	{
		//opening a second level menu
		if (oNextTR.style.display == "none")
		{
			if(blnKeepOneSecondLevelOpenForEachParnetOnChange)
			{
				//run through all TR element of this level (to find other second 
				//leveled menus)
				for(var i=0;i<oTable.childNodes.length;i++)
				{
					//if this TR is a menu and not "this" menu -> close it
					if(oTR!=oTable.childNodes[i] && (oTable.childNodes[i].className=="Channel" || oTable.childNodes[i].className=="HighlightedChannel") && oTable.childNodes[i].isChild=="0")
					{
						//close only the ones that are not "childs" but menus.
						if(oTable.childNodes[i].nextSibling)
						{
							//check if "oTable.childNodes[i].nextSibling" is the 
							//CHILDREN table , of the "oTable.childNodes[i]" row
							//the MenuLevel property could only be reached in a "brother" row,
							//and it is undefined in a "Children" row.
							 if(typeof(oTable.childNodes[i].nextSibling.MenuLevel)=="undefined")
							 {
								if (oTable.childNodes[i].nextSibling.style.display == "")
								{
									oTable.childNodes[i].nextSibling.style.display = "none";
									//changing the image to show that the menu is open
									oTable.childNodes[i].childNodes[1].childNodes[0].src=sCollapsdImgSrc;
									oTable.childNodes[i].className="Channel";
								}
							}
						}
					}
				}
			}
			oNextTR.style.display = "";
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sExpandedImgSrc;
			oTR.childNodes[0].className="HighlightedChannel";
		}
		else   //closing a second level menu
		{
			oNextTR.style.display = "none";
			//changing the image to show that the menu is open
			oTR.childNodes[1].childNodes[0].src=sCollapsdImgSrc;
			oTR.childNodes[0].className="Channel";
		}
	}
}
			
function highlightPosting(oTD)
{
	var oTR;
	oTR = oTD.parentElement;
	
	if(oTR.childNodes[0].className=="Posting")
	{
		oTR.childNodes[0].className="HighlightedPosting";
		oTR.className="HighlightedPosting";//add by sharon - set the class of highlight also to the TR
	}
	else if(oTR.childNodes[0].className=="TopPosting")
	{
		oTR.childNodes[0].className="HighlightedTopPosting";
		oTR.className="HighlightedTopPosting";//add by sharon - set the class of highlight also to the TR
	}
	else if(oTR.childNodes[0].className=="MiddlePosting")
	{
		oTR.childNodes[0].className="HighlightedMiddlePosting";
		oTR.className="HighlightedMiddlePosting";//add by sharon - set the class of highlight also to the TR
	}
	
	//changing the image to show that the page is active
	if(sActiveChildImgSrc!="")
	{
		oTR.childNodes[1].childNodes[0].src=sActiveChildImgSrc;
	}
	
	if(guidHighlightedUniqueID!="")
		disableSubSub(guidHighlightedUniqueID)
	guidHighlightedUniqueID=oTR.LevelUniqueID;
}
			
function disableSubSub(guidLevelUniqueID)
{
	//go through all "TR" element recoursively(on all levels)
	//in order to find the requested UniqueID
	var oTRCol = FirstLevelMainMenu.all.tags("TR");
	var oParentMenu;
	for(var i=0;i<oTRCol.length;i++)
	{
		//if the requested "TR" was found
		if(oTRCol[i].LevelUniqueID==guidLevelUniqueID)
		{
			if(oTRCol[i].childNodes[0].className=="HighlightedPosting")
			{
				oTRCol[i].childNodes[0].className="Posting";
			}
			else if(oTRCol[i].childNodes[0].className=="HighlightedTopPosting")
			{
				oTRCol[i].childNodes[0].className="TopPosting";
			}
			else if(oTRCol[i].childNodes[0].className=="HighlightedMiddlePosting")
			{
				oTRCol[i].childNodes[0].className="MiddlePosting";
			}
			//changing the image to show that the page is active
			if(sChildImgSrc!="")
			{
				//Changed by Eran on 15/04/2003
				//oTRCol[i].childNodes[0].childNodes[1].childNodes[0].src=sChildImgSrc;
				oTRCol[i].childNodes[0].src=sChildImgSrc;
			}
		}
	}
}

function OpenMenu(LevelUniqueID)
{
	//go through all "TR" element recoursively(on all levels)
	//in order to find the requested UniqueID
	var oTRCol = FirstLevelMainMenu.all.tags("TR");
	var oParentMenu;

	for(var i=0;i<oTRCol.length;i++)
	{
		//if the requested "TR" was found
		if(oTRCol[i].LevelUniqueID==LevelUniqueID)
		{
			//the row has been found by it's Unique level ID
			bIsRowFound=true;
			//case this is a child:
			if(oTRCol[i].isChild=="1")
			{
				highlightPosting(oTRCol[i].childNodes[1]);
				var iParentUniqueID,oParent;
				oParentMenu=getParentMenu(oTRCol[i]);

				//in case that this child has a parent and is not a top leveled child
				if(oParentMenu!=null)
				{
					OpenMenu(oParentMenu.previousSibling.LevelUniqueID)
				}
			}
			else //case this is a parent ("folder")
			{
				oParentMenu=getParentMenu(oTRCol[i]);
				//if this is the top level, and there is no parent
				if(oParentMenu==null)
				{
					//changing the image to show that the menu is open
					//oTRCol[i].childNodes[1].childNodes[0].src=sTopExpandedImgSrc;
					oTRCol[i].childNodes[0].className="HighlightedTopChannel";
					//Tatyana Dubinsky 23.08.05
				 if (oTRCol[i].childNodes[1].childNodes[0].document.location.pathname!="")
					 { 
						if (oTRCol[i].childNodes[1].childNodes[0].document.location.pathname.indexOf("/NR/exeres")>-1)
								oTRCol[i].childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
							else
								oTRCol[i].childNodes[1].childNodes[0].src=sChildImgSrc;
					  }	
				else
						oTRCol[i].childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
					//if there are any children for this folder -> open it, otherwise
					//only highlight it.
					if(oTRCol[i].nextSibling!=null)
					{
						if(oTRCol[i].nextSibling.style.display=="none")
						{
							oTRCol[i].nextSibling.style.display="";
							oFirstLevelMenuOpen=oTRCol[i].nextSibling;
						}
						else
						{
							oFirstLevelMenuOpen=CONST_MENU_WITH_NO_CHILDREN;
							oFirstLevelIndicator=oTRCol[i];
							//In case that there are no children we will change back
							//the Icon to "Collapsd Iamge" inorder not to show 
							//and "Open" icon with no children or sub menues.
							//oTRCol[i].childNodes[1].childNodes[0].src=sTopCollapsdImgSrc;
						}
					}
					else
					{
						oFirstLevelMenuOpen=CONST_MENU_WITH_NO_CHILDREN;
						oFirstLevelIndicator=oTRCol[i];
					}
					
					return;
				}
				else
				{
					//changing the image to show that the menu is open
					oTRCol[i].childNodes[1].childNodes[0].src=sExpandedImgSrc;
					oTRCol[i].childNodes[0].className="HighlightedChannel";
					if(oTRCol[i].nextSibling)
						oTRCol[i].nextSibling.style.display="";
					
					oParentMenu.style.display=""
					oFirstLevelMenuOpen=oParentMenu;
					//changing the image to show that the menu is open
					oParentMenu.previousSibling.childNodes[1].childNodes[0].src=sTopExpandedImgSrc;
					oParentMenu.previousSibling.childNodes[0].className="HighlightedTopChannel";
				}
			}
		}
	}
}
			
function getParentMenu(currMenuTR)
{
	var oParentMenu;
	var bGoUp=true;
	var bTopLevelMenu=false;
	oParentMenu=currMenuTR.parentElement;
	while(bGoUp)
	{
		if(oParentMenu.tagName=="TR")
			bGoUp=false;
		else if(oParentMenu.id=="FirstLevelMainMenu")
		{ //this is atop keveled menu, there is no parent menu
			bGoUp=false;
			return null;
		}
		else
			oParentMenu=oParentMenu.parentElement;
	}
	return oParentMenu;
}
			
function writeMenuStart()
{
	var str;
	str="<TABLE id='FirstLevelMainMenu' cellSpacing='0' cellPadding='0' width='" + iMenuTableWidth + "'   align=center>\n";
	return str;
}
function writeMenuEnd()
{
	var str;
	str="</TABLE>\n";
	return str;
}
function DrawNewLevelTableEnd()
{
	var str="";
	str+="</table>";
	str+="</TD>"
	str+="</TR>\n"
	return str;
}
			
function DrawNewLevelTableStart()
{
	var str="";
	str+="<TR style='DISPLAY: none'>";
	str+="<TD align=right colSpan='2'>";
	str+="<TABLE id='Table" + iTableUniqueID + "' cellSpacing='0' cellPadding='0' width='" + iMenuTableWidth + "'  border='0' align=right>\n";
	iTableUniqueID++;
	return str;
}

//separator row between channel groups
function addSeparatorRow()
{
	var sSeparatorRow = "";
	sSeparatorRow = "<TR><TD class='MenuSeparatorRow' colspan='2'></td></tr>\n";
	sSeparatorRow = sSeparatorRow + addSeparatorExtension();
	return sSeparatorRow;
}	

function addSeparatorExtension()
{
	var sSeparatorRow = "";
	//sSeparatorRow =  "<TR><TD class='MenuSeparatorRow1' colspan='2'>555555</td></tr>\n";
	return sSeparatorRow;
}			


function writeMenuRow(iRowIndex)
{
	var str,sClassName,sRightTDOnClick,sLeftTDOnClick;
	var iLevel,sCaption,sLink,bIsDirectory,sImgSrc;
	var iChannelGroup;
	str="";
	sRightTDOnClick="";
	sLeftTDOnClick="";
	iIsChild="-1";
	sCaption=mnuArr[iRowIndex][0];
	iLevel=parseInt(mnuArr[iRowIndex][1]);
	sLink=mnuArr[iRowIndex][2];
	//set the Guid for this menu entry
	guidLevelUniqueID=mnuArr[iRowIndex][4];
	//get the channel group
	iChannelGroup = mnuArr[iRowIndex][5];
	//alert(sCaption+' '+ iLevel+' '+sLink +' '+ mnuArr[iRowIndex][3]+' '+iChannelGroup);
	
	//"c" stands for channel which is a directory
	//"p" stands for posting which is a page
	if(mnuArr[iRowIndex][3]=="c")
		bIsDirectory=true;
	else
		bIsDirectory=false;
				
	switch(iLevel)
	{
		case 0:
			if(bIsDirectory)
			{
				sRightTDOnClick="openCloseFirstLevelSub(this);";
				sLeftTDOnClick="openCloseFirstLevelSub(this.nextSibling);";
				iIsChild="0";
				sClassName="TopChannel";
				//sImgSrc=sTopCollapsdImgSrc;
				//Tatyana Dubinsky 23.08.05
				 if (sLink!="")
					 { 
						if (sLink.indexOf("/NR/exeres")>-1)
								sImgSrc=sTopCollapsdImgSrc;
							else
								sImgSrc=sChildImgSrc;
					  }	
				else
						sImgSrc=sTopCollapsdImgSrc;
							
			}
			else
			{
				sClassName="TopPosting";
				sRightTDOnClick="highlightPosting(this);";
				sLeftTDOnClick="highlightPosting(this.nextSibling);";
				iIsChild="1";
				//sImgSrc=sTopCollapsdImgSrc;
				sImgSrc=sChildImgSrc;
			}
			
		break;
		case 1:
			if(bIsDirectory)
			{
				sClassName="Channel";
				sRightTDOnClick="openCloseSecondLevelSub(this);";
				sLeftTDOnClick="openCloseSecondLevelSub(this.nextSibling);";
				iIsChild="0";
				//Tatyana Dubinsky 23.08.05
				if (sLink!="")
					 { 
						if (sLink.indexOf("/NR/exeres")>-1)
								sImgSrc=sTopCollapsdImgSrc;
							else
								sImgSrc=sChildImgSrc;
					  }
					  //if it there is no link so it will be collased
				else
						sImgSrc=sTopCollapsdImgSrc;
					
			}
			else
			{
				sClassName="MiddlePosting";
				sRightTDOnClick="highlightPosting(this);";
				sLeftTDOnClick="highlightPosting(this.nextSibling);";
				iIsChild="1";
				sImgSrc=sChildImgSrc;
			}
			
		break;
		case 2:
			sClassName="Posting";
			sRightTDOnClick="highlightPosting(this);";
			sLeftTDOnClick="highlightPosting(this.nextSibling);";
			iIsChild="1";
			sImgSrc=sChildImgSrc;
		break;
		default:
			sClassName="Posting";
			sRightTDOnClick="highlightPosting(this);";
			sLeftTDOnClick="highlightPosting(this.nextSibling);";
			iIsChild="1";
			sImgSrc=sChildImgSrc;
		break;
	}
	
	if(sLink!="")
	{
		sLeftTDOnClick+="document.location=\"" + sLink + "\";"
		if((iLevel==2)||(!bIsDirectory))
		{
			sRightTDOnClick+="document.location=\"" + sLink + "\";"
		}
	}
				
	if(iLastDrawnLevel>=0)
	{
		//if this is a new level of menus, a table should be added
		if(iLastDrawnLevel>iLevel)
		{
			var iNumOfTablesToClose;
			iNumOfTablesToClose=iLastDrawnLevel-iLevel;
			for(var j=0;j<iNumOfTablesToClose;j++)
			{
				str+=DrawNewLevelTableEnd();
			}
		}
	}
				
	if(iLastDrawnLevel>=0)
	{
		//if this is a new level of menus, a table should be added
		if(iLastDrawnLevel<iLevel)
		{
			str+=DrawNewLevelTableStart();
		}
	}
	
	//add an separate row before the menu row every time the gChannelGroup value is change 
	//(it Happened every time that the last parameter at mnuArr is change - represent the group of channel)	
	//if (gChannelGroup != iChannelGroup)//gChannelGroup & iChannelGroup start from 1
	//{
		str+=addSeparatorRow();
	//	gChannelGroup = iChannelGroup;//update the global param with the new channel group id
	//}
	
	str+="<TR class='" + sClassName + "' isChild='" + iIsChild + "' LevelUniqueID=" + guidLevelUniqueID + " MenuLevel=" + iLevel + ">";
	str+="<TD class='" + sClassName + "' onclick='" + sLeftTDOnClick + "' "
	str+=" dir=rtl align=right width='" + iMenuLeftCellWidth + "'>" + sCaption + "</TD>";
	str+="<TD class='BulletCell' align=center ";
				
	//determine if this row represents a directory or a page
	if(sImgSrc!="")
	{
		str+="onclick='" + sRightTDOnClick + "' width='" + iMenuBulletCellwidth + "' valign='center'><IMG SRC='" + sImgSrc + "' BORDER=0 vspace='2'></TD>";
	}
	else
	{
		str+="onclick='" + sRightTDOnClick + "' width='" + iMenuBulletCellwidth + "'>&nbsp;</TD>";
	}

	str+="</TR>\n";
	
	iLastDrawnLevel=iLevel;
	//alert(str)
	return str;
}

function drawMenu(strAppPath)
{
	sTopCollapsdImgSrc = strAppPath + sTopCollapsdImgSrc;
	sTopExpandedImgSrc = strAppPath + sTopExpandedImgSrc;
	sCollapsdImgSrc = strAppPath + sCollapsdImgSrc;
	sExpandedImgSrc = strAppPath + sExpandedImgSrc;
	if(sChildImgSrc!="")
	{
		sChildImgSrc = strAppPath + sChildImgSrc;
	}
	if(sActiveChildImgSrc!="")
	{
		sActiveChildImgSrc = strAppPath + sActiveChildImgSrc;
	}

	var str;
	str="";
	str+=writeMenuStart();
				
	str+=addSeparatorRow()
	for(var i=0;i<mnuArr.length;i++)
	{
		str+=writeMenuRow(i);
	}
	str+=writeMenuEnd();
	//alert(str)
	MenuDiv.innerHTML = str;
}