// Web site scripts

function GetBookmark (loc)
  {
  var l = loc.length
  var p = loc.indexOf ("?")
  if (p == -1)
    p = loc.indexOf ("#")
    
  if (p == -1)
    return ""
    
  return loc.slice (p+1, l)
  }

var CME = 'Clear Mountain Garden Treasures'
var SeedList = 'Seed List'

function MainActivate (id)
  {
  var url = PictureUrl[id]
  
  if (url != null)
    {
    var doc = window.parent.document.frames.main.document
    // Navigate to page
    var loc = new String (document.location.pathname)
    if (loc.indexOf (url) = -1)
      doc.open (url, "_self", "", true)

    return true
    }

  return false
  }

function SeedActivate (id)
  {
  // Seed pages
  for (var f = 0; f < SeedPages.length; f++)
    {
    var SeedPage = SeedPages[f]
    if ((SeedPage != null) && (SeedPage.name == id))
      {
      document.frames.main.document.open (SeedPage.url, "_self", "", true)
      return
      }
    }
  // Seeds
  for (f in SeedsMap)
    {
    var seed = SeedsMap[f]
    if ((seed != null) && (seed.name == id))
      {
      document.frames.main.document.open (SeedPages[seed.index].url + '?' + id, "_self", "", true)
      return
      }
    }
  }

function MainLoaded ()
  {
  var bookmark = GetBookmark (document.location.toString())
  if (bookmark != "")
    {
    if (MainActivate (bookmark.toLowerCase()))
      return
    }

  // ShowMain (document.frames.main.document)
  }
  
function ShowMain (doc)
  {
  var doc = window.parent.document.frames.main.document
  doc.open ('home.htm', "_self", "", true)
  }
  
function SeedLoaded ()
  {
  var bookmark = GetBookmark (document.location.toString())
  if (bookmark == "")
    {
    return
    }

  SeedActivate (bookmark.toLowerCase())
  }
  
function SetTitle (Doc, Title)
  {
  if (Title == null)
    {
    Doc.all.lblTitle.innerHTML = ''
    Doc.title = CME
    }
  else
    {
    Doc.all.lblTitle.innerHTML = '- ' + Title
    Doc.title = CME + ' (' + Title + ')'
    }
  }

function SetParentTitle (Title)
  {
  var p = window.parent
  while (p != null)
    {
    var n = p.parent
    if (n == p)
      break
    p = n
    }
  var parent_document = p.document

  SetTitle (parent_document, Title)
  }

function SelectTree (Doc, Name)
  {
  var tdc = Doc.all.tags('td')
  for (var f = 0; f < tdc.length; f++)
    {
    var td = tdc[f]
    if (td.name == Name)
      td.className = 'TableTreeSelected'
    else
      td.className = 'TableTree'
    }
  }

function HandleNewWindow (Title, Name)
  {
  if (window.name == 'print')
    {
    var tb = document.all['toolbar']
    if (tb)
      {
      tb.style.cssText = '{display: none;}'
      }
    window.print()
    window.close()
    }
  else
    {
    var temp = document.all.PrintHref
    if (temp)
      temp.href = document.location
    var full_frame = document.all.FullFrameButton
    if (full_frame)
      {
      var docname = full_frame.docname
      full_frame.href = new String (full_frame.href).slice (0, -9) + docname
      UpdateReferrer()
      }

    var parent_document = window.parent.document
    var tree = window.parent.document.frames.Tree
    
    if (!tree)
      {
      if (full_frame)
        {
        full_frame.style.cssText = '{display: inline;}'
        }
      }
    if (tree)
      {
      var tree_document = tree.document

      SetParentTitle (Title)
      if (Name != null)
        SelectTree (tree_document, Name)
      }
    }
  }

var OtherPictureWidth = 4

function FindSeedPicture (id)
  {
  return SeedData[id]
  }

function FindPicture (id)
  {
  var l = id.indexOf ('-')
  Summary = ''
  
  if (l != -1)
    {
    Summary = id.slice (l+1)
    id = id.slice (0, l)
    }

  if (id.slice (0, 5) == 'seed.')
    {
    var Picture = FindSeedPicture (id.slice (5))
    if (Summary != '')
      Picture.summaryurl = Summary
    return Picture
    }
  
  var Picture = Data
  var s
  var PictIndex = ''
  l = id.indexOf ('.')

  if (l != -1)
    {
    PictIndex = id.slice (l+1, id.length)
    id = id.slice (0, l)
    }

  Picture = Picture.Pictures[id]

  if (PictIndex != '')
    {
    var temp = Picture.AdditionalPictures[PictIndex]
    temp.Parent = Picture
    Picture = temp
    }

  if (Summary != '')
    Picture.summaryurl = Summary
  
  return Picture
  }

function ShowPictureActivate (id)
  {
  var Picture = FindPicture (id)
  var Title, BackURL, Name

  if (Picture.Parent == undefined)
    {
    Title = Picture.title + ' (' + Picture.caption + ')'
    BackURL = Picture.summaryurl
    Name = Picture.name
    }
  else
    {
    Title = Picture.Parent.title + ' (' + Picture.caption + ')'
    BackURL = Picture.Parent.summaryurl
    Name = Picture.Parent.name
    }
    
  document.title = '[' + Title + '] ' + CME

  document.all.PrintTitle.innerHTML = Title
  document.all.MainPicture.src = Picture.imageurl
  document.all.BackLink.href = BackURL
  var Description = Picture.imagedescription

  if (Description != '')
    {
    document.all.WholeDescription.style.cssText = ''
    document.all.Description.innerHTML = Description
    }
    
  HandleNewWindow (Title, Name)
  }
  
function PrintPageActivate (page)
  {
  window.parent.document.frames.main.document.open (page, "_self", "", true)
  }

function ShowPictureError()
  {
  document.title = "Error 404: Resource Cannot be Located"
  document.all.PrintTitle.innerHTML = "Error 404: Resource Cannot be Located"
  document.all.PrintTitle.className = ''
  document.all.WholeText.innerHTML = 'The page or resource that you are trying to download is not located on this server.  Please go to the <a href="index.htm">home page</a>.'

  alert ("Error: Resource Cannot be Located")
  }

function ShowPictureLoaded ()
  {
  var bookmark = GetBookmark (document.location.toString())
  if (bookmark == "")
    {
    ShowPictureError()
    return
    }

  ShowPictureActivate (bookmark)
  }

function PrintPageLoaded ()
  {
  var bookmark = GetBookmark (window.parent.document.location.toString())
  if (bookmark == "")
    {
    //ShowPictureError()
    return
    }

  PrintPageActivate (bookmark)
  }
  
var
  FocusedElement = null

function FocusIn()
  {
  FocusedElement = event.toElement
  }

function SetFocusToControl (To, Check)
  {
  if (To != FocusedElement)
    {
    for (var f = 0; f < Check.length; f++)
      {
      var control = Check[f]
      if ((control != null) && (control == FocusedElement))
        {
        return
        }
      }
    To.focus()
    }
  }

function Trim (s)
  {
  if (s == null)
    s = ''
  s = new String (s)

  while (s.substring(0,1) == ' ')
    {
    s = s.substring(1,s.length);
    }
  while (s.substring(s.length-1,s.length) == ' ')
    {
    s = s.substring(0,s.length-1);
    }
  return s;
  }

function HasTrimValue (control, message)
  {
  return CheckValue (control, Trim (control.value), message)
  }

function HasValue (control, message)
  {
  return CheckValue (control, control.value, message)
  }

function CheckValue (control, value, message)
  {
  var retval = value != ''

  if (!retval)
    {
    alert (message)
    control.focus()
    }

  return retval
  }
  
function FindTag (name, tag)
  {
  var tagarray
  tagarray = document.all.tags (tag)
  
  var f
  var temp
  for (f = 0; f < tagarray.length; f++)
    {
    temp = tagarray(f)
    if (temp.name == name)
      return temp
    }

  return null
  }
  
function SwapImage (name)
  {
  var item
  var temp
  
  item = FindTag (name, 'img')
  if (item)
    {
    temp = item.src
    item.src = item.dsrc
    item.dsrc = temp
    }
  }
  
function ToggleTreeVisibility (name)
  {
  var item

  // Show / hide children
  item = FindTag (name + '-div', 'div')
  if (item)
    item = item.style
  if (item)
    {
    if (item.display == 'none')
      item.display = 'block'
    else
      item.display = 'none'
    }
    
  // Change button pictures
  SwapImage (name + '-btn')
  // Change icon pictures
  SwapImage (name + '-icn')
  }
  
function ShowDiv (name)
  {
  ShowHideItem (name, 'div', 'block')
  }
  
function HideDiv (name)
  {
  ShowHideItem (name, 'div', 'none')
  }
  
function ToggleDivVisibility (name)
  {
  var item
  item = FindTag (name, 'div')
  if (item)
    item = item.style
  if (item)
    {
    if (item.display == 'none')
      item.display = 'block'
    else
      item.display = 'none'
    }
  }
  
function ShowHideItem (name, tag, showhide)
  {
  var item
  item = FindTag (name, tag)
  if (item)
    item = item.style
  if (item)
    item.display = showhide
  }
  
function ShowHideAll (test, set)
  {
  var tagarray
  tagarray = document.all.tags ('div')

  var f
  var temp
  var s
  var process
  for (f = 0; f < tagarray.length; f++)
    {
    temp = tagarray(f)
    s = temp.name

    if (s.substr (s.length - 4, 4) == '-div')
      {
      temp = temp.style
      process = temp.display == test

      if (process)
        {
        temp.display = set
        s = s.substr (0, s.length-4)
        // Change button pictures
        SwapImage (s + '-btn')
        // Change icon pictures
        SwapImage (s + '-icn')
        }
      }
    }
  }

function TreeExpandAll ()
  {
  // Show all
  ShowHideAll ('none', 'block')

  // Show/hide collapse all / expand all
  HideDiv ('expand-all')
  ShowDiv ('collapse-all')
  }
  
function TreeCollapseAll ()
  {
  // Hide all
  ShowHideAll ('block', 'none')

  // Show/hide collapse all / expand all
  HideDiv ('collapse-all')
  ShowDiv ('expand-all')
  }

function ToggleAdds (name, position)
  {
  var item
  item = FindTag (name, 'div')
  if (item)
    item = item.style
  var isshown = item.display == 'none'
  if (isshown)
    item.display = 'block'
  else
    item.display = 'none'
    
  var cookie = ExtractCookie ('visible', '111')
  cookie = cookie.slice (0, position) + (isshown ? '1' : '0') + cookie.slice (position+1)
  WriteCookie ('visible', cookie)
  }

function ExtractCookie (name, defaultvalue)
  {
  var cookie = new String (document.cookie)

  var re = new RegExp (name + '=([^;]+)', 'i')
  var r = cookie.match(re)
  var retval = RegExp.$1
  if (retval == '')
    {
    WriteCookie (name, defaultvalue)

    return defaultvalue
    }
  else
    return retval
  }
  
function WriteCookie (name, value)
  {
  var c = name + '=' + value + '; expires=Wed, 1 Jan 2020 00:00:00 UTC'
  document.cookie = c
  }
  
function HideAdds (name)
  {
  var item
  item = FindTag (name, 'div')
  if (item)
    item = item.style
  if (item)
    item.display = 'none'
  }
  
function ShowAds()
  {
  var cookie = ExtractCookie ('visible', '111')
  if (cookie.charAt(0) == '0')
    HideAdds ('random')
  if (cookie.charAt(1) == '0')
    HideAdds ('adds')
  if (cookie.charAt(2) == '0')
    HideAdds ('todo')
  }
  
function ShowSeeds (names)
  {
  ShowSeedsTag (names, 'div', 'block')
  }

function ShowSeedsTag (names, tag, value)
  {
  var loc = new String (document.location)
  if (loc.indexOf ('/tm.') == -1)
    {
    for (var f = 0; f < names.length; f++)
      {
      var item
      item = FindTag (names[f], tag)
      if (item)
        item = item.style
      if (item)
        item.display = value
      }
    }
  }

// Do nothing
function Unprotect()
  {
  }

/*

Hide / show popup div

PopupName = name of popup div
ref = reference element
align = 1 - left, 2 - right

*/
function TogglePopup (PopupName, ref, align)
  {
  var item, itemstyle, l, t, b, r

  item = FindTag (PopupName, 'div')
  if (item)
    {
    itemstyle = item.style
    if (itemstyle)
      {
      if (itemstyle.display == 'none')
        {
        l = 0
        t = 0
        b = ref.offsetHeight
        r = ref.offsetWidth
        while (ref)
          {
          if (ref.tagName == 'div')
            break
          l = l + ref.offsetLeft
          t = t + ref.offsetTop
          ref = ref.offsetParent
          }
        b = b + t
        r = r + l

        itemstyle.posTop = b
        itemstyle.display = 'block'
        if (align == 2)
          itemstyle.posLeft = r - item.offsetWidth
        else
          itemstyle.posLeft = l
        }
      else
        itemstyle.display = 'none'
      }
    }
  }

function UpdateReferrer()
  {
  var referrer = document.referrer
  
  if (referrer)
    {
    referrer = referrer.toLowerCase()
    if (referrer.indexOf ('inetgardens.com') == -1)
      {
      pageref.document.location = pageref.document.location + '&referrer=' + referrer
      }
    }
  }
  
