// Print functions

// Open a print preview, in which the current page has its stylesheets
// changed from media=print to media=print,screen
function printPreview(numStyleSheets,doIAlert) {
	// Do I alert?
	if (typeof doIAlert == "undefined") doIAlert = false;
	if (doIAlert) {
		alert("You can print a clean version of the page, without print\npreview, by simply clicking on 'print this page'.");
	}
	// Open a new window
	printWin = window.open(window.location,"print_win");

	// Save print window and wait for window to load
	printWin.opener = window;
	window.printWin = printWin;

	window.printWinNumSS = numStyleSheets;
	window.printWinChangeSS = window.setInterval("changeSS();",1000);

	return false;
}

// The change stylesheet function
function changeSS() {
	// Window still exists?
	if (window.printWin.document == null) {
		return false;
	}

	// Do we have stylesheets?
	ss = window.printWin.document.styleSheets;
	if ((typeof ss == "undefined") || (window.printWin.document.styleSheets.length < window.printWinNumSS) ) {
		return false;
	}

	// Activate print stylesheets
	var changed = false;
	for(var i=0;i< ss.length; i++) {
		ssMediaText = ss[i].media.mediaText;
		if ((ssMediaText.indexOf("print") != -1) && (ssMediaText.indexOf("screen") == -1) ) {
			ss[i].media.mediaText += ",screen";
			changed = true;
		}
	}

	// We don't want to clear the timeout, in case the user reloads the page.
	return false;

	// Clear timeout
	if (typeof window.printWinChangeSS != "undefined") {
		window.clearInterval(window.printWinChangeSS);
	}
	return false;
}
