/******************************************************************
Javascript to print friendly

Version	: 2.0
Author	: irc@isd.gov.hk
Date	: 7 Sept 2007

Usage   :
1.	Include this file to your HTML pages
	e.g. <script type="text/javascript" src="print.js"></script>
2. If your web pages are UTF-8 encoding, convert this file to UTF-8
	encoding
3.  In the HTML pages, add the following tags to warp the desire 
	printing area. Only three area are provided and with no repeat.
	
	<div id="title"></div>
	<div id="printarea"></div>
	<div id="lastupdate"></div>
4.	In the HTML pages, add thr following code into the appropriate
	area.
	
	javascript:printThis();
	
	e.g. <a href="javascript:printThis();"><img src="print.gif" /></a>
******************************************************************/

var printed = false;

function writePrinterVersion() {
	document.body.style.background="#FFFFFF";
	document.body.style.textAlign="center";
	var a = document.getElementsByTagName("a");
	for (var i=0; i<a.length; i++) {
		a[i].href="#";
	}
	var t = document.getElementById("title");
	var p = document.getElementById("printarea");
	var l = document.getElementById("lastupdate");
	var nt = t.cloneNode(true);
	var np = p.cloneNode(true);
	var nl = l.cloneNode(true);
	while (document.body.childNodes[0]) {
		document.body.removeChild(document.body.childNodes[0]);
	}
	var d = document.createElement("div");
	d.style.width="500px";
	d.style.margin="50px auto";
	d.style.textAlign="left";
	d.appendChild(nt);
	d.appendChild(np);
	d.appendChild(nl);
	document.body.appendChild(d);
}

function printThis() {
	printed = true;
	var win = window.open(window.location.href, "printWin");
}

function init(e) {
	var o = window.opener;
	if ((o) && (o.location.href==window.location.href) && (o.printed)) {
		o.printed = false;
		writePrinterVersion();
		window.print();
	}
}

if (window.addEventListener) {
	window.addEventListener("load", init, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", init);
}
//for IE/Mac
document.onreadystatechange = function(e) {
	if (document.readyState=="interactive")
	init();
}

