//=====================================================
// PARAMETERS
//=====================================================
var linkContainerId = 'PrintFunction'; //ID of element to contain preview link - should be a container that is hidden on print
var linkText = 'Print This Page '; //Text of preview link

var messageContainerId = 'Viewport'; //ID of element to contain preview message
var messageHeader = 'Print Preview'; //Preview message header
var messageBody = 'Below is a print preview of this page. This message box will not be included on the printed page. '; //Preview message text

var printText = ' Print This Page '; //Text of print link (used only if autoPrint is false)
var returnText = ' Exit Print Preview '; //Text of link to exit preview

var autoPrint = false; //true: Automatically prints after clicking preview link. false: Renders print link in preview 

var useIcons = true; //true: Places appropriate icons before link text. false: Uses only link text.
//=====================================================

//This boolean value is true while in print preview mode
//  Tip: Ensure this variable is false before performing any
//       display-oriented javascript functions
var isPreview = false;

function setActiveStyleSheet(title) {  // select the stylesheet
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent(function() {
	/* more code to run on page load */
	if (document.getElementById){
		//     add extra functions to page tools list
		var print_page = document.getElementById(linkContainerId);                                                        
if(print_page)
{
		//     create print link
		var print_function = document.createElement('div');
		// print_function.setAttribute('class', 'print');
			//IE doesn't like this
		var print_function_link = document.createElement('a');
			print_function_link.onclick = function() { print_preview(); return false; }
			print_function_link.setAttribute('href', '#');
	
		var print_function_link_text = document.createTextNode(linkText);
				print_function_link.appendChild(print_function_link_text);	
	

		print_function.appendChild(print_function_link);            
		print_page.appendChild(print_function);            
	}
	}
});

function print_preview() {

	// Switch the stylesheet
	setActiveStyleSheet('Print Preview');

	// Create preview message
	add_preview_message();
	
	// Set preview status
	isPreview = true;

	// Print the page
	if (autoPrint) window.print();
}

function add_preview_message(){

	var main_content = document.getElementById(messageContainerId);
	var main_body = main_content.parentNode;

	if (document.getElementById){

		var preview_message = document.createElement('div');
		preview_message.id = 'preview-message';

		// Create Heading
		var preview_header = document.createElement('h3');
		var preview_header_text = document.createTextNode(messageHeader);
		preview_header.appendChild(preview_header_text);

		// Create paragraph
		var preview_para = document.createElement('p');
		var preview_para_text = document.createTextNode(messageBody);

		var print_function_link = document.createElement('a');
			print_function_link.onclick = function() { window.print(); return false; };
			print_function_link.setAttribute('href', '#');  
			print_function_link.setAttribute('alt', printText);  
		var print_function_link_icon = document.createElement('img');
			print_function_link_icon.setAttribute('src', '/shared/scripts/images/printer.gif'); 
			print_function_link_icon.setAttribute('alt', printText);
			print_function_link_icon.setAttribute('width', '16'); 
			print_function_link_icon.setAttribute('height', '16'); 
		var print_function_link_text = document.createTextNode(printText); 
		if (useIcons) print_function_link.appendChild(print_function_link_icon);
		print_function_link.appendChild(print_function_link_text);
			
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function() { cancel_print_preview(); return false; };
			cancel_function_link.setAttribute('href', '#');
			cancel_function_link.setAttribute('alt', printText);
		var cancel_function_link_icon = document.createElement('img');
			cancel_function_link_icon.setAttribute('src', '/shared/scripts/images/cancel.gif'); 
			cancel_function_link_icon.setAttribute('alt', returnText);
			cancel_function_link_icon.setAttribute('width', '16'); 
			cancel_function_link_icon.setAttribute('height', '16');   
		var cancel_function_link_text = document.createTextNode(returnText);
		if (useIcons) cancel_function_link.appendChild(cancel_function_link_icon);
		cancel_function_link.appendChild(cancel_function_link_text);
		
		var space_text = document.createTextNode(' ');
		
		preview_para.appendChild(preview_para_text);
		preview_para.appendChild(space_text);
		if (!autoPrint) {
			preview_para.appendChild(print_function_link);
			preview_para.appendChild(space_text);
		}
		preview_para.appendChild(cancel_function_link);

		// Put it all together
		preview_message.appendChild(preview_header); 
		preview_message.appendChild(preview_para);
		main_body.insertBefore(preview_message, main_content);
		
		window.scrollTo(0,0);
	}
}

function cancel_print_preview() {
	// Destroy the preview message
	var print_preview = document.getElementById('preview-message');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);

	// Switch back stylesheets
	setActiveStyleSheet('all');	
	setActiveStyleSheet('default');
	
	// Reset preview status
	isPreview = false;
}
