var ie4=document.all
var ns6=document.getElementById&&!document.all

// Disable for debugging
function errorHandler() {
  return true;
}

window.onerror = errorHandler;

if (ie4||ns6)
	document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+'; aonMouseover="clearhidemenu()" aonMouseout="dynamichide(event)"><iframe width="100%" id="menucontainer" src="/rpinweb/menucontents.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" onMouseover="clearhidemenu()" onMouseout="dynamichide(event);"></iframe></div>')

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}


function showhide(obj, e, visible, hidden, menuwidth){
	if (ie4||ns6)
		dropmenuobj.style.left=dropmenuobj.style.top=-500
		;

	if (menuwidth!=""){
		dropmenuobj.widthobj=dropmenuobj.style
		dropmenuobj.widthobj.width=menuwidth
	}

	if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover" || e.type=="focus")
		obj.visibility=visible
	else if (e.type=="click")
		obj.visibility=visible
}

function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
	var edgeoffset=0
	if (whichedge=="rightedge"){
		var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
	}
	else{
		var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
	if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
		edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
	}
	return edgeoffset
}

function populatemenu(what){
  var containerobj = document.getElementById ? document.getElementById("menucontainer") : menucontainer;
  //alert(what.join(""));
  if (containerobj.Document)
  {
	//containerobj.Document.open();
  	//containerobj.Document.write ('<html lang="en"><head><link rel="stylesheet" type="text/css" href="/rpinweb/menustyle.css"><base target="_parent"></head><body id="dropmenudiv"><div style="height:150px">&nbsp;</div></body></html>');
  	//containerobj.Document.close();
  }
  if (containerobj.contentDocument) 
  {
	// This does not work in firefox for some reason -
	// In order to make it work, I have included the content directly into the iframe source above using menucontents.html
	// this trick worked - Rajeev
	//
	//containerobj.contentDocument.open();
  	//containerobj.contentDocument.write ('<html lang="en"><head><link rel="stylesheet" type="text/css" href="/rpinweb/menustyle.css"><base target="_parent"></head><body id="dropmenudiv"></body></html>');
  	//containerobj.contentDocument.close();
  }	

	if (ie4||ns6)
  {
		if (containerobj.Document) 
    {
			containerobj.Document.body.innerHTML =  what.join("");
			// Do not remove this window.status message. For some reason, IE does not report proper scrollHeight if 
			// this is removed.
			window.status = containerobj.Document.body.scrollHeight + "-" + containerobj.Document.body.offsetHeight;
			dropmenuobj.height= containerobj.Document.body.scrollHeight;
			//alert(dropmenuobj.height);
			//dropmenuobj.width= containerobj.Document.body.wcrollWidth;
		}

    if (containerobj.contentDocument) 
		{
			containerobj.contentDocument.body.innerHTML = what.join("");
			dropmenuobj.height= containerobj.contentDocument.body.scrollHeight;
			//dropmenuobj.width= containerobj.contentDocument.body.scrollWidth;
		}
  }
  containerobj.height= dropmenuobj.height;

  //var tags = containerobj.Document.getElementsByTagName("a");
  //alert(tags[0].name);
  //tags[0].focus();
  //containerobj.Document.body.focus();
  
  //containerobj.width= dropmenuobj.width;
}

function dropdownmenu(obj, e, menucontents, menuwidth){
	if (window.event) 
		event.cancelBubble=true
	else if (e.stopPropagation) e.stopPropagation()
		clearhidemenu()
		;
		
	dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
	populatemenu(menucontents)

	if (ie4 || ns6)
	{
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)

		dropmenuobj.x=getposOffset(obj, "left")
		dropmenuobj.y=getposOffset(obj, "top") + menuoffsethorizontal;
		
		dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
		dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
	}

	if (ns6){

		dropmenuobj.style.left=getposOffset(obj, "left")-clearbrowseredge(obj, "rightedge")+"px"
		//dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
		//dropmenuobj.style.left=dropmenuobj.x +"px"
		dropmenuobj.style.top=getposOffset(obj, "top") + menuoffsethorizontal-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
		//dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
		//dropmenuobj.style.top=getposOffset(obj, "top") + menuoffsethorizontal + obj.offsetHeight + "px"
		
		dropmenuobj.x=getposOffset(obj, "left")
		dropmenuobj.y=getposOffset(obj, "top") + menuoffsethorizontal;

		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
	}

	return clickreturnvalue()
}

function dropdownmenuvertical(obj, e, menucontents, menuwidth){
	if (window.event) 
		event.cancelBubble=true
	else if (e.stopPropagation) e.stopPropagation()
		clearhidemenu()

	dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
	populatemenu(menucontents)

	if (ie4){
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)

		dropmenuobj.style.left=getposOffset(obj, "left") + menuoffsetvertical-clearbrowseredge(obj, "rightedge")+"px"
		dropmenuobj.style.top=getposOffset(obj, "top")-clearbrowseredge(obj, "bottomedge")+"px"

		dropmenuobj.x=getposOffset(obj, "left") + menuoffsetvertical;
		dropmenuobj.y=getposOffset(obj, "top")
		
	}

	if (ns6){
		dropmenuobj.style.left=getposOffset(obj, "left") + menuoffsetvertical-clearbrowseredge(obj, "rightedge")+"px"
		dropmenuobj.style.top=getposOffset(obj, "top")-clearbrowseredge(obj, "bottomedge")+"px"

		dropmenuobj.x=getposOffset(obj, "left") + menuoffsetvertical;
		dropmenuobj.y=getposOffset(obj, "top")
		
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
	}

	return clickreturnvalue()
}

function clickreturnvalue(){
	if (ie4||ns6) return false
	else return true
}

function contains_ns6(a, b) {
	if (typeof(b) != "undefined" && b)
	{
		while (b.parentNode)
			if ((b = b.parentNode) == a)
				return true;
	}
	return false;
}

function dynamichide(e){
	if (ie4&&!dropmenuobj.contains(e.toElement))
		delayhidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		delayhidemenu()
}

function hidemenu(e){
	if (typeof dropmenuobj!="undefined"){
		if (ie4||ns6)
			dropmenuobj.style.visibility="hidden"
	}
}

function delayhidemenu(){
	if (ie4||ns6)
		delayhide=setTimeout("hidemenu()",disappeardelay)
}

function clearhidemenu(){
	if (typeof delayhide!="undefined")
		clearTimeout(delayhide)
}

if (hidemenu_onclick=="yes")
	document.onclick=hidemenu
