// tabUtils.js VERSION 1.0

// This may not work, there was initial failure using it in Firefox but
// it may have been due to other problems.
function include2 (scriptFilename)
  {
    var docHead = document.getElementsByTagName('head').item(0);

    var scriptElem = document.createElement('script');
    scriptElem.setAttribute('language', 'javascript');
    scriptElem.setAttribute('type', 'text/javascript');
    scriptElem.setAttribute('src', scriptFilename);

    docHead.appendChild(scriptElem);
  }

function include (scriptFilename)
  {
    document.write('<script language="javascript" type="text/javascript"');
    document.write(' src="' + scriptFilename + '">');
    document.write('</script>');
  }

// Include the general utilities javascript file
include("../../utilities.js");

function initializeTabSettings ()
  {
    // First check to see if we have an explicit parameter.  If we do
    // then use that to determine which tab to select.
    var params = parseQueryString(window.location);

    // Is there a tab parameter?  Select it!
    if (typeof(params.tab) != 'undefined')
      {
        selectTab(params.tab);
        return; // We're done.
      }

    // If we didn't have an explicit parameter, check to see if we
    // have any memory of the last tab visited.  If we do, visit it!
    var tabCookie = getCookie("tab");
    if (null != tabCookie)
      {
        selectTab(tabCookie);
      }
  }


// This method shows the selected tab and hides all others. It does
// this by walking the tree looking for IDs and class names with the
// expected patterns.
function selectTab (tabName)
  {
    var tabsTable = document.getElementById("tabs");

    // The tabsTable is where we will search for tabs to change
    // around.

    // Find all sub-elements of tabsTable
    var subElems = tabsTable.getElementsByTagName("*");

    for (i=0; i < subElems.length; i++)
      {
        var nextElem = subElems[i];

        // See if they have a class of "selectedTab" or "normalTab"
        if (nextElem.className == "selectedTab" || nextElem.className == "normalTab")
          {

            // See if they have an ID that starts with tabName (if
            // so set it to selectedTab, else set to normalTab.
            var theID = nextElem.id;
            if (theID.substring(0, tabName.length) == tabName)
              {
                nextElem.className = "selectedTab";

                // Remember which tab we are on for one day.
                // We set it way down in here because we want to make
                // sure there really was a tab to select first
                setCookie("tab", tabName, 1);
              }
            else
              nextElem.className = "normalTab";

            // Find the span named "raquo" and either hide or display it.
            if (nextElem.tagName == "H2")
              {
                var spans = nextElem.getElementsByTagName("span");
                for (j=0; j < spans.length; j++)
                  {
                    if (spans[j].className == "raquo")
                      {
                        spans[j].style.display = (  nextElem.className == "selectedTab"
                                                  ? "none"
                                                  : "inline");
                      }
                  }
              }

            // TODo: Find the content divs and hide them all except for the one
            //       with the id of tabName+"_contents".
            var thisTabName = theID.substring(0, theID.length-1);
            var contents = document.getElementById(thisTabName+"_contents");

            if (contents != null)
              {
                contents.className
                    = (  nextElem.className == "selectedTab"
                       ? "selectedTabContents"
                       : "normalTabContents");
              }
          }
      }
  }
