///////////////////////////////////////////////////////////////////////////////
//
// Einstand TreeControl Javascript
//
var BrowserVersionGlb    = "OTHER";      // browser version (IE, NS or OTHER)
var items                = new Array();  // array of all items in the sitemap
var topItems             = new Array();  // top level items only
var mouseOverColor       = "#0083fd";    // mouse over color
var nEntries             = 0;			 //
var GenerateTreeIndex    = 0;			 // 04-06-2001

var BlankTreeNodeIconGlb = "/ImgSys/NT_BlankTreeNode.gif";
var CrossLineIconGlb     = "/ImgSys/NT_CrossLineNode.gif";
var LastNodeIconGlb      = "/ImgSys/NT_LastNode.gif";
var MinusLastNodeIconGlb = "/ImgSys/NT_MinusLastNode.gif";
var MinusRootNodeIconGlb = "/ImgSys/NT_MinusRootNode.gif";
var MinusTreeNodeIconGlb = "/ImgSys/NT_MinusTreeNode.gif";
var PlusLastNodeIconGlb  = "/ImgSys/NT_PlusLastNode.gif";
var PlusRootNodeIconGlb  = "/ImgSys/NT_PlusRootNode.gif";
var PlusTreeNodeIconGlb  = "/ImgSys/NT_PlusTreeNode.gif";
var VertLineIconGlb      = "/ImgSys/NT_VertLineNode.gif";


// ----------------------------------------------------------------------------
// Writes a string to the sitemap document.
// ----------------------------------------------------------------------------
function _write(s)
{
   document.write(s);
}

// ----------------------------------------------------------------------------
// Writes a string and a CRLF to the sitemap document.
// ----------------------------------------------------------------------------
function _writeln(s)
{
   document.writeln(s);
}

// ----------------------------------------------------------------------------
// Called when the mouse moves over an item from outside.
// ----------------------------------------------------------------------------
function itemMouseOver(folderId)
{
   var siteItem=items[folderId];
   siteItem.m_MouseOver = true;
   //if (siteItem.style!=null)//&&(!siteItem.m_IsSelected))
   if (siteItem.style!=null)
   {
      siteItem.style.color=siteItem.m_overcolor;//mouseOverColor; // Jan 9 2001
      //siteItem.style.backgroundColor=mouseOverColor;
   }
   
   //state = siteItem.m_IsExpanded;
 
   //siteItem.SetState(state); //open<->close  

   //siteItem.Display();
   //propagateChangesInState(siteItem);//updateIcons( siteItem );
   //adjustY();
   var message = "url=";
   if (siteItem.m_message!=null)  message=message+siteItem.m_message;
   else if (siteItem.m_url!=null) message=message+siteItem.m_url;
   return message;
}

// ----------------------------------------------------------------------------
// Called when the mouse moves out of an item.
// ----------------------------------------------------------------------------
function itemMouseOut(folderId)
{
   var siteItem = items[folderId];
   siteItem.m_MouseOver=false;
   if (siteItem.style!=null)// && (!siteItem.m_IsSelected) )
   {
      siteItem.style.color="";
      //siteItem.style.backgroundColor="";
   }
   //propagateChangesInState(siteItem);//updateIcons( siteItem );
   //adjustY();
}
 
//***************************************************************** 
// Definition of class Folder 
//
// Parameters:
//   text           item text
//   url            item url
//   target         url target
//   message        status bar mouse over message
//   style          item style = "font-size:9pt;"
//   size           font size
//   color          text color
//   bold           true to make the font bold
//   italic         true to make the font italic
//   IconOpend      icon to be used when the item is expanded
//   IconClosed     icon to be used when the item is colapsed

function Folder(text, url, target, message, css_class, style, face, size, color, overcolor, bold, italic, IconOpend, IconClosed) //constructor 
{ 
  //constant data 
  this.m_IsFolder=true;
  this.m_text = text; 
  this.m_url=url;
  this.m_target=target;
  this.m_message=message;
  this.id = -1;
  this.navObj = 0;
  this.iconImg=null;
  this.nodeImg=null;
  this.isRootNode = 0;
  this.isLastNode = 0; 
  this.m_expandedIcon=IconOpend;
  this.m_colapsedIcon=IconClosed;
  this.m_IsSelected=false;
  this.m_MouseOver=false;
  this.m_class=css_class;
  this.m_FontStyle=style;
  this.m_face=face;
  this.m_size=size;
  this.m_color=color;
  this.m_overcolor=overcolor;			 // Jan 9 2001
  this.m_bold=bold;
  this.m_italic=italic;
  this.m_IsExpanded=true;

  //dynamic data 
  this.iconSrc = this.m_expandedIcon;
  this.children = new Array;
  this.nChildren = 0;
 
  //methods 
  this.m_InitializeProc=initializeFolder;
  this.SetState = setStateFolder;
  this.addChild = addChild;
  this.m_CreateIndexProc=createEntryIndex;
  this.hide = hideFolder;
  this.Display = display;
  this.m_RenderProc=drawFolder;
  this.totalHeight = totalHeight;
  this.subEntries = folderSubEntries;
  this.m_OutputLinkProc=OutputAnchor;
  this.m_OutputTextProc=OutputText;
  this.m_MouseOverProc=itemMouseOver;
} 

// ************************************************************* 
// Definition of class Item (a document or link inside a Folder) 
// ************************************************************* 
//
// Parameters:
//   text           item text
//   url            item url
//   target         url target
//   message        status bar mouse over message
//   sIcon          icon to be used.
//   style          item style = "font-size:9pt;"
//   size           font size
//   color          text color
//   bold           true to make the font bold
//   italic         true to make the font italic
function Item(text, url, target, message, css_class, style, face, size, color, overcolor, bold, italic, sIcon) // Constructor 
{ 
  // constant data 
  this.m_IsFolder=false;
  this.m_text= text;
  this.m_url = url;
  this.m_target=target;
  this.m_message=message;
  this.iconSrc=sIcon;
  this.m_IsSelected=false;
  this.m_MouseOver=false;
  this.m_class=css_class;
  this.m_FontStyle=style;//"font-size:9pt;";
  this.m_face=face;
  this.m_size=size;
  this.m_color=color;//"#005889";
  this.m_overcolor=overcolor;		 // Jan 9 2001
  this.m_bold=bold;
  this.m_italic=italic;
  this.m_IsExpanded=true;

  this.id = -1; //initialized in initalize() 
  this.navObj = 0; //initialized in render() 
  this.iconImg=null;//initialized in render() 
  // methods 
  this.m_InitializeProc=initializeItem;
  this.SetState = setStateItem;
  this.m_CreateIndexProc=createEntryIndex;
  this.hide = hideItem;
  this.Display = display;
  this.m_RenderProc=drawItem;
  this.totalHeight = totalHeight;
  this.m_OutputLinkProc=OutputAnchor;
  this.m_OutputTextProc=OutputText;
  this.m_MouseOverProc=itemMouseOver;
} 
 
function setStateFolder(isOpen) 
{ 
   if (isOpen==this.m_IsExpanded) return;
 
   if (BrowserVersionGlb=="NS")
   { 
      var subEntries;
      var fIt=0;
      var i=0;
      var totalHeight=0;
      for (i=0; i < this.nChildren; i++)
      {
         totalHeight = totalHeight + this.children[i].navObj.clip.height;
      }
      subEntries=this.subEntries();
      if (this.m_IsExpanded)
      {
         totalHeight = 0 - totalHeight;
      }
      for (fIt=this.id+subEntries+1; fIt < nEntries; fIt++)
      {
         items[fIt].navObj.moveBy(0, totalHeight);
      }
   }  
   this.m_IsExpanded=isOpen;
   propagateChangesInState(this);
} 

function setStateItem(isOpen) 
{ 
} 
 
function propagateChangesInState(folder) 
{   
   var i=0 
   if (folder.m_IsExpanded) 
   { 
      if (folder.nodeImg)
      {
         if (folder.isLastNode)
         {
            if (folder.isRootNode)
            {
               folder.nodeImg.src = MinusRootNodeIconGlb;
            }
		      else
            {
               folder.nodeImg.src = MinusLastNodeIconGlb;
            }
         }
         else folder.nodeImg.src = MinusTreeNodeIconGlb
      }
      folder.iconImg.src = folder.m_expandedIcon;
      for (i=0;i<folder.nChildren;i++) folder.children[i].Display();
   } 
   else 
   { 
      if (folder.nodeImg)
      {
         if (folder.isLastNode)
         {
            if (folder.isRootNode)
            {
               folder.nodeImg.src=PlusRootNodeIconGlb;
            }
            else
            {
               folder.nodeImg.src=PlusLastNodeIconGlb;
            }
         }
         else folder.nodeImg.src=PlusTreeNodeIconGlb;
      }
      if (folder.iconImg!=null)
      {
         folder.iconImg.src=folder.m_colapsedIcon;
      }
      for (i=0;i<folder.nChildren;i++) folder.children[i].hide();
   }  
} 
 
function hideFolder() 
{ 
   if (BrowserVersionGlb=="IE")
   { 
      if (this.navObj.style.display == "none") return;
      this.navObj.style.display = "none";
   } 
   else 
   { 
      if (this.navObj.visibility == "hiden") return;
      this.navObj.visibility = "hiden";
   } 
   this.SetState(0);
} 
 
function initializeFolder(level, lastNode, leftSide) 
{ 
   var nc=this.nChildren;
   
   this.m_CreateIndexProc();
 
   var auxEv = "";
 
   if (BrowserVersionGlb!="OTHER")
      auxEv = "<a href='javascript:clickOnNode("+this.id+")'>";
   else 
      auxEv = "<a>";

   if (level>0)
   {
      if (lastNode) //the last 'brother' in the children array 
      { 
         if (this.isRootNode)
         {
            this.m_RenderProc(leftSide+auxEv+"<img name='nodeIcon"+this.id+"' src='"+MinusRootNodeIconGlb+"' width=16 height=22 border=0></a>");
         }
         else
         {
	         this.m_RenderProc(leftSide+auxEv+"<img name='nodeIcon"+this.id +"' src='"+MinusLastNodeIconGlb+"' width=16 height=22 border=0></a>");
         }
         leftSide=leftSide+"<img src='"+BlankTreeNodeIconGlb+"' width=16 height=22>";
         this.isLastNode=true;
      } 
      else 
      { 
         this.m_RenderProc(leftSide+auxEv + "<img name='nodeIcon"+this.id+"' src='"+MinusTreeNodeIconGlb+"' width=16 height=22 border=0></a>");
         leftSide=leftSide+"<img src='"+VertLineIconGlb+"' width=16 height=22>";
         this.isLastNode=false;
      } 
   }
   else this.m_RenderProc("");
   
   if (nc > 0) 
   { 
      var i;
      level=level+1;
      for (i=0; i<this.nChildren; i++)
      { 
         if (i==this.nChildren-1)
         {
            this.children[i].m_InitializeProc(level, 1, leftSide);
         }
         else
         {
            this.children[i].m_InitializeProc(level, 0, leftSide);
         }
      } 
  } 
} 
 
function drawFolder(leftSide) 
{
   if (BrowserVersionGlb=="NS")
   {
      if (!document.yPos) document.yPos=8;
      _writeln("<layer id='folder" + this.id + "' top=" + document.yPos + " visibility=hiden>");
   }
   
   _write("<table");
   if (BrowserVersionGlb=="IE")
   {
      _write(" id='folder" + this.id + "' style='position:block;' ");
   }
   _write(" border=0 cellspacing=0 cellpadding=0>");
   _writeln("<tr><td valign=top>");
   _write(leftSide);

   if (BrowserVersionGlb=="IE")
   {
      _write("<a href='javascript:clickOnNode("+this.id+")'>"); // 11-21-00, 將 Click 在 Icon 的動作改為開關 Tree
   }

   _write("<img name='folderIcon" + this.id + "' ");
   _write("src='" + this.iconSrc+"' border=0>");
   if (BrowserVersionGlb=="IE")
   {
      _write("</a>");
   }
   _write("</td><td valign=top nowrap>");//_write("</td><td valign=middle nowrap>");
   if (USETEXTLINKS) 
   {
      this.m_OutputLinkProc();
      this.m_OutputTextProc();
   } 
   else 
   {
      _write(this.m_text);
   }
   _write("</td></tr>");
   _writeln("</table>");
   
   if (BrowserVersionGlb=="NS")
   { 
      _write("</layer>");
   } 
   
   if (BrowserVersionGlb=="IE")
   { 
      this.navObj = document.all["folder"+this.id];
      this.iconImg = document.all["folderIcon"+this.id];
      this.nodeImg = document.all["nodeIcon"+this.id];
      if (this.m_text!=null) this.style=document.all["style"+this.id].style;
   } 
   else if (BrowserVersionGlb=="NS")
   { 
      this.navObj = document.layers["folder"+this.id];
      this.iconImg = this.navObj.document.images["folderIcon"+this.id];
      this.nodeImg = this.navObj.document.images["nodeIcon"+this.id];
      document.yPos=document.yPos+this.navObj.clip.height;
   }
   else
   {
      //Do Something
   }
}
 
function OutputAnchor() 
{ 
   if (this.m_url!=null)
   { 
      _write("<a href='" + this.m_url + "'");
      if (this.m_target!=null)
      {
         _write(" target='" + this.m_target +"'");
      }
		if (this.m_class!=null)
		{
			_write(" class='" + this.m_class+"'");
		}
      if (BrowserVersionGlb!="OTHER")
      {
         if (this.m_IsFolder)
         {
            _write(" onClick='javascript:clickOnFolder("+this.id+")'");
         }
         _write(" onMouseOver='window.status=itemMouseOver("+this.id+"); return true'");
         _write(" onMouseOut ='itemMouseOut("+this.id+"); window.status=\"\"; return true'");
      }
      _write(">");
   } 
   else 
   {
      if (BrowserVersionGlb!="OTHER")
      {
         _write("<a href='javascript:clickOnFolder("+this.id+")'");
			if (this.m_class!=null)
			{
				_write(" class='" + this.m_class+"'");
			}
         _write(" onMouseOver='window.status=itemMouseOver("+this.id+"); return true'");
         _write(" onMouseOut='itemMouseOut("+this.id+"); window.status=\"\"; return true'");
         _write(">");
      }
      else
      {
         _write("<a>");
      }
   }
} 

function OutputText()
{
   var s1,s2;
   if ((this.m_class!=null)||(this.m_FontStyle!=null)||(this.m_size!=null)||(this.m_color!=null)||(BrowserVersionGlb=="IE"))
   {
      s1="<font";
      if (this.m_class!=null)      s1=s1+" class='"+this.m_class+"'";
      if (BrowserVersionGlb=="IE") s1=s1+" id='style"+this.id+"'";
      if (this.m_size!=null)       s1=s1+" size='" +this.m_size +"'";
      if (this.m_color!=null)      s1=s1+" color='"+this.m_color+"'";
      s1=s1+">";
      s2=this.m_text;
      if (this.m_bold)   s2="<b>"+s2+"</b>";
      if (this.m_italic) s2="<i>"+s2+"</i>";
      if (this.m_FontStyle!=null)   
      {
         s2="<P STYLE='" +this.m_FontStyle +"'>"+s2+"</P>";
      }
      s1=s1+s2+"</font></a>";
      _write(s1);
   }
   else
   {
      _write(this.m_text + "</a>");
   }
}

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;
} 
 
function hideItem() 
{ 
   if (BrowserVersionGlb=="IE")
   { 
      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.m_CreateIndexProc();
   if (level>0) 
   {
      if (lastNode) //the last 'brother' in the children array 
      { 
         this.m_RenderProc(leftSide + "<img src='"+LastNodeIconGlb+"' width=16 height=22>");
         leftSide = leftSide + "<img src='"+BlankTreeNodeIconGlb+"' width=16 height=22>";
      } 
      else 
      { 
         this.m_RenderProc(leftSide + "<img src='"+CrossLineIconGlb+"' width=16 height=22>");
         leftSide = leftSide + "<img src='"+VertLineIconGlb+"' width=16 height=22>";
      } 
   }
   else 
   {
      this.m_RenderProc("");
   }
} 
 
function drawItem(leftSide) 
{ 
   if (BrowserVersionGlb=="NS")
   {
      _writeln("<layer id='item" + this.id + "' top=" + document.yPos + " visibility=hiden>");
   }
   _write("<table");
   if (BrowserVersionGlb=="IE")
   {
      _write(" id='item" + this.id + "' style='position:block;' ");
   }
   _write(" border=0 cellspacing=0 cellpadding=0>");
   _write("<tr><td valign=top>");
   _write(leftSide);
   if (this.link!=null)
   {
      _write("<a href=" + this.link + ">");
   }
   _write("<img id='itemIcon"+this.id+"' ");
   _write("src='"+this.iconSrc+"' border=0>");
   if (this.link!=null)
   {
      _write("</a>");
   }
   if (this.m_IsSelected)
   {
      _write("</td><td valign=middle nowrap bgcolor=#FF00FF>");
   }
   else
   {
	   _write("</td><td valign=top nowrap>");//_write("</td><td valign=middle nowrap>");
   }

   this.m_OutputLinkProc();
   this.m_OutputTextProc();

   _writeln("</td></tr></table>"); // 10-06-00
  //_writeln("</table>"); // 10-06-00
   
   if (BrowserVersionGlb=="NS")
   {
      _write("</layer>");
   }
 
   if (BrowserVersionGlb=="IE")
   { 
      this.navObj = document.all["item"+this.id];
      this.iconImg = document.all["itemIcon"+this.id];
      if (this.m_text!=null) this.style=document.all["style"+this.id].style;
   } 
   else if (BrowserVersionGlb=="NS")
   { 
      this.navObj = document.layers["item"+this.id];
      this.iconImg = this.navObj.document.images["itemIcon"+this.id];
      document.yPos=document.yPos+this.navObj.clip.height;
   } 
} 
 
 
// Methods common to both objects (pseudo-inheritance) 
// ******************************************************** 
 
function display() 
{ 
   if (BrowserVersionGlb=="IE")
      this.navObj.style.display = "block"; 
   else 
      this.navObj.visibility = "show"; 
} 
 
function createEntryIndex() 
{ 
   this.id = nEntries;
   items[nEntries] = this;
   IsEntrySelected[nEntries]=false;
   nEntries++;
} 
 
// total height of subEntries open 
function totalHeight() //used with BrowserVersionGlb=="NS"
{ 
   var h=this.navObj.clip.height;
   if (this.m_IsExpanded) //is a folder and _is_ open 
   {
      var i = 0; 
      for (i=0 ; i < this.nChildren; i++)
      {
         h=h+this.children[i].totalHeight();
      }
   }
   return h;
} 
 
 
// Events 
// ********************************************************* 
 
function clickOnFolder(folderId) 
{ 
   var clicked = items[folderId];

  //document.Mainfrm.CurSelItem.value="test"+folderId;
 
   if (clicked.m_IsExpanded)
   {
      clickOnNode(folderId);
   }
   else
   {
	   state=clicked.m_IsExpanded;
      clicked.SetState(!state); //open<->close  
   }
 
   return;
 
   if (clicked.m_IsSelected) return;
} 
 
function clickOnNode(folderId) 
{ 
   var clickedFolder=0;
   var state=0;

//  document.Mainfrm.CurSelItem.value="test"+folderId;
 
   clickedFolder = items[folderId];
   state = clickedFolder.m_IsExpanded;
   clickedFolder.SetState(!state); //open<->close  
} 

function clickOnItem(folderId) 
{ 
   var ItemClicked=items[folderId];
   var bSelected=0;

   bSelected=ItemClicked.m_IsSelected;

   ItemClicked.m_IsSelected=!bSelected;
  //var clickedFolder = 0 

   if (ItemClicked.m_IsSelected)
   {
      document.Mainfrm.CurSelItem.value="Selected"+folderId;
   }
   else
   {
	   document.Mainfrm.CurSelItem.value="NotSelect"+folderId;
   }
   //propagateChangesInState(foldersTree) 
   //ItemClicked.Display();
} 
 
function GenerateTreeMap(LineAtRoot, CloseChildren) 
{ 
   if (document.all)
   {
     //BrowserVersionGlbVersion=1; //IE4   
     BrowserVersionGlb="IE";
   }
   else
   {
      if (document.layers)
      {
         //BrowserVersionGlbVersion=2; //NS4 
         BrowserVersionGlb="NS";
      }
      else
      {
         //BrowserVersionGlbVersion=0; //other 
         BrowserVersionGlb="OTHER";
      }
      BrowserVersionGlb="OTHER";
   }
   var i;
   for (i=GenerateTreeIndex;i<topItems.length;i++)
   {
      if (LineAtRoot) topItems[i].m_InitializeProc(1, true, "");
      else            topItems[i].m_InitializeProc(0, true, "");
      topItems[i].Display();
      if (CloseChildren) topItems[i].SetState(false);
   }
   GenerateTreeIndex=topItems.length;
//-- 10-06-00 kill "layer" --  
   if (BrowserVersionGlb!="OTHER")
   { 
      //_writeln("<layer top="+items[nEntries-1].navObj.top+">&nbsp;</layer>");
      //clickOnNode(0); // close the whole tree 
      //clickOnNode(0); // open the root folder 
   }
} 

// ----------------------------------------------------------------------------
// Adds a new item to the sitemap.
//
// Parameters:
//   siteItem     item to add
//   parent       item parent (if null, adds to top level)
// ----------------------------------------------------------------------------
function AddRootItem(siteItem)
{
   siteItem.isRootNode=true;
   topItems[topItems.length]=siteItem;
}

function AddNewItem(parentFolder, childFolder) 
{ 
   return parentFolder.addChild(childFolder);
} 
 
// Auxiliary Functions for Folder-Treee backward compatibility 
// ********************************************************* 
 
// Global variables 
// **************** 
 
USETEXTLINKS=true;
IsEntrySelected= new Array;
//selectedFolder=0;
