
function LMenu (sTitle, sCssFile, nLivello, dDoc, pImages)
{
  this.title = sTitle;
  this.cssFile = sCssFile;
  this.toplevel = new Array();
  this.PathImages = pImages;
  for(var i = 1; i < nLivello ; i++) 
  {
    eval("this.level" + i + "items = new CDictionary");
    eval("this.level" + i + "active = false");
  } 
  this.doc = dDoc;
  
  this.ItemSel=0;

  this.activeMenuItems     = new Array(nLivello);
  for(var i = 1; i < nLivello ; i++) 
  {
    this.activeMenuItems[i]    = -1;
  }

  /* Definizione comandi */
  this.addMenuItem = LMenu_AddMenuItem;
  this.WritePage   = LMenu_WritePage;
  this.DisLivello = LMenu_DisLivello;
  this.openFolder = LMenu_openFolder;
  this.closeFolder = LMenu_closeFolder;
}

function LMenu_AddMenuItem(menu) 
{  
  var n = menu.level; 
  var size;
  if(n == 0) 
  {
    size = this.toplevel.length;    
    this.toplevel[size] = menu;   
  }
  else 
  {
    var levelitems = eval("this.level" + n + "items");
    if(levelitems != null) 
    {
      var parentId = "" + menu.parentID;
      var arr = levelitems.GetItem("" + parentId);
      if( arr == null) 
      {        
        arr = new Array();
        arr[0]  = menu;
        levelitems.Add("" + parentId,arr);        
      }
      else 
      {
        size = arr.length;
        arr[size] = menu;   
      }     
    }
  } 
}

function LMenu_openFolder(livello,ID)
{
  this.activeMenuItems[(livello-1)] = ID;
  for(var i = livello; i < this.activeMenuItems.length; i++) 
  {
    this.activeMenuItems[i]    = -1;
  }
  this.WritePage();
}

function LMenu_closeFolder(livello,ID)
{
  for(var i = (livello-1); i < this.activeMenuItems.length; i++) 
  {
    this.activeMenuItems[i]    = -1;
  }
  this.WritePage();
}

function LMenu_WritePage()
{
  this.doc.open("text/html");
  this.doc.clear();
  this.doc.write("<HTML>\n");
  this.doc.write("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n");
  this.doc.write("<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-store\">\n");
  this.doc.write("<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">\n");
  this.doc.write("<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">\n");
  this.doc.write("<Title>"+this.title+"</Title>\n");
  this.doc.write("<script language=\"JavaScript\">\n");
  this.doc.write("function openFinestra(itemSel, livello, url, target, finestra)\n");
  this.doc.write("{\n");
  this.doc.write("  top.Functions.cm.ItemSel=itemSel;\n");
  this.doc.write("  top.Functions.cm.openFolder(livello,itemSel);\n");
  this.doc.write("  if (url!=\"\")\n");
  this.doc.write("  {\n");
  this.doc.write("    var g_fIsSP2 = false;\n");
  this.doc.write("    g_fIsSP2 = (window.navigator.userAgent.indexOf(\"SV1\") != -1);\n");
  this.doc.write("    if (g_fIsSP2)\n");
  this.doc.write("    {\n");
  this.doc.write("//      alert(\"Hai installato il Service Pack 2!\");\n");
  this.doc.write("      target.location=url;\n");
  this.doc.write("    }\n");
  this.doc.write("    else\n");
  this.doc.write("    {\n");
  this.doc.write("//      alert(\"Non hai ancora installato il Service Pack 2!\");\n");
  this.doc.write("      window.open(url, finestra);\n");
  this.doc.write("  }\n");
  this.doc.write("}\n");

  this.doc.write("return false;\n");
  this.doc.write("}\n");
  this.doc.write("function closeFinestra(itemSel, livello, url, target, finestra)\n");
  this.doc.write("{\n");
  this.doc.write("  if (url!=\"\")\n");
  this.doc.write("  {\n");
  this.doc.write("    top.Functions.cm.ItemSel=-1;\n");
  this.doc.write("    top.Functions.cm.closeFolder(livello,itemSel);\n");
  this.doc.write("    var g_fIsSP2 = false;\n");
  this.doc.write("    g_fIsSP2 = (window.navigator.userAgent.indexOf(\"SV1\") != -1);\n");
  this.doc.write("    if (g_fIsSP2)\n");
  this.doc.write("    {\n");
  this.doc.write("//      alert(\"Hai installato il Service Pack 2!\");\n");
  this.doc.write("      target.location=url;\n");
  this.doc.write("    }\n");
  this.doc.write("    else\n");
  this.doc.write("    {\n");
  this.doc.write("//      alert(\"Non hai ancora installato il Service Pack 2!\");\n");
  this.doc.write("      window.open(url, finestra);\n");
  this.doc.write("    }\n");
  this.doc.write("  }\n");
  this.doc.write("  else\n");
  this.doc.write("  {\n");
  this.doc.write("    top.Functions.cm.closeFolder(livello,itemSel);\n");
  this.doc.write("  }\n");
  this.doc.write("  return false;\n");
  this.doc.write("}\n");
  this.doc.write("</script>\n");
  this.doc.write("<link  href=\""+this.cssFile+"\" rel=\"stylesheet\" type=\"text/css\">\n");
  this.doc.write("<BODY leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n");

  this.DisLivello(0,0);

  this.doc.write("</BODY>\n");
  this.doc.write("</HTML>\n");
  this.doc.close();
}

function LMenu_DisLivello(livello, Item)
{
  if (livello == 0)
  {
    this.doc.write("<Table class=\"TableLevel"+livello+"\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");
    var mainLevel = this.toplevel;
    for(var z=0 ;z < mainLevel.length;z++)  
    {
      this.doc.write("<TR>");
      this.doc.write("<TD>");
      this.doc.write("<font class=\"CatLevel0\">");
      this.doc.write(mainLevel[z].Name);
      this.doc.write("</font>\n");
      this.doc.write("</TD>");
      this.doc.write("</TR>\n");
      
      this.doc.write("<TR>");
      this.doc.write("<TD>");
      //this.doc.write("&nbsp;");
      this.doc.write("<Table class=\"TableLevel1\" width=100%  cellspacing=\"0\" cellpadding=\"0\"  border=\"0\">\n");
      this.DisLivello(1,mainLevel[z].ID);
      this.doc.write("</Table>\n");
      this.doc.write("</TD>");
      this.doc.write("</TR>\n");
    }
    this.doc.write("</Table>\n");
  }
  else
  {
    var levelitems = eval("this.level" + livello + "items");
    if(levelitems != null) 
    {
      var arr = levelitems.GetItem(""+Item);
      if( arr != null) 
      {
        var ln = arr.length;                
        for(var j=0;j<ln;j++) 
        {
          this.doc.write("<TR>");
          this.doc.write("<TD width=\"5\" height=\"4\" valign=\"top\">");
          if (arr[j].Folder)
          {
            //if (this.activeMenuItems[(livello-1)] == arr[j].ID && (top.Functions.Abilita || ((!top.Functions.Abilita) && (!arr[j].BloccaRamo)))
            if (this.activeMenuItems[(livello-1)] == arr[j].ID && (top.Functions.Loggato || ((!top.Functions.Loggato) && (!arr[j].BloccaRamo))))
            {
              this.doc.write("<Img src=\""+this.PathImages+"/folder_open.gif\">&nbsp;");
              this.doc.write("</TD>");
              this.doc.write("<TD>");
              this.doc.write("<font class=\"CatLevel"+livello+"\">");

              this.doc.write("<A href=\"#\" onClick=\"return closeFinestra('"+arr[j].ID+"','"+livello+"','"+arr[j].url+"',top."+arr[j].Target+",'"+arr[j].Target+"');\" title=\""+arr[j].Name+"\">");
/*

              if (arr[j].url != "")
                this.doc.write("<A href=\""+arr[j].url+"\" onClick=\"");
              else
                this.doc.write("<A href=\"#\" onClick=\"");
              if (arr[j].url != "")
                this.doc.write("top.Functions.cm.ItemSel=-1;");
              this.doc.write("top.Functions.cm.closeFolder("+livello+","+arr[j].ID+");");

              this.doc.write("return false;\" title=\""+arr[j].Name+"\" target=\""+arr[j].Target+"\">");

              this.doc.write("<A href=\"#\" onClick=\"");
              if (arr[j].url != "")
                this.doc.write("top.Functions.cm.ItemSel=-1;");
              this.doc.write("top.Functions.cm.closeFolder("+livello+","+arr[j].ID+");");
              if (arr[j].url != "")
                this.doc.write("window.open('"+arr[j].url+"','"+arr[j].Target+"');");

              this.doc.write("return false;\" title=\""+arr[j].Name+"\">");
*/
              this.doc.write(arr[j].Name);
              this.doc.write("</A>");
              this.doc.write("</font>\n");
              if (livello == 1)
                this.doc.write("<BR><Img src=\""+this.PathImages+"/nav_line.gif\" width='128' height='1'>&nbsp;");
              this.doc.write("<Table class=\"TableLevel"+(livello+1)+"\" width=100% cellspacing=\"0\" cellpadding=\"0\">\n");
              this.doc.write("<TR>");
              this.doc.write("<TD>");
              this.DisLivello((livello+1),arr[j].ID);
              this.doc.write("</TD>");
              this.doc.write("</TR>\n");
              this.doc.write("</Table>\n");
              if (livello == 1)
                this.doc.write("<Img src=\""+this.PathImages+"/nav_line.gif\" width='128' height='1'>&nbsp;\n");
            }
            else
            {
              this.doc.write("<Img src=\""+this.PathImages+"/folder_close.gif\">&nbsp;");
              this.doc.write("</TD>");
              this.doc.write("<TD>");
              this.doc.write("<font class=\"CatLevel"+livello+"\">");


              this.doc.write("<A href=\"#\" onClick=\"return openFinestra('"+arr[j].ID+"','"+livello+"','"+arr[j].url+"',top."+arr[j].Target+",'"+arr[j].Target+"');\" title=\""+arr[j].Name+"\">");


/*
              if (arr[j].url != "")
                this.doc.write("<A href=\""+arr[j].url+"\" onClick=\"");
              else
                this.doc.write("<A href=\"#\" onClick=\"");
              if (arr[j].url != "")
                this.doc.write("top.Functions.cm.ItemSel=-1;");
              this.doc.write("top.Functions.cm.closeFolder("+livello+","+arr[j].ID+");");

              this.doc.write("return false;\" title=\""+arr[j].Name+"\" target=\""+arr[j].Target+"\">U");
              this.doc.write("<A href=\"#\" onClick=\"");
              if (arr[j].url != "")
                this.doc.write("top.Functions.cm.ItemSel=-1;");
              this.doc.write("top.Functions.cm.openFolder("+livello+","+arr[j].ID+");");
              if (arr[j].url != "")
                this.doc.write("window.open('"+arr[j].url+"','"+arr[j].Target+"');");

              this.doc.write("return false;\" title=\""+arr[j].Name+"\">");
*/
              this.doc.write(arr[j].Name);
              this.doc.write("</A>");
              this.doc.write("</font>\n");
            }
          }
          else
          {
//            alert(top.Functions.Abilita);
            if ( arr[j].Name != "--")
            {
              if (this.ItemSel == arr[j].ID)
              {
                top.document.title=arr[j].TitoloFinestra;
                this.doc.write("<Img src=\""+this.PathImages+"/check.gif\">&nbsp;");
              }
              else
                this.doc.write("<Img src=\""+this.PathImages+"/folder_openbl.gif\">&nbsp;");
            }
            this.doc.write("</TD>");
            this.doc.write("<TD>");
            this.doc.write("<font class=\"CatLevel"+livello+"\">");
            if ( arr[j].Name != "--")
            {
              if (top.Functions.Abilita)
              {
//                this.doc.write("<A href=\"#\" onClick=\"top.Functions.cm.ItemSel="+arr[j].ID+";top.Functions.cm.openFolder("+livello+","+arr[j].ID+");window.open('"+arr[j].url+"','"+arr[j].Target+"');return false;\" title=\""+arr[j].Name+"\">");
//                this.doc.write("<A href=\""+arr[j].url+"\" onClick=\"return openFinestra(top.Functions.cm.ItemSel="+arr[j].ID+";top.Functions.cm.openFolder("+livello+","+arr[j].ID+");\" Target=\""+arr[j].Target+"\" title=\""+arr[j].Name+"\">y");
                if (arr[j].Target == "")
                {
                  this.doc.write("<A href=\""+arr[j].url+"\" title=\""+arr[j].Name+"\" target=\"_blank\">");
                }
                else if (arr[j].Target == "_blank")
                {
                  this.doc.write("<A href=\""+arr[j].url+"\" title=\""+arr[j].Name+"\" target=\"_blank\">");
                }
                else
                {
                  this.doc.write("<A href=\"#\" onClick=\"return openFinestra('"+arr[j].ID+"','"+livello+"','"+arr[j].url+"',top."+arr[j].Target+",'"+arr[j].Target+"');\" title=\""+arr[j].Name+"\">");
                }
              }
              else
              {
                if (arr[j].SemprePres)
                {
//                  this.doc.write("<A href=\"#\" onClick=\"top.Functions.cm.ItemSel="+arr[j].ID+";top.Functions.cm.openFolder("+livello+","+arr[j].ID+");window.open('"+arr[j].url+"','"+arr[j].Target+"');return false;\" title=\""+arr[j].Name+"\">");
//                  this.doc.write("<A href=\""+arr[j].url+"\" onClick=\"top.Functions.cm.ItemSel="+arr[j].ID+";top.Functions.cm.openFolder("+livello+","+arr[j].ID+");\" title=\""+arr[j].Name+"\" target=\""+arr[j].Target+"\">z");
                  if (arr[j].Target == "")
                  {
                    this.doc.write("<A href=\""+arr[j].url+"\" title=\""+arr[j].Name+"\" target=\"_blank\">");
                  }
                  else if (arr[j].Target == "_blank")
                  {
                    this.doc.write("<A href=\""+arr[j].url+"\" title=\""+arr[j].Name+"\" target=\"_blank\">");
                  }
                  else
                  {
                    this.doc.write("<A href=\"#\" onClick=\"return openFinestra('"+arr[j].ID+"','"+livello+"','"+arr[j].url+"',top."+arr[j].Target+",'"+arr[j].Target+"');\" title=\""+arr[j].Name+"\">");
                  }
                }
              }
              this.doc.write(arr[j].Name);
              if (top.Functions.Abilita)
              {
                this.doc.write("</A>");
              }
              else
              {
                if (arr[j].SemprePres)
                {
                  this.doc.write("</A>");
                }
              }
            }
            else
            {
              this.doc.write("<Img src=\""+this.PathImages+"/nav_line.gif\" width='128' height='1'>&nbsp;\n");
            }
            this.doc.write("</font>\n");
          }
          this.doc.write("</TD>");
          this.doc.write("</TR>\n");
        }
      }
    }
  }

}
