function openPrintDialog() {
	window.print();
	return false;
}
function emailThisPage() {
	/* Sets up E-mail This Page link, inserting a subject and URL into a message. */
	var el = document.getElementById("emailThis");
	var eSubject = 'Check out this page from LFCE.org';
	var eBody = 'Check out this page from LFCE.org: ' + location.href;
	el.href = 'mailto:?subject=' + escape(eSubject) + '&body=' + escape(eBody);
}
function printThisPage() {
	/* Sets up Print This Page link, adding an event listener to open a print dialog when the link is clicked. */
	var el = document.getElementById("printThis");
	el.setAttribute("href", "#");
	if (el.addEventListener) {
		el.addEventListener('click', openPrintDialog, false); 
	} else if (el.attachEvent) { /* For IE */
		el.attachEvent('onclick', openPrintDialog);
	}
}
function addPageTools() {
	//(bodyID != "home") || 
	//alert(bodyID);
	var pageTools = document.getElementById("pageTools");
	
	/* Create element and text nodes. */
	var printLink = document.createElement("a");
	var printLinkTxt = document.createTextNode("Print this page");
	var emailLink = document.createElement("a");
	var emailLinkTxt = document.createTextNode("E-mail this page");
	
	/* Add IDs to links. */
	printLink.setAttribute("id", "printThis");
	emailLink.setAttribute("id", "emailThis");
	
	/* Append text nodes to link element nodes. */
	printLink.appendChild(printLinkTxt);
	emailLink.appendChild(emailLinkTxt);
	
	/* Append both links to pageTools div. */
	pageTools.appendChild(printLink);
	pageTools.appendChild(emailLink);
}

window.onload = function () {
	var bodyTag = document.getElementsByTagName("body");
	var bodyID = bodyTag[0].id;
	
	/* Do not apply to home and contact pages. */
	if (bodyID != "home" && bodyID != "contact") {	
		addPageTools();
		emailThisPage();
		printThisPage(); 
	}
}

/* whitepaper download functions*/
function getFileName(cookieName) { /* given the cookie name, which is the whitepaper folder ID number, return the whitepaper file name*/
 	 var whitePaperFileName = readCookie(cookieName);
	 return whitePaperFileName;
}
 
function setFileName(cookieName, linkId) { /* set the 'go here' link to redirect to the badcookie page if user has not entered info, OR set the link to the pdf file if they have entered their info  */
	 var linkValue = getFileName(cookieName);
	 if(linkValue==""||linkValue==null){document.getElementById(linkId).href="../badCookie.html";}else{
	 document.getElementById(linkId).href = getFileName(cookieName);
	 document.getElementById(linkId).target = "_blank";
	 }
}
 
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

