/* ADDED A.C.Bellini */
// Variabile per il debug di quello che viene scritto nel browser
var TreeText = "";
var debug = false;
// metodo usato al posto della dc.write
function docWrite(s) {
        document.write(s);
        if (debug) {
                TreeText += s + "\n";
        }
}


function dalert(s) {
        if (debug)
                alert(s);
}
/* END ADDITION */



function Folder(folderDescription, hreference, flagLink) //constructor
//function Folder(folderDescription, hreference)
{
  //constant data
  /* ADDED BY M.TEBALDI */
  this.cod = new String("")
  /* END ADDITION */
  this.desc = folderDescription
  this.hreference = hreference
  this.id = -1
  this.navObj = 0
  this.iconImg = 0
  this.nodeImg = 0
  this.isLastNode = 0
  //Proprietà che indica se deve possedere o no un'iperlink
  this.linkState=flagLink

  //dynamic data
  this.isOpen = true
  this.iconSrc = pathIco + "/ftv2folderopen.gif"
  this.children = new Array
  this.nChildren = 0

  //methods
  this.initialize = initializeFolder
  this.setState = setStateFolder
  this.addChild = addChild
  this.createIndex = createEntryIndex
  this.hide = hideFolder
  this.display = display
  this.renderOb = drawFolder
  this.totalHeight = totalHeight
  this.subEntries = folderSubEntries
  this.outputLink = outputFolderLink
  this.propagateChangesInState = FolderPropagateChangesInState
  /* ADDED BY M.TEBALDI */
  this.setCod = setCod
  /* END ADDITION */
}



function setStateFolder(isOpen)
{
  var subEntries
  var totalHeight
  var fIt = 0
  var i=0

  if (isOpen == this.isOpen)
    return

  if (browserVersion == 2)
  {
    totalHeight = 0
    for (i=0; i < this.nChildren; i++)
      totalHeight = totalHeight + this.children[i].navObj.clip.height
      subEntries = this.subEntries()
    if (this.isOpen)
      totalHeight = 0 - totalHeight
    for (fIt = this.id + subEntries + 1; fIt < nEntries; fIt++)
      indexOfEntries[fIt].navObj.moveBy(0, totalHeight)
  }
  this.isOpen = isOpen
  this.propagateChangesInState() ;
}




function FolderPropagateChangesInState()
{
  var i=0

  if (this.isOpen)
  {
    if (this.nodeImg)
      if (this.isLastNode)
        this.nodeImg.src = pathIco + "/ftv2mlastnode.gif"
      else
          this.nodeImg.src = pathIco + "/ftv2mnode.gif"
    this.iconImg.src = pathIco + "/ftv2folderopen.gif"
    for (i=0; i<this.nChildren; i++)
      this.children[i].display()
  }
  else
  {
    if (this.nodeImg) {
      if (this.isLastNode) {
        this.nodeImg.src = pathIco + "/ftv2plastnode.gif"
      } else {
           this.nodeImg.src = pathIco + "/ftv2pnode.gif"
          }
        }
    this.iconImg.src = pathIco + "/ftv2folderclosed.gif"
    for (i=0; i<this.nChildren; i++) {
      this.children[i].hide()
        }
  }
}



function hideFolder()
{
// CHANGED A.C.Bellini
  if (browserVersion == 1 || browserVersion == 3) {
    if (this.navObj.style.display == "none")
      return
    this.navObj.style.display = "none"
  } else if (browserVersion == 2) {
    if (this.navObj.visibility == "hide")
      return
    this.navObj.visibility = "hide"
  }

  this.setState(0)
}




function initializeFolder(level, lastNode, leftSide)
{

var j=0
var i=0
var numberOfFolders
var numberOfDocs
var nc

  nc = this.nChildren
  this.createIndex()

  var auxEv = ""

  if (browserVersion > 0)
	  auxEv = "<a class='albero' href='javascript:clickOnNode(" + this.id + ")'>"
//    auxEv = "<a href='javascript:clickOnNode("+this.id+")'>"

  else
    auxEv = "<a>"


  if (level>0)
    if (lastNode) //the last 'brother' in the children array
    {
      this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='" + pathIco + "/ftv2mlastnode.gif' border=0></a>")
      leftSide = leftSide + "<img src='" + pathIco + "/ftv2blank.gif'>"
      this.isLastNode = 1
    }
    else
    {

      dalert("not last node");
      this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='" + pathIco + "/ftv2mlastnode.gif' border=0></a>")
      leftSide = leftSide + "<img src='" + pathIco + "/ftv2vertline.gif'>"
      this.isLastNode = 0
    }
  else {
    this.renderOb("")
  }
  if (nc > 0)
  {
    level = level + 1
    for (i=0 ; i < this.nChildren; i++)
    {
      if (i == this.nChildren-1) {
            dalert("last child: "+i);
        this.children[i].initialize(level, 1, leftSide)
      } else {
        this.children[i].initialize(level, 0, leftSide)
            dalert("childinit: "+i);
          }
    }
  }
  dalert(TreeText);

}




function drawFolder(leftSide)
{
  this.linkState
  if (browserVersion == 2) {
    if (!doc.yPos)
      doc.yPos=8
    docWrite("<layer id='folder" + this.id + "' top=" + doc.yPos + " visibility=hide>")
  }
  dalert("drawing Folder: "+this.desc);
  docWrite("<table ")
  if (browserVersion == 1 || browserVersion == 3)
//  docWrite("id='folder" + this.id + "' style='display:block;padding:0' ")
  docWrite("id='folder" + this.id + "' style='padding:0' ")
  //docWrite(" border=0 cellspacing=0 cellpadding=0 marginwidth=0 marginheight=0 bgcolor='#FBF1DC'>")
  docWrite(" border=0 cellspacing=0 cellpadding=0 marginwidth=0 marginheight=0 background='bg_repartosin.gif'>")
  var s =  this.outputLink();
  docWrite("<tr><td>"
                +leftSide
                +s
                +"<img "
                +"name='folderIcon" + this.id + "' "
                +"src='" + this.iconSrc+"' "
                +"border=0 id='folderIcon" + this.id + "'></a>"
                +"</td>")
  // docWrite(leftSide)

  // docWrite("<img name='folderIcon" + this.id + "' ")
  // docWrite("src='" + this.iconSrc+"' border=0 id='folderIcon" + this.id + "'></a>")
  // docWrite("</td><td valign=middle nowrap>")
  s = ""

  //MODIFICA DA if (USETEXTLINKS)A if (this.linkState)
  if (this.linkState)
  {
    //MODIFICA DA outputLink() A this.outputLink()
    s += this.outputLink()
    s+= this.desc + "</a>"
  }
  else {
    s += this.desc
  }
  docWrite("<td align='left' valign=top >"+s+"</td></TR>")
  docWrite("</table>")

  if (browserVersion == 2) {
    docWrite("</layer>")
  }
  if (browserVersion == 1 ) {
    this.navObj = doc.all["folder"+this.id]
    this.iconImg = doc.all["folderIcon"+this.id]
    this.nodeImg = doc.all["nodeIcon"+this.id]
  } else if (browserVersion == 2) {
    this.navObj = doc.layers["folder"+this.id]
    this.iconImg = this.navObj.document.images["folderIcon"+this.id]
    this.nodeImg = this.navObj.document.images["nodeIcon"+this.id]
    doc.yPos=doc.yPos+this.navObj.clip.height
  } else if (browserVersion == 3) {
          this.navObj = document.getElementById("folder"+this.id);
        dalert(TreeText);


    this.iconImg = document.getElementById("folderIcon"+this.id);
    this.nodeImg = document.getElementById("nodeIcon"+this.id);
  }
  dalert(TreeText);
}




function outputFolderLink()
{
  var ret = "";
  if (this.hreference)
  { 
		ret +="<a href='" + this.hreference + "' ";
	  //ret +="<a href='javascript:GoToIRef(" + this.id + ")' ";	  
    if (browserVersion > 0)
      //ret+="onClick='javascript:GoToIRef(" + this.id + ")'";
      //ret+="onClick='javascript:clickOnFolder("+this.id+")'";
    ret += ">"
  }
  else
    ret +="<a>"
//  docWrite("<a href='javascript:clickOnFolder("+this.id+")'>")
  return ret;
}




function addChild(childNode)
{
  this.children[this.nChildren] = childNode
  this.nChildren++
  return childNode
}




function folderSubEntries()
{
  var i = 0
  var se = this.nChildren

  for (i=0; i < this.nChildren; i++){
    if (this.children[i].children) //is a folder
      se = se + this.children[i].subEntries()
  }

  return se
}




// Definition of class Item (a document or link inside a Folder)
// *************************************************************

// MODIFICA BY M.TEBALDI
// Aggiunto il campo flagLink per indicare se debba contenere un iperlink


function Item(itemDescription, itemLink, flagLink, flagCheck) // Constructor
{
  // constant data
  this.desc = itemDescription
  this.link = itemLink
  this.id = -1 //initialized in initalize()
  this.navObj = 0 //initialized in render()
  this.iconImg = 0 //initialized in render()
  this.iconSrc = pathIco + "/ftv2doc.gif"
  //Proprietà che mi indica se la foglia debba avere o no un iperlink
  this.linkState=flagLink
  this.checkState=flagCheck

  // methods
  this.initialize = initializeItem
  this.createIndex = createEntryIndex
  this.hide = hideItem
  this.display = displayItem
  this.renderOb = drawItem
  this.totalHeight = totalHeight
  /* ADDED BY M.TEBALDI */
  this.setCod = setCod
  /* END ADDITION */
}



function hideItem()
{
  if (browserVersion == 1 || browserVersion == 3) {
    if (this.navObj.style.display == "none")
      return
    this.navObj.style.display = "none"
  } else {
    if (this.navObj.visibility == "hiden")
      return
    this.navObj.visibility = "hiden"
  }
}




function initializeItem(level, lastNode, leftSide)
{
  this.createIndex()
  dalert("init item: "+this.desc);

  if (level>0){
      //AGGIUNTA CHECKBOX BY M.TEBALDI
      var strchk=""
  if (USECHECKBOX){	
      if (this.checkState) {
         strchk="<input valign=middle type='checkbox' name=" + this.desc + " value=" + this.id + " CHECKED>"
         }
         else{
         strchk="<input valign=middle type='checkbox' name=" + this.desc + " value=" + this.id + ">"
         }
  }
    if (lastNode) //the last 'brother' in the children array
    {
      this.renderOb(leftSide + "<img src='" + pathIco + "/ftv2lastnode.gif'>" + strchk)
      leftSide = leftSide + "<img src='" + pathIco + "/ftv2blank.gif'>"
    }
    else
    {
      this.renderOb(leftSide + "<img src='" + pathIco + "/ftv2lastnode.gif'>" + strchk)
      leftSide = leftSide + "<img src='" + pathIco + "/ftv2vertline.gif'>"
    }
  }else
    this.renderOb("")
}




function drawItem(leftSide)
{
  dalert("draw item: "+this.desc);
  if (browserVersion == 2)
    docWrite("<layer id='item" + this.id + "' top=" + doc.yPos + " visibility=hiden>")
  docWrite("<table ")
  if (browserVersion == 1 || browserVersion == 3) {
//    docWrite("id='item" + this.id + "' style='display:block;border:0;margin-top: 0;margin-left: 0;margin-right: 0;padding: 0;' ")
    docWrite("id='item" + this.id + "' style='border: 0;margin-top: 0;margin-left: 0;margin-right: 0;padding: 0; ")
  }
  /*docWrite("background='#D6E0FF' border=0 cellspacing=0 cellpadding=0>")
  docWrite("<tr bgcolor='#D6E0FF' ><td align='left' >")*/
  docWrite("background='#FFFFFF' border=0 cellspacing=0 cellpadding=0>")
  //docWrite("<tr bgcolor='#FFFFFF'><td align='left' >")
  docWrite("<tr class='fondoMedio'><td align='left' >")
  docWrite(leftSide)
  dalert(leftSide);
  //docWrite("<a href=" + this.link + ">")
  if (!USECHECKBOX){
  docWrite("<img id='itemIcon"+this.id+"' ")
  docWrite("src='"+this.iconSrc+"' border=0>")
  }
  docWrite("</a>")
  docWrite("</td><td align='left' valign=top>")
//if (USETEXTLINKS)
  if (this.linkState)
    docWrite("<a href=" + this.link + ">" + this.desc + "</a>")
  else
    docWrite(this.desc)
  docWrite("</td></tr></table>")

  if (browserVersion == 2)
    docWrite("</layer>")

  if (browserVersion == 1) {
    this.navObj = doc.all["item"+this.id]
    this.iconImg = doc.all["itemIcon"+this.id]
  } else if (browserVersion == 2) {
    this.navObj = doc.layers["item"+this.id]
    this.iconImg = this.navObj.document.images["itemIcon"+this.id]
    doc.yPos=doc.yPos+this.navObj.clip.height
  } else if (browserVersion == 3) {
    this.navObj = doc.getElementById("item"+this.id);
    this.iconImg = doc.getElementById("itemIcon"+this.id)
  }
}




function displayItem()
{
  if (browserVersion == 1 || browserVersion == 3)
    this.navObj.style.display = ""
  else
    this.navObj.visibility = "show"
}

// Methods common to both objects (pseudo-inheritance)
// ********************************************************




/* ADDED BY M.TEBALDI */
function setCod(idCod){
this.cod = idCod
}
/* END ADDITION */




function display()
{
  if (browserVersion == 1 || browserVersion == 3)
    this.navObj.style.display = ""
  else
    this.navObj.visibility = "show"
}




function createEntryIndex()
{
  this.id = nEntries
  indexOfEntries[nEntries] = this
  nEntries++
}



// total height of subEntries open
function totalHeight() //used with browserVersion == 2
{
  var h = this.navObj.clip.height
  var i = 0

  if (this.isOpen) //is a folder and _is_ open
    for (i=0 ; i < this.nChildren; i++)
      h = h + this.children[i].totalHeight()

  return h
}


// Events
// *********************************************************

function clickOnFolder(folderId)
{
  var clicked = indexOfEntries[folderId]

  if (!clicked.isOpen)
    clickOnNode(folderId)

  return

  if (clicked.isSelected)
    return
}

function clickOnNode(folderId)
{
  var clickedFolder = 0
  var state = 0
  clickedFolder = indexOfEntries[folderId]
  state = clickedFolder.isOpen
  clickedFolder.setState(!state) //open<->close
  state = clickedFolder.isOpen
}

// CHANGED A.C.Bellini
function initializeDocument()
{
  if (doc.all) {
    browserVersion = 1 //IE4
  } else if (doc.layers) {
      browserVersion = 2 //NS4
  } else if (navigator.appName=="Netscape" && navigator.appVersion.indexOf("5.0") == 0) {
            browserVersion = 3 // Mozilla
  }  else {
      browserVersion = 0 //other
  }

  dalert("browser version: "+browserVersion);	
  foldersTree.initialize(0, 1, "")
  foldersTree.display()
  dalert(TreeText);
  dalert("forms: "+document.forms.length);

  if (browserVersion > 0)
  {
    docWrite("<layer top="+indexOfEntries[nEntries-1].navObj.top+">&nbsp;</layer>")

    // close the whole tree
    clickOnNode(0)
    // open the root folder
    clickOnNode(0)
  }	
  }
  


// Global variables
// ****************
USECHECKBOX = 0
USETEXTLINKS = 0
indexOfEntries = new Array
nEntries = 0
doc = document
browserVersion = 0
selectedFolder=0
//pathIco='../img/albero'
pathIco='/web/Mall/img/albero'
