var print_on = 'Print';
var print_off = 'Close';

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(){
	add_print_link('nav')
	}
);

function add_print_link(id){
	if(!document.getElementById || !document.getElementById(id)) return;
	var print_page = document.getElementById( id );
	var print_function = document.createElement('p');
	print_function.className = 'print-link';
	print_function.onclick = function(){ print_preview(); return false; };
	print_function.appendChild( document.createTextNode( 'Print the Page' ) );
}

function print_preview(){
	setActiveStyleSheet('Preview');
	add_preview_message();
}

function print_orderform(){
	setActiveStyleSheet('Preview');
	window.print();
}

function add_preview_message(){
	var main_content = document.getElementById('content');
	var main_body = main_content.parentNode;
	if (document.getElementById){
	var preview_message = document.createElement('div');
		preview_message.id = 'print-msg';
		var preview_para = document.createElement('div');
		var on = document.createElement('a');
			on.setAttribute('href', 'javascript:window.print()');
			on.setAttribute('class', 'on');
		var on_t = document.createTextNode(print_on);
		on.appendChild(on_t);
		preview_para.appendChild(on);
		var off = document.createElement('a');
			off.setAttribute('href', 'javascript:cancel_print_preview()');
			off.setAttribute('class', 'off');
		var off_t = document.createTextNode(print_off);
		off.appendChild(off_t);
		preview_para.appendChild(off);
		preview_message.appendChild(preview_para);
		main_body.insertBefore(preview_message, main_content);
	}
}

function cancel_print_preview() {
	var print_preview = document.getElementById('print-msg');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);
	setActiveStyleSheet('default');
}

function setActiveStyleSheet(title) {
   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;
     }
   }
}
