String.prototype.endsWith = function(sEnd)
{
	return (this.substr(this.length-sEnd.length)==sEnd);
}
String.prototype.startsWith = function(sStart)
{
	return (this.substr(0,sStart.length)==sStart);
}
String.prototype.standardize = function()
{
	return (this.replace(/[^a-zA-z0-9]/g,'').toLowerCase())
}

function setmouseover(obj)
{
	obj.style.textDecoration = 'underline';
	obj.style.cursor = 'hand';
}

function setmouseout(obj,style)
{
	obj.style.textDecoration = 'none';
	obj.style.cursor ='pointer';
}
function menu(){
   this.subMenus = new Array();
   this.add = addSubMenu;
   this.type = 'menu';
   this.write = writeMenu;
}

function submenu(id, text,link, url,styleclass,shownugget){

   this.id = id.standardize();
   this.text = text;
   this.url = url;
   this.link = link;
   this.type = 'submenu';
   this.styleClass = styleclass;
   this.write = writeSubMenu;
   this.add = addMenuItem;
   this.nugget = shownugget;
   this.menuItems = new Array();
}

function menuitem(id,text, link,url,submenuname,styleclass,shownugget){
	 this.id = (submenuname +  id).standardize();
	 this.menuname= id.standardize();
   this.text = text;
   this.link = link;
   this.url = url;
   this.type = 'menuitem';
   this.subMenuName =submenuname.standardize();
   this.styleClass=styleclass;
   this.write = writeMenuItem;
   this.nugget = shownugget;
}

function writeMenuItem(){
	 var html =	'<div width="100%" class="' + this.styleClass + '" id="'+ this.id +'div" ><a   class="' + this.styleClass + '" id="'+ this.id +'" href="' + this.link + '">'+ writeSubmenuNugget(this.nugget,this.styleClass )   +  this.text + '</a>' + writeItemNugget(this.nugget,this.styleClass ) + '</div>';
	 
   return html;
   
}

function writeMenu(){
   var menuStr = '';
   var numSubMenus = this.subMenus.length;
  
   for (var i=0;i <numSubMenus;i++)
      menuStr += this.subMenus[i].write();
      
   document.write(menuStr);
   
}
function writeSubmenuNugget(nugget,type)
{	var returnstr='';
	if ((nugget == true)&&(type!='menuitem'))
	{
		returnstr = '<span class="menunugget"></span>';
	}	
	
		return returnstr
}
function writeItemNugget(nugget,type)
{
	var returnstr='';
	if ((nugget == true)&&(type!='boldmenuitem'))
	{
			returnstr = '<span class="itemnugget"></span>';
	}

	return returnstr
}
function writeSubMenu(){
	var numItems = this.menuItems.length;
	var subMenuStr ='';
			subMenuStr ='<div id="' + this.id + 'menu" width="100%"  class="' + this.styleClass + '" onmouseover="setmouseover(this);" onmouseout="setmouseout(this,\'' + this.styleClass + '\');" onClick="' + this.link + '"';
			subMenuStr += '>'+ writeSubmenuNugget(this.nugget) + this.text;
			subMenuStr += '</div>';
			
			subMenuStr += '<div width="100%" class="submenu" id="';
			subMenuStr += this.id + '">';
  
  
		for (var j=0;j<numItems;j++)
			subMenuStr += this.menuItems[j].write();
		
		subMenuStr += '</div>';

  return subMenuStr;
}

function addMenuItem(menuitem){
   this.menuItems[this.menuItems.length] = menuitem;
}

function addSubMenu(submenu){
   this.subMenus[this.subMenus.length] = submenu;
}

function ExpandMenu(menuname)
{
	
	if (menuname.indexOf('~')>0)
	{
		var menus= menuname.split("~");
		for (i=0;i<menus.length;i++)
		{
			Highlight(menus[i]);
		}
	}
	else 
	{
		Highlight(menuname);
	}
	
}

function Highlight(menuname)
{
	var currMenu=menuname.standardize();
	var SubMenuNum = oMenu.subMenus.length;
	var submenuname;
	for (var i=0;i<SubMenuNum;i++)
	{
		if (oMenu.subMenus[i].id==currMenu) 
		{
			HighlightSubmenu(currMenu,oMenu.subMenus[i].type=='submenu');
		}
		if (oMenu.subMenus[i].type=='submenu')
		{
			MenuItemNum =oMenu.subMenus[i].menuItems.length;
				for (var j=0;j<MenuItemNum;j++)
				{
					if (oMenu.subMenus[i].menuItems[j].menuname == currMenu)
					{
						HighlightMenuItem(oMenu.subMenus[i].menuItems[j].subMenuName+currMenu);
					}
				}
		}
	}
	
}

function HighlightMenuItem(menuname)
{
	document.getElementById(menuname).className  = 'menuitemselected';
	document.getElementById(menuname + 'div').className  = 'menuitemselected';
}

function HighlightSubmenu(menuname,isSubMenu)
{
	
	if (isSubMenu)
	{
		document.getElementById(menuname + 'menu').className  = 'boldmenuselected';
		document.getElementById(menuname).style.display = 'block';
	}
	else 
	{
			document.getElementById(menuname + 'div').className  = 'boldmenuitemselected';
			document.getElementById(menuname ).className  = 'boldmenuitemselected';
	}
}

