// -----------------------------------------------------------
// Parent Page - Print Page Functions
// (Theses functions are used by the parent page)
// -----------------------------------------------------------

var g_intPageLevel			= 0;
var g_strPrintOptionText	= "";
var g_intHeaderNo			= 1;
var g_intFooterNo			= 1;
var g_intFooterSpacerHeight = 1;

function OpenInNewWindowForPrinting()
{
	// Display the contents of this page in a print window.
	// These window dimension display the window full screen.
	var strWindowDimensions = ",left=0,top=0,width=" + String(screen.availWidth - 12) + ",height=" + String(screen.availHeight - 30);
	var strFeatures			= "toolbar=no,menubar=no,scrollbars=yes,resizable=no" + strWindowDimensions;

	switch(g_intPageLevel)
	{
		case 1:
			var strPath = "../PrintPage1/PrintPage1.htm";
			break;
		case 2:
			var strPath = "../../PrintPage1/PrintPage2/PrintPage2.htm";
			break;
		case 3:
			var strPath = "../../../PrintPage1/PrintPage2/PrintPage3/PrintPage3.htm";
			break;
		default:
			break;
	}

	// Open the new window and set a reference to it in global variable g_objPrintWindow of the 
	// main parent page. This variable is defined in script GenericFunctions.js
	window.parent.g_objPrintWindow = window.open(strPath, "PrintWindow", strFeatures);
	
	// Set focus to the window. Required when the window is already open.
	window.parent.g_objPrintWindow.focus();
}

// 
// intPageLevel			: The directory level the page is at.
//						  Optional. Default 2.
// strPrintOptionText	: The Print Options text that appears in the message box.
//						  Optional. Defaults to "" if not specified, so the default
//						  text will be displayed.
// intHeaderNo			: A number that indicates which header to use.
//						  Optional. defaults to 1 if not specified.	
// intFooterNo			: A number that indicates which footer to use.
//						  Optional. defaults to 1 if not specified.	
// intFooterSpacerHeight: The number of pixels between the bottom of the page contents and the page footer.
//						  This is used to fine tune to get the footer on the very bottom of the page.	
//						  Optional. defaults to 1 if not specified.
//
function DisplayPrintButton(intPageLevel, strPrintOptionText, intHeaderNo, intFooterNo, intFooterSpacerHeight)
{
	// Set argument default if they are not defined.

	if(typeof(intPageLevel) == "undefined")
	{
		// If argument intPageLevel is not defined then set the default to 2, as this is
		// where most pages will be.
		var intPageLevel = 2;
	};
	
	if(typeof(strPrintOptionText) == "undefined")
	{
		var strPrintOptionText = "";
	};
	
	if(typeof(intHeaderNo) == "undefined")
	{
		var intHeaderNo = 1;
	};
	
	if(typeof(intFooterNo) == "undefined")
	{
		var intFooterNo = 1;
	};
	
	if(typeof(intFooterSpacerHeight) == "undefined")
	{
		var intFooterSpacerHeight = 1;
		
//		var intFooterSpacerHeight = GetFooterSpacerHeight();
	};
	
	// Store the page level value in a global variable. This is used by function OpenInNewWindowForPrinting,
	// but is passed into this function because it more logical and clearer in HTML page.
	g_intPageLevel = intPageLevel;
	
	// Store other value in global variables. These are accessed by the Print Pages.
	g_strPrintOptionText	= strPrintOptionText;
	g_intHeaderNo	  		= intHeaderNo;
	g_intFooterNo			= intFooterNo;
	g_intFooterSpacerHeight = intFooterSpacerHeight;

	var objButton = document.all.item("divPrintButton");
	
	objButton.style.display = "block";
}

// This function does work for more than one page, but it will not work
// if page breaks have been used.
// It isn't guaranteed to position the footer perfectly, but it will do a good job!
// This function is designed to be called in the 5th argument of DisplayPrintButton in the HTML page.
//
function GetFooterSpacerHeight()
{
	var intA4PageHeight = 840;
	var intContentsHeight = document.all.divContainer.scrollHeight;
	var intPageCount = Math.floor(intContentsHeight / intA4PageHeight) + 1;
	
	// Find the size of the contents on the Last Page.
	var intContentsHeight_LP = (intContentsHeight - ((intPageCount - 1) * intA4PageHeight));
	
	// Set the average height of the footer on the print page.
	var intFooterHeight = 20;
	
	var intFooterSpacerHeight = intA4PageHeight - intContentsHeight_LP - intFooterHeight;

	return intFooterSpacerHeight;
}

// -----------------------------------------------------------
// Print Page Functions
// (Theses functions are used by the PrintPage1.htm,
//  PrintPage2.htm and PrintPage3.htm pages)
// -----------------------------------------------------------

function PopulatePrintPageContents()
{
	SetPrintWindow_Header();
	SetPrintWindow_Footer();
		
	// Get a reference to the web page that opened this print page.
	var objParentDocument = window.parent.window.opener.document;

	// Put the HTML contents of divContent from the page that opened this page into
	// divPrintPageContents of this page.
	
	// First we deactivate any anchors on the page.
	var strHTML = RemoveAnchors(objParentDocument.all.item("divContent").innerHTML, false);
	
	document.all.item("divPrintPageContents").innerHTML = strHTML;
	
	SetPrintWindow_FooterSpacerHeight();
}
		
// Message Box Functions
		
function DisplayHelpWindow()
{
	var intScreenWidth  = screen.availWidth;
	var intScreenHeight = screen.availHeight;
			
	var intLeft = ((intScreenWidth / 2)  - 20) - 300;
	var intTop = ((intScreenHeight / 2) - 20)  - 250;
				
	var objMsgBox = document.all.item("divPrintHelp");
	objMsgBox.style.left = intLeft;
	objMsgBox.style.top  = intTop;
	
	SetPrintWindow_OptionsText();
	
	objMsgBox.style.display = "block";
}
		
function PrintPage()
{
	window.print();
}
	
function ClosePrintHelp()
{
	var objMsgBox = document.all.item("divPrintHelp");
	objMsgBox.style.display = "none";
}
		
function ClosePrintWindow()
{
	window.close();
}
		
// This function puts text in 'Print Options' of the message box. This is set in the parent page
// by passing the print option text as an argument to function DisplayPrintButton.
function SetPrintWindow_OptionsText()
{
	// Get a reference to the web page that opened this print page.
	var objParentDocument = window.parent.window.opener;
			
	// Set the Print Option text is global variable g_strPrintOptionText exist in the parent page
	// and it has been populated with a value.
	if(typeof(objParentDocument.g_strPrintOptionText) == "string")
	{
		var strOptionText = objParentDocument.g_strPrintOptionText;
			
		if(strOptionText.length > 0)
		{
			document.all.item("PrintOptions").innerHTML = strOptionText;
		};	
	};
}
		
function SetPrintWindow_Header()
{
	var objParentDocument = window.parent.window.opener;
		
	// Set the Print Option text if global variable g_strPrintOptionText exist in the parent page
	// and it has been populated with a value.
			
	if(typeof(objParentDocument.g_intHeaderNo) == "number")
	{
		var intHeaderNo = objParentDocument.g_intHeaderNo;
		
		if(intHeaderNo != 0)
		{
			var objHeader	= document.all.item("divPrintPageHeader" + String(intHeaderNo));
		
			if(objHeader == null)
			{
				// If the header specified doesn't exist then display the first header as the default.
				var objHeader = document.all.item("divPrintPageHeader1");
			};
				
			objHeader.style.display = "block";
		};
	};
}
		
function SetPrintWindow_Footer()
{
	var objParentDocument = window.parent.window.opener;
		
	// Set the Print Option text is global variable g_strPrintOptionText exist in the parent page
	// and it has been populated with a value.
			
	if(typeof(objParentDocument.g_intFooterNo) == "number")
	{
		var intFooterNo = objParentDocument.g_intFooterNo;
		
		if(intFooterNo != 0)
		{
			var objFooter	 = document.all.item("divPrintPageFooter" + String(intFooterNo));
			
			if(objFooter == null)
			{
				// If the footer specified doesn't exist then display the first footer as the default.
				var objFooter = document.all.item("divPrintPageFooter1");
			};
	
			objFooter.style.display = "block";
		};
	};
}

function SetPrintWindow_FooterSpacerHeight()
{
	var objParentDocument = window.parent.window.opener;
			
	if(typeof(objParentDocument.g_intFooterNo) == "number")
	{
		var intFooterNo			  = objParentDocument.g_intFooterNo;
		var intFooterSpacerHeight = objParentDocument.g_intFooterSpacerHeight;
		
		if(intFooterNo != 0)
		{
			var objSpacerRow = document.all.item("rowSpacerFooter" + String(intFooterNo));
			
			if(objSpacerRow != null)
			{
				objSpacerRow.height = intFooterSpacerHeight;
			};

		};
	};
}

// ------------------------------------------------------------------------------
// Remove Anchor Functions
// Function RemoveAnchors is called by function PopulatePrintPageContents above.
// All other functions are helper functions for RemoveAnchors.
// -------------------------------------------------------------------------------

// Removes all anchors from the HTML.
// See function RemoveAnchor for more details.
//
function RemoveAnchors(strHTML, blnFormatAsInactive)
{			
	var strNewHTML = strHTML;

	while (AnchorExists(strNewHTML) == true)
	{
		strNewHTML = RemoveAnchor(strNewHTML, blnFormatAsInactive);
	}
				
	return strNewHTML;
}
			
// Returns the HTML with the first anchor found make inactive, by replacing it with just the text of the anchor.
// If blnFormatAsInactive is true then a format is applied to the anchor text to make it visible inactive.
// If blnFormatAsInactive is false this format will not be applied and the text will appear the same as the other
// text.
//
function RemoveAnchor(strHTML, blnFormatAsInactive)
{
	// The length of the end tag i.e. </A>
	var intLengthEndTag = 4;

	// Find the start ("<A") and end ("</A>") positions of the Anchor.
	var intStartPos = strHTML.search(/<A/);
	var intEndPos   = strHTML.search(/<\/A/);

	// Extract the anchor from the HTML.
	var strAnchor = strHTML.substr(intStartPos, (intEndPos - intStartPos + intLengthEndTag) );

	// Find the start position of the anchor text. This is the first character after the first ">" found.
	var intStartPos   = strAnchor.search(/>/) + 1;

	// Extract the anchor text from the anchor HTML. 
	var strAnchorText = strAnchor.substr(intStartPos, (strAnchor.length - (intStartPos + intLengthEndTag)) );

	// If required, format the anchor text, so it is displayed as inactive.
	if(blnFormatAsInactive == true)
	{
		strAnchorText = "<B><FONT color=black>" + strAnchorText + "</FONT></B>";
	};
				
	return strHTML.replace(strAnchor, strAnchorText);
}

// Returns true is any anchors exist in the HTML.
//			
function AnchorExists(strHTML)
{
	var intEndPos   = strHTML.search(/<\/A/);

	return (intEndPos > 0);
}
			
			