// color schemes, variables, object, and function definitions

// book styles
var cBookBorderColor = '#000000';         // color of book border
var cBookPageColor = '#FFFFFF';           // background for book page
var cPageHeaderColor = '#FFFFFF';        // background for book page header
var cSectionBackColor = '#D9E7C5';       // background for section description and title on the book.html page

// font styles
var cChapterFontColor = cBookBorderColor;      // font color for chapter text
var cHeaderFontColor = cBookBorderColor;       // font color for header text
var cNormalFontColor = '#000000';                  // font color for normal text
var cFooterFontColor = cBookBorderColor;        // font color for footer text

// tab styles
var cSelTabColor = cBookPageColor;               // background for selected tab
var cNonSelTabColor = '#E8E7DB';                  // background for non selected tab
var cLibraryTabColor = cNonSelTabColor;         // background for non library tab
var cTabLinkColor = '#000000';                      // color of tab link

// misc settings
var cChapterFontSize = 2;                            // font Size for chapter title

function isAssigned(obj)
{
  var result = false;
  if ((typeof(obj) != "undefined") && (obj != null))
  {
    result = true;
  }
  return result;
}

// verifies that object has been created
function HasInstance(obj)
{
   return isAssigned(obj);
   //return (obj != null) && (obj != 'undefined');
}

// standard chapter style
function toChapter(text)
{
   return '<center><span tabIndex="0"><strong><font face="' + cGeneralFont + '" size="' + cChapterFontSize + '" color="' + cChapterFontColor + '">' + text + '</font></strong></span></center>';
}

// standard header style
function toHeader(text)
{
   if( ( text != null ) && ( text.toString() != "" ) )
   {
     return '<span tabIndex="0"><font face="' + cGeneralFont + '" size="2" color="' + cHeaderFontColor + '"><strong>' + text + '</strong></font></span>';
   }
   return '<span><font face="' + cGeneralFont + '" size="2" color="' + cHeaderFontColor + '"><strong>' + '</strong></font></span>';
}

// replace function
function pbwSearchAndReplace(text, SearchFor, ReplaceWith) {
  var tmpContent = text;
  var tmpBefore = '';
  var tmpAfter = '';
  var tmpOutput = '';
  var intBefore = 0;
  var intAfter = 0;

  if (SearchFor.length > 0) {
    while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {

      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;

      // Get the rest of the content after the match until the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);

    }
  }
  return tmpOutput + tmpContent;
}

// normal text style
function toNormal(text, notabstop)
{
  var s = pbwSearchAndReplace(text, "&#13;&#10;", "<BR>");
  s = pbwSearchAndReplace(s, "&#10;&#13;", "<BR>");
  s = pbwSearchAndReplace(s, "&#13;", "<BR>");
  s = pbwSearchAndReplace(s, "&#10;", "<BR>");
  var hasts = true;
  if (isAssigned(notabstop))
  {
    hasts = (notabstop.toString().toLowerCase() == "true") ? false : true;
  }
  var ti = hasts ? " tabIndex=\"0\"" : "";
  return '<span' + ti.toString() + '><font face="' + cGeneralFont + '" size="1">' + s + '</font></span>';
}

function toPlain(text)
{
  var s = pbwSearchAndReplace(text, "&#13;&#10;", "<BR>");
  s = pbwSearchAndReplace(s, "&#10;&#13;", "<BR>");
  s = pbwSearchAndReplace(s, "&#13;", "<BR>");
  s = pbwSearchAndReplace(s, "&#10;", "<BR>");

  return s;
}

// footer style
function toFooter(text)
{
   if( ( text != null ) && ( text.toString() != "" ) )
   {
     return '<span tabIndex="0"><font face="' + cGeneralFont + '" size="1" color="' + cFooterFontColor + '"><em>' + text + '</em></font></span>';
   }
   return '<span ><font face="' + cGeneralFont + '" size="1" color="' + cFooterFontColor + '"><em>' + '</em></font></span>';
}

// tab link text
function toTabLink(text, isTitleTab)
{
   if (isTitleTab.toString().toLowerCase() == "true")
   {
      return '<strong><font face="' + cGeneralFont +'" size="3" color="' + cTabLinkColor + '">' + text + '</font></strong>';
   }
   else
   {
      return '<font face="' + cGeneralFont +'" size="1" color="' + cTabLinkColor + '">' + text + '</font>';
   }
}

// insert animated url image
var aniCounter = 0;
function insertAnimatedUrl(url, img1, img2, alt)
{
   aniCounter += 1;
   var aniName = 'aniUrl' + aniCounter;
   if (isAssigned(alt))
   {
     document.write('<a title="' + alt + '" href="' + url+ '" onmouseout="' + aniName +'.src=\'' + img1 + '\';" onmouseover="' + aniName +'.src=\'' + img2 + '\';">');
   }
   else
   {
     document.write('<a href="' + url+ '" onmouseout="' + aniName +'.src=\'' + img1 + '\';" onmouseover="' + aniName +'.src=\'' + img2 + '\';">');
   }
   if (isAssigned(alt))
   {
      document.write('<img name="' + aniName + '" src="' + img1 + '" border="0" alt="' + alt + '"></a>');
   }
   else
   {
      document.write('<img name="' + aniName + '" src="' + img1 + '" border="0"></a>');
   }
}

// vertical tabs generator
function addVTab(text, url, root, color, isTitleTab)
{
   if (url == '')
   {
      document.write('<tr><td bgcolor="' + color + '" align="left">');
      document.write('<table bgcolor="' + color + '" cellpadding="2"><tr><td align="left" width="50">');
      document.write(toNormal(text));
   }
   else
   {
      document.write('<tr><td style="border-right: 1px solid black;" bgcolor="' + color + '" align="left">');
      document.write('<table bgcolor="' + color + '" cellpadding="2"><tr><td align="left" width="50">');
      document.write('<a href="' + url + '">' + toTabLink(text, isTitleTab) + '</a>');
   }
   document.write('</td></tr></table></td></tr>');
   document.write('<tr><td bgcolor="' + cBookBorderColor + '" height = "1"><img src="' + root + 'images/1_1.gif" width="1" height="1"></td></tr>');
   document.write('<tr><td bgcolor="' + cBookBorderColor + '" height = "1"><img src="' + root + 'images/1_1.gif" width="1" height="1"></td></tr>');
}

function getString(key)
{
  var result = "";
  if (key && key.toString().length > 0 && jsstringarr)
  {
    for (var i = 0; i < jsstringarr.length; i++)
    {
      if (jsstringarr[i].id == key)
      {
        result = jsstringarr[i].val;
      }
    }
  }
  return result;
}
