/*
	PrintMe function for Energy Security Website
	
	Purpose:
		This script generates a printer-friendly version of the Energy Security website.
		It uses javascript to create the page dynamically

	General Procedure:
		1) Extract content from current page
		2) Build new, simple template around it
		3) spawn new window, write html code to that window
		
		Also, since much of the page content is generated automatically, we need to make sure to carry over any needed scripts and variables to the printer-friendly page

	Arguments:
		PrintMe() takes one argument, main, which is the id of the block-level container holding the main content of the page
		Only stuff inside main will be included on the printer-friendly version
*/

function PrintMe(main) {

//Some variables for internal use
var printer_content; // holder for the content to transfer from the main page to the printer-friendly page
var page_code; // holder for the entire code for the new page
var holder = 'holder'; // id of div holder from archive-app-xml.js... do not change this, else the script will break
var finalsort_table = 'finalsort_table'; //id of table holding output from archive-app-xml.js
var breadcrumbs_id = 'breadcrumbs'; // ID of div tag holding breadcrumbs
var header_graphic_id = 'header_graphic'; // ID of header graphic

// First we need to extract the necessary content from the page
if (document.getElementById(main)) { // check that main exists
	printer_content = document.getElementById(main).innerHTML;
}

// Second, let's build the html code around this content
// We need a header section and a body section
// For the header section, to keep things simple, let's just grab the header from the current document
page_code = '<html><head>';
page_code = page_code + '<script type="text/javascript">var printer_friendly = "true";</script>'; // add a global variable to signal that this a printer-friendly page
page_code = page_code + document.getElementsByTagName("head")[0].innerHTML;
page_code =  page_code + '</head>';

// Let's now build the body section
// We need to get the exact body tag from the current document, because it likely contains attributes that we should carry to the next document
page_code = page_code + '<body';
for (var i = 0; i < document.body.attributes.length; i++) {
	page_code = page_code + ' ' + document.body.attributes[i].nodeName + '="' + document.body.attributes[i].nodeValue + '"'; // get each attribute and its value
}
page_code = page_code + ' align="left"><div>'; // close out the body tag and open a div to hold the printer_content

// Let's build some formatting for the page
// From the old page, let't take the "breadcrumbs" and one of the header graphics
if (document.getElementById(breadcrumbs_id)) {
	page_code = page_code + '<div';
	for (var i = 0; i < document.getElementById(breadcrumbs_id).attributes.length; i++) { // get all the attributes to the div tag containing the breadcrumbs
		page_code = page_code + ' ' + document.getElementById(breadcrumbs_id).attributes[i].nodeName + '="' + document.getElementById(breadcrumbs_id).attributes[i].nodeValue + '"'; // get each attribute and its value
	}
	page_code = page_code + '>' + document.getElementById(breadcrumbs_id).innerHTML + '</div>';
}

// Now do the same for the header graphic
// This should be an img tag, so we don't need the innerHTML attribute or a closing tag
if (document.getElementById(header_graphic_id)) {
	page_code = page_code + '<' + document.getElementById(header_graphic_id).tagName;
	for (var i = 0; i < document.getElementById(header_graphic_id).attributes.length; i++) { // get all the attributes to the tag containing the breadcrumbs
		page_code = page_code + ' ' + document.getElementById(header_graphic_id).attributes[i].nodeName + '="' + document.getElementById(header_graphic_id).attributes[i].nodeValue + '"'; // get each attribute and its value
	}
	page_code = page_code + '>';
	
}

page_code = page_code + printer_content;
page_code = page_code + '</body></html>'; // close out html code for the page

// Ok, we have the html code for out new page, let's write it to a new window
var new_window = window.open("","","");
new_window.document.write(page_code);
new_window.document.close();


//EOF
}