// JavaScript Document


function setNav()
{
//Stores all a tags in which the href matches the URL
var matchesFound = new Array();

//Stores all a tags that from matchesFound that match the section of the left hand nav
var sectionMatches = new Array();

//Get full URL
url = String(location);

//Get URL, only capturing after the domain
path = String(location.pathname);

//Substring the path to remove the leading /
path = path.substring(1);

//Split the path on / to get levels
levels_array = path.split("/");

//If the second level is blank meaning the first level had a trailing /, you are on the root of the page group
if ((levels_array.length==1) && (document.getElementById(levels_array[0])))
{
document.getElementById(levels_array[0]).className="current";
}
else if (document.getElementById(levels_array[1]))
{
   document.getElementById(levels_array[1]).className="current";
}
else
{
if (document.getElementById(levels_array[0]))
   document.getElementById(levels_array[0]).className="current";
}

//Process the left hand navigation only if it exists
if (document.getElementById("secondLevelNav")){
  link = document.getElementById("secondLevelNav").getElementsByTagName("a");
  for (i=0;i<link.length;i++){
	//Check if href is inside of url to determine a match
	if (url.indexOf(link[i].href) != -1){
	  matchesFound.push(link[i]);
    }
  }
//alert(matchesFound.length);

//Get the parent id of the lowest matched link to determine which section we are in
str=matchesFound[matchesFound.length-1].parentNode.id;
//If there is a - in the id, substring up to it
if (str.indexOf("-")!=-1)
str=str.substring(0,str.indexOf("-"));

//Loop through matchesFound to make sure they are all in the same section has the lowest level link
//ie https://www.navsup.navy.mil/navsup/ourteam/comfiscs
//and https://www.navsup.navy.mil/navsup/ourteam/comfiscs/prod_serv/atac/contact
//are on the same page.  COMFISC is really a parent link but shown in the left nav 
//https://www.navsup.navy.mil/navsup/ourteam/comfiscs/prod_serv/atac/contact
for(i=0;i<matchesFound.length;i++){
  current_str=matchesFound[i].parentNode.id;
  //If there is a - in the id, substring up to it
  if (current_str.indexOf("-")!=-1)
  current_str=current_str.substring(0,current_str.indexOf("-"));

 // alert(current_str+"-"+str);
  if (current_str==str)
  sectionMatches.push(matchesFound[i]);
}
//alert(sectionMatches.length);

if (sectionMatches.length==1)
   sectionMatches[0].parentNode.className="current";
if (sectionMatches.length==2){
  sectionMatches[0].parentNode.className="currentSection";
  sectionMatches[1].parentNode.className="current";
}
if (sectionMatches.length==3){
  sectionMatches[0].parentNode.className="currentSection";
  sectionMatches[1].parentNode.className="currentSubSection";
  sectionMatches[2].parentNode.className="current";
}

//Set the last (lowest) match found to current, after checking if there is at least on match in the left navigation
/*
if (matchesFound.length>0)
{
  matchesFound[matchesFound.length-1].parentNode.className="current";

  //Get the parent id of the lowest matched link
  str=matchesFound[matchesFound.length-1].parentNode.id;

  //if the id has a "-" then the link is a lower level, so we must set the current section
  if (str.indexOf("-")!=-1)
  {
    str=str.substring(0,str.indexOf("-"));
    document.getElementById(str).className="currentSection";
  }
}

*/
} //End if for processing the left hand navigation
} //End function
setNav();
