var timer1;
var subMenuDelay=120;
var menuDelay=120;
var left,top;
var timerhide;
var loopcount=0;
var scrollUpTimer;
var scrollDownTimer;

var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.MOUSEMOVE)
if (!IE) document.captureEvents(Event.MOUSECLICK)

document.onmousemove = getMouseXY;
document.onmouseup = hideOnMoustDown;

var tempX = 0;
var tempY = 0;

var curDiv = new Array();
var SelObj = new Array(8);

SelObj[0]="AjaxRootDiv1";
SelObj[1]="AjaxRootDiv1";
SelObj[2]="AjaxRootDiv1";
SelObj[3]="AjaxRootDiv1";
SelObj[4]="AjaxRootDiv1";
SelObj[5]="AjaxRootDiv1";
SelObj[6]="AjaxRootDiv1";
SelObj[7]="AjaxRootDiv1";



for(i=1; i<=7; i++)
{
	document.getElementById("AjaxRootDiv"+i).style.visibility = "hidden";
	document.getElementById("AjaxRootDiv"+i).style.position = "absolute"; 
	document.getElementById("AjaxRootDiv"+i).style.width = "1px"; 
	document.getElementById("AjaxRootDiv"+i).style.top = 0;
	document.getElementById("AjaxRootDiv"+i).style.left = 0;
}


function hideOnMoustDown()
{	
	if (timerhide)
	{
		setTimeout(function(){removeAjaxMenuFunction()},120);
	}
}
function getMouseXY(e)
{
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}
	return true;
}
function removeAjaxMenuFunction()
{
	for(i=1;i<=7;i++)
	{
		document.getElementById("AjaxRootDiv"+i).style.visibility = "hidden";
	}
}
function removeAjaxSubMenuFunction(divName)
{
	for(i=divName;i<7;i++)
	{
		document.getElementById("AjaxRootDiv"+i).style.visibility = "hidden";
		document.getElementById("AjaxRootDiv"+i).style.height= "auto";
		document.getElementById("AjaxRootDiv"+i).innerHTML="";
	}
}

function ajaxFunction(ides,divName, thisObj )
{
	// document.title = navigator.appName ;
	var pos;
	if( divName >5) {return 0}
	if( divName >1)
	 {
		var divnam = divName - 1;
		
		if (navigator.appVersion.indexOf("MSIE")!=-1)
		{
			pos = GetAbsPosition2(document.getElementById(thisObj));
			pos.x += 161;
			pos.y -= 8;
		}
		
		else
		{	pos = GetAbsPosition(document.getElementById(thisObj));
			pos.x += 161;
			pos.y += 0;
		}
	 }
	 else
	 {
		if (navigator.appVersion.indexOf("MSIE")!=-1)
		{
			pos = GetAbsPosition2(document.getElementById(thisObj));
			pos.x += 175;
			pos.y  = 0 + pos.y ;
		}else{
			pos = GetAbsPosition(document.getElementById(thisObj));
		}
	 }
	for(i=6;i>divName;i--)
	{
		SelObj[i]="AjaxRootDiv1";
	}
	 document.getElementById(SelObj[divName]).style.backgroundColor =  "#CC6600";
	 SelObj[divName] = thisObj;
	 document.getElementById(thisObj).style.backgroundColor =  "#EE8844";
	 
	for(i=divName;i<7;i++)
	{
		document.getElementById("AjaxRootDiv"+i).style.visibility = "hidden";	
		document.getElementById("AjaxRootDiv"+i).style.height= "auto";
	}
	var xmlHttp;
	try
	{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	  // Internet Explorer
		try
	    {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
		catch (e)
	    {
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				// alert("Your browser does not support AJAX!");
				return false;
			}
	    }
	}
	var temp=0;
	//  Check for Google Chrome Correction
	if ( InStr(navigator.userAgent,"Chrome") >0 )
		if ( divName == 1 )	
			pos.x += 180;
			
	document.getElementById("AjaxRootDiv"+divName).style.visibility = "visible";
	document.getElementById("AjaxRootDiv"+divName).style.position="absolute"; 
	document.getElementById("AjaxRootDiv"+divName).style.top =  pos.y;
	document.getElementById("AjaxRootDiv"+divName).style.left = pos.x ;
	

	if ( parseInt(divName)>1 ) { 
		if (document.getElementById("AjaxRootDiv"+(parseInt(divName)-1)+"AjaxRootDiv"+(parseInt(divName)-1)))
			document.getElementById("AjaxRootDiv"+divName).style.top = pos.y - document.getElementById("AjaxRootDiv"+(parseInt(divName)-1)+"AjaxRootDiv"+(parseInt(divName)-1)).scrollTop;
	}
	document.getElementById("AjaxRootDiv"+divName).innerHTML="<table width='160px'><tr><td bgcolor='#CC6600'><font color='#FFFFFF'><B>Loading ...</B></font></td></tr></table>";
	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==4)
		{
			if (xmlHttp.responseText == "")
			{
			  document.getElementById("AjaxRootDiv"+divName).innerHTML = "No Item Found";
			}
			else
			{
				document.getElementById("AjaxRootDiv"+divName).innerHTML = xmlHttp.responseText;
				setPosition("AjaxRootDiv"+divName);			  
			}
		}
    }  
	xmlHttp.open("GET","http://www.pelltechnology.com/menu/menu.asp?myid="+ides+"&divToFill="+divName,true);
	xmlHttp.send(null);
  }
  
function setPosition(objName)
{
	var obj = document.getElementById(objName);
	var nPos = GetAbsPosition(document.getElementById(objName));
	var subDivNo = 6;
	subDivNo = parseInt(objName.substr(objName.length-1,1));
	subDivNo = subDivNo+1;
	
	if (nPos.cy >  document.body.clientHeight)
	{
			
		var nDiv = new Object(); 
		nDiv = document.createElement("div");
		nDiv.style.top = 15 ;
		nDiv.style.height = document.body.clientHeight-45;
		nDiv.style.width  = 180; //+iwidth;
		nDiv.style.left = 0 ;
		nDiv.style.overflow = "auto";
		nDiv.style.overflowX = "hidden";
		nDiv.style.position = "absolute";
		nDiv.id = objName + objName;
		nDiv.name = objName + objName;
		
		var scrollUpTop = "<div  id='"+objName + objName+"UT' style=\"z-Index:1; align:left;  vertical-align: bottom; position:absolute; left:0px; top:0px; width:160px; height:15px; background-color:#abcdef; \"><span style=\"top:15px; vertical-align: middle; display:block;\" onmouseover=\"setScrollUp('"+objName + objName+"'); clearTimeout(timerhide);setTimeout(function(){ removeAjaxSubMenuFunction("+subDivNo+");},120);\" onmouseout=\"clearTimeout(scrollUpTimer); timerhide=setTimeout(function(){removeAjaxMenuFunction();},3000);\" ><table border='0' width='160'><tr><td  align='center' valign='bottom' ><img align='center' src='http://www.pelltechnology.com/menu/up_arrow.gif' /></td></tr></table></span></div>";		
		var scrollDownBot;
		var scrollUpBot;		
		scrollDownBot = "<div id='"+objName + objName+"DB' style=\" z-index:10; align:left;  vertical-align: middle; position:absolute; top:"+(document.body.clientHeight-30)+"px; left:0px; width:160px; height:15px; background-color:#abcdef;\"><span style=\" vertical-align: middle;\" onmouseover=\"setScrollDown('"+objName + objName+"'); clearTimeout(timerhide);setTimeout(function(){removeAjaxSubMenuFunction("+subDivNo+");},120);\" onmouseout=\"clearTimeout(scrollDownTimer); timerhide=setTimeout(function(){removeAjaxMenuFunction();},3000);\" ><table border='0' borderCollapse='collapse' width='160'><tr><td align='center'><img align='center' src='http://www.pelltechnology.com/menu/down_arrow.gif' /></td></tr></table></span></div>";
		
		var dhtml = document.getElementById(objName).innerHTML;
		document.getElementById(objName).innerHTML ="";
		document.getElementById(objName).appendChild(nDiv);
				
		var pHtml = document.getElementById(objName).innerHTML ;		
		
		nDiv.innerHTML = dhtml ; 
		pHtml = document.getElementById(objName).innerHTML ;		
		
		document.getElementById(objName).innerHTML = scrollUpTop +  pHtml + scrollDownBot; // single scroll button  up, down
		
		document.getElementById(objName).style.top = 10 + document.body.scrollTop;
		document.getElementById(objName).style.height = document.body.clientHeight -15;
		document.getElementById(objName).style.width  = 161;
		document.getElementById(objName).style.overflow = "hidden";

	}else
	{		
		var pName = "AjaxRootDiv"+ (objName.substring(objName.length-1)-1);			
		var parPos = GetAbsPosition(document.getElementById(pName))
		var parScroll = 0;
		if(document.getElementById(pName+pName))
		{
			parScroll = document.getElementById(pName+pName).scrollTop;
		}
		if(nPos.y + nPos.cy - document.body.scrollTop > document.body.clientHeight ){
			document.getElementById(objName).style.top = nPos.y - ((nPos.y+nPos.cy)-document.body.clientHeight- document.body.scrollTop) -5;
		}else{
			document.getElementById(objName).style.top = nPos.y;
		}		
		return 0;		
	}
}

function setScrollDown(objName)
{
	var scrollTop = 0;
	var scrollHeight = 0;
	var clientHeight = 0;
	
	scrollTop = document.getElementById(objName).scrollTop;
	scrollHeight = document.getElementById(objName).scrollHeight;
	clientHeight = document.getElementById(objName).clientHeight;
	
	if (scrollTop + 5 < scrollHeight-clientHeight)
	{
		document.getElementById(objName).scrollTop = scrollTop + 5;		
		scrollDownTimer = setTimeout(function(){setScrollDown(objName);},5);
	}else
	{
		document.getElementById(objName).scrollTop = scrollHeight - clientHeight;		
	}
}
function setScrollUp(objName)
{
	var scrollTop = 0;
	scrollTop = document.getElementById(objName).scrollTop;
	if (scrollTop - 5 > 0)
	{
		document.getElementById(objName).scrollTop = scrollTop - 5;
		scrollUpTimer = setTimeout(function(){setScrollUp(objName)},5);
	}else
	{
		document.getElementById(objName).scrollTop = 0;
	}
}
function GetAbsPosition(object) {
	var position = new Object;
	position.x = 0;
	position.y = 0;

	if( object ) {
		position.x = object.offsetLeft;
		position.y = object.offsetTop;

		if( object.offsetParent ) {
			var parentpos = GetAbsPosition(object.offsetParent);
			position.x += parentpos.x;
			position.y += parentpos.y;
		}
	}
	try{
		position.cx = object.offsetWidth;
		position.cy = object.offsetHeight;
	}catch( e)
	{
		position.cx = 0;
		position.cy = 0;
	}

	return position;
}

function GetAbsPosition2(object) {
	var position = new Object;
	position.x = 0;
	position.y = 0;

	if( object ) {
		position.x = object.offsetLeft;
		position.y = object.offsetTop;

		if( object.offsetParent ) {
			var parentpos = GetAbsPosition2(object.offsetParent);
			position.x += parentpos.x;
			position.y += parentpos.y;
		}
	}
	return position;
}
function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, charSearchFor.length))
                  {
                        return i;
                  }
            }
            return -1;
}
function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}