/* 
[Program]
	Add Print Button
[Language]
	JavaScript
[Purpose]
	This script is a collection of functions to add a print "button" 
	to a page. The script consists of two functions: one to attach 
	event listeners and one that creates the button. When a page is 
	loaded that contains this function the button will be added.
[Requirements]
	A W3C DOM-compliant browser with JavaScript and CSS support enabled.
	Tested and functional:
		* IE on Windows XP SP2
		* Firefox v1.5 on Windows XP SP2 and Mac OS X 10.3.9
		* Safari v1.3.1
[Parameters]
	None
[Usage]
	Attach the JS file to a document using <script src...></script>.
[Notes]
	The function names in this script are prefixed by "printFunc_" to 
	avoid interfering with other scripts.
[To do]
	* Place styling in a separate document.
	* Define a print style so that the print button does not appear on 
	the output.

-----------------------------------------------------------------
Ver    Date    Description of modification
--- ---------- --------------------------------------------------
1.1 2006/02/07 Documentation added
1.0 2005/02/01 Original write
-----------------------------------------------------------------
*/

function printFunc_addEvent(obj, evType, fn, useCapture){
	// Cross-browser code to add an event handler to a DOM object
	// http://www.scottandrew.com/weblog/articles/cbs-events
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

function printFunc_init () {
	// Ensure that the browser contains the function necessary to set up the print link.
	if (window.print && document.getElementsByTagName && document.createElement) {
		var docBody;
		var printMenu;
		var printLink;
		var printLinkText;
		
		// Assign the document body to a variable for future reference.
		// The print link will be absolute positioned so we need to ensure 
		// that the content of the page is not overlapped by it. This is 
		// done through a top padding on the body.
		docBody = document.getElementsByTagName("body")[0]
		docBody.style.paddingTop = ".75em";
		
		// Create the container div for the print link and apply the 
		// appropriate styling.
		printMenu = document.createElement("div");
		printMenu.style.position = "absolute";
		printMenu.style.left = "0px";
		printMenu.style.top = "0px";
		printMenu.style.width = "100%";
		printMenu.style.padding = ".25em";
		printMenu.style.paddingRight = "0em";
		printMenu.style.paddingLeft = "0em";
		printMenu.style.backgroundColor = "#f0f0f0";
		printMenu.style.textAlign = "right";
		
		// Create the print link, apply the appropriate styling, and 
		// define the click event handler to print the document.
		printLink = document.createElement("a");
		printLink.style.paddingRight = "1.5em";
		printLink.style.color = "#000000";
		printLink.style.textDecoration = "none";
		printLink.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
		printLink.style.fontSize = ".8em";
		printLink.href = "#all";
		printLink.onclick = function () { window.print(); }
		
		// Create the link text.
		printLinkText = document.createTextNode("[PRINT]");
		
		// Append the created nodes to their parent objects.
		printLink.appendChild(printLinkText);
		printMenu.appendChild(printLink);
		docBody.appendChild(printMenu);
	}
}


printFunc_addEvent(window, 'load', printFunc_init, false);

