function addEvent(elm, evType, fn, useCapture)
{
		if(elm.addEventListener)
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		}
		else if (elm.attachEvent)
		{
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		}
		else
		{
			elm['on' + evType] = fn;
		}
}

function addPrintLinks()
{
var el = document.getElementsByTagName("div");
for (i=0;i<el.length;i++)
	{
	if (el[i].className=="section")
		{
		var newLink = document.createElement("A");
		var newLink2 = document.createElement("A");
		var newLinkText = document.createTextNode("print *this* section only");
		var newLinkText2 = document.createTextNode("print the *whole page*");

		var newLinkPara = document.createElement("P");

		newLinkPara.setAttribute("class","printbutton");
		
		newLink.setAttribute("href","#");
		var btId = "printbut_" + el[i].id;
		newLink.setAttribute("id",btId);
		newLink.appendChild(newLinkText);
		newLink.setAttribute("href","#");
		newLinkPara.appendChild(newLink);
		
		var spacer = document.createTextNode(" | ");
		newLinkPara.appendChild(spacer);

		newLink2.setAttribute("href","#");
		var bt2Id = "printall_" + el[i].id;
		newLink2.setAttribute("id",bt2Id);
		newLink2.appendChild(newLinkText2);
		newLink2.setAttribute("href","#");
		newLinkPara.appendChild(newLink2);

		el[i].appendChild(newLinkPara);
		
		var newLinkEl = document.getElementById("printbut_" + el[i].id);
		newLinkEl.onclick		=	togglePrintDisplay;
		newLinkEl.onkeypress	=	togglePrintDisplay;

		var newLinkEl2 = document.getElementById("printall_" + el[i].id);
		newLinkEl2.onclick		=	printAll;
		newLinkEl2.onkeypress	=	printAll;

		}
	}
}

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].className.indexOf("section")!=-1)
		{
		el[i].removeAttribute("className");
		if (el[i].id==whatSection)
			{
			//show only this section for print
			el[i].setAttribute("className","section print");
			el[i].setAttribute("class","section print");
			}
		else
			{
			el[i].setAttribute("className","section noprint");
			el[i].setAttribute("class","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("section")!=-1)
		{
		el[i].setAttribute("className","section print");
		el[i].setAttribute("class","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);
