// Code from top of file to beginning of TOC utility functions executes immediately.
//   If this is a topic file and not in a frameset reload in this collection's frameset
//   If this is a frameset, populate and load it, and declare variables to be used
//     in orienting TOC to current topic

   if (filetype=="topic" && top.location == self.location)  // if page is a doc topic page and not in its frameset
   {
    if (document.referrer == "")
    {
     var newlocation = framesetpath + "?" + window.location.pathname + window.location.hash;
    }
    else
    {
     var newlocation = framesetpath + "?" + window.location.pathname + "&" + document.referrer + window.location.hash;
    }

    top.location.replace(newlocation);

   }

   if (filetype=="frameset") // if page is a frameset file
   {
    // start by declaring variables to be used in orienting TOC
    // justclicked will be processed below.
    
    var last_clicked = "unassigned";
    var just_clicked = "unassigned";
    var toc_state = "";
    var tocframeindexloaded = false;
    var tocframedivdisplaystates = new Array();

    // process query string, extract referrer, reassemble query string, determine URL to load in content_frame

    var queryarray = window.location.search.split("\&");
    
    var real_referrer = "";

    for (i = 1; i < queryarray.length; i++)
    {
     if (i == 1)
     {
      real_referrer = queryarray[i];
     }
     else
     {
      real_referrer = real_referrer + "&" + queryarray[i];
     }
    }

    if (real_referrer === undefined || real_referrer == "undefined" || real_referrer == "")
    {
     real_referrer = document.referrer;
    }

    var cleanpathname = queryarray[0].replace(/\?/,"");
    
    var requestedcontentfilename = cleanpathname + window.location.hash;

//window.alert("requestedcontentfilename = " + requestedcontentfilename);    

    if (requestedcontentfilename != "")
    {
     contentfilename = requestedcontentfilename;
     last_clicked = requestedcontentfilename;
    }
    else
    {
     last_clicked = "newly_loaded_frameset";
    }

    // assemble HTML for the frameset and write it into the document stream

    var documentstring =

    "<frameset rows=\"128,*\">" +
    " <frame scrolling=\"no\" Src=\"topbanner.html\" name=\"upper_frame\" frameborder=0 title=\"" + titlestring + "\">" +
    "  <frameset Cols=\"30%,*\">" +
    "   <frame scrolling=\"auto\" Src=\"" + tocfilename + "\" name=\"toc_frame\" marginheight=4 marginwidth=6 frameborder=0 title=\"Table of Contents\">" +
    "   <frame scrolling=\"auto\" Src=\"" + contentfilename + "\" name=\"content_frame\" marginheight=4 marginwidth=6 frameborder=0 title=\"Document Frame\">" +
    " </frameset>" +
    "</frameset>";

    document.write(documentstring);
   }


// If we get this far into the file, then load utility functions for TOC operation

   function navtabs(tabtype)
   {

    var navtabstring = 
  
    "<table id=\"navlinks\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
    "<tr id=\"navlinkitems\" class=\"navfrm\">";
  
    if (tabtype == "contents")
    {
     navtabstring = navtabstring +
      
     "<td id=\"navitemcontents\" width=\"50%\" class=\"activebg\" align=\"center\">" +
     "<span>Contents</span>" +
     "</td>" +
      
     "<td id=\"navitemindex\" width=\"50%\" align=\"center\" " +
     "onMouseOver=\"topNavHover(this.id)\" onMouseOut=\"topNavHoverOff(this.id)\" " +
     "onclick=\"savetocstate\(\)\;top\.toc_frame\.location\.href=\'helpindex.html\'\;\"\>" +
     "<span>Index</span>" +
     "</td>";    
    }
    
    if (tabtype == "contents_no_index")
    {
     navtabstring = navtabstring +
      
     "<td colspan=\"2\" id=\"navitemcontents\" class=\"activebg\" align=\"center\">" +
     "<span>Contents</span>" +
     "</td>";
    }
    
    if (tabtype == "index")
    {
     navtabstring = navtabstring +
      
     "<td id=\"navitemcontents\" width=\"50%\" align=\"center\"" +
     "onMouseOver=\"topNavHover(this.id)\" onMouseOut=\"topNavHoverOff(this.id)\" " +
     "onclick=\"top\.toc_frame\.location\.href=\'helptoc.html\'\;\"\>" +
     "<span>Contents</span>" +
     "</td>" +
      
     "<td id=\"navitemindex\" width=\"50%\" class=\"activebg\" align=\"center\">" +
     "<span>Index</span>" +
     "</td>";    
    }

    navtabstring = navtabstring +

    "</tr>" +
    "<tr>" +
    "<td><img src=\"/includes_content/images/global_nav/nav_inactive\.gif\" " +
    "width=\"100%\" height=\"3\"></td>" +
    "<td><img src=\"/includes_content/images/global_nav/nav_inactive\.gif\" " +
    "width=\"100%\" height=\"3\"></td>" +
    "</tr>" +
    "</table>";

    document.write(navtabstring);
   
   }


   function toggletoc(div_id)
   {

    // divid = tocid_x for the div element we are going to show or hide
    // then we need to swap the image for this element which is always going to be tocimg_x

    tocsection = document.getElementById(div_id);

    // deals with the case of the very first time the link in clicked

    if (tocsection.style.display === undefined || tocsection.style.display == "undefined" || tocsection.style.display == "")
    {
     tocsection.style.display = "none";
    }

    var img_id_number = div_id.match(/[0-9]+$/)[0];
    var img_id = "tocimg_" + img_id_number;

    tocicon = document.getElementById(img_id);

    if (tocsection.style.display == "none")
    {
     // currently collapsed, so expand it.

     tocsection.style.display = "block";
     tocicon.src = "/access/helpdesk/help/toc_section_nonempty_open.gif";
    }
    else 
    {
     // currently expanded, so collapse it.

     tocsection.style.display = "none";
     tocicon.src = "/access/helpdesk/help/toc_section_nonempty_closed.gif";
    }

   }


   function registerlastclicked(tocitemid)
   {

    // allow this function to run only if the TOC is loaded, not if the Index is loaded
    // it is called from Index links but the calls are extraneous
 
    if (top.toc_frame.filetype == "toc")
    {
 

     // un-highlight the currently highlighted item in the TOC 

     if(top.last_clicked.search(/tocitem_/) != -1)
     {
      var currentlyclickedpara = document.getElementById(top.last_clicked);
      var currentlyclickedanchor = currentlyclickedpara.getElementsByTagName("a")[0];
      var currentlyclickedspan = currentlyclickedanchor.getElementsByTagName("span")[0];
      currentlyclickedspan.style.backgroundColor = "#ffffff";
     }

     // highlight the clicked item in the TOC 

     var newlyclickedpara = document.getElementById(tocitemid);
     var newlyclickedanchor = newlyclickedpara.getElementsByTagName("a")[0];
     var newlyclickedspan = newlyclickedanchor.getElementsByTagName("span")[0];
     newlyclickedspan.style.backgroundColor = "#d7d7d7";

     // assign the ID of the clicked item to top.last_clicked

     top.last_clicked = tocitemid;

    }

   }
 

   // orienttoc can be called in two distinct ways:
   //
   // first, it is called when a file is newly loaded in the content_frame
   // when it is called in this way, it assigns an onclick event to all the <a> elements 
   // in the content frame, which allows them to call this same function
   // in the second way...
   //
   // which is that it is called whenever a link is clicked in a file that is already-loaded
   // in the content frame, in this case, its functionality is limited to link targets on the 
   // same page. otherwise, the TOC is oriented when the new target page is loaded.
   //
   // the distinction is made by code that detects whether an argument has been passed in
   // with the function call. if not, it behaves in the first way. if so, the second way.
 
   function orienttoc(clickedlinkhref)
   {
 
    // allow this function to run only if the TOC is loaded, not if the Index is loaded
 
    if (toc_frame.filetype == "toc")
    {

     // establish necessary variables

     var contentpathname = content_frame.location.pathname;
     var contentpathnamearray = contentpathname.split("/");
     var contentframefilename = contentpathnamearray[(contentpathnamearray.length - 1)];
     var contentframeanchor = "unassigned";

     // if this function has been called by the body.onload function, 
     // i.e. is called without any arguments, 
     // assign an onclick event to each a element to execute this function
     //
     // also try to populate the content frame anchor value variable
     
     if (clickedlinkhref === undefined || clickedlinkhref == "undefined" || clickedlinkhref == "")
     {
     
      var contentframeanchorelements = content_frame.document.getElementsByTagName("a");
      
      for (i = 0; i < contentframeanchorelements.length -1; i++)
      {
       
       // see if the onclick event is already defined, as for expanders
       // if so, don't stomp on the existing event
       if (contentframeanchorelements[i].onclick == null)
       {
        contentframeanchorelements[i].onclick = function() { top.orienttoc(this.href); };
       }
      }
      
       if(content_frame.location.href.search(/#[0-9 a-z A-Z _ \-]+/) != -1)
       {
        contentframeanchor = content_frame.location.href.match(/#[0-9 a-z A-Z _ \-]+/)[0];
       }

     }

     else
     {

      // try to populate the content frame anchor value variable
      // in case the target of the link is inside the currently loaded file 

      if(clickedlinkhref.search(/#[0-9 a-z A-Z _ \-]+/) != -1)
      {
       contentframeanchor = clickedlinkhref.match(/#[0-9 a-z A-Z _ \-]+/)[0];
      }

      // now check to see if the target of this link is outside the currently loaded file
      // if so, end execution of this function right here and let the target file's 
      // body.onload event handler (top.orienttoc) deal with the situation
      
      var clickedlinkhrefarray = clickedlinkhref.split("/");
      var clickedlinkfilename = clickedlinkhrefarray[(clickedlinkhrefarray.length - 1)];
      clickedlinkfilename = clickedlinkfilename.match(/.*\.html/)[0];
 
      if (contentframefilename != clickedlinkfilename)
      {
       return;
      }

     }

     // if this function has not been terminated by the code immediately above, 
     // that is, the file has either been newly loaded or a link has been clicked
     // the target of which is an anchor within the same file, then
     // attempt to complete the mission of the function

     // but before proceeding any further, check to see if the currently loaded file is 
     // a refpage, which is indicated by the presence of a SYNCHTO: comment in the file
     // if such a comment is found, then redefine the contentframefilename and
     // contentframeanchor variables accordingly, superseding the assignments made above.
     // otherwise proceed with these two values as defined above.
     
     var contentframedocbody = content_frame.document.getElementsByTagName("body")[0];
     var contentframebodychildren = contentframedocbody.childNodes;

     for (i = 0; i < contentframebodychildren.length; i++)
     {
      if (contentframebodychildren[i].nodeType == 8 && contentframebodychildren[i].nodeValue.search(/SYNCHTO/) != -1)
      {
       var contentframesynchtovalue = contentframebodychildren[i].nodeValue.match(/[a-z0-9_\-\.]+/)[0];
       contentframefilename = contentframesynchtovalue;
       contentframeanchor = "unassigned";
      }
     }
     
     // now declare some variables to use in processing the anchor elements in the toc_frame

     var tocframeanchorelements = toc_frame.document.getElementsByTagName("a");
     var tocframedivelements = toc_frame.document.getElementsByTagName("div");
     var anchorhreffilename = "unassigned";
     var anchorhrefanchor = "unassigned";
     var tocframeanchorelementmatch = "unassigned";
     var tocframetocitemid = "unassigned";
     
     // next, try to determine the toc item id of the newly loaded file

     // if content frame's url contains no anchor, then do the loop in forward order
     
     if (contentframeanchor == "unassigned")
     {

      for (i = 0; i < tocframeanchorelements.length - 1; i++)
      {
       anchorhref = tocframeanchorelements[i].href;
       anchorhrefarray = anchorhref.split("/");
       anchorhreffilename = anchorhrefarray[(anchorhrefarray.length - 1)];
       anchorhreffilename = anchorhreffilename.match(/.*\.html/)[0];
   
       if (anchorhreffilename == contentframefilename)
       {
        tocframeanchorelementmatch = tocframeanchorelements[i];
        break;
       }

      }

     }
 
     // but if the content frame's url does contain an anchor, then do the loop in reverse order

     else
     {
      
      for (i = tocframeanchorelements.length - 1; i > 0; i--)
      {
       anchorhref = tocframeanchorelements[i].href;
       anchorhrefarray = anchorhref.split("/");
       anchorhreffilename = anchorhrefarray[(anchorhrefarray.length - 1)];
       
       // now attempt to assign the anchor value to the var anchorhrefanchor
       
       if(anchorhreffilename.search(/#[0-9 a-z A-Z _ \-]+/) != -1)
       {
        anchorhrefanchor = anchorhreffilename.match(/#[0-9 a-z A-Z _ \-]+/)[0];
       }

       anchorhreffilename = anchorhreffilename.match(/.*\.html/)[0];
   
       if (anchorhreffilename == contentframefilename && anchorhrefanchor == contentframeanchor)
       {
        tocframeanchorelementmatch = tocframeanchorelements[i];
        break;
       }
       
       else if (anchorhreffilename == contentframefilename && anchorhrefanchor == "unassigned")
       {
        tocframeanchorelementmatch = tocframeanchorelements[i];
        break;
       }

      }
      
     }

     // unhighlight the currently clicked item, highlight the newly loaded item,
     // and open all div elements necessary to display the newly loaded item in the TOC.
     // then reset top.last_clicked to the tocitemid of the newly loaded item.

     // deal with the fact that tocframeanchorelement has no meaningful value when frameset is first loaded

     if ( tocframeanchorelementmatch != "unassigned" )
     {
      tocframetocitemid = tocframeanchorelementmatch.onclick.toString();
      tocframetocitemid = tocframetocitemid.match(/tocitem_[0-9]+/)[0];

      // un-highlight the currently highlighted item in the TOC 

      if(top.last_clicked.search(/tocitem_/) != -1)
      {
       var currentlyclickedpara = toc_frame.document.getElementById(top.last_clicked);
       var currentlyclickedanchor = currentlyclickedpara.getElementsByTagName("a")[0];
       var currentlyclickedspan = currentlyclickedanchor.getElementsByTagName("span")[0];
       currentlyclickedspan.style.backgroundColor = "#ffffff";
      }
   
      // highlight the item corresponding to the file displayed in the content_frame

      var newlyclickedspan = tocframeanchorelementmatch.getElementsByTagName("span")[0];
      newlyclickedspan.style.backgroundColor = "#d7d7d7";

      // open all div's necessary to display the newly loaded item in the TOC 

      var tocframetocitempara = toc_frame.document.getElementById(tocframetocitemid);
      var tocframeparent = tocframetocitempara.parentNode;
    
      while (tocframeparent.tagName == "DIV" && tocframeparent.id != "navbody")
      {
       // open the parent div element
       
       tocframeparent.style.display = "block";

       // change the expander icon for that div to the down arrow
       
       var parent_img_id_number = tocframeparent.id.match(/[0-9]+$/)[0];
       var parent_img_id = "tocimg_" + parent_img_id_number;
       var parent_toc_icon = toc_frame.document.getElementById(parent_img_id);
       parent_toc_icon.src = "/access/helpdesk/help/toc_section_nonempty_open.gif";
       
       // move up the containment tree one level
       
       tocframeparent = tocframeparent.parentNode;
      }
    
      // if the highlighted item has scrolled out of view, 
      // scroll the toc frame so that the item's parent is in view
  
      var tocframescrolltop = top.toc_frame.document.body.scrollTop;
      var tocitemscrolltop = tocframetocitempara.offsetTop;
  
      if (tocitemscrolltop > tocframescrolltop + toc_frame.document.body.clientHeight ||
          tocitemscrolltop < tocframescrolltop)
      {
       top.toc_frame.scrollTo(0,tocitemscrolltop);
      }
    
      // save the newly highlighted item's ID in top.last_clicked

      top.last_clicked = tocframetocitemid;
    
     }

     // lastly, if the above procedure fails to identify a TOC node as 
     // corresponding to the item in the content_frame, 
     // then unhighlight any currently highlighted item anyway

     else
     {
      if(top.last_clicked.search(/tocitem_/) != -1)
      {
       var currentlyclickedpara = toc_frame.document.getElementById(top.last_clicked);
       var currentlyclickedanchor = currentlyclickedpara.getElementsByTagName("a")[0];
       var currentlyclickedspan = currentlyclickedanchor.getElementsByTagName("span")[0];
       currentlyclickedspan.style.backgroundColor = "#ffffff";
      }
     }

    }
 
   }


   function savetocstate()
   {
 
    // called from the Index link in helptoc.html's Contents-Index buttons
 
    top.tocframeindexloaded = true;
    var tocframedivelements = top.toc_frame.document.getElementsByTagName("DIV");
  
    for (i = 0; i < tocframedivelements.length; i++)
    {
     top.tocframedivdisplaystates[i] = tocframedivelements[i].style.display;
    }
 
   }
 
   function restoretocstate()
   {
 
    // called from helptoc.html's body.onload handler
 
    if (top.tocframeindexloaded)
    {
     var tocframedivelements = top.toc_frame.document.getElementsByTagName("DIV");

     for (i = 0; i < tocframedivelements.length; i++)
     {
      tocframedivelements[i].style.display = top.tocframedivdisplaystates[i];
     }

    top.orienttoc();

    }
   
    top.tocframeindexloaded = false;
 
   }
   
   function clickDynamic(obj, target_url, tracking_code) {
	   // to dynamically add tracking script to links

    var pos=target_url.indexOf("?");
    if (pos<=0)
    { 
    var linkComponents = target_url + tracking_code;
    obj.href=linkComponents;
    } 
 
   }
   
   function hideNav() {
	//alert("here");
document.getElementById("frameless_top").style.display = "none";
	document.getElementById("frameless_contents").style.display = "none";
	 top.orienttoc();

}
