/* This script was created by Ian Lloyd (http://lloydi.com/). I'd appreciate it 
if you could keep this comment in the script if you choose to use it (albeit 
in an amended form. The original blog entry relating to this can be found here: 
http://tinyurl.com/p9tqb.  The addEvent function comes courtesy of Scott 
Andrew: http://tinyurl.com/qcmrd */

/*function addEvent(elm, evType, fn, useCapture){
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}else if (elm.attachEvent){
		return elm.attachEvent('on' + evType, fn);
	}else{
		elm['on' + evType] = fn;
	}
}*/

function addPrintLinks(){
	
	// Add print_section class names to all divs that have the "section" in their id
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++){
		if (divs[i].id.indexOf("section") != -1){
			divs[i].setAttribute("className", "print_section");
			divs[i].setAttribute("class","print_section");
		}
	}
	// Add subm_01, or subm_02 class names to all spans that have "sub" in their id, and "-".
	var spans = document.getElementsByTagName("span");
	for (i=0;i<spans.length;i++){
		if(spans[i].id.indexOf("sub") != -1){
			if(spans[i].id.indexOf("-") == -1){
				spans[i].setAttribute("className", "subm_01");
				spans[i].setAttribute("class","subm_01");
			}else{
				spans[i].setAttribute("className", "subm_02");
				spans[i].setAttribute("class","subm_02");
			}
		}
	}

	var el = document.getElementsByTagName("span");
	for (i=0;i<el.length;i++){
		if (el[i].className.indexOf("subm") != -1){
			if (el[i].className == "subm_01"){
				var parentDiv = el[i].parentNode; 
				var newLinkText = document.createTextNode("Print");
				var newLink = document.createElement("a");
						newLink.setAttribute("href","#");
						newLink.setAttribute("id","printbut_" + parentDiv.id);
						newLink.appendChild(newLinkText);
						newLink.onclick = togglePrintDisplay;
						newLink.onkeypress = togglePrintDisplay;
				var newSpan = document.createElement("span");		
						newSpan.setAttribute("className","printbtn");
						newSpan.setAttribute("class","printbtn");
						newSpan.appendChild(newLink);
				var newDiv = document.createElement("div");
						newDiv.setAttribute("className","ecSub");
						newDiv.setAttribute("class","ecSub");
						newDiv.appendChild(newSpan);
				var newText = document.createTextNode("Expand All");
				var newLink = document.createElement("a");
						newLink.setAttribute("href","#");
						newLink.appendChild(newText);
						newLink.setAttribute("id", "but_"+el[i].id);
						newLink.onclick = function(){
							 expandCollapseSubMenus(this);
						}
						newLink.onkeypress = function(){
							 expandCollapseSubMenus(this);
						}
				var newSpan2 = document.createElement("span");
						newSpan2.appendChild(newLink);
						newSpan2.setAttribute("id", "but_"+el[i].id);
						newSpan2.setAttribute("className","butc");
						newSpan2.setAttribute("class","butc");
						newDiv.appendChild(newSpan2);
				el[i].insertBefore(newDiv, el[i].childNodes[0]);
			}
			if (el[i].className == "subm_02"){
				var parentDiv = el[i].parentNode; 
				var newLinkText = document.createTextNode("Print");
				var newLink = document.createElement("a");
						newLink.setAttribute("href","#");
						newLink.setAttribute("id","printbut_" + parentDiv.id);
						newLink.appendChild(newLinkText);
						newLink.onclick = togglePrintDisplay;
						newLink.onkeypress = togglePrintDisplay;
				var anchorLink = document.createElement("a");
				    anchorLink.setAttribute("href", "#"+parentDiv.id);
				var newSpan = document.createElement("span");		
						newSpan.setAttribute("className","printbtn");
						newSpan.setAttribute("class","printbtn");
						newSpan.appendChild(newLink);
				var newDiv = document.createElement("div");
						newDiv.setAttribute("className","ecSub");
						newDiv.setAttribute("class","ecSub");
						newDiv.appendChild(newSpan);
						newDiv.appendChild(anchorLink);
				el[i].insertBefore(newDiv, el[i].childNodes[0]);
			}
		}
		
		if(el[i].id.indexOf("but") != -1){
			el[i].setAttribute("className","butc");
			el[i].setAttribute("class","butc");
			
			el[i].onclick = function(){
				  SwitchMenu(this);
			}
			el[i].onkeypress = function(){
				  SwitchMenu(this);
			}
		}
	}
}


function togglePrintDisplay(e){
	var whatSection = this.id.split("_");
	whatSection = whatSection[1];
	var el = document.getElementsByTagName("div");
	
	for(i=0;i<el.length;i++){
		if(el[i].id == "leftColumn"){
			el[i].setAttribute("className","print_section leftColumn_noprint");
			el[i].setAttribute("class","print_section leftColumn_noprint");
		}
		if(el[i].className == "faqBox"){
			el[i].setAttribute("className","faqBox rightColumn_print ");
			el[i].setAttribute("class","faqBox rightColumn_print ");
		}
		if(el[i].className.indexOf("section")!=-1){
			if(this.id.indexOf("-") == -1){
				el[i].removeAttribute("className");
				if(el[i].id==whatSection || el[i].id.indexOf(whatSection) != -1){
					//show only this section for print
					el[i].setAttribute("className","print_section print");
					el[i].setAttribute("class","print_section print");
				}else{
					//hide the sections from print-out
					el[i].setAttribute("className","print_section noprint");
					el[i].setAttribute("class","print_section noprint");
				}
			}else{
				el[i].removeAttribute("className");
				if ((el[i].id.indexOf("-") != -1 && el[i].id==whatSection) || (el[i].id.indexOf("-") == -1 && whatSection.indexOf(el[i].id) != -1)){
					//show only this section for print
					el[i].setAttribute("className","print_section print");
					el[i].setAttribute("class","print_section print");
				}else{
					//hide the sections from print-out
					el[i].setAttribute("className","print_section noprint");
					el[i].setAttribute("class","print_section noprint");
				}
			}
		}
	}
	
	if(window.event){
		window.event.returnValue = false;
		window.event.cancelBubble = true;
  }else if(e){
		e.stopPropagation();
		e.preventDefault();
	}
	window.print();
}


/*function printAll(e)
{
var el = document.getElementsByTagName("div");
for (i=0;i<el.length;i++)
	{
	if (el[i].className.indexOf("print_section")!=-1)
		{
		el[i].setAttribute("className","print_section print");
		el[i].setAttribute("class","print_section print");
		}
	}
if (window.event) 
	{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
	} 
else if (e) 
	{
	e.stopPropagation();
	e.preventDefault();
	}
window.print();
}*/
//addEvent(window, 'load', addPrintLinks, false);