//----------------------------------------------------------
// Copyright - Gallaware, Inc. 2000
// All rights reserved
// These JavaScript functions are copyrighted by Gallaware, Inc.
// They can not be used, copied, altered, edited, or included 
// with or within any software, or published or distributed without
// the expressed written consent of Gallaware, Inc.
//----------------------------------------------------------

//--------------------
// Table of Contents utility functions
// A Table of contents is a set of hyperlinks, that allow navigation through a web site
// or a series of web pages.  These links can be either with text, images, and/or 
// rollover images.
//
// These functions rely on the following JS files
//      imageutils.js
//      textutils.js
//      tableutils.js
//
// The functions included in this file are:
//      TOCitem()  - the TOC object
//      showTOCDown()
//      showTOCTextHorizontal()
//      showTOCTextHorizontal2()
//      showTOCColumns()
//--------------------

//--------------------
// This function creates the Table of contents object used
// by the other functions in this library
// param: name - the link name
// param: 
//--------------------
function TOCitem(link, text, level, name, img1, img2, alt, target)
{
  // debug the text
  if (text == null)
  {
    alert("objTOCEntry() : text == null")
    this.text = "defaul text"
  }
  else
    this.text = text

  // debug the link
  if (link == null)
  {
    alert("objTOCEntry() : link == null")
    this.link = ""
  }
  else
    this.link = link

  this.level = (level != null ? level : 1)
  this.name = (name != null ? name : "X" + Math.random())
  this.img1 = (img1 != null ? img1 : "")
  this.img2 = (img2 != null ? img2 : "")
  this.alt = (alt != null ? alt : "")
  this.target = (target != null ? target : "")
  this.submenu = null
}

function addSubmenu(tocitem, index, submenuname)
{
  tocitem[index].submenu = submenuname
}

//--------------------
// this TOC displays the images and text straight down with the image above the link
// the TOC is placed within a table of its own
// param: inpercent - true if the table width (twidth) is in percent
// param: twidth - table width
// param: title - TOC title
// param: tocArr - an Array() of objTOCEntry objects
// param: bgcolor - table background color
// param: yes_text - include text - default is true
// param: yes_image - include images - default is true
// param: yes_rollover - include rollover image - default is false, always false if yes_image is false
// returns the number of row created
//--------------------
function showTOCDown(inpercent, twidth, title, bgcolor, tocArr, yes_text, yes_image, yes_rollover)
{
  var idx
  var numrows = 0

  // check optional parameters
  if (yes_text == null)
    yes_text = true

  if (yes_image == null)
    yes_image = true

  // if there is no image displayed do not use the rollover
  if (!yes_image)
    yes_rollover = false
  else if (yes_rollover == null)
    yes_rollover = true


  startTable(inpercent, 0, twidth, bgcolor, "", 0, 0, 2, 0, 0)

  if (title.length > 0)
  {
    startRow()
    startCol(0,0, "", "", "top", "center")
    document.write(title)
    endCol()
    endRow()
    numrows++
  }

  for (idx = 0; idx < tocArr.length; idx++)
  {
    if (yes_image)
    {
      startRow()
      startCol(0,0,"","","top", "center")
      imageLinkEx(tocArr[idx].name, tocArr[idx].link, tocArr[idx].img1, 
		(yes_rollover ? tocArr[idx].img2 : ""), 
		0, 0, tocArr[idx].alt, tocArr[idx].target)

      endCol()
      endRow()
      numrows++
    }
       
    if (yes_text)
    {
      startRow()
      startCol(0,0,"","","top", "center")

      //Updated: Feb 27, 2001 - added target
      textLinkEx(tocArr[idx].name, tocArr[idx].link, tocArr[idx].img1, 
		(yes_rollover ? tocArr[idx].img2 : ""),
		tocArr[idx].text, tocArr[idx].target)
      endCol()
      endRow()
      numrows++
    }

    // gap between rows
    startRow()
    startCol(0,0,"","","top", "center")
    document.write("&nbsp;")
    endCol()
    endRow()
    numrows++
  }

  endTable()

  return numrows
}

//--------------------
// This TOC displays the information the following manner:
//   item | item | item | item | item <break>
//            item | item | item
// param: justify - should be a number: 0 = default, 1 = "LEFT", 2 = "CENTER", 3 = "RIGHT"
// param: breakat - the number of items to list before putting in a break
// param: tocArr - an Array() of objTOCEntry
//--------------------
function showTOCTextHorizontal(justify, breakat, fontsize, tocArr)
{
  var idx
  var numitems = 0

  document.write("<font size=\"", fontsize, "\">")
  if (justify == 2)
    document.write("<center>")
  else if (justify == 3)
   document.write("<p align=\"right\">")

  for (idx = 0; idx < tocArr.length; idx++)
  {
    if (numitems > 0)
      document.write(" | ")

    textLinkEx(tocArr[idx].name, tocArr[idx].link, "", "", tocArr[idx].text, tocArr[idx].target)

    if (++numitems == breakat)
    {
      document.write("<BR>")
      numitems = 0
    }
  }

  if (justify == 2)
    document.write("</center>")
  else if (justify == 3)
    document.write("</p>")

  document.write("</font>")
}

//--------------------
// This TOC displays the information the following manner:
//   item  item  item  item  item </tr>
//   item  item  item
// param: justify - should be a number: 0 = default, 1 = "LEFT", 2 = "CENTER", 3 = "RIGHT"
// param: breakat - the number of items to list before putting in a break
// param: fontsize - the size of the text
// param: tocArr - an Array() of objTOCEntry
// param: inpercent - identifies that the h and w are in percent
// param: bgcolor - background color of the table
// param: bgimg - background image of the table
// param: border - the border size of the table
//--------------------
function showTOCTextHorizontal2(justify, breakat, fontsize, tocArr, inpercent, h, w, bgcolor, bgimg, border)
{
  var idx
  var numitems = 0
  var tmpstring

  if (justify == 2)
    document.write("<CENTER>")

  startTable(inpercent,h,w,bgcolor, bgimg, border,2,2,0,0)

  // loop through the array  
  for (idx = 0; idx < tocArr.length; idx++)
  {
    // start the row
    if (numitems == 0)
      startRow()

    // start the column
    startCol(0,0,"","","TOP","CENTER", 0,0)

    tmpstring = "<FONT SIZE=\"" + fontsize + "\">&nbsp;" + tocArr[idx].text + "&nbsp;</FONT>"
    textLinkEx(tocArr[idx].name, tocArr[idx].link, "", "", tmpstring, tocArr[idx].target)

    endCol()
    if (++numitems == breakat)
    {
      endRow()
      numitems = 0
    }
  }

  endTable()
  if (justify == 2)
    document.write("</CENTER>")
}

//--------------------
// This function displays a TOC in columns of (hopefully) even distribution.
//   Each column in the table holds N / Columns items.
//   The left column holds the first N items, the next column holds the next N items
//   until all of the columns are full.  The left most columns may have 1 more item
//   than the right most columns if the number of items is not evenly divisable by the
//   number of columns
//
// param: justify - 1 for LEFT, 2 for CENTER, 3 for RIGHT
// param: numcol - The number of colums in the TOC table
// param: fontsize - Size of the fonts for the links
// param: fontname - the name of the font for the link text
// param: tocArr - an array of objTOCEntry objects
// param: inpercent - determines if the height and width are in percent or pixels
// param: h - height of the table
// param: w - width of the table
// param: bgcolor - background color for the table
// param: bgimg - background image for the table
// param: border - the size of the border
// param: yes_image - present the images for the link - default is false
// param: yes_text - present the text for the link - default is true
// param: spacing - table's column cell spacing - default is 2
// param: yes_link - whether to show the TOC items as links or just the text default is true
// param: yes_even - spread the columns out evenly in the table - default is true
// param: coljust - left, center or right justify the columns - default is left
//--------------------
function showTOCColumns(justify, numcol, fontsize, fontname, tocArr, inpercent, h, w, bgcolor, bgimg, 
			border, yes_image, yes_text, spacing, yes_link, yes_even, coljust)
{
  var idx
  var numitems = 0
  var tmpstring
  var numrows
  var currow
  var colwidth = 0
  var curcol

  // check for optional parameters
  if (typeof(yes_image) == "undefined")
    yes_image = false

  if (typeof(yes_text) == "undefined")
    yes_text = true

  if (typeof(spacing) == "undefined")
    spacing = 2

  if (typeof(yes_link) == "undefined")
    yes_link = true

  if (typeof(yes_even) == "undefined")
    yes_even = true

  if (typeof(coljust) == "undefined")
    coljust = 1

  // determine if the columns are an even distribution
  if (yes_even)
    colwidth = Math.floor(100 / numcol)

  // determine the number of rows
  // if the remainder is greater than 0 add another row
  numrows = (tocArr.length % numcol == 0 ? tocArr.length / numcol :  Math.floor((tocArr.length / numcol) + 1) )

  // add the justification
  if (justify == 2)
    document.write("<CENTER>")

  startTable(inpercent,h,w,bgcolor, bgimg, border, spacing,2,0,0)

  // loop through the number of rows
  for (currow = 0; currow < numrows; currow++)
  {
    startRow()

    // loop for the number of columns
    for (idx = currow, curcol = 0;
	 curcol < numcol && idx < tocArr.length;
	 curcol++, idx += numrows)
    {
      // start the column
      // if there is an image then center the text
      if (colwidth > 0)
      {
        startCol(0, colwidth + "%", "", "", "TOP",
	        (yes_image || coljust == 2 ? "CENTER" : "LEFT"),
		0,0)
	} else {
        startCol(0,0,"","","TOP",
		(yes_image || coljust == 2 ? "CENTER" : "LEFT"),
		0,0)
      }
      displayTOCItem(justify, numcol, fontsize, fontname, tocArr[idx], inpercent, h, w, bgcolor, bgimg, 
			border, yes_image, yes_text, spacing, yes_link, yes_even, coljust, colwidth)

      endCol()
    }
    endRow()
  }

  endTable()
  if (justify == 2)
    document.write("</CENTER>")
}

function displayTOCItem(justify, numcol, fontsize, fontname, tocitem, inpercent, h, w, bgcolor, bgimg, 
			border, yes_image, yes_text, spacing, yes_link, yes_even, coljust, colwidth)
{
	if (yes_image)  {
		imageLinkEx(tocitem.name, tocitem.link, tocitem.img1, tocitem.img2, 0, 0,
			tocitem.alt, tocitem.target,
			(tocitem.submenu != null ? "popUp(\"" + tocitem.submenu + "\", event);" : ""),
			(tocitem.submenu != null ? "popDown(\"" + tocitem.submenu + "\");" : "")
			);
	}
      
	if (yes_text)  {        
		if (yes_image)
			document.write("<br>");

		// build the font description
		if (fontsize > 0 || (fontname != null && fontname.length > 0)) {
			tmpstring = "<font ";
			if (fontname != null && fontname.length > 0)
				tmpstring += "face=\"" + fontname + "\" ";

			if (fontsize > 0)
				tmpstring += "SIZE=\"" + fontsize + "\" ";

			tmpstring += ">" + tocitem.text + "</font>";
		} else
			tmpstring = tocitem.text;

		// determine whether to show the link or just the text
		if (yes_link)	{
			// the submenu/Popmenus use a third party DHTML package to display
			// the menus.  We need to identify it here with the submenu name 
			// and they are initiated during the mouseOver and removed during the
			// the mouseOut
			textLinkEx(tocitem.name, tocitem.link, "", "", tmpstring, tocitem.target,
				(tocitem.submenu != null ? "popUp(\"" + tocitem.submenu + "\", event);" : ""),
				(tocitem.submenu != null ? "popDown(\"" + tocitem.submenu + "\");" : "")
				);
		} else
			document.write(tmpstring);
	}
}


//--------------------
// When there is more than one table of contents displayed in a web page and they both
// use the same array, and both have rollover images, the browser
// may get mixed up to which link to update. Consequently, the wrong image link may be
// rolledover.  newName() helps with this by changing the name of the link to a <prefix> + a number
// This function will return the TOC Array sent in
// param: prefix - The prefix of the name - must be text
// param: tocArr - the Table of contents array
// return: tocArr
//--------------------
function newLinkName(prefix, tocArr)
{
  var idx
  for (idx = 0; idx < tocArr.length; idx++)
    tocArr[idx].name = prefix + idx

  return tocArr
}

//-------
// this method will return an array from the HM_? menu structure that is reconfigured as an array
// of TOCItems
// param: hm_array - this is an HM_? array structure
// return: an array of TOCitem
//-------
function toTOCArray(hm_array)
{
  var newArr = new Array()
  var idx

  for (idx = 1; idx < hm_array.length; idx++) {
     // TOCitem(link, text, level, name, img1, img2, alt, target)
     newArr[newArr.length] = new TOCitem(hm_array[idx][1],hm_array[idx][0], 1, "item_" + idx, null, null, null, null);
  }

  return newArr
}
