// MenuViewCtrl - Zucchetti Corporate Portal

function Browser() {
  var ua, s, i;
  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape  
  this.version = null;
  ua = navigator.userAgent;
  
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }  
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;   
    this.version = 6.1;        
    return;    
  }  
        
}

var browser = new Browser();

ZtVWeb.MenuViewCtrl=function(form,id,name,x,y,w,h,font,font_size,font_weight,font_color,font_color_smenu,bg_color,overcolor,textovercolor,orizontal,css,className,root_value,separator,separator_vert,sx_btn_image,dx_btn_image,sx_btnover_image,dx_btnover_image,arrow_image,bg_color_smenu,over_color_smenu,textover_color_smenu,btn_imageWidth,z_index,btn_image,btnover_image){  //name--->id
	
  this.arrMenu = new Array();
  this.arrMenuImg = new Array();
  
  //variabili per deselezionare l'ultimo elemento evidenziato evitando il ciclo su tutti gli elementi
  this.deCtrl_sel = "";
	
  // CUSTOM PATH - Path Nodo Corrente  
  if (Empty(RTrim(root_value))) root_value ="";
  this.currentPath=root_value;

  this.form = form;  
  this.id = id;
  this.name = name;  
  this.font=font 
 
  this.bg_color=bg_color
	this.css=css;        
	this.className=className;	
	this.font_color_smenu=font_color_smenu
	this.textover_color_smenu = Lower(textover_color_smenu)
	this.bg_color_smenu = Lower(bg_color_smenu)
	this.over_color_smenu = Lower(over_color_smenu)
	this.arrow_image = arrow_image
	this.btn_image = btn_image
	this.btnover_image = btnover_image
	this.sx_btn_image = sx_btn_image
	this.sx_btnover_image = sx_btnover_image
	this.dx_btn_image = dx_btn_image
	this.dx_btnover_image = dx_btnover_image
	this.separator = separator
	this.separator_vert = separator_vert
	this.root_value=root_value;	
	this.btn_imageWidth=btn_imageWidth;	
	this.z_index=z_index;		
	
  this.orizontal=eval(orizontal)
  this.overcolor=Lower(overcolor)
  this.textovercolor=Lower(textovercolor)
  this.fontweight=font_weight
  this.fontsize=font_size
  this.font_color=font_color  
  this.Ctrl = document.getElementById(this.id);  // Riferimento all'oggetto HTML
  this.setCtrlPos(this.Ctrl,x,y,w,h);
  this.addToForm(this.form,this)
  
  this.querynode=null
  var datasource=null
  //variabili per deselezionare l'ultimo elemento evidenziato evitando il ciclo su tutti gli elementi
  this.level_old = 0;
  this.out_old = 0;
  this.over = false; // attiva e disattiva l'aperture al mouseover dei sottomenu

  this.tree=function(ctrl){

    this.ctrl=ctrl	     
    this.allnodes=new Array()    
    //ctrl.root_value=-1
    this.root=new tree_node(this,ctrl.root_value,ctrl.root_label,ctrl.form,ctrl.name)
    this.root.executequery=false
    this.currentnode=this.root

    this.getRoot=function(){
			return this.root;
		}

    this.render=function(){    
	   
      var html
      if(this.ctrl.orizontal)	
	html=this.root.viewOrizontalRoot(this.ctrl.datasource)	
      else
        html=this.root.viewAsRoot(this.ctrl.datasource)

      var ctrl=document.getElementById(this.ctrl.id)
      ctrl.innerHTML=html
    }
        
  }

  this.Value=function(){
		if (typeof(this.Tree.currentnode)!='undefined'){
			return this.Tree.currentnode.nodeid
    }else
			return 0
  }
  
  
  	//modifica per recuperare il valore del bordo superiore dei sottomenu per allinearli correttamente anche in presenza di bordi
	var top_border=0;
	var exit="false";
	var i=0;
	try{
	if (window.navigator.userAgent.indexOf("MSIE") > 0) {//ie
		while (i<document.styleSheets.length && exit=="false"){	
			k=0;		
			while (k<document.styleSheets[i].rules.length && exit=="false"){
				if (document.styleSheets[i].rules[k].selectorText.indexOf(this.className + " .submenu_border")>0){
					top_border = parseInt(document.styleSheets[i].rules[k].style.borderTopWidth);						
					exit="true";
				}	
				k++;		
			}	
			i++;
		}
	}else{//mozilla
		while (i<document.styleSheets.length && exit=="false"){	
			k=0;		
			while (k<document.styleSheets[i].cssRules.length && exit=="false"){
				if (document.styleSheets[i].cssRules[k].selectorText.indexOf(this.className + " .submenu_border")>0){
					top_border = parseInt(document.styleSheets[i].cssRules[k].style.borderTopWidth);						
					exit="true";
				}	
				k++;		
			}	
			i++;
		}
	}
	}catch(e){}
	
	

  function tree_node(tree,nodeid,descr,form,name,childnum,nodeFather){
	this.tree=tree
	tree.allnodes.push(this)
	this.nIdx=tree.allnodes.length-1
    this.nodeid=nodeid
    this.nodeFather=nodeFather
		this.descr=descr
    this.link=''
    this.target=''
    this.icons=''
		this.hasChildNum=(childnum>0)
		this.children=new Array()
    this.executequery=(childnum==null?true:(childnum==0?false:true))
    this.nodeDiv=''
    this.nodeLines=''

    this.addNode=function(nodeid,descr,childnum){
      var newnode=new tree_node(this.tree,nodeid,descr,form,name,childnum,this.tree.currentnode)
      this.children.push(newnode)
      return newnode;
		}


    this.RemoveChildren=function(){
      var i,node
      for(i=0;i<this.children.length;i++){
        node=this.children[i]
        node.RemoveChildren()
        this.tree.allnodes[node.nIdx]=null
      }
      this.children=new Array()
      this.hasChildNum=false
      this.executequery=true
    }       
    
    this.select=function(index,level,orizontal,nodoId,numChild){ 	
	//quando clicco su una voce  o quando mouseover su una nuova voce chiamo la funzione view per il refresh del menu			
	if((this.tree.ctrl.out_old!=index)||this.tree.ctrl.over){
	   this.view(index,level,this.tree.ctrl.datasource,orizontal,nodoId,numChild)
	   this.tree.ctrl.out_old=index;	   
	}	      
    }        
    
    //chiude il menu all'uscita dai sottomenu
    this.select_out=function(index,event){	    	
	if (browser.isIE) el = window.event.srcElement;
	else el = event.target;		
	//deseleziono se l'elemento non ha id	
	if(Empty(el.id)){
		this.deselect_all();
	}
    }  
    
    //disabilita le voci di menu principali e i sottomenu (click su portlet e su voce menu - mouseout dalle voci senza figli)
    this.deselect_all=function(click_portlet){        
    
	index = this.tree.ctrl.id+'_'+0		
	
	this.view(index,0,this.tree.ctrl.datasource,true,-1,0)  
	
	var id=index.substring(0,index.lastIndexOf("_"))	
        var i=1    
        var deCtrl=document.getElementById(id+"_0") 
	if (!Empty(LRTrim(this.tree.ctrl.btn_image)))
	   document.getElementById(deCtrl.id+"_back").style.background="url("+this.tree.ctrl.btn_image+")"	  	
	else document.getElementById(deCtrl.id+"_back").className="menu_img"
        if (!Empty(LRTrim(this.tree.ctrl.sx_btn_image)))
	   document.getElementById(deCtrl.id+"_sx").style.background="url("+this.tree.ctrl.sx_btn_image+")"		   		
	else  document.getElementById(deCtrl.id+"_sx").className="menu_img_sx"
	if (!Empty(LRTrim(this.tree.ctrl.dx_btn_image)))		   
	   document.getElementById(deCtrl.id+"_dx").style.background="url("+this.tree.ctrl.dx_btn_image+")"
	else  document.getElementById(deCtrl.id+"_dx").className="menu_img_dx"
	//ripristino text
	if (!Empty(LRTrim(this.tree.ctrl.font_color))&&!Empty(LRTrim(this.tree.ctrl.overcolor))){
		deCtrl.style.color=this.tree.ctrl.font_color
		document.getElementById(deCtrl.id+"_descr").style.color=this.tree.ctrl.font_color
		deCtrl.style.backgroundColor=''     
	} else {
		deCtrl.className="menu_font"	
		if (this.tree.ctrl.orizontal) document.getElementById(deCtrl.id+"_descr").className = "menu_font"
	}
	
	//per menu verticale				
	if (!this.tree.ctrl.orizontal && deCtrl.id!=this.deCtrl_sel) {
		document.getElementById(deCtrl.id+"_arr").style.backgroundColor=''				
	}else{
		if (!Empty(document.getElementById(this.deCtrl_sel+"_arr"))) {
			document.getElementById(this.deCtrl_sel+"_arr").className="menu_font"
		}		
	}	
	
    } 
    
    
    this.onMenuClick=function(frame,node_link){

        node_link = unescape(node_link);	
	frame = unescape(frame);			
	if (node_link.indexOf('http://') !=0 && node_link.indexOf('https://') !=0 && !Empty(node_link)){
		node_link='../servlet/'+node_link;
	}
	// Tolgo spazi in fondo		
	node_link = node_link.replace(/\s+$/g,"");	
        var width = frame.substring(frame.indexOf('|')+1,frame.lastIndexOf('|'));
	var height = frame.substr(frame.lastIndexOf('|')+1);
	if (frame.indexOf('dialogwindow') == 0){	
		// Il TARGET mi arriva nel formato dialogwindow|altezza|larghezza			
		if ((width.length > 0) && (height.length > 0))
			window.open(node_link,'','toolbar=no,scrollbars=no,resizable=yes'+','+'width='+width+','+'height='+height);		
		else
			window.open(node_link,'','toolbar=no,scrollbars=no,resizable=yes');		
	}else		
		if (!Empty(node_link))
			window.open(node_link,frame,'toolbar=no,scrollbars=no,resizable=yes');				
		
	// attiva e disattiva l'aperture al mouseover dei sottomenu	
	if (this.tree.ctrl.over==true) { 
		this.tree.ctrl.over=false; 
	}else{ 
		if (Empty(node_link))this.tree.ctrl.over=true; 			
	}	
	//refresh del menu
	this.deselect_all();
	
    }
            
    // Fix per Memory Leak
    if (window.attachEvent) {
    //var clearElementProps = ['data','onmouseover','onmouseout','onmousedown','onmouseup','ondblclick','onclick','onselectstart','oncontextmenu'];
    var clearElementProps = ['data','onmouseover','onmouseout','onmousedown','onmouseup','ondblclick','onclick'];
    window.attachEvent("onunload", function() {
        var el;
        for(var d = document.all.length;d--;){
            el = document.all[d];
            for(var c = clearElementProps.length;c--;){
                el[clearElementProps[c]] = null;
            }
        }
        });
    }

   //x mozilla: convertire da #000000 a rgb(00,00,00)   
   function convert(e){	   
	if (!Empty(e)){
	a='0x'+e.charAt(1)
	es1=eval(a)
	b='0x'+e.charAt(2)
	es2=eval(b)
	r=es1*16+es2	
	a='0x'+e.charAt(3)
	es1=eval(a)
	b='0x'+e.charAt(4)
	es2=eval(b)
	g=eval(es1*16+es2)	
	a='0x'+e.charAt(5)
	es1=eval(a)
	b='0x'+e.charAt(6)
	es2=eval(b)
	b=eval(es1*16+es2)		
	return "rgb("+r+", "+g+", "+b+")" 	
	}
   }
   
    this.view=function(index,level,datasource,orizontal,nodoId,numChild){	 
    
    //deselezione 	        
	if (!Empty(this.deCtrl_sel)) {		    
		var deCtrl=document.getElementById(this.deCtrl_sel);
	} else{		    
		var id=index.substring(0,index.lastIndexOf("_"));		
		var deCtrl=document.getElementById(id+"_0");
	}
      
        if(!Empty(this.tree.ctrl.css)||(deCtrl.style.backgroundColor==this.tree.ctrl.overcolor)||(deCtrl.style.backgroundColor==this.tree.ctrl.over_color_smenu)||(deCtrl.style.backgroundColor==convert(this.tree.ctrl.overcolor))||(deCtrl.style.backgroundColor==convert(this.tree.ctrl.over_color_smenu))){     //mozilla
	  	  
	  //ripristino colori	
	  if (this.level_old==0){
		try{
		if (!Empty(LRTrim(this.tree.ctrl.btn_image)))
		   document.getElementById(deCtrl.id+"_back").style.background="url("+this.tree.ctrl.btn_image+")"
		else document.getElementById(deCtrl.id+"_back").className="menu_img"
		}catch(e){}
		try{
		if (!Empty(LRTrim(this.tree.ctrl.sx_btn_image)))		
		   document.getElementById(deCtrl.id+"_sx").style.background="url("+this.tree.ctrl.sx_btn_image+")"
		else  document.getElementById(deCtrl.id+"_sx").className="menu_img_sx"
		}catch(e){}
		try{
		if (!Empty(LRTrim(this.tree.ctrl.dx_btn_image)))		   
		   document.getElementById(deCtrl.id+"_dx").style.background="url("+this.tree.ctrl.dx_btn_image+")"
		else  document.getElementById(deCtrl.id+"_dx").className="menu_img_dx"
		}catch(e){}		
		//ripristino text
		try{
		if (!Empty(LRTrim(this.tree.ctrl.font_color))&&!Empty(LRTrim(this.tree.ctrl.bg_color))){						
			deCtrl.style.color=this.tree.ctrl.font_color
			document.getElementById(deCtrl.id+"_descr").style.color=this.tree.ctrl.font_color
			deCtrl.style.backgroundColor=''
		}else{
			deCtrl.className="menu_font"
			if (this.tree.ctrl.orizontal) document.getElementById(deCtrl.id+"_descr").className="menu_font"
		}
		}catch(e){}		
		//per menu verticale	
		try{
		if (!this.tree.ctrl.orizontal && deCtrl.id!=this.deCtrl_sel) {
			document.getElementById(deCtrl.id+"_arr").style.backgroundColor=''				
		}else{
			if (!Empty(document.getElementById(this.deCtrl_sel+"_arr"))) {
				document.getElementById(this.deCtrl_sel+"_arr").className="menu_font"
			}		
		}	
		}catch(e){}
	  }else{
		try{
		if (!Empty(LRTrim(this.tree.ctrl.font_color_smenu))&&!Empty(LRTrim(this.tree.ctrl.font_color_smenu))) {
			deCtrl.style.color=this.tree.ctrl.font_color_smenu	 
			deCtrl.style.backgroundColor=''     
		}else deCtrl.className="submenu_font"
		}catch(e){}
	  }
	  
        }
            
	//selezione
      this.deCtrl_sel=index	
      this.level_old=level	      
      var Selection=document.getElementById(index)	      
      var SelectionBack=document.getElementById(index+"_back")
      var SelectionSx=document.getElementById(index+"_sx")
      var SelectionDx=document.getElementById(index+"_dx")
      var SelectionArr=document.getElementById(index+"_arr")  
      var SelectionDescr=document.getElementById(index+"_descr")  
      var SelectionSmOut=document.getElementById(index+"_smout")  
                 
      if (level==0){	      
	      if (!Empty(this.tree.ctrl.overcolor) && !Empty(this.tree.ctrl.textovercolor)){
		      Selection.style.backgroundColor=this.tree.ctrl.overcolor
	              Selection.style.color=this.tree.ctrl.textovercolor
		      SelectionDescr.style.color=this.tree.ctrl.textovercolor		      
	      }else{
		      Selection.className="menu_font_over";
		      if (this.tree.ctrl.orizontal) SelectionDescr.className="menu_font_over";
		}
	      
	      //per menu verticale
	      if (!this.tree.ctrl.orizontal) {
		      if (!Empty(this.tree.ctrl.overcolor) && !Empty(this.tree.ctrl.textovercolor)){
				SelectionArr.style.backgroundColor=this.tree.ctrl.overcolor
				SelectionArr.style.color=this.tree.ctrl.textover_color
		      }else SelectionArr.className="menu_font_over" 
	      }	      
	      
	      if (!Empty(LRTrim(this.tree.ctrl.btnover_image)))
		   SelectionBack.style.background="url("+this.tree.ctrl.btn_image+")"	  	
	      else SelectionBack.className="menu_img_over"	
	      
	      if (LRTrim(this.tree.ctrl.sx_btnover_image)!="")
			SelectionSx.style.background="url("+this.tree.ctrl.sx_btnover_image+")"		
	      else  SelectionSx.className="menu_img_sx_over"
	      if (LRTrim(this.tree.ctrl.dx_btnover_image)!="")				
			SelectionDx.style.background="url("+this.tree.ctrl.dx_btnover_image+")"		
	      else SelectionDx.className="menu_img_dx_over"
      } else{  	     
	       if (!Empty(this.tree.ctrl.over_color_smenu) && !Empty(this.tree.ctrl.textover_color_smenu)){		       
		      Selection.style.backgroundColor=this.tree.ctrl.over_color_smenu
		      Selection.style.color=this.tree.ctrl.textover_color_smenu
	       }else {	       	
		       Selection.className="submenu_font_over"    
	       }
      }      

      var top=Selection.offsetTop
      var left=Selection.offsetLeft
      var ctrl=document.getElementById(this.tree.ctrl.id+"-"+level)
      level++

      var html=""
           
     
//sottomenu ************************************


if (numChild>0&&this.tree.ctrl.over){     
      if(orizontal){	      
        if(left>0) 
		html+="<table border=0 cellspacing=0 cellpadding=0><tr><td><div style='width:"+left+";z-index:"+this.tree.ctrl.z_index+"'></div><td>"	
      }else{	
        if(top>(0+top_border)) 		
		html+="<div style='height:"+top+";z-index:"+this.tree.ctrl.z_index+"'></div>"
      }                 
      //window.document.getElementById(this.tree.ctrl.form.formid).style.width
      
      html+="<table border=0 cellspacing=0 cellpadding=0><tr><td valign=top>"     
     
      html+="<table border=0 class='submenu_border' cellpadding=3 cellspacing=0>"

      i=0;            
      while (this.tree.ctrl.arrMenu[i][9]!=nodoId){ i++; }      
      while (this.tree.ctrl.arrMenu[i][9]==nodoId){
	
	//controllo frame e node_link      	
	var portletname=this.tree.ctrl.form.portletname
	var node_link=this.tree.ctrl.arrMenu[i][4]
        if (Substr(node_link,1,9) == "function:" ){
          node_link = Strtran(node_link,"function:","");
          node_link = "javascript:ZtVWeb.getPortlet(\"" +portletname + "\")." +node_link
        }
        while(node_link.indexOf('|')>-1)
          node_link=node_link.replace('|',',')
	  
	var frame=URLenc(Trim(this.tree.ctrl.arrMenu[i][5]))
	if(Empty(Trim(frame))) frame="_self"
	
	//VISIBILITA' CONDIZIONATA VOCI MENU'		
	var visualizer;
	try{ var visualizer = eval(this.tree.ctrl.form.formid+"."+name+"_Validate('"+URLenc(Trim(this.tree.ctrl.arrMenu[i][7]))+"','"+URLenc(Trim(this.tree.ctrl.arrMenu[i][8]))+"')");
	}catch(e){ var visualizer = true; }
	//visualizer = true se devo far vedere la voce di menu
	//**********************************************************************************************
	
        var clickfunc=this.tree.ctrl.form.formid+"."+name+".Tree.root.onMenuClick('"+this.tree.ctrl.arrMenu[i][5]+"','"+this.tree.ctrl.arrMenu[i][4]+"')"        
	var selectfunc=this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select(\""+index+"_"+i+"\","+level+",false,"+this.tree.ctrl.arrMenu[i][1]+","+this.tree.ctrl.arrMenu[i][3]+")"	
	var selectout = this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select_out(\""+index+"\",event)"
	
	if (visualizer){
	
	if (!Empty(LRTrim(this.tree.ctrl.font)) && this.tree.ctrl.fontsize>0 && !Empty(LRTrim(this.tree.ctrl.fontweight)) && !Empty(LRTrim(this.tree.ctrl.font_color_smenu)) && !Empty(LRTrim(this.tree.ctrl.bg_color_smenu)))	
		html+="<tr id='"+index+"_"+i+"' bgcolor='"+this.tree.ctrl.bg_color_smenu+"' onclick="+clickfunc+" style='font-family:"+this.tree.ctrl.font+";font-size:"+this.tree.ctrl.fontsize+";font-weight:"+this.tree.ctrl.fontweight+";color:"+this.tree.ctrl.font_color_smenu+";cursor:pointer'"
	else		
		html+="<tr id='"+index+"_"+i+"' onclick="+clickfunc+" class='submenu_font'"
	
	//controllo che la voce precedente non sia già un separatore	
        if(this.tree.ctrl.arrMenu[i][2].indexOf("-----")==0){		
		
		if (i < this.tree.ctrl.arrMenu.length-1){	
			if (this.tree.ctrl.arrMenu[i+1][2].indexOf("-----")!=0) {
			
				if (!Empty(RTrim(this.tree.ctrl.css))) html+="><td colspan=3  id='"+index+"_"+i+"_descr'><table cellpadding=0 cellspacing=0 class='submenu_sep_vert'><tr><td></td></tr></table>"		  
				else html+="><td colspan=3  id='"+index+"_"+i+"_descr'><table cellpadding=0 cellspacing=0 class='submenu_sep_vert' style='height:1;width:100%;border-bottom:1px solid'><tr><td></td></tr></table>"		  		
			
			}
		}		
		
        }else{
          html+=" onmouseover='"+selectfunc+"'>"	
		
	  if (!Empty(this.tree.ctrl.arrMenu[i][6]))
		  html+="<td style='padding-top:0px;padding-bottom:0px'><img id='"+index+"_"+i+"_icons' src='.."+this.tree.ctrl.arrMenu[i][6]+"'></td>"
	  else
		  html+="<td style='padding-top:0px;padding-bottom:0px' id='"+index+"_"+i+"_icons'></td>"
	  
	  html+="<td id='"+index+"_"+i+"_descr'>"+this.tree.ctrl.arrMenu[i][2]
          html+="</td>"
		  
	  //arrow image
          if(this.tree.ctrl.arrMenu[i][3]>0){
		if (!Empty(RTrim(this.tree.ctrl.arrow_image)))  html+="<td><img id='"+index+"_"+i+"_img' src='"+this.tree.ctrl.arrow_image+"'></img></td>"
		else 
		   if (!Empty(RTrim(this.tree.ctrl.css))) html+="<td><div id='"+index+"_"+i+"_img' class='menu_img_arrow'></div></td>"		   		   
          }else{            
		html+="<td></td>"	    
          }

          html+="</tr>"
	  if (i < this.tree.ctrl.arrMenu.length-1){	
		if (this.tree.ctrl.arrMenu[i][9] != this.tree.ctrl.arrMenu[i+1][9]) {
			if (!Empty(LRTrim(this.tree.ctrl.font)) && this.tree.ctrl.fontsize>0 && !Empty(LRTrim(this.tree.ctrl.fontweight)) && !Empty(LRTrim(this.tree.ctrl.font_color_smenu)) && !Empty(LRTrim(this.tree.ctrl.bg_color_smenu)))
				html+="<tr id='"+index+"_"+i+"_smout' height=1px onmouseout='"+selectout+"' bgcolor='"+this.tree.ctrl.bg_color_smenu+"'><td class='icons_smenu'></td><td colspan=2></td></tr>"
			else
				html+="<tr id='"+index+"_"+i+"_smout' height=1px onmouseout='"+selectout+"' class='submenu_font'><td class='icons_smenu'></td><td colspan=2></td></tr>"
		}
	  }
	  else{
		try{
			html+="<tr id='"+index+"_"+i+"_smout' height=1px onmouseout='"+selectout+"'><td class='icons_smenu'></td><td colspan=2></td></tr>"
		}catch(e){}
	  }
	  
         }//end no sep
	 
	}//end visualizer
	i++;
	if (i==this.tree.ctrl.arrMenu.length){break;}	
      }//end while
      
      html+="</table></td><td valign=top id='"+this.tree.ctrl.id+"-"+level+"'></td></tr></table>"
      if((orizontal)&&(top>0))
        html+="</tr></table>"
      ctrl.innerHTML=html
	
	//per evitare la riga vuota che mi copre i link dei portlet sottostanti (es. documenti)
	}else{
		
		ctrl.innerHTML=""
	}	

}

	
//****************************menu principale verticale ************************************

    this.viewAsRoot=function(datasource){

      document.getElementById(id).style.zIndex=this.tree.ctrl.z_index  
      this.tree.ctrl.form.Ctrl.style.zIndex=this.tree.ctrl.z_index
       
      var html="<table width="+(w)+" border=0 cellspacing=0 cellpadding=0><tr  width="+(w)+"><td valign=top  width="+(w)+">"
      
      html+="<table width="+(w)+" border=0  class='menu_border' cellpadding=0 cellspacing=0>"     
      
	var j=0;      
	// seek root
	 while (j<this.tree.ctrl.arrMenu.length && this.tree.ctrl.arrMenu[j][9]!=this.tree.ctrl.root_value) j++;
	// loop
	i=0;
	 while (j<this.tree.ctrl.arrMenu.length && this.tree.ctrl.arrMenu[j][9]==this.tree.ctrl.root_value){
	 
	//controllo frame e node_link      	
	var portletname=this.tree.ctrl.form.portletname
	var node_link=this.tree.ctrl.arrMenu[j][4]
        if (Substr(node_link,1,9) == "function:" ){
          node_link = Strtran(node_link,"function:","");
          node_link = "javascript:ZtVWeb.getPortlet(\"" +portletname + "\")." +node_link
        }
        while(node_link.indexOf('|')>-1)
          node_link=node_link.replace('|',',')
	  
	var frame=URLenc(Trim(this.tree.ctrl.arrMenu[j][5]))
	if(Empty(Trim(frame))) frame="_self"

	//VISIBILITA' CONDIZIONATA VOCI MENU'		
	var visualizer;
	try{ var visualizer = eval(this.tree.ctrl.form.formid+"."+name+"_Validate('"+URLenc(Trim(this.tree.ctrl.arrMenu[j][7]))+"','"+URLenc(Trim(this.tree.ctrl.arrMenu[j][8]))+"')");
	}catch(e){ var visualizer = true; }
	//visualizer = true se devo far vedere la voce di menu
	//**********************************************************************************************	
	
        var clickfunc=this.tree.ctrl.form.formid+"."+name+".Tree.root.onMenuClick('"+frame+"','"+node_link+"')"
	var selectoutfunc=this.tree.ctrl.form.formid+"."+name+".Tree.root.onMenuOut(\""+(this.tree.ctrl.arrMenu[j][3]>0)+"\")"
        var selectfunc=this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select(\""+this.tree.ctrl.id+"_"+i+"\",0,false,"+this.tree.ctrl.arrMenu[j][1]+","+this.tree.ctrl.arrMenu[j][3]+")"			
	var selectfunc2=this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select(\""+this.tree.ctrl.id+"_"+i+"\",0,false,"+this.tree.ctrl.arrMenu[j][1]+","+this.tree.ctrl.arrMenu[j][3]+")"	
     
	if (visualizer){
	
        //riga di tre pixel per il mouseout
        if (i == 0) html+="<tr><td colspan='4' height=3 onmouseout='"+selectoutfunc+"'></td></tr>"	
		
	if (!Empty(LRTrim(this.tree.ctrl.btn_image)))
		html+="<tr id='"+this.tree.ctrl.id+"_"+i+"_back' height="+(h)+" onclick="+clickfunc+";"+selectfunc2+" style='background:transparent;background-image:url("+this.tree.ctrl.btn_image+")'>"
	else
		html+="<tr id='"+this.tree.ctrl.id+"_"+i+"_back' height="+(h)+"  onclick="+clickfunc+";"+selectfunc2+" class='menu_img'>"
	
	//img sinistra	
	if (!Empty(LRTrim(this.tree.ctrl.sx_btn_image)))		
		html+="<td cellpadding=0 cellspacing=0 width="+this.tree.ctrl.btn_imageWidth+" id='"+this.tree.ctrl.id+"_"+i+"_sx' align='right' style='padding-left:0px;padding-right:0px;border:0px;background-image:url("+this.tree.ctrl.sx_btn_image+")'></td>"
	else 
		if (!Empty(LRTrim(this.tree.ctrl.css)))			
			html+="<td cellpadding=0 cellspacing=0 id='"+this.tree.ctrl.id+"_"+i+"_sx' align='right' class='menu_img_sx'></td>"			
		else
			html+="<span id='"+this.tree.ctrl.id+"_"+i+"_sx' width='0px'></span>"
	
        if (!Empty(LRTrim(this.tree.ctrl.font)) && this.tree.ctrl.fontsize>0 && !Empty(LRTrim(this.tree.ctrl.fontweight)) && !Empty(LRTrim(this.tree.ctrl.font_color)) && !Empty(LRTrim(this.tree.ctrl.bg_color))){
		html+="<td bgcolor='"+this.tree.ctrl.bg_color+"' id='"+this.tree.ctrl.id+"_"+i+"' style='font-family:"+this.tree.ctrl.font+";font-size:"+this.tree.ctrl.fontsize+";font-weight:"+this.tree.ctrl.fontweight+";color:"+this.tree.ctrl.font_color+"' onmouseover='"+selectfunc+"'>"
		html+="<span style='padding-left:6px;padding-right:6px'>"+this.tree.ctrl.arrMenu[j][2]+"</span></td>"			
		html+="<td bgcolor='"+this.tree.ctrl.bg_color+"' id='"+this.tree.ctrl.id+"_"+i+"_arr' onmouseover='"+selectfunc+"'>"			
	}else{
		html+="<td id='"+this.tree.ctrl.id+"_"+i+"' class='menu_font' onmouseover='"+selectfunc+"'>"
		html+="<span style='padding-left:6px;padding-right:6px'>"+this.tree.ctrl.arrMenu[j][2]+"</span>"+"</td>"		
		html+="<td id='"+this.tree.ctrl.id+"_"+i+"_arr' class='menu_font' onmouseover='"+selectfunc+"'>"
	}
	
	if(this.tree.ctrl.arrMenu[j][3]>0){
	    if (!Empty(RTrim(this.tree.ctrl.arrow_image)))  html+="<img src='" +this.tree.ctrl.arrow_image+ "' style='border:none'>"	    
	    else 
		   if (!Empty(RTrim(this.tree.ctrl.css))) html+="<table cellpadding=0 cellspacing=0><tr><td class='menu_img_arrow'></td></tr></table>"
	}
	html+="</td>"	  
	
	//img destra
	if (LRTrim(this.tree.ctrl.dx_btn_image)!="")		
		html+="<td onmouseout='"+selectoutfunc+"' cellpadding=0 cellspacing=0 width="+this.tree.ctrl.btn_imageWidth+" id='"+this.tree.ctrl.id+"_"+i+"_dx' align='left' style='border:0px;background-image:url("+this.tree.ctrl.dx_btn_image+")'></td>"
	else 
		if (LRTrim(this.tree.ctrl.css)!="")		
			html+="<td onmouseout='"+selectoutfunc+"' id='"+this.tree.ctrl.id+"_"+i+"_dx' align='left' width='1px' class='menu_img_dx'></td>"
		else
			html+="<span id='"+this.tree.ctrl.id+"_"+i+"_dx' width='0px'></span>"			
	  
	html+="</tr>"	
	//separatore	
	if (j< this.tree.ctrl.arrMenu.length-1){	
		if (this.tree.ctrl.arrMenu[j][9] == this.tree.ctrl.arrMenu[j+1][9]) {
			if (!Empty(LRTrim(this.tree.ctrl.separator_vert)))
				html+="<tr><td colspan='4' align='center'><img src='"+this.tree.ctrl.separator_vert+"'></td></tr>"
			else
				if (!Empty(LRTrim(this.tree.ctrl.css)))
					html+="<tr><td colspan='4' class='menu_sep_vert'></td></tr>"			
		//riga di tre pixel per il mouseout
		}else html+="<tr><td colspan='4' height=3 onmouseout='"+selectoutfunc+"'></td></tr>"			
	}else html+="<tr><td colspan='4' height=3 onmouseout='"+selectoutfunc+"'></td></tr>"
	
	i++;
	}//end visualizer	
	j++;
	}//end while
      html+="</table></td><td valign=top id='"+this.tree.ctrl.id+"-0'></td></tr></table>"

      //ridimensionamento portlet che contiene il menu
      // distanza da top del portlet + (N° voci * altezza voce) + distanza da top del portlet + N° voci (per separatore orizzontale di 1px)                       
      if ((y + (i * h) + i) > this.tree.ctrl.form.Ctrl.style.height.replace("px","")){
	      this.tree.ctrl.form.Ctrl.style.height = y + (i * h) + i + 4;
      }  
	
      return html;
    }
  
//****************************menu principale orizzontale ************************************

	//click sul portlet che contiene il menu
	this.tree.ctrl.form.Ctrl.onmousedown=function(event){
		if (browser.isIE) el = "window.event.srcElement";
		else el = "event.target";		
		if(eval(el+".id.indexOf('"+name+"')")==-1){
			// disattivo l'aperture al mouseover dei sottomenu	
			tree.ctrl.over=false;		
			eval(tree.ctrl.form.formid+"."+name+".Tree.root.deselect_all('true')")
		}				
	}	
	
	//mouseout dalle voci di menu che non hanno figli		
	this.onMenuOut=function(hasChild,idx){		
		if ((hasChild=='false')||(!this.tree.ctrl.over)){
			this.deselect_all();
			//per il mouseout mouseover sulla stessa voce
			this.tree.ctrl.out_old=0;
		}
	}

    this.viewOrizontalRoot=function(datasource){	    
	
	document.getElementById(id).style.zIndex=this.tree.ctrl.z_index  
	this.tree.ctrl.form.Ctrl.style.zIndex=this.tree.ctrl.z_index
	 
        var html="<table border=0 cellspacing=0 cellpadding=0 marginheight=0 marginwidth=0 topmargin=0 leftmargin=0><tr><td>"
      	    
	html+="<table border=0 marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 class='menu_border' cellpadding=0 cellspacing=0><tr>"	   
      
	var j=0;      
	// seek root
	 while (j<this.tree.ctrl.arrMenu.length && this.tree.ctrl.arrMenu[j][9]!=this.tree.ctrl.root_value) j++;
	// loop
	var i=0;
	while (j<this.tree.ctrl.arrMenu.length && this.tree.ctrl.arrMenu[j][9]==this.tree.ctrl.root_value){

	//controllo frame e link
        var portletname=this.tree.ctrl.form.portletname
	var node_link=this.tree.ctrl.arrMenu[j][4]
        if (Substr(node_link,1,9) == "function:" ){
          node_link = Strtran(node_link,"function:","");
          node_link = "javascript:ZtVWeb.getPortlet(\"" +portletname + "\")." +node_link
        }
        while(node_link.indexOf('|')>-1)
          node_link=node_link.replace('|',',')
	
	var frame=URLenc(Trim(this.tree.ctrl.arrMenu[j][5]))
	if(Empty(Trim(frame))) frame="_self"
	
	//VISIBILITA' CONDIZIONATA VOCI MENU'	
	var visualizer;
	try{
		var visualizer = eval(this.tree.ctrl.form.formid+"."+name+"_Validate('"+URLenc(Trim(this.tree.ctrl.arrMenu[j][7]))+"','"+URLenc(Trim(this.tree.ctrl.arrMenu[j][8]))+"')");
	}catch(e){
		var visualizer = true;
	}
	//visualizer = true se devo far vedere la voce di menu
	//**********************************************************************************************
	  
        var clickfunc=this.tree.ctrl.form.formid+"."+name+".Tree.root.onMenuClick('"+frame+"','"+node_link+"')"
	var selectoutfunc=this.tree.ctrl.form.formid+"."+name+".Tree.root.onMenuOut(\""+(this.tree.ctrl.arrMenu[j][3]>0)+"\",\""+this.tree.ctrl.arrMenu[j][2]+"_"+i+"\")"	
        var selectfunc=this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select(\""+this.tree.ctrl.id+"_"+i+"\",0,true,"+this.tree.ctrl.arrMenu[j][1]+","+this.tree.ctrl.arrMenu[j][3]+")"	
	var selectfunc_click=this.tree.ctrl.form.formid+"."+this.tree.ctrl.name+".Tree.root.select(\""+this.tree.ctrl.id+"_"+i+"\",0,true,"+this.tree.ctrl.arrMenu[j][1]+","+this.tree.ctrl.arrMenu[j][3]+")"	
	
	if (visualizer){
		
	//img sinistra	
	if (!Empty(LRTrim(this.tree.ctrl.sx_btn_image)))		
		html+="<td height="+(h)+" width="+this.tree.ctrl.btn_imageWidth+" id='"+this.tree.ctrl.id+"_"+i+"_sx' align='bottom' style='border:0px;background-image:url("+this.tree.ctrl.sx_btn_image+")'></td>"
	else html+="<td height="+(h)+" id='"+this.tree.ctrl.id+"_"+i+"_sx' align='bottom' class='menu_img_sx'></td>"
	
	if (!Empty(LRTrim(this.tree.ctrl.font)) && this.tree.ctrl.fontsize>0 && !Empty(LRTrim(this.tree.ctrl.fontweight)) && !Empty(LRTrim(this.tree.ctrl.font_color)) && !Empty(LRTrim(this.tree.ctrl.bg_color)))
		html+="<td height="+(h)+" id='"+this.tree.ctrl.id+"_"+i+"' bgcolor='"+this.tree.ctrl.bg_color+"' onclick="+clickfunc+";"+selectfunc_click+" style='font-family:"+this.tree.ctrl.font+";font-size:"+this.tree.ctrl.fontsize+";font-weight:"+this.tree.ctrl.fontweight+";color:"+this.tree.ctrl.font_color+";cursor:hand'>"	
	else html+="<td height="+(h)+" id='"+this.tree.ctrl.id+"_"+i+"' onclick="+clickfunc+";"+selectfunc_click+ " class='menu_font'>" 
	
	if (!Empty(LRTrim(this.tree.ctrl.btn_image)))
		html+="<table id='"+this.tree.ctrl.id+"_"+i+"_back' cellspacing=0 cellpadding=0 margin=0 height="+(h)+" style='height:"+(h)+";background:transparent;background-image:url("+this.tree.ctrl.btn_image+");padding-left:3px;padding-right:3px'>"		
	else
		html+="<table id='"+this.tree.ctrl.id+"_"+i+"_back' cellspacing=0 cellpadding=0 margin=0 height="+(h)+" class='menu_img'><tr>"
		
	if (!Empty(LRTrim(this.tree.ctrl.font)) && this.tree.ctrl.fontsize>0 && !Empty(LRTrim(this.tree.ctrl.fontweight)) && !Empty(LRTrim(this.tree.ctrl.font_color)) && !Empty(LRTrim(this.tree.ctrl.bg_color)))
		html+="<td  onmouseover='"+selectfunc+"' onmouseout='"+selectoutfunc+"' id='"+this.tree.ctrl.id+"_"+i+"_descr' valign=middle style='font-family:"+this.tree.ctrl.font+";font-size:"+this.tree.ctrl.fontsize+";font-weight:"+this.tree.ctrl.fontweight+";color:"+this.tree.ctrl.font_color+";cursor:hand;background:transparent'>"+this.tree.ctrl.arrMenu[i][2]+"</td></tr></table>"
	else
		html+="<td  onmouseover='"+selectfunc+"' onmouseout='"+selectoutfunc+"' id='"+this.tree.ctrl.id+"_"+i+"_descr' valign=middle class='menu_font' style='background:transparent'>"+this.tree.ctrl.arrMenu[j][2]+"</td></tr></table>"
	
	//html+="</div></td>"
	html+="</td>"
	
	//img destra	
	if (!Empty(LRTrim(this.tree.ctrl.dx_btn_image)))
		html+="<td height="+(h)+" width="+this.tree.ctrl.btn_imageWidth+" id='"+this.tree.ctrl.id+"_"+i+"_dx' align='bottom' style='border:0px;background-image:url("+this.tree.ctrl.dx_btn_image+")'></td>"		
	else html+="<td height="+(h)+" id='"+this.tree.ctrl.id+"_"+i+"_dx' align='bottom' class='menu_img_dx'></td>"
		
	//controllo ultima posizione per separatore
	if (j < this.tree.ctrl.arrMenu.length-1){		
		if (this.tree.ctrl.arrMenu[j][9] == this.tree.ctrl.arrMenu[j+1][9]){
			if (LRTrim(this.tree.ctrl.separator)!="") 
				html+="<td height="+(h)+"><img src='"+this.tree.ctrl.separator+"'></img></td>"
			else
				if (LRTrim(this.tree.ctrl.css)!="")
					html+="<td height="+(h)+" class='menu_sep'></td>"
		}
	}
	  i++;
      }//end visualizer
      j++;
      }//end ciclo
      
      html+="</tr></table></td></tr><tr><td id='"+this.tree.ctrl.id+"-0'></td></tr></table>"      
      return html;     
    }

  }

	this.Tree=new this.tree(this)    
	this.FillData=function(data){
		this.datasource=data		
		var node,NodeID,NodeName,NodeChild,NodeSeq,NodeParent,Icons,Expr,ExprNot,Frame,NodeLink
		var root=(this.Tree.querynode!=null?this.Tree.querynode:this.Tree.getRoot())		
		var idxFieldNodeID=this.datasource.getFldIdx(this.field_NodeID);
		var idxFieldNodeName=this.datasource.getFldIdx(this.field_NodeDescr);
		var idxFieldChildNum=this.datasource.getFldIdx(this.field_ChildCount);
		var idxFieldNodeSeq=this.datasource.getFldIdx(this.field_NodeSeq);
		var idxFieldNodeParent=this.datasource.getFldIdx(this.field_NodeParent);
		var idxFieldIcons=this.datasource.getFldIdx(this.field_Icons);
		var idxFieldExpr=this.datasource.getFldIdx(this.field_Expr);
		var idxFieldExprNot=this.datasource.getFldIdx(this.field_ExprNot);
		var idxFieldFrame=this.datasource.getFldIdx(this.field_Frame);
		var idxFieldNodeLink=this.datasource.getFldIdx(this.field_NodeLink);
		
		    this.arrMenu.length = this.datasource.nRecs;
		    //this.arrMenuImg.length = this.datasource.nRecs;
		    for (k=0; k<this.datasource.nRecs; k++) {
			this.arrMenu[k] = new Array(10);
			//per precaricamento immagini
			//this.arrMenuImg[k]=new Image();
		    }
			
		    for (i=0; i<this.datasource.nRecs; i++) {			
			NodeID=RTrim(this.datasource.Data[i][idxFieldNodeID])
			NodeName=RTrim(this.datasource.Data[i][idxFieldNodeName])
			if (idxFieldChildNum!=-1)
				NodeChild=RTrim(this.datasource.Data[i][idxFieldChildNum])
			NodeSeq=RTrim(this.datasource.Data[i][idxFieldNodeSeq])
			NodeParent=RTrim(this.datasource.Data[i][idxFieldNodeParent])
			Icons=RTrim(this.datasource.Data[i][idxFieldIcons])
			Expr=RTrim(this.datasource.Data[i][idxFieldExpr])
			ExprNot=RTrim(this.datasource.Data[i][idxFieldExprNot])
			Frame=RTrim(this.datasource.Data[i][idxFieldFrame])
			NodeLink=RTrim(this.datasource.Data[i][idxFieldNodeLink])
			node=root.addNode(NodeID,NodeName,NodeChild)
			this.arrMenu[i][0] = Trim(NodeSeq)
			this.arrMenu[i][1] = Trim(NodeID)
			this.arrMenu[i][2] = Trim(NodeName)
			this.arrMenu[i][3] = Trim(NodeChild)
			this.arrMenu[i][4] = Trim(NodeLink)			
			this.arrMenu[i][5] = Trim(Frame)
			this.arrMenu[i][6] = Trim(Icons)
			//per precaricamento immagini
			//this.arrMenuImg[i] = Trim(Icons)
			this.arrMenu[i][7] = Trim(Expr)
			this.arrMenu[i][8] = Trim(ExprNot)
			this.arrMenu[i][9] = Trim(NodeParent)			
			
                    }
    
		if(this.datasource.nRecs>0 && this.Tree.querynode==null){			
		      this.Tree.render()
		}
	}

	this.setRowsCols=function(NodeID,ChildCount,NodeDescr,NodeSeq,NodeParent,Icons,Expr,ExprNot,Frame,NodeLink){
		this.field_NodeID=NodeID;
		this.field_ChildCount=ChildCount;
		this.field_NodeDescr=NodeDescr;
		this.field_NodeSeq=NodeSeq;
		this.field_NodeParent=NodeParent;
		this.field_Icons=Icons;
		this.field_Expr=Expr;
		this.field_ExprNot=ExprNot;
		this.field_Frame=Frame;
		this.field_NodeLink=NodeLink;
	}

  // parameter consumer
  this.paramconsumers=new Array()
  this.dataconsumers=new Array()

  this.addParmConsumer=function(ctrl){
    this.paramconsumers[this.paramconsumers.length] = ctrl
  }

  this.addDataConsumer=function(ctrl,fld){
    this.dataconsumers[this.dataconsumers.length] = new Array(ctrl,fld)
  }

  this.FindNodeByID=function(nodeid){
    var node,tmpnode,i
    for(i=0;i<this.Tree.allnodes.length;i++){
      tmpnode=this.Tree.allnodes[i]
      if (tmpnode!=null && tmpnode.nodeid==nodeid)
        node=tmpnode
    }
    return node
  }

  this.getFather=function(){
    if(typeof(this.Tree.currentnode.nodeFather)!='undefined')
      return this.Tree.currentnode.nodeFather.nodeid
    else
      return null
  }

  this.getParam=function(fld){
		if (typeof(this.Tree.currentnode)!='undefined'){
      if (fld==this.field_NodeID) {return this.Tree.currentnode.nodeid
      }else if (fld==this.field_NodeDescr) {
        return this.Tree.currentnode.descr
      }else{
        return ""
      }
    }
		return ""
  }
}


ZtVWeb.MenuViewCtrl.prototype=new ZtVWeb.StdControl;
