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;

  // add extra functions to page tools list
  var print_page = document.getElementById( id );

  // create print link
  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() {
	// Switch the stylesheet
	setActiveStyleSheet('Print Preview');
	
	// Create preview message
	add_preview_message();

	// Print the page
	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 = 'preview-message';
	
		// Create Heading
		var preview_header = document.createElement('h3');
		var preview_header_text = document.createTextNode('This is a print preview of this page');
		preview_header.appendChild(preview_header_text);
		
		// Create paragraph
		var preview_para = document.createElement('p');
		var preview_para_text = document.createTextNode('This box will not appear when you print. ');
		
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function(){ cancel_print_preview(); return false; };
			cancel_function_link.setAttribute('href', '#');	
		var cancel_function_link_text = document.createTextNode('Return to the existing page.');
		cancel_function_link.appendChild(cancel_function_link_text);
		preview_para.appendChild(preview_para_text); //
		preview_para.appendChild(cancel_function_link);
		
		// Put it all toegether
		preview_message.appendChild(preview_header); 
		preview_message.appendChild(preview_para);
		main_body.insertBefore(preview_message, main_content);

	}
}

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 stylesheet
	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;
     }
   }
}

//Generating Pop-up Print Preview page
function getPrint(print_area,printLabel,closeLabel) {
    //Creating new page
    //var hdrLogo = "/CMSTemplates/DCWebsite/Templates/images/DC_H_B_ENG.gif";
    //var copyrightText = "&copy; Dietitians of Canada. 2010. All rights reserved. Permission to reprint in its entirety. For noncommercial use only.";
    var sOption = "toolbar=no,location=no,directories=no,menubar=no,";
    sOption += "scrollbars=yes,width=750,height=600,left=100,top=25";

   // if (m_current_culture == 'fr') {
     //   hdrLogo = "/CMSTemplates/DCWebsite/Templates/images/DC_H_B_FRE.gif";
    //    copyrightText = "&copy; Les di&egrave;t&egrave;tistes du Canada. 2010. Tous droits r&egrave;serv&egrave;s. R&egrave;imprimer int&egrave;grale autoris&egrave;e, R&egrave;serv&egrave; &agrave; un usage non commercial.";
  //  }

    var pp = window.open("", "", sOption);
    //Adding HTML opening tag with <HEAD> … </HEAD> portion 
    pp.document.writeln('<HTML><HEAD><title>Print Preview</title>')
    pp.document.writeln('<LINK href="/CMSTemplates/DCWebsite/Templates/styles/print.css" type="text/css" rel="stylesheet">')
    pp.document.writeln('<LINK href="/CMSTemplates/DCWebsite/Templates/styles/print.css" type="text/css" rel="stylesheet" media="print">')
    pp.document.writeln('<base target="_self"></HEAD>')

    //Adding Body Tag
    pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
    pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0" onload="javascript:window.print();">');
    //pp.document.writeln('<img src="' + hdrLogo + '"/>');
    pp.document.writeln('<br><br>');
    //Adding form Tag
    //pp.document.writeln('<form method="post">');

    //Creating two buttons Print and Close within a HTML table
    //pp.document.writeln('<TABLE width="100%"><TR><TD></TD></TR><TR><TD align=left>');
   // pp.document.writeln('<a ID="PRINT" type="print" class="printpreview" ');
    //pp.document.writeln('onclick="javascript:window.print();"><span>' + printLabel + '</span>');
    //pp.document.writeln('<a ID="CLOSE" type="button" class="close" ' +
    //                    ' onclick="window.close();"><span>' + closeLabel + '</span>');
    //pp.document.writeln('</TD></TR><TR><TD></TD></TR></TABLE><hr>');

    //Writing print area of the calling page
    pp.document.writeln(document.getElementById(print_area).innerHTML);
    
    //Ending Tag of </form>, </body> and </HTML>
    //pp.document.writeln('</form></body></HTML>');
    //pp.document.writeln('<br>');
    //pp.document.writeln(copyrightText);
    pp.document.writeln('</body></HTML>');
    pp.document.close();
}


function PrintThisPage() {
    var sOption = "toolbar=yes,location=no,directories=yes,menubar=yes,";
    sOption += "scrollbars=yes,width=750,height=600,left=100,top=25";

    var sWinHTML = document.getElementById('content').innerHTML;

    var winprint = window.open(document.URL , "", sOption);
    
    winprint.document.open();
    winprint.document.write('<html><LINK href="/CMSTemplates/DCWebsite/Templates/styles/print.css" type="text/css" rel="stylesheet"><body>');
    winprint.document.write('testestsetset');
    winprint.document.write('</body></html>');
    winprint.document.close();
    winprint.focus();
}
