NS4=(document.layers) ? true : false;
IE4=(document.all)?true:false;
ver4 = (NS4 || IE4) ? true : false;
areCreated=false;
if (!ver4) 
   event=null;
else 
   {
   menuWidth = 165;
   childOverlap = 3;
   childOffset = 5;
   perCentOver = null;
   secondsVisible = 0.5;
   fntCol = "#FFFFFF";
   overFnt = "white";
   fntSiz = "13px";
   fntWgh = "normal";
   fntSty = "bold";
   fntFam = "Verdana";
//   fntFam = "arial";
//   backCol = "#339966";
   backCol = "#a4a4a4";
   overCol = "#9500ff";
//   overCol = "#0000CC";
//   menuElemOverCol = "#9500ff";
   menuElemOverCol = "#ffffff";
//   menuElemOverCol = "#ED1C2E";
//Lucio   menuElemCol = "#000000";
   menuElemCol = "#000000";
   linHgt = "normal";
   borWid = 1;
   borCol = "#535353"
   borSty = "outset";
   imgSrc = "graph/arrow.png";
   imgSiz = 7;

   if (perCentOver != null) {
	  childOverlap = (perCentOver/100) * menuWidth
      }
   mSecsVis = secondsVisible*1000;

   semi = ";";
   styleStr = "<STYLE TYPE='text/css'>"
   styleStr += ".items {"
   styleStr += "width:" + menuWidth + semi
   styleStr += "color:"+ fntCol + semi
   styleStr += "font-size:"+ fntSiz + semi
   styleStr += "font-weight:"+ fntWgh + semi
   styleStr += "font-style:"+ fntSty + semi
   styleStr += "font-family:"+ fntFam + semi
   styleStr += "border-width:" + borWid + semi
   styleStr += "border-color:" + borCol + semi
   styleStr += "border-style:" + borSty + semi
   styleStr += "line-height:" + linHgt + semi
   styleStr += "}"
   styleStr += "</STYLE>";
 
   document.write(styleStr);

   imgStr = "<IMG SRC=" + imgSrc + " WIDTH=" + imgSiz + " HEIGHT=" + imgSiz +" BORDER=0 VSPACE=2 ALIGN=RIGHT>"

   topCount = 1;
   topCountDestp = 1;
   areCreated = false;
   isOverMenu = false;
   currentMenu = null;
   allTimer = null;
   }

function image_open(direc, file_htm, larga, alta) {
    win = null
	var file_completo = direc + file_htm
    var win_w = larga;
    var win_h = alta + 20;
    var left = Math.floor((screen.width - win_w) / 2);
    var top = Math.floor((screen.height - win_h) / 2);
    var str = 'top=' + top + ',left=' + left + ',height=' + win_h + ',width=' + win_w;
    if (win != null) {
        win.close();
    }

    win = window.open(file_completo,'', 
    "toolbar=0,location=0,status=0,menubar=0,scrollbars=NO,resizable=NO, fullscreen=no,"+str) 
    win.focus();
}


function changeColor(menuID) {
	// Posiziona il colore 'hover' della voce di menu
//    alert("menuID="+menuID);
	document.getElementById(menuID).style.color = menuElemOverCol;
//    alert("DOPO");

}

function originalColor(menuID) {
	// Posiziona il colore 'hover' della voce di menu
//    alert("menuID="+menuID);
	document.getElementById(menuID).style.color = menuElemCol;
//    alert("DOPO");

}

function menuSetup(hasParent,lastItem,openCont,openItem) {

	this.menuOver = menuOver;
	this.menuOut = menuOut;

	this.onmouseover = this.menuOver;
	this.onmouseout = this.menuOut;

	this.showIt = showIt;
	this.keepInWindow = keepInWindow;

	this.hideTree = hideTree
	this.hideParents = hideParents;
	this.hideChildren = hideChildren;
	this.hideTop = hideTop;
	
	this.hasChildVisible = false;
	this.isOn = false;
	
	this.hideTimer = null;

	if (hasParent) {
		this.hasParent = true;
		this.parentMenu = openCont;
		this.parentItem = openItem;
		this.parentItem.child = this;
	}
	else {
		this.hasParent = false;
		this.hideSelf = hideSelf;
	}

	if (NS4) {
		this.fullHeight = lastItem.top + lastItem.document.height;
		this.clip.bottom = this.fullHeight;
	}
	else {
	    this.fullHeight = lastItem.style.pixelTop + lastItem.offsetHeight;
		this.showIt(false);
		this.onselectstart = cancelSelect;
		this.moveTo = moveTo;
		this.moveTo(0,0);
	}
}

function lancia (pagina)
{
	window.location=pagina
}

function itemClick()
{
	if (this.linkText == "#")
	   return;
	if (this.popupDx == "1000")
	   {
	   lancia (this.linkText);
	   return
	   }
	popupName = this.linkText.substring(0, this.linkText.indexOf("."));
	ShowPopup(popupName, this.linkText, this.popupDx, this.popupDy, this.popupPosX, this.popupPosY, 1);
}

function itemSetup(arrayPointer,whichArray) {

	this.itemOver = itemOver;
	this.itemOut = itemOut;
	this.itemClick = itemClick;
	
	this.onmouseover = this.itemOver;
	this.onmouseout = this.itemOut;
	this.dispText = whichArray[arrayPointer];
	this.linkText = whichArray[arrayPointer + 1];
	this.baseLinkText = whichArray[arrayPointer + 1];
	this.hasMore = whichArray[arrayPointer + 2];
	this.popupDx = whichArray[arrayPointer + 3];
	this.popupDy = whichArray[arrayPointer + 4];
	this.popupPosX = whichArray[arrayPointer + 5];
	this.popupPosY = whichArray[arrayPointer + 6];

	if (this.linkText.length > 0) {
		this.linkIt = linkIt;
		if (NS4) {
			this.onfocus = this.linkIt;
		}
		else {
		    if (whichArray[arrayPointer + 7] == 1)
               this.onclick = this.itemClick;
			else
			   this.onclick = this.linkIt;
			this.style.cursor = "hand";
		}
	}
      
	if (this.hasMore) {
		htmStr = imgStr + this.dispText;
	}
	else {
		htmStr = this.dispText;
	}

	if (NS4) {
		layStr = "<SPAN CLASS=items>" + htmStr+ "</SPAN>";
		this.document.write(layStr);
		this.document.close();

		this.bgColor = backCol;
		this.clip.right = menuWidth;
		this.visibility = "inherit";
		this.container = this.parentLayer;

		if (arrayPointer == 0) {
			this.top = 0;
		}
		else {
			this.top = this.prevItem.top + this.prevItem.document.height - borWid;
		}
		this.left = 0;
	}
	else {
		this.className = "items";
		this.style.padding = 3;
		this.innerHTML = htmStr;

		this.style.backgroundColor = backCol; 
		this.container = this.offsetParent;

		if (arrayPointer == 0) {
			this.style.pixelTop = 0;
		}
		else {
			this.style.pixelTop = this.prevItem.style.pixelTop + this.prevItem.offsetHeight - borWid;
		}
		this.style.pixelLeft = 0;
	}
}

function makeElement(whichEl,whichContainer) {
	if (arguments.length==1)
		whichContainer = (NS4) ? window : document.body;

	if (NS4) {
		eval(whichEl + "= new Layer(menuWidth,whichContainer)");
	}
	else {
		elStr = "<DIV ID=" + whichEl + " STYLE='position:absolute'></DIV>";
		whichContainer.insertAdjacentHTML("BeforeEnd",elStr);
	}

	return eval(whichEl);
}

function makeTop() {
//    areCreated = true;  lucio
	while(eval("window.arMenu" + topCount)) {
		topArray = eval("arMenu" + topCount);
		topName = "elMenu" + topCount;

		topMenu = makeElement(topName);
    	topMenu.setup = menuSetup;

		topItemCount = 0;
		for (i=0; i<topArray.length; i+=NUM_ELEM_PER_ITEM) {	
			topItemCount++;
			status = "Creazione menu in corso, attendere per favore: " + topCount + " / " + topItemCount;
			topItemName = "item" + topCount + "_" + topItemCount;
			topItem = makeElement(topItemName,topMenu);

			if (topItemCount >1)
				topItem.prevItem = eval("item" + topCount + "_" + (topItemCount-1));

			topItem.setup = itemSetup;
			topItem.setup(i,topArray);
			if (topItem.hasMore) makeSecond();
		}
		topMenu.setup(false,topItem);
		topCount++
	}

    status = (topCount-1) + " Creazione del menu completata";

	areCreated = true;
}

function makeSecond() {

	secondCount = topCount + "_" + topItemCount;
	
	secondArray = eval("arMenu" + secondCount);
	secondName = "elChild" + secondCount;
	
	secondMenu = makeElement(secondName);
	secondMenu.setup = menuSetup;

	secondItemCount=0;
	for (j=0; j<secondArray.length; j+=NUM_ELEM_PER_ITEM) {
		secondItemCount++;
		secondItemName = "item" + secondCount +"_" + secondItemCount;

		secondItem = makeElement(secondItemName,secondMenu)		
		
		if (secondItemCount >1)
			secondItem.prevItem = eval("item" + secondCount  + "_" + (secondItemCount-1));

		secondItem.setup = itemSetup;
		secondItem.setup(j,secondArray);

		if (secondItem.hasMore) makeThird();
	}

	secondMenu.setup(true,secondItem,topMenu,topItem);
}

function makeThird() {
	thirdCounter = secondCount + "_" + secondItemCount 
	
	thirdArray = eval("arMenu" + thirdCounter);
	thirdName = "elGrandChild" + thirdCounter;
	thirdMenu = makeElement(thirdName)
	
	thirdMenu.setup = menuSetup;

	thirdItemCount=0;
	for (k=0; k<thirdArray.length; k+=NUM_ELEM_PER_ITEM) {
		thirdItemCount++;
		thirdItemName = "item" + thirdCounter + "_" + thirdItemCount;
		thirdItem = makeElement(thirdItemName,thirdMenu);

		if (thirdItemCount >1)
			thirdItem.prevItem = eval("item" + thirdCounter + "_" +(thirdItemCount-1));

		thirdItem.setup = itemSetup;
		thirdItem.setup(k,thirdArray);

	}

	thirdMenu.setup(true,thirdItem,secondMenu,secondItem);
}

function linkIt() {
	location.href = this.linkText;
}

function showIt(on) {
	if (NS4) {this.visibility = (on) ? "show" : "hide"}
		else {this.style.visibility = (on) ? "visible" : "hidden"}
}

function keepInWindow() {
	scrBars = 20;

	if (NS4) {
		winRight = (window.pageXOffset + window.innerWidth) - scrBars;
		rightPos = this.left + menuWidth;
   
		if (rightPos > winRight) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.left;
				newLeft = ((parentLeft-menuWidth) + childOverlap);
				this.left = newLeft;
			}
			else {
				dif = rightPos - winRight;
				this.left -= dif;
			}
		}

		winBot = (window.pageYOffset + window.innerHeight) - scrBars;
		botPos = this.top + this.fullHeight;

		if (botPos > winBot) {
			dif = botPos - winBot;
			this.top -= dif;
		}
	}
	else {
    	winRight = (document.body.scrollLeft + document.body.clientWidth) - scrBars;
		rightPos = this.style.pixelLeft + menuWidth;
	
		if (rightPos > winRight) {
			if (this.hasParent) {
				parentLeft = this.parentMenu.style.pixelLeft;
				newLeft = ((parentLeft - menuWidth) + childOverlap);
				this.style.pixelLeft = newLeft;
			}
			else {
				dif = rightPos - winRight;
				this.style.pixelLeft -= dif;
			}
		}

		winBot = (document.body.scrollTop + document.body.clientHeight) - scrBars;
		botPos = this.style.pixelTop + this.fullHeight;

		if (botPos > winBot) {
			dif = botPos - winBot;
			this.style.pixelTop -= dif;
		}
	}
}

function popUp(menuName,e,offsetx,offsety){

	if (!areCreated) return;
	hideAll();

 	currentMenu = eval(menuName);
	xPos = (NS4) ? e.pageX : event.x;
	yPos = (NS4) ? e.pageY : event.y;
//    alert (xPos+" "+yPos);
	currentMenu.moveTo(xPos+offsetx,yPos+offsety);
	currentMenu.keepInWindow()
	currentMenu.isOn = true;
	currentMenu.showIt(true);
    currentMenu.focus();
}

function popDown(menuName, menuID){ 

	if (!areCreated) return;
	whichEl = eval(menuName);
 	whichEl.isOn = false;
 	whichEl.hideTop();
	// Riposiziona il colore della voce di menu appena selezionata
	document.getElementById(menuID).style.color = menuElemCol;
}

function menuOver() {
	this.isOn = true;
	isOverMenu = true;
	currentMenu = this;
	if (this.hideTimer) clearTimeout(this.hideTimer);
}

function menuOut() {
	if (IE4 && event.srcElement.contains(event.toElement)) return;
	this.isOn = false;
	isOverMenu = false;
	if (IE4) allTimer = setTimeout("currentMenu.hideTree()",10); 
}

function itemOver(){
	if (IE4 && event.srcElement.tagName == "IMG") return;

	if (NS4) {
		this.bgColor = overCol;
	}
	else {
		this.style.backgroundColor = overCol;
		this.style.color = overFnt;
	}
	
	if (this.container.hasChildVisible) {
		this.container.hideChildren(this);
	}            

	if(this.hasMore) {
		if (NS4) {
			this.childX = this.container.left + (menuWidth - childOverlap);
			this.childY = this.pageY + childOffset;
		}
		else {
			this.childX = this.container.style.pixelLeft + (menuWidth - childOverlap);
			this.childY = this.style.pixelTop + this.container.style.pixelTop + childOffset;
		}

		this.child.moveTo(this.childX,this.childY);
		this.child.keepInWindow();
		this.container.hasChildVisible = true;
		this.container.visibleChild = this.child;
		this.child.showIt(true);
	}
}


function itemOut() {
    if (IE4 && (event.srcElement.contains(event.toElement)
     || (event.fromElement.tagName=="IMG" && event.toElement.contains(event.fromElement))))
        return;

	if (NS4) {
		this.bgColor = backCol;
		if (!isOverMenu) {
			allTimer = setTimeout("currentMenu.hideTree()",10);
		}
	}
	else {
		this.style.backgroundColor = backCol;
		this.style.color = fntCol;
	}
}

function hideAll() {
	for(i=1; i<topCount; i++) {
		temp = eval("elMenu" + i);
		temp.isOn = false;
		if (temp.hasChildVisible) temp.hideChildren();
		temp.showIt(false);
	}	
}

function hideTree() { 
	allTimer = null;
	if (isOverMenu) return;
	if (this.hasChildVisible) {
		this.hideChildren();
	}
	this.hideParents();
}

function hideChildren(item) {
	if (this.visibleChild.hasChildVisible) {
		this.visibleChild.visibleChild.showIt(false);
		this.visibleChild.hasChildVisible = false;
	}

	if (!this.isOn || !item.hasMore || this.visibleChild != this.child) {
		this.visibleChild.showIt(false);
		this.hasChildVisible = false;
	}
}

function hideParents() {     

	if (this.hasParent) {
		this.showIt(false);
		if (this.parentMenu.hasParent) {
			this.parentMenu.isOn = false;		
			this.parentMenu.showIt(false);
			this.parentMenu.parentMenu.isOn = false;
			whichEl = this.parentMenu.parentMenu
		}
		else {
			this.parentMenu.isOn = false;
			whichEl = this.parentMenu;
		}
	}
	else {
		whichEl = this;
	}

	whichEl.hideTop();
}

function hideTop() {
	whichEl = this;
	this.hideTimer = setTimeout("whichEl.hideSelf()",mSecsVis);
}

function hideSelf() {
	this.hideTimer = null;
	if (!this.isOn && !isOverMenu) { 
		this.showIt(false);
	}
}

function cancelSelect(){return false}

function moveTo(xPos,yPos) {
	this.style.pixelLeft = xPos;
	this.style.pixelTop = yPos;
}

// MENU DI POP-UP USATI SOLO NEL CASO DI DESTp
// Costruttore PopUp menu per DESTp
function makeDestpPopup(tipoDevice, statoDevice, idDevice, statoSistema, livelloSessione, evt) 
{

switch (tipoDevice)
   {
   case DEVICE_LINEA:
   if (statoSistema == ST_SISTEMA_OFFLINE)
      return false;

   if (statoDevice == ST_DEVICE_FUORISERV) 
      {
       idx_ar = 1;
	  }
   else
      {	  
      idx_ar = 2;
	  }
   break;
	  
   	case DEVICE_DIP:
   	if (statoSistema == ST_SISTEMA_OFFLINE)
		idx_ar = 3;
	else if (statoDevice == ST_DEVICE_FUORISERV)
		idx_ar = 6
	else if (statoDevice == ST_DEVICE_GUASTO)
		idx_ar = 5;
	else
		idx_ar = 4;
   break;

   case DEVICE_TERMINALE:
   idx_ar = 7;
   break;

   case DEVICE_FLOPPY:
   	if (statoDevice != ST_DEVICE_OPERATIVO)
		return false;
	idx_ar = 8;
   break;

   case DEVICE_HD:
   	if ((statoDevice != ST_DEVICE_OPERATIVO) || (livelloSessione < 2))
		return false;
	idx_ar = 9;

   break;

   case DEVICE_IOMEGAZIP:
   	if ((statoDevice != ST_DEVICE_OPERATIVO) || (livelloSessione < 1))
		return false;
	idx_ar = 10;
   break;
 
   default:
   // tipo device che non prevedono popUp Menu
   return false;
   }

popUpDestp(idx_ar, idDevice, evt);

return false;
}

function hideAllDestp() {
	for(i=1; i<topCountDestp; i++) {
		temp = eval("elMenuDestp" + i);
		temp.isOn = false;
		if (temp.hasChildVisible) temp.hideChildren();
		temp.showIt(false);
	}	
}

function popUpDestp(idx,param,e){
    menuName = "elMenuDestp" + idx;
	arrayName = eval("arMenuDestp" + idx);
	
	if (!areCreated) return;

	hideAll();
	hideAllDestp();

 	currentMenu = eval(menuName);
	xPos = (NS4) ? e.pageX : event.x;
	yPos = (NS4) ? e.pageY : event.y;
	currentMenu.moveTo(xPos,yPos);

    for (i=1; i<=(arrayName.length/NUM_ELEM_PER_ITEM); i++)
	   {
	   itemMenu = "itemDestp" + idx + "_" + i;
       eval(itemMenu).linkText = eval(itemMenu).baseLinkText + param;
	   }
	    
	currentMenu.keepInWindow()
	currentMenu.isOn = true;
	currentMenu.showIt(true);
}

function makeTopDestp() {

	while(eval("window.arMenuDestp" + topCountDestp)) {
		
		topArray = eval("arMenuDestp" + topCountDestp);
		topName = "elMenuDestp" + topCountDestp;

		topMenu = makeElement(topName);
    	topMenu.setup = menuSetup;

		topItemCount = 0;
		for (i=0; i<topArray.length; i+=NUM_ELEM_PER_ITEM) {
			topItemCount++;
			status = "Creazione menu DESTp in corso, attendere per favore: " + topCountDestp + " / " + topItemCount;
			topItemName = "itemDestp" + topCountDestp + "_" + topItemCount;
			topItem = makeElement(topItemName,topMenu);

			if (topItemCount >1)
				topItem.prevItem = eval("itemDestp" + topCountDestp + "_" + (topItemCount-1));

			topItem.setup = itemSetup;
			topItem.setup(i,topArray);
		}
		
		topMenu.setup(false,topItem);
		topCountDestp++
	}

	status = (topCountDestp-1) + " Creazione del menu DESTp completata"
	areCreated = true;
}

function popDownDestp(tipoDevice, statoDevice, statoSistema, livelloSessione) 
{

switch (tipoDevice)
   {
   case DEVICE_LINEA:
   if (statoSistema == ST_SISTEMA_OFFLINE)
      return false;

   if (statoDevice == ST_DEVICE_FUORISERV) 
      {
       nome_ar = "elMenuDestp1";
	  }
   else
      {	  
      nome_ar = "elMenuDestp2";
	  }
   break;
	  
   	case DEVICE_DIP:
   	if (statoSistema == ST_SISTEMA_OFFLINE)
		nome_ar = "elMenuDestp3";
	else if (statoDevice == ST_DEVICE_FUORISERV)
		nome_ar = "elMenuDestp6";
	else if (statoDevice == ST_DEVICE_GUASTO)
		nome_ar = "elMenuDestp5";
	else
		nome_ar = "elMenuDestp4";

   break;

   case DEVICE_TERMINALE:
   nome_ar = "elMenuDestp7";
   break;

   case DEVICE_FLOPPY:
   	if (statoDevice != ST_DEVICE_OPERATIVO)
		return false;
	nome_ar = "elMenuDestp8";
   break;

   case DEVICE_HD:
   	if ((statoDevice != ST_DEVICE_OPERATIVO) || (livelloSessione < 2))
		return false;
	nome_ar = "elMenuDestp9";

   break;

   case DEVICE_IOMEGAZIP:
   	if ((statoDevice != ST_DEVICE_OPERATIVO) || (livelloSessione < 1))
		return false;
	nome_ar = "elMenuDestp10";
   break;
 
   default:
   // tipo device che non prevedono popUp Menu
   return false;
   }


   popDown(nome_ar);
}

function make_menu_somm(name, gruppo, e)
{
var val_all = Array(4);

	if (event.button != MOUSE_RIGHT_BUTTON) 
		return false;
		
	start=0;
	for(i=0; i<4; i++)
	{
		ofs = name.indexOf("_", start)
		if (ofs == -1)
			ofs = name.length;
		val_all[i] = name.substring(start, ofs);
		start = ofs+1;
	}
	arMenuSomm[0] = val_all[0] + " Grandi Allarmi " + gruppo + " attivi";
	arMenuSomm[8] = val_all[1] + " Piccoli Allarmi " + gruppo + " attivi";
   	arMenuSomm[16] = val_all[3] + " Allarmi acquisiti " + gruppo;
    arMenuSomm[24] = val_all[2] + " Allarmi scomparsi " + gruppo;

    if (areCreated)
       document.getElementById("elMenuSomm").outerHTML = "";

	topArray = eval("arMenuSomm");
	topName = "elMenuSomm";
	topMenu = makeElement(topName);
   	topMenu.setup = menuSetup;

	topItemCount = 0;
	for (i=0; i<topArray.length; i+=NUM_ELEM_PER_ITEM) {
		topItemCount++;
		topItemName = "itemSomm_" + topItemCount;
		topItem = makeElement(topItemName,topMenu);

		if (topItemCount >1)
			topItem.prevItem = eval("itemSomm_" + (topItemCount-1));

		topItem.setup = itemSetup;
		topItem.setup(i,topArray);
	}
		
	topMenu.setup(false,topItem);
	areCreated = true;
	popUp(topName, e);
}
function popDownSomm()
{
	popDown("elMenuSomm");
}