// JavaScript Document

function initPage()
	{
		//adding javascript functionality to the toc
		var allChildren = document.getElementsByName("plusImg");
		for (i = 0; i < allChildren.length; i++){
	        el = allChildren[i];
			var serv = (el.src.substring(0, el.src.length - 10));
			var pm = (el.src.substring(el.src.length - 10, el.src.length));
	   		if (pm == "spacer.gif"){
				el.src = serv + "p.gif";
				el.height="9";
				el.width="9";
	   		} 
	   	}
		
		//hiding the children of the base nodes
		var rootUL = document.getElementById("ulbase");
		
		var baseChildren = rootUL.getElementsByTagName("ul");
			
		for (e = 0; e < baseChildren.length; e++){
        		var bc = baseChildren[e];
				bc.style.display="none";
		}
		
		//removing the plus sign from last node
		var remPlusChildren = document.getElementsByTagName("li");
		for(t=0;t<remPlusChildren.length;t++){
			var pel = remPlusChildren[t];
			
			remPlusBaseChildren = pel.getElementsByTagName("ul");
			
			if(remPlusBaseChildren.length == 0){
				imgTags = pel.getElementsByTagName("img");
				pel.className="noChild";
				for(u=0;u<imgTags.length;u++){
				var pserv = (imgTags[u].src.substring(0, imgTags[u].src.length - 5));
				
				imgTags[u].src = pserv + "spacer.gif";
				imgTags[u].height="0";
				imgTags[u].width="0";
				}
			}
		}
  	}
	
	function togglePlus(plusminus)
	{		
		var tserv = (plusminus.src.substring(0,plusminus.src.length - 5));
		var tpm = (plusminus.src.substring(plusminus.src.length - 5,plusminus.src.length));
	   	if (tpm=="p.gif"){
			plusminus.src= tserv + "m.gif";
	    } 
	    else{
	    	plusminus.src= tserv + "p.gif";
		}
	        
	   	base = plusminus.parentNode;
	  	var tallChildren = base.getElementsByTagName("ul");
		var tel = tallChildren[0];
	   	if (tel.style.display!="block"){
			tel.style.display="block";
	   	} 
	   	else{
			tel.style.display="none";
	   	}
	}
	
	function highlightLocation()
	{
		var detect = navigator.userAgent.toLowerCase();
		var locator;
		if(detect.indexOf("msie") > -1)
		{
			locator = new PageLocator("document.frames['ajaxnav'].getLocation()", "?hash=");		
		}
		else
		{
			locator = new PageLocator("window.location.href", "#");
		}
		
		var hash = parseInt(locator.getHash());
		var rootLi = document.getElementById("ulbase").getElementsByTagName("li")[0];
		var i = 1;
			
		while (rootLi) {
			var navContentsHd = "";
			// First we get rid of the <span> tag if it exists
			if(rootLi.innerHTML) {
				
				if(rootLi.innerHTML.toLowerCase().indexOf("navcontentshd") > -1)
				{
					navContentsHd = true;
				}
				
				if (rootLi.innerHTML.toLowerCase().indexOf("<span class=\"there\">") > -1 ) {			
					var anchorNode = document.createElement("a");
					
					if(detect.indexOf("msie") > -1)
					{
						anchorNode.setAttribute("href", "javascript:document.getElementById('ajaxnav').setAttribute('src', 'mock-page.asp?hash=" + i + "');");
					}
					else
					{
						anchorNode.setAttribute("href", "default.html#" + i);
					}
						
					var span = rootLi.getElementsByTagName("span")[0];
					var textOfAnchor = document.createTextNode(span.innerHTML);
					anchorNode.appendChild(textOfAnchor);
					
					rootLi.replaceChild(anchorNode, span);
				}
			}
			
			// Then move to the next sibling
			rootLi = rootLi.nextSibling;
			
			// Make sure our next sibling is an element (get rid of whitespace nodes)
			while (rootLi && rootLi.nodeType !=1)
			{
				rootLi = rootLi.nextSibling;
			}
								
			// increment our index
			if(navContentsHd != true)
			{
				i=i+1;
			}
		}
			
		rootLi = document.getElementById("ulbase").getElementsByTagName("li")[0];
		i = 1;
		
		while (rootLi) {
			navContentsHd = "";
			// Then we check to see if our nav element corresponds to the page we are on
			if (hash == i) {
				// We have found our location
				var anchorNode = rootLi.getElementsByTagName("a")[0];
				if(anchorNode)
				{
					var span = document.createElement("span");
					span.setAttribute("class", "there");
					var textOfSpan = document.createTextNode(anchorNode.innerHTML);
					span.appendChild(textOfSpan);
				
					rootLi.replaceChild(span, anchorNode);
				}
			}
				
			if(rootLi.innerHTML.toLowerCase().indexOf("navcontentshd") > -1)
			{
				navContentsHd = true;
			}
				
			// Then move to the next sibling
			rootLi = rootLi.nextSibling;
			
			// Make sure our next sibling is an element (get rid of whitespace nodes)
			while (rootLi && rootLi.nodeType !=1)
			{
				rootLi = rootLi.nextSibling;
			}
			// increment our index
				
			if(rootLi)
			{
				if(rootLi.innerHTML.toLowerCase().indexOf("navcontentshd") <= -1)
				{
					i=i+1;
				}
			}
		}
	}
	
	function hijaxBackNext()
	{
		if (document.getElementsByName("content")) 
		{
						
			var templinks = document.getElementsByName("content");
			var links = templinks[0].getElementsByTagName("A");
			//alert(templinks.length);
			for (var i=0; i < links.length; i++) {
				var href = links[i].getAttribute("href");
				if (href) 
				{
					var detect = navigator.userAgent.toLowerCase();
					if(href.indexOf("tst.energyplan.qp.gov.bc.ca") > -1 | href.indexOf("142.36.155.106") > -1 | href.indexOf("energyplan.gov.bc.ca") > -1 | href.indexOf("http://") == -1)
					{
						if(href.indexOf("mailto") == -1)
						{
							if(!links[i].getAttribute("target"))
							{
								var hash
								if (href.indexOf("hash=") > 0) {
									hash = href.substr(href.indexOf("hash=")+5);
								} else {
									hash = href;
								}
								if (hash.indexOf("default.htm") < 0) 
								{
									
									if (hash.indexOf('a') > -1) 
									{
										hash = hash.split('a')[1];
									}
									
									if(hash.indexOf("#") >-1)
									{
										hash = hash.replace("#","a");
									}
									
									if(detect.indexOf("msie") > -1) 
									{
										links[i].setAttribute("href", "javascript:document.getElementById('ajaxnav').setAttribute('src', 'mock-page.asp?hash=" + hash + "');");
									} 
									else 
									{
										links[i].setAttribute("href", "#" + hash);
									}
								}
							}
						}
					}
				}
			}
		}	
	}