/******************************************************/
/*                  Colgate-Palmolive                 */
/*         Web Printing for SAP BW Version 2.0        */
/*                                                    */
/*           Development Team:                        */
/*           --------------------                     */
/*           Phil Young                               */
/*           Daniel McWeeney                          */
/*           Jeff Kinman                              */
/*           Nat Grauman                              */
/*                                                    */
/*           Last changed on 6/24/2005                */
/*           by: Phil Young                           */
/******************************************************/



// Global Variables
//-----------------------------------------------------------------
var startTable;
var colHeadersTable; //stores column headers
var dataTable; //used to hold values to go in tBodies
var dataCellx; //row position of first data cell
var dataCelly; //cell position of first data cell
var newTables; //stores print pages
var endColMap; //stores row numbers of last row for each page
var Fpt;       //stores resizing font-size (in pt)
var tableCaption; //stores Caption for applicable tables.
var foundCaption = false; //stores whether caption was found for current table
var pixelsPerIn; // stores pixels per inch
var errMinPagesWide = 0; //stores resize error info
var errMinPagesHigh = 0; //stores resize error info
var isPrintGridLines;
//-----------------------------------------------------------------

// Variables to store start and end times for performance analysis
//-----------------------------------------------------------------
var startTime, endTime;
var parseStart, parseEnd;
var stripStart, stripEnd;
var createStart, createEnd;
var breakStart, breakEnd;
var fitStart, fitEnd;
var applyStart, applyEnd;
var numCells;
//-----------------------------------------------------------------

// Configuration variables
//-----------------------------------------------------------------
var dispPgSetup = "on"; //set dispPgSetup = "on" to display page setup properties to user.
//-----------------------------------------------------------------

// Performance analysis triggers
//-----------------------------------------------------------------
var debug = "off"; // set debug = "on" to see alerts for troubleshooting.
var trackRunTimes = "off"; // set trackRunTimes = "on" to see application runtimes.
//-----------------------------------------------------------------


// called from page body onbeforeprint, receives names of tables to resize as parameters
function run(){
	initialization();
	var myDate;	
	var pagesWide;
	var pagesHigh;
	var pageWidthIn;
	var pageHeightIn;
	var tableList;
	var lockHeaders;
	var i,j,k,m;
	var gridTable,gridRow,gridCell;
	var pageDocFragment = document.createDocumentFragment();
	var captionDiv, captionRow, captionCell, captionTHead, captionTBody, captionTable;
	var repHeaderTable;
	var headerHeight;
	var numTablesPrinted = 0;
	var isPortrait;
	var helpMsg;


	if (document.cookie != ""){
		document.printUI.setDefault(document.cookie);
	}
	document.printUI.display();
	while (document.printUI.getResponse() == 2){
		setCookie("pageswide", document.printUI.getPagesWide());
		setCookie("pagestall", document.printUI.getPagesTall());
		setCookie("portrait", document.printUI.isPortrait());
		setCookie("lockHeaders", document.printUI.isLockHeaders());
		setCookie("printGridLines", document.printUI.isPrintGridLines());
		document.printUI.setDefault(document.cookie);
		
		helpMsg = "Colgate-Palmolive BW Web Printing v2.0\n";
		helpMsg = helpMsg + "--------------------------------------------------------------------------------------\n\n";
		helpMsg = helpMsg + "FAQ\n\n";
		helpMsg = helpMsg + "1. Gridlines are not appearing in printout.\n\n";
		helpMsg = helpMsg + "Go to: Tools > Internet Options > Advanced > Printing.\n";
		helpMsg = helpMsg + "Check the option 'Print background colors and images.'\n\n\n";
		helpMsg = helpMsg + "2. Part of the printout is clipping on the right edge.\n";
		helpMsg = helpMsg + "- Or -\n";
		helpMsg = helpMsg + "3. The right edge has a lot of wasted space.\n\n";
		helpMsg = helpMsg + "When printing, the user will be presented with two\n";
		helpMsg = helpMsg + "Page Setup dialogues. The first controls the resizing\n";
		helpMsg = helpMsg + "application and the second controls Internet Explorer.\n";
		helpMsg = helpMsg + "Please ensure that the two dialogues have the same option\n";
		helpMsg = helpMsg + "selected for the orientation of the page.\n\n";
		helpMsg = helpMsg + "In addition, the resizing application requires that the\n";
		helpMsg = helpMsg + "print margins (displayed in the second Page Setup dialogue)\n";
		helpMsg = helpMsg + "be set to no greater than the Internet Explorer default\n";
		helpMsg = helpMsg + "value of 0.75 in each direction.\n";
		alert(helpMsg);
		document.printUI.display();
	}
	if (document.printUI.getResponse() == 1){
		
		//displays the page setup print properties.
		if(dispPgSetup == 'on'){
			displayPageSetup(); 
		}
		
		if (trackRunTimes == "on"){
			myDate = new Date();
			startTime = myDate.getTime();
		}
		setCookie("pageswide", document.printUI.getPagesWide());
		setCookie("pagestall", document.printUI.getPagesTall());
		setCookie("portrait", document.printUI.isPortrait());
		setCookie("lockHeaders", document.printUI.isLockHeaders());
		setCookie("printGridLines", document.printUI.isPrintGridLines());
	
		pagesWide = document.printUI.getPagesWide();
		pagesHigh = document.printUI.getPagesTall();
		lockHeaders = document.printUI.isLockHeaders();
		isPortrait = document.printUI.isPortrait();
		isPrintGridLines = document.printUI.isPrintGridLines();

		tableList = document.getElementsByTagName("TABLE");

		repHeaderTable = findHeader(tableList)

		for(i=0;i<run.arguments.length;i++){
			for(j=0;j<tableList.length;j++){
				if(tableList[j].getAttribute("name") == run.arguments[i]){
					startTable = tableList[j];
				}
			}
			
			if (startTable == null){
				if (debug == "on") {
					alert("Could not find table: " + run.arguments[i]);
				}
			} else {
				if(startTable.getAttribute("name") != run.arguments[i]){
					if (debug == "on") {
						alert("Could not find table: " + run.arguments[i]);
					}
					//return;
				} else {

					numTablesPrinted++;
					endColMap = new Array;
					newTables = new Array;
					
					dataTable = startTable.cloneNode(true);		
					if (trackRunTimes == "on"){
						numCells = startTable.getElementsByTagName("TD").length;
					}
					document.printUI.showProgessMeter(100);
					
					if(findCaption(startTable)){
						foundCaption = true;
						captionDiv = pageDocFragment.createElement("DIV");
						captionDiv.innerHTML = tableCaption;
						captionDiv.style.height = '.3in';
						captionDiv.style.fontWeight = 'bold';
						captionDiv.style.fontSize = '10pt';
						captionDiv.align = 'left';
						captionDiv.verticalAlign = 'text-bottom';
						
						//set page width to papersize - margins - Header height - Caption height - .1
						if ( isPortrait == 'true'){
							pageWidthIn = 6.9;
							repHeaderTable.style.width = pageWidthIn + "in";
							headerHeight = repHeaderTable.offsetHeight/pixelsPerIn;
							pageHeightIn = 8.9 - headerHeight;
						}
						else {
							pageWidthIn = 9.4; 
							repHeaderTable.style.width = pageWidthIn + "in";
							headerHeight = repHeaderTable.offsetHeight/pixelsPerIn;
							pageHeightIn = 6.4 - headerHeight;
						}
						
						captionDiv.style.width = pageWidthIn + "in";
						repHeaderTable.style.width = pageWidthIn + "in";
						repHeaderTable.style.height = headerHeight + "in";
					} else {
						foundCaption = false;
						
						//set page width to papersize - margins - Header height - .1
						if ( isPortrait == 'true'){
							pageWidthIn = 6.9;
							repHeaderTable.style.width = pageWidthIn + "in";
							headerHeight = repHeaderTable.offsetHeight/pixelsPerIn;
							pageHeightIn = 9.4 - headerHeight;
						}
						else {
							pageWidthIn = 9.4; 
							repHeaderTable.style.width = pageWidthIn + "in";
							headerHeight = repHeaderTable.offsetHeight/pixelsPerIn;
							pageHeightIn = 6.9 - headerHeight;
						}
						
						//repHeaderTable.style.width = pageWidthIn + "in";
						repHeaderTable.style.height = headerHeight + "in";
					}
					getFirstData();
					stripHeaders();
					
					if (!(makeItFit(pagesWide,pagesHigh,pageWidthIn,pageHeightIn,lockHeaders))){
						changeBack();
						return false;
					}
					
					
					if (trackRunTimes == "on"){
						myDate = new Date();
						applyStart = myDate.getTime();
					}

					captionTable = pageDocFragment.createElement("TABLE");
					if(numTablesPrinted != 1){
						captionTable.style.pageBreakBefore="always";
					}
					captionTHead = pageDocFragment.createElement("THEAD");
					captionTHead.style.display = "table-header-group";
					
					captionRow = pageDocFragment.createElement("TR");
					captionCell = pageDocFragment.createElement("TD");
					captionCell.appendChild(repHeaderTable.cloneNode(true));
					captionRow.appendChild(captionCell);
					captionTHead.appendChild(captionRow);
					
					if(foundCaption){
						captionRow = pageDocFragment.createElement("TR");
						captionCell = pageDocFragment.createElement("TD");
						captionCell.appendChild(captionDiv);
						captionRow.appendChild(captionCell);
						captionTHead.appendChild(captionRow);
					}
						
					captionTable.appendChild(captionTHead);						
					captionTBody = pageDocFragment.createElement("TBODY");
					
					for (k = 0; k < newTables.length;k++){

						gridTable = pageDocFragment.createElement("TABLE");
						if( k !=0){
							gridTable.style.pageBreakBefore="always";
						}
						gridRow = gridTable.insertRow();
						gridCell = gridRow.insertCell();
						if(isPrintGridLines == 'true'){
							gridCell.style.backgroundColor = "#000000";
						}
						applyFonts(newTables[k],(k+1));
						gridCell.appendChild(newTables[k]);
						captionRow = pageDocFragment.createElement("TR");
						captionCell = pageDocFragment.createElement("TD");
						
						captionCell.appendChild(gridTable);
						captionRow.appendChild(captionCell);
						captionTBody.appendChild(captionRow);
					}

					captionTable.appendChild(captionTBody);
					pageDocFragment.appendChild(captionTable);

					if (trackRunTimes == "on"){
						myDate = new Date();
						applyEnd = myDate.getTime();
				
						myDate = new Date();
						endTime = myDate.getTime();
					}
					document.printUI.closeProgressMeter();
					if (trackRunTimes == "on"){
						alert("Processed " + numCells + " Cells");
						alert("That Took:  " + (( endTime - startTime ) / 1000) + " seconds");						
						alert("Header Stripping Took: " + ((( stripEnd - stripStart) - ( breakEnd - breakStart)) / 1000) + " seconds");
						alert("Header Breaking Took: " + (( breakEnd - breakStart) / 1000) + " seconds");
						alert("Tables Creation Took: " + (( createEnd - createStart) / 1000) + " seconds");
						alert("Parsing Took: " + (( parseEnd - parseStart) / 1000) + " seconds");
						alert("Fitting Took: " + ((( fitEnd - fitStart)-( createEnd - createStart)-( parseEnd - parseStart)) / 1000) + " seconds");
						alert("Font Application Took: " + (( applyEnd - applyStart) / 1000) + " seconds");
						
					}
				} // else of (startTable.getAttribute("name") != run.arguments[i])
			} // else of (startTable == null)
		} // for loop of run arguments 
		document.body.innerHTML = '';
		resizeAlert();
		if(debug == "on"){
			document.body.appendChild(dataTable);
		}
		document.body.appendChild(pageDocFragment);
		return true;
	} // if (document.printUI.getResponse() == 1)
} // run()

// performs initialization for application
function initialization(){
	// make sure cookie values do not violate param restrictions.
	var paramList;
	paramList = document.printUI.getElementsByTagName('PARAM');
	for(m=0;m<paramList.length;m++){
		if(paramList[m].getAttribute("NAME") == 'maxpageswide'){
			if(parseInt(paramList[m].getAttribute("VALUE")) < parseInt(readCookie('pageswide'))){
				setCookie('pageswide', '1');
			}
		}
		if(paramList[m].getAttribute("NAME") == 'maxpagestall'){
			if(parseInt(paramList[m].getAttribute("VALUE")) < parseInt(readCookie('pagestall'))){
				setCookie('pagestall', '1');
			}
		}
	}
	pixelsPerIn = conversionMarker.offsetLeft;
} // initialization()

// locates and returns the report header table
function findHeader(tableList){
	var i;
	var headerTable;
	headerTable = document.getElementById("header");
	if (headerTable != null){
		return headerTable;
	} else {
		for (i=0;i<tableList.length;i++){
			if(tableList[i].className == 'SAPBEXBorderFlexBoxTtl' || tableList[i].className == 'SAPBEXBorderlessFlexBoxTtl'){
				return tableList[i];
			}
		}
	}
} // findHeader()

// retrieves caption for table if present.
function findCaption(currObj){
	var parentTabCount =0;
	var tdList;
	
	while (currObj.parentNode != null){
		currObj = currObj.parentNode;
		if (currObj.tagName == 'TABLE'){
			parentTabCount++;
			if (parentTabCount == 1){
				if(!(currObj.parentNode.className == "SAPBEXFlexBoxStdBg" || currObj.parentNode.className == "SAPBEXFlexBoxFormBg")){
					return false;
				}
			}
			
			if (parentTabCount == 3){
				tdList = currObj.rows(0).cells(0).getElementsByTagName("TD");
				tableCaption = tdList[tdList.length-1].innerHTML;
				return true;
			}
			
		}
	}	
	return false;
} // findCaption()

// retrieves location of 1st data cell.
function getFirstData(){
	var x,y;
	//alert(startTable.innerHTML);
	//alert("getting first data");
	for(x=0;x<startTable.rows.length;x++){
		for(y=0;y<startTable.rows(x).cells.length;y++){
			if(startTable.rows(x).cells(y).className == 'SAPBEXstdData' || startTable.rows(x).cells(y).className == 'SAPBEXstdDataOdd' || startTable.rows(x).cells(y).className == 'SAPBEXstdDataEmph' ||  startTable.rows(x).cells(y).className == 'SAPBEXstdDataEmphOdd' || startTable.rows(x).cells(y).className == 'SAPBEXaggData' || startTable.rows(x).cells(y).className == 'SAPBEXaggDataEmph'){
				dataCellx = x;
				dataCelly = y;
				if (debug == "on") {
					startTable.rows(dataCellx).cells(dataCelly).style.backgroundColor = "#FF0000";
					alert("First data cell \n\nRow: " + dataCellx + "\nColumn: " + dataCelly);
				}
				return true;
			}										
		}		
	}
	// if no key figures used to be able to find data cells, use first stdItem
	for(x=0;x<startTable.rows.length;x++){
		for(y=0;y<startTable.rows(x).cells.length;y++){
			if(startTable.rows(x).cells(y).className == 'SAPBEXstdItem'){
				dataCellx = x;
				dataCelly = y;
				if (debug == "on") {
					startTable.rows(dataCellx).cells(dataCelly).style.backgroundColor = "#FF0000";
					alert("First data cell \n\nRow: " + dataCellx + "\nColumn: " + dataCelly);
				}
				return true;
			}										
		}		
	}
	//alert("didn't find first data cell");
} // getFirstData()

// strips column headers from dataTable
function stripHeaders(){
	if (trackRunTimes == "on"){
		myDate = new Date();
		stripStart = myDate.getTime();
	}
	
	var i;
	var myrow;

	colHeadersTable=document.createElement("TABLE");
	for (i=0;i<dataCellx;i++){
		myrow=colHeadersTable.insertRow();
		myrow.replaceNode(dataTable.rows(0).cloneNode(true));
		dataTable.deleteRow(0);
	}
	
	breakColHeaders();

	if (trackRunTimes == "on"){
		myDate = new Date();
		stripEnd = myDate.getTime();
	}
}  // stripHeaders()

// calculates font size and page column mappings for resize
function makeItFit(pagesWide,pagesHigh,pageWidthIn,pageHeightIn,lockHeaders){
	if (trackRunTimes == "on"){
		myDate = new Date();
		fitStart = myDate.getTime();
	}
	
	var h,i,j;
	var totalWidthAcross, totalHeight;
	var newWidthAcross, newHeight;
	
	var padPixels = 0;
	var padPixelsHigh = 0;
	var padPixelsWide = 0;
	var initPagesWide,initPagesHigh;
	var adjustmentRatio,adjustmentHigh,adjustmentWide,colAdjustmentWide;
	var actualPagesWide = 1;
	var myDate;
	var FpxH,FptH,FptW;	
	var minPagesWide, minPagesHigh;
	var minFpt = 2.0;
	var minFpx;
	var lineAdjustment, lineAdjustmentWide;

	
	
	//determine how many times row headers will be needed and add width and height to table
	//adds buffer of 1 average cell width and height for every page over 1
	if (lockHeaders == 'true'){
		//if(((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop)/((pageHeightIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetTop)) > pagesHigh){
		if(((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop)/(pageHeightIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetTop/startTable.offsetHeight)))) > pagesHigh){
			totalHeight = startTable.offsetHeight + ((pagesHigh-1) * (startTable.rows(dataCellx).cells(dataCelly).offsetTop + startTable.offsetHeight/startTable.rows.length));
			for (j=0;j<dataCellx;j++){
				padPixelsHigh = (padPixelsHigh + pagesHigh*(parseInt(startTable.rows(j).lastChild.currentStyle.paddingTop.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingTop.length - 2))) + parseInt(startTable.rows(j).lastChild.currentStyle.paddingBottom.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingBottom.length - 2)))));
			}
		} else {
			//totalHeight = startTable.offsetHeight + (Math.ceil((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop) / ((pageHeightIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetTop)) - 1) * (startTable.rows(dataCellx).cells(dataCelly).offsetTop + startTable.offsetHeight/startTable.rows.length);
			totalHeight = (startTable.offsetHeight + (startTable.rows(dataCellx).cells(dataCelly).offsetTop + startTable.offsetHeight / startTable.rows.length) * (Math.ceil((startTable.offsetHeight-startTable.rows(dataCellx).cells(dataCelly).offsetTop)/(pageHeightIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetTop/startTable.offsetHeight))))-1));
			for (j=0;j<dataCellx;j++){
				//padPixelsHigh = (padPixelsHigh + Math.ceil((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop) / ((pageHeightIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetTop))*(parseInt(startTable.rows(j).lastChild.currentStyle.paddingTop.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingTop.length - 2))) + parseInt(startTable.rows(j).lastChild.currentStyle.paddingBottom.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingBottom.length - 2)))));
				padPixelsHigh = (padPixelsHigh + Math.ceil((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop) / (pageHeightIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetTop/startTable.offsetHeight))))*(parseInt(startTable.rows(j).lastChild.currentStyle.paddingTop.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingTop.length - 2))) + parseInt(startTable.rows(j).lastChild.currentStyle.paddingBottom.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingBottom.length - 2)))));
			}
		}
		if(((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft)/(pageWidthIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetLeft/startTable.offsetWidth)))) > pagesWide){
		//if(((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft)/((pageWidthIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetLeft)) > pagesWide){
			totalWidthAcross = startTable.offsetWidth + ((pagesWide-1) * (startTable.rows(dataCellx).cells(dataCelly).offsetLeft + startTable.offsetWidth/startTable.rows(dataCellx).cells.length));
			if(debug == "on"){
				alert("TWA 1");
			}
			for (j=0;j<dataCelly;j++){
				padPixelsWide = (padPixelsWide + pagesWide*(parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.length - 2))) + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.length - 2)))));
			}
		} else {
			// totalWidthAcross = startTable.offsetWidth + (Math.ceil((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft) / ((pageWidthIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetLeft)) - 1) * (startTable.rows(dataCellx).cells(dataCelly).offsetLeft + startTable.offsetWidth/startTable.rows(dataCellx).cells.length);
			totalWidthAcross = (startTable.offsetWidth + (startTable.rows(dataCellx).cells(dataCelly).offsetLeft + startTable.offsetWidth / startTable.rows(dataCellx).cells.length) * (Math.ceil((startTable.offsetWidth-startTable.rows(dataCellx).cells(dataCelly).offsetLeft)/(pageWidthIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetLeft/startTable.offsetWidth))))-1));

			if(debug == "on"){
				alert("TWA 2");
				alert("totalWidthAcross = " + totalWidthAcross);
				alert("startTable.offsetWidth = " + startTable.offsetWidth);
				//alert("(startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft) = " + (startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft));
				//alert("((pageWidthIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetLeft) = " + ((pageWidthIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetLeft));
			}
			for (j=0;j<dataCelly;j++){
				//padPixelsWide = (padPixelsWide + Math.ceil((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft) / ((pageWidthIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetLeft))                              *(parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.length - 2))) + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.length - 2)))));
				  padPixelsWide = (padPixelsWide + Math.ceil((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft) / (pageWidthIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetLeft/startTable.offsetWidth))))*(parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.length - 2))) + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.length - 2)))));
			}
			if(debug == "on"){
				alert("padPixelsWide: " + padPixelsWide);
			}
		}
	} else {		
		
		for (j=0;j<dataCellx;j++){
			padPixelsHigh = (padPixelsHigh + (parseInt(startTable.rows(j).lastChild.currentStyle.paddingTop.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingTop.length - 2))) + parseInt(startTable.rows(j).lastChild.currentStyle.paddingBottom.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingBottom.length - 2)))));
		}
		for (j=0;j<dataCelly;j++){
			padPixelsWide = (padPixelsWide + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.length - 2))) + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.length - 2))));
		}
		
		//if(((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop)/((pageHeightIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetTop)) > pagesHigh){
		if(((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop)/(pageHeightIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetTop/startTable.offsetHeight)))) > pagesHigh){
			totalHeight = startTable.offsetHeight + ((pagesHigh-1) * (startTable.offsetHeight/startTable.rows.length));
		} else {
			//totalHeight = startTable.offsetHeight + (Math.ceil((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop) / ((pageHeightIn*pixelsPerIn)-startTable.rows(dataCellx).cells(dataCelly).offsetTop)) - 1) * (startTable.offsetHeight/startTable.rows.length);
			totalHeight = startTable.offsetHeight + (Math.ceil(((startTable.offsetHeight - startTable.rows(dataCellx).cells(dataCelly).offsetTop)/(pageHeightIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetTop/startTable.offsetHeight))))) - 1) * (startTable.offsetHeight/startTable.rows.length);
		}
		if(((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft)/(pageWidthIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetLeft/startTable.offsetWidth)))) > pagesWide){
			totalWidthAcross = startTable.offsetWidth + ((pagesWide-1) * (startTable.offsetWidth/startTable.rows(dataCellx).cells.length));
			if(debug == "on"){
				alert("TWA 3");
			}
		} else {
			totalWidthAcross = startTable.offsetWidth + (Math.ceil(((startTable.offsetWidth - startTable.rows(dataCellx).cells(dataCelly).offsetLeft)/(pageWidthIn*pixelsPerIn*(1-(startTable.rows(dataCellx).cells(dataCelly).offsetLeft/startTable.offsetWidth))))) - 1) * (startTable.offsetWidth/startTable.rows(dataCellx).cells.length);
			if(debug == "on"){
				alert("TWA 4");
			}
		}
	}
	if(debug == "on"){
		alert("totalWidthAcross: " + totalWidthAcross);
		alert("totalHeight: " + totalHeight);
	}
	//resize to limiting dimension
	initPagesWide = (totalWidthAcross)/(pageWidthIn*pixelsPerIn);
	if(debug == "on"){
		alert("initPagesWide: " + initPagesWide);
	}
	adjustmentWide = pagesWide / initPagesWide / 1.1;	
	initPagesHigh = (totalHeight)/(pageHeightIn*pixelsPerIn);
	adjustmentHigh = pagesHigh / initPagesHigh;

	//padding for headers added above.
	padPixelsHigh = padPixelsHigh + (startTable.cellSpacing * startTable.rows.length * 2);
	padPixelsHigh = padPixelsHigh + 2; //borders
	for (j=dataCellx;j<startTable.rows.length;j++){
		padPixelsHigh = (padPixelsHigh + parseInt(startTable.rows(j).lastChild.currentStyle.paddingTop.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingTop.length - 2))) + parseInt(startTable.rows(j).lastChild.currentStyle.paddingBottom.substring(0,(startTable.rows(j).lastChild.currentStyle.paddingBottom.length - 2))));
	}
	
	padPixelsWide = padPixelsWide + (startTable.cellSpacing * startTable.rows(dataCellx).cells.length * 2);
	padPixelsWide = padPixelsWide + 2; //borders
	for (j=dataCelly;j<startTable.rows(dataCellx).cells.length;j++){
		padPixelsWide = (padPixelsWide + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingLeft.length - 2))) + parseInt(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.substring(0,(startTable.rows(dataCellx).cells(j).currentStyle.paddingRight.length - 2))));
	}

	minFpx = lookupFpx(minFpt);
	lineAdjustment = 1.18;
	if (lockHeaders == 'true'){
		FpxH = ((pagesHigh*pageHeightIn*pixelsPerIn)-padPixelsHigh)/(startTable.rows.length + ((pagesHigh-1)*(dataCelly+3)))/lineAdjustment;
		minPagesHigh = Math.ceil((((minFpx * lineAdjustment) * (startTable.rows.length - (dataCelly+3))) + padPixelsHigh)/(pageHeightIn*pixelsPerIn - (minFpx * lineAdjustment * (dataCelly+3))));
	} else {
		FpxH = ((pagesHigh*pageHeightIn*pixelsPerIn)-padPixelsHigh)/(startTable.rows.length + ((pagesHigh-1)*(3)))/lineAdjustment;
		minPagesHigh = Math.ceil((((minFpx * lineAdjustment) * (startTable.rows.length - 3)) + padPixelsHigh)/(pageHeightIn*pixelsPerIn - (minFpx * lineAdjustment * 3)));
	}
	
	FptH = lookupFpt(FpxH);

	lineAdjustmentWide = 1.1;
	if (adjustmentWide >= 1 ){
		adjustmentWide = 1 / lineAdjustmentWide;
		FptW = getPoints(startTable.rows(dataCellx).cells(dataCelly).currentStyle.fontSize) / lineAdjustmentWide;
	} else {
		FptW = (((pagesWide*pageWidthIn*pixelsPerIn)-padPixelsWide)*getPoints(startTable.rows(dataCellx).cells(dataCelly).currentStyle.fontSize))/(totalWidthAcross-padPixelsWide) / lineAdjustmentWide;
		if(debug == "on"){
			alert("minFpt: " + minFpt + "\nlineAdjustmentWide: " + lineAdjustmentWide + "\nstartTable.offsetWidth: " + startTable.offsetWidth + "\ntotalWidthAcross: " + totalWidthAcross + "\npadPixelsWide: " + padPixelsWide + "\ntotalWidthAcross-padPixelsWide" + (totalWidthAcross-padPixelsWide));
		}
		//minPagesWide = pagesWide + 1;
		minPagesWide = Math.ceil(((minFpt * lineAdjustmentWide * (totalWidthAcross-padPixelsWide)) / getPoints(startTable.rows(dataCellx).cells(dataCelly).currentStyle.fontSize) + padPixelsWide) / (pageWidthIn*pixelsPerIn));
	}
	
	if (FptH < FptW){
		Fpt = FptH;
	} else {
		Fpt = FptW;
	}
	
	if (Fpt < minFpt) {
		if ((FptW < minFpt) && (minPagesWide > errMinPagesWide)){
			//minPagesWide = pagesWide + 1;
			errMinPagesWide = minPagesWide;
		}
		if ((FptH < minFpt) && (minPagesHigh > errMinPagesHigh)){
			errMinPagesHigh = minPagesHigh;
		}
		if((minPagesHigh > pagesHigh) && (minPagesWide > pagesWide)) {
			if(debug == "on"){
				alert("pages high and wide adjusted \n\nOld pages high: " + pagesHigh + "\nNew pages high: " + minPagesHigh + "\n\nOld pages wide: " + pagesWide + "\nNew pages wide: " + minPagesWide);
			}
			makeItFit(minPagesWide,minPagesHigh,pageWidthIn,pageHeightIn,lockHeaders);
		} else if(minPagesHigh > pagesHigh) {
			if(debug == "on"){
				alert("pages high adjusted \n\nOld pages high: " + pagesHigh + "\nNew pages high: " + minPagesHigh);
			}
			makeItFit(pagesWide,minPagesHigh,pageWidthIn,pageHeightIn,lockHeaders);
		} else {
			if(debug == "on"){
				alert("pages wide adjusted \n\nOld pages wide: " + pagesWide + "\nNew pages wide: " + minPagesWide);
			}
			makeItFit(minPagesWide,pagesHigh,pageWidthIn,pageHeightIn,lockHeaders);
		}
		return true;
	}
	
	var pageEndCol;
	if (FptH >= FptW){
		if (lockHeaders == 'true'){
			pageEndCol = dataCelly;
			for (j=dataCelly;j<startTable.rows(dataCellx).cells.length;j++){
				if((((startTable.rows(dataCellx).cells(dataCelly).offsetLeft + startTable.rows(dataCellx).cells(j).offsetLeft - startTable.rows(dataCellx).cells(pageEndCol).offsetLeft ) * adjustmentWide)+(startTable.rows(dataCellx).cells(j).offsetWidth * adjustmentWide)) > (pageWidthIn * pixelsPerIn)){
					pageEndCol = j;
					endColMap[actualPagesWide-1] = (pageEndCol - 1);
					actualPagesWide++; 
				}
			}
		} else {
			pageEndCol = 0;
			for (j=0;j<startTable.rows(dataCellx).cells.length;j++){
				if((((startTable.rows(dataCellx).cells(j).offsetLeft - startTable.rows(dataCellx).cells(pageEndCol).offsetLeft ) * adjustmentWide)+(startTable.rows(dataCellx).cells(j).offsetWidth * adjustmentWide)) > (pageWidthIn * pixelsPerIn)){
					pageEndCol = j;
					endColMap[actualPagesWide-1] = (pageEndCol - 1);
					actualPagesWide++; 
				}
			}
		}
	} else {
		colAdjustmentWide = ((((FptH*(totalWidthAcross-padPixelsWide))/getPoints(startTable.rows(dataCellx).cells(dataCelly).currentStyle.fontSize))+padPixelsWide)/(pageWidthIn*pixelsPerIn))/initPagesWide;
		if (lockHeaders == 'true'){
			pageEndCol = dataCelly;
			for (j=dataCelly;j<startTable.rows(dataCellx).cells.length;j++){
				if((((startTable.rows(dataCellx).cells(dataCelly).offsetLeft + startTable.rows(dataCellx).cells(j).offsetLeft - startTable.rows(dataCellx).cells(pageEndCol).offsetLeft ) * colAdjustmentWide)+(startTable.rows(dataCellx).cells(j).offsetWidth * colAdjustmentWide)) > (pageWidthIn * pixelsPerIn)){
					pageEndCol = j;
					endColMap[actualPagesWide-1] = (pageEndCol - 1);
					actualPagesWide++; 
				}
			}
		} else {
			pageEndCol = 0;
			for (j=0;j<startTable.rows(dataCellx).cells.length;j++){
				if((((startTable.rows(dataCellx).cells(j).offsetLeft - startTable.rows(dataCellx).cells(pageEndCol).offsetLeft ) * colAdjustmentWide)+(startTable.rows(dataCellx).cells(j).offsetWidth * colAdjustmentWide)) > (pageWidthIn * pixelsPerIn)){
					pageEndCol = j;
					endColMap[actualPagesWide-1] = (pageEndCol - 1);
					actualPagesWide++; 
				}
			}
		}
	}
	endColMap[actualPagesWide-1] = (startTable.rows(dataCellx).cells.length);
	actualPagesWide = endColMap.length;
	if(actualPagesWide > pagesWide){
		//minPagesWide = pagesWide + 1;
		//errMinPagesWide = minPagesWide;
		//makeItFit(pagesWide,minPagesHigh,pageWidthIn,pageHeightIn,lockHeaders);
		document.printUI.closeProgressMeter();
		alert ("Resize Failed. \nPlease Notify Development Pool.");
		return false;
	} else {
		createExtraTables(actualPagesWide);
		if(endColMap[0] < dataCelly){
			if(debug == "on"){
				alert("endColMap[0]: " + endColMap[0] + "\ndataCelly: " + dataCelly);
			}
			breakRowHeaders();
		}
		if (trackRunTimes == "on"){
			myDate = new Date();
			parseStart = myDate.getTime();
		}		
		parseTable(lockHeaders);
		if (trackRunTimes == "on"){
			myDate = new Date();
			parseEnd = myDate.getTime();
		}
		startTable.parentNode.removeChild(startTable);
	}
	
	if (trackRunTimes == "on"){
		myDate = new Date();
		fitEnd = myDate.getTime();
	}
	return true;
} // makeItFit()

// Displays alert if there was a resize error
function resizeAlert(){
	var errMsg;

	if ((errMinPagesWide != 0) || (errMinPagesHigh != 0)){
		errMsg = "\n";
		if (errMinPagesWide != 0){
			errMsg = errMsg + "Specified number of pages wide too small for data.\n";
			errMsg = errMsg + "Data will require at least " + errMinPagesWide + " pages wide.\n\n";
		}
		if (errMinPagesHigh != 0){
			errMsg = errMsg + "Specified number of pages tall too small for data.\n";
			errMsg = errMsg + "Data will require at least " + errMinPagesHigh + " pages tall.\n\n";
		}
		errMsg = errMsg + "Adjustments have been automatically applied.\n";
		alert(errMsg);
	}	
} // resizeAlert()

// Separates column headers when colSpan  > 1 to aid in column mapping.
function breakColHeaders(){

	if (trackRunTimes == "on"){
		myDate = new Date();
		breakStart = myDate.getTime();
	}
	var i,j,k;
	var nextCell;
	var currentColSpan;
	var td;
	var colHeadRowLength;
	colHeadRowLength = colHeadersTable.rows.length;
	for(i=0;i<colHeadRowLength;i++){
		for(j=0;j<colHeadersTable.rows[i].cells.length;j++){
			nextCell = colHeadersTable.rows[i].cells[j+1];
			currentColSpan = colHeadersTable.rows[i].cells[j].getAttribute("colSpan");
			colHeadersTable.rows[i].cells[j].setAttribute("colSpan","1");
			for(k=1;k<currentColSpan;k++){
				j++;
				td = document.createElement("td");
				td.style.backgroundColor = "#FFFFFF";
				td.innerText = "~!?~";
				if (nextCell == null)
					colHeadersTable.rows[i].appendChild(td);
				else
					colHeadersTable.rows[i].insertBefore(td,nextCell);
			}
		}
	}
	if (trackRunTimes == "on"){
		myDate = new Date();
		breakEnd = myDate.getTime();
	}
} // breakColHeaders()

// Separates row headers when colSpan  > 1 to aid in column mapping.
function breakRowHeaders(){

	if (trackRunTimes == "on"){
		myDate = new Date();
		breakStart = myDate.getTime();
	}
	var a,b,c;
	c = dataTable.rows(0).cells.length - dataCelly;
	
	var i,j,k;
	var nextCell;
	var currentColSpan;
	var td;

	for(i=0;i<dataTable.rows.length;i++){
		b = dataTable.rows(i).cells.length - c;
		for(j=0;j<b;j++){
			nextCell = dataTable.rows[i].cells[j+1];
			currentColSpan = dataTable.rows[i].cells[j].getAttribute("colSpan");
			dataTable.rows[i].cells[j].setAttribute("colSpan","1");
			for(k=1;k<currentColSpan;k++){
				j++;
				td = document.createElement("td");
				td.style.backgroundColor = "#FFFFFF";
				td.innerText = "~!?~";
				if (nextCell == null)
					dataTable.rows[i].appendChild(td);
				else
					dataTable.rows[i].insertBefore(td,nextCell);
			}
		}
	}
	if (trackRunTimes == "on"){
		myDate = new Date();
		breakEnd = myDate.getTime();
	}
} // breakRowHeaders()


//Parses dataTable and colHeadersTable based on endColMap
function parseTable(lockHeaders){
	var i,j,k,m,x,y,z;
	var mycell,mythead,myrow;
	var currpercent = 0;
	m = dataCelly;
	var docFragment = document.createDocumentFragment();
	var a,b,c;
	c = dataTable.rows(0).cells.length - dataCelly;
	x = newTables.length;
	y = dataTable.rows.length;
	z = colHeadersTable.rows.length;
	for (i = 0; i < x;i++){		
		for (j = 0; j < y;j++){
			b = dataTable.rows(j).cells.length - c;
			myrow=(dataTable.rows(j).cloneNode(false));
			if (lockHeaders == 'true'){
				for (a=0;a<b;a++){
					//mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(true));
					
					if(dataTable.rows(j).cells(a).innerHTML != "~!?~"){
						mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(true));
					} else if (a == m){
						mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(false));
						mycell.innerHTML = "&nbsp;";
					} 
					else {
						mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
					}
					//mycell.style.fontSize = Fpt + 'pt';
				}
				for (k = (m-dataCelly+b);k < (endColMap[i]-dataCelly+b);k++){
					mycell = myrow.appendChild(dataTable.rows(j).cells(k).cloneNode(true));
					//mycell.style.fontSize = Fpt + 'pt';
				}
			} else {
				if (i == 0) {
					for (a=0;((a<b) && (a<(endColMap[i]-dataCelly+b)));a++){
						//mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(true));
						
						if(dataTable.rows(j).cells(a).innerHTML != "~!?~"){
							mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(true));
						} else if (a == m){
							mycell = myrow.appendChild(dataTable.rows(j).cells(a).cloneNode(false));
							mycell.innerHTML = "&nbsp;";
						} 
						else {
							mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
						}
						//mycell.style.fontSize = Fpt + 'pt';
					}
				}
				for (k = (m-dataCelly+b);k < (endColMap[i]-dataCelly+b);k++){
					if(k >= b){
						mycell = myrow.appendChild(dataTable.rows(j).cells(k).cloneNode(true));
					} else{
						if(dataTable.rows(j).cells(k).innerHTML != "~!?~"){
							mycell = myrow.appendChild(dataTable.rows(j).cells(k).cloneNode(true));
						} else if (k == (m-dataCelly+b)){
							if(debug == "on"){
								alert("added cell");
							}
							mycell = myrow.appendChild(dataTable.rows(j).cells(k).cloneNode(false));
							mycell.innerHTML = "&nbsp;";
						} 
						else {
							mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
						}
					}
					//mycell.style.fontSize = Fpt + 'pt';
				}
			}
			docFragment.appendChild(myrow);
			if (Math.round(((j+1)*(i+1)/(x*y))*100) > currpercent){
				document.printUI.setPercent(Math.round(((j+1)*(i+1)/(x*y))*100));
				currpercent = Math.round(((j+1)*(i+1)/(x*y))*100);
			}
		}
		
		newTables[i].tBodies(0).appendChild(docFragment);
		mythead = newTables[i].createTHead();
		if (lockHeaders == 'true'){
			mythead.style.display = "table-header-group";
		}
		for (j = 0; j < z;j++){
			myrow=(dataTable.rows(j).cloneNode(false));
			b = colHeadersTable.rows(j).cells.length - c;
			if (lockHeaders == 'true'){
				for (a=0;a<b;a++){					
					//myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(true));
					if(colHeadersTable.rows(j).cells(a).innerHTML != "~!?~"){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(true));
					} else if (a == m){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(false));
						mycell.innerHTML = "&nbsp;";
					} 
					else {
						mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
					}
				}
				for (k = (m-dataCelly+b);k < (endColMap[i]-dataCelly+b);k++){
					if(colHeadersTable.rows(j).cells(k).innerHTML != "~!?~"){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(k).cloneNode(true));
					} else if (k == m){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(k).cloneNode(false));
						mycell.innerHTML = "&nbsp;";
					} 
					else {
						mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
					}
				}
			} else {
				if (i == 0) {
					for (a=0;((a<b) && (a<(endColMap[i]-dataCelly+b)));a++){
						//myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(true));
						if(colHeadersTable.rows(j).cells(a).innerHTML != "~!?~"){
							mycell = myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(true));
						} else if (a == m){
							mycell = myrow.appendChild(colHeadersTable.rows(j).cells(a).cloneNode(false));
							mycell.innerHTML = "&nbsp;";
						} 
						else {
							mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
						}
					}
				}
				for (k = (m-dataCelly+b);k < (endColMap[i]-dataCelly+b);k++){
					if(colHeadersTable.rows(j).cells(k).innerHTML != "~!?~"){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(k).cloneNode(true));
					} else if (k == m){
						mycell = myrow.appendChild(colHeadersTable.rows(j).cells(k).cloneNode(false));
						mycell.innerHTML = "&nbsp;";
					} 
					else {
						mycell.setAttribute("colSpan",(mycell.getAttribute("colSpan") + 1));
					}
				}
			}
			docFragment.appendChild(myrow);
		}
		
		myrow = document.createElement("TR");
		mycell = document.createElement("TD");
		mycell.style.height = "1px";
		mycell.style.backgroundColor = "#000000";
		mycell.setAttribute("colSpan",(endColMap[i]-dataCelly+b));
		myrow.appendChild(mycell);
		docFragment.appendChild(myrow);
		
		newTables[i].tHead.appendChild(docFragment);
		m = endColMap[i];
	}
} // parseTable()

// creates a table for each print page wide.
function createExtraTables(numTables){
	if (trackRunTimes == "on"){
		myDate = new Date();
		createStart = myDate.getTime();
	}
	
	var table,tbody;
	var i,j;
	var tr;
	
	for (i = 0; i < numTables;i++){
		table=(startTable.cloneNode(false));
		table.style.width = "0px";
		tbody = document.createElement("TBODY");
		table.appendChild(tbody);
		newTables[i] = table;
	}	
	if (trackRunTimes == "on"){
		myDate = new Date();
		createEnd = myDate.getTime();
	}
} // createExtraTables()

//applies fonts to headers
function applyFonts(Table,tabnum){	
	var a,h,i,x,y,z;
	var currpercent = 0;
	var tdList;

	Table.style.fontSize = Fpt + 'pt';
	for(h=0;h<dataCellx;h++){
		x=Table.rows(h).cells.length;
		for(i=0;i<x;i++){
			tdList = Table.rows(h).cells(i).getElementsByTagName("TD");
			Table.rows(h).cells(i).style.fontSize = Fpt + 'pt';
			z = tdList.length;
			//remove unaccounted padding from any internal tables of header.
			for(j=0;j<z;j++){
				tdList[j].style.fontSize = Fpt + 'pt';
				tdList[j].style.paddingLeft = '0px';
				tdList[j].style.paddingRight = '0px';
				tdList[j].style.paddingTop = '0px';
				tdList[j].style.paddingBottom = '0px';
			}
		}
	}
	Table.cellSpacing = 1;
	//Table.border = '1px';
		
	/*
	if(isPrintGridLines == 'true'){		
		Table.borderColor = '#000000';
	} else {
		Table.borderColor = '#FFFFFF';
	}
	*/
} // applyFonts()

//converts font-size from px to pt
function lookupFpt(Fpx){
	var ptPerPx;

	if(pixelsPerIn == 96){
		ptPerPx = 4/3;
	} else if (pixelsPerIn == 120){
		ptPerPx = 5/3;
	} else if (pixelsPerIn == 72) {
		ptPerPx = 1;
	}
	
	return Fpx/ptPerPx;
} // lookupFpt()

//converts font-size from pt to px
function lookupFpx(Fpt){
	var ptPerPx;

	if(pixelsPerIn == 96){
		ptPerPx = 4/3;
	} else if (pixelsPerIn == 120){
		ptPerPx = 5/3;
	} else if (pixelsPerIn == 72) {
		ptPerPx = 1;
	}
	
	return Fpt*ptPerPx;
} // lookupFpx()

// returns font-size in pt
function getPoints(name){
	var ptPerPx;

	if(pixelsPerIn == 96){
		ptPerPx = 4/3;
	} else if (pixelsPerIn == 120){
		ptPerPx = 5/3;
	} else if (pixelsPerIn == 72) {
		ptPerPx = 1;
	}
		
	if (name.indexOf("pt") == -1){
		if (name.toUpperCase() == "XX-SMALL")
			//return 7.5;
			return 9*ptPerPx;
		else if (name.toUpperCase() == "X-SMALL")
			//return 10;
			return 11*ptPerPx;
		else if (name.toUpperCase() == "SMALL")
			//return 12;
			return 13*ptPerPx;
		else if (name.toUpperCase() == "MEDIUM")
			//return 13.5;
			return 16*ptPerPx;
		else if (name.toUpperCase() == "LARGE")
			//return 18;
			return 19*ptPerPx;
		else if (name.toUpperCase() == "X-LARGE")
			//return 24;
			return 23*ptPerPx;
		else if (name.toUpperCase() == "XX-LARGE")
			//return 36;
			return 28*ptPerPx;
		else
		return 0;
	}
	else{
		return name.substring(0,(name.length - 2 ));
	}	
} // getPoints()

//displays print page setup properties
function displayPageSetup(){
	var WBString = "<OBJECT ID=\"WB\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>";
	document.body.insertAdjacentHTML("beforeEnd",WBString);
	WB.ExecWB(8, 1);
} // displayPageSetup()

// sets cookies to remember selected values
function setCookie(name, value){
	var date = new Date();
	date.setYear(date.getYear() + 1);
	document.cookie = name + "=" + escape(value) + " ;expires=" + date;
}  // setCookie()

// reads cookies and returns requested value
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
} // readCookie()

// restores page to original state
function changeBack(){
	//WB.ExecWB(22, 1);
	history.go();
} //changeBack()
