// aimsPrint.js
/*
*  JavaScript template file for ArcIMS HTML Viewer
*		dependent on aimsXML.js, ArcIMSparam.js, aimsCommon.js, aimsMap.js,
*		aimsLayers.js, aimsDHTML.js
*		aimsClick.js, aimsNavigation.js,
*		aimsLegend.js
*/

aimsPrintPresent=true;

var printTitle = titleList[4];
var printMapURL="";
var printOVURL="";
var printLegURL="";
var PrintMode="";
var legVis2=false;

/*
***************************************************************************************

Print functions 

***************************************************************************************
*/
//get today's date
function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
}

function getDateStr(){
   	var today = new Date()
    var year = today.getYear()
    var todayStr = GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
}

// display print form
function printIt() {
	hideLayer("measureBox");
	if (useQueryFrame) {
		parent.QueryFrame.document.location = parent.MapFrame.appDir + "printform.htm";
	} else {
		var Win1 = open("printform.htm","PrintFormWindow","width=575,height=150,scrollbars=yes,resizable=yes");
	}
}

// create web page for printing
// first get Map
function getPrintMap(title,PrintMode,pQuality,createdBy) {
	showRetrieveMap();
	PrintType=PrintMode;
	printTitle=title;
	group=createdBy;
	//alert("created by in aimsPrint " + createdBy);
	var tempWidth = iWidth;
	var tempHeight = iHeight;
		if (PrintType=="portrait") {
			iWidth=450;
			iHeight=650;
			} else {
			iWidth=700;
			iHeight=475;
		}
    iWidth = Math.floor( parseFloat(iWidth) * pQuality );
    iHeight = Math.floor( parseFloat(iHeight) * pQuality );
	legVis2=legendVisible;
	if (aimsLegendPresent) legendVisible=true;
	var theString = writeXML();
	iWidth=tempWidth;
	iHeight = tempHeight;
	legendVisible = legVis2;
	sendToServer(imsURL,theString,101);
	tempWidth=null;
	tempHeight=null;
	theString=null;
}
// second, get OVMap
function getPrintOV() {
	var tempWidth = i2Width;
	var tempHeight = i2Height;
	i2Width=190;
	i2Height=150;
	var tempDraw=drawOVExtentBox;
	drawOVExtentBox=true;
	var theString = writeOVXML();
	drawOVExtentBox=tempDraw;
	i2Width=tempWidth;
	i2Height = tempHeight;
	sendToServer(imsOVURL,theString,102);
	tempWidth=null;
	tempHeight=null;
	theString=null;
}
// third, get Legend
function getPrintLegend() {
	//  waiting for Legend tags
	if (printLegURL=="") printLegURL = "images/nolegend.gif";
	writePrintPage(PrintType,group);
}
// fourth, write the web page (landscape)
function writePrintPage(PrintType,group) {
//alert("created by in aimsPrint, write map " + group);
switch(PrintType) {
	case "landscape":
		var Win1 = open("","PrintPage");
		//Win1.document.open();
		Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
		Win1.document.writeln('	<title>' + titleList[5] + '</title>');
		//css settings for landscape printing, not yet supported by most browsers
		/*Win1.document.writeln('<style type="text/css">');
		Win1.document.writeln('@page orientation {');
		Win1.document.writeln('size: landscape;');
		Win1.document.writeln('margin: 3cm;}');
		Win1.document.writeln('BODY {page: orientation;}');
		Win1.document.writeln('</style>');*/
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.document.writeln('<FONT FACE="Arial"><B>');
		Win1.document.writeln('<TABLE WIDTH="900" BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
		Win1.document.writeln('	<TR>'); //changed colspan to 3
		Win1.document.writeln('		<TH COLSPAN="3">' + printTitle + '</TH>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR valign="top">');
		Win1.document.write('		<TD WIDTH="700" HEIGHT="450"'); //added colspan
		if (hasOVMap) Win1.document.write(' ROWSPAN="2" colspan="2"');
			Win1.document.writeln('>');
			Win1.document.writeln('			<IMG SRC="' + printMapURL + '" WIDTH=700 HEIGHT=475 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
			Win1.document.writeln('		</TD>');
		if (hasOVMap) {
			Win1.document.writeln('		<TD WIDTH="200" HEIGHT="150" ALIGN="CENTER" valign="top">');
			Win1.document.writeln('			<IMG SRC="' + printOVURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
			Win1.document.writeln('		</TD>');
		}
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>'); //added rowspan and removed height
		Win1.document.writeln('		<TD width="300" ALIGN="TOP" VALIGN="TOP" rowspan="3">');
		Win1.document.writeln('			<IMG SRC="' + printLegURL + '" WIDTH=170 height=470 HSPACE=0 VSPACE=0 BORDER=0 align="top" ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('		<TD height="350" valign="top"></TD>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR valign="top">');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
		if (group=="") {
		Win1.document.writeln('<P>Map created on ');
		Win1.document.writeln(getDateStr());
		Win1.document.writeln('</P>');
		} else {
		Win1.document.writeln('<P>Map created by ' + group + ' on ');
		Win1.document.writeln(getDateStr());
		Win1.document.writeln('</P>');
		}
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
		Win1.document.writeln('			<IMG SRC="http://' + hostName + appDir + 'images/NPWS.jpg" WIDTH=78 HEIGHT=77 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>'); //changed colspan
		Win1.document.writeln('		<TD ALIGN="TOP" VALIGN="TOP" COLSPAN="2"><FONT FACE="Arial" size="-2">' + msgList[124] + msgList[125] + '</FONT>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('</TABLE>');
		Win1.document.writeln('</B></FONT>');
		//Win1.document.writeln("<SCRIPT language=Javascript>window.print();</SCRIPT>");
		Win1.document.writeln('</body></html>');
		Win1.document.close();
	
		legendVisible=legVis2;
		Win1=null;
		PrintMode="";
		hideRetrieveMap();
	break;
	
	case "portrait": 
		var Win1 = open("","PrintPage");
		//Win1.document.open();
		Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
		Win1.document.writeln('	<title>' + titleList[5] + '</title>');
		Win1.document.writeln('</head>');
		Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
		Win1.document.writeln('<FONT FACE="Arial"><B>');
		Win1.document.writeln('<TABLE WIDTH="670" BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
		Win1.document.writeln('	<TR>');
		Win1.document.writeln('		<TH COLSPAN="3">' + printTitle + '</TH>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>');
		Win1.document.writeln('		<TD WIDTH="450" HEIGHT="650" rowspan="2" colspan="2">');
		Win1.document.writeln('			<IMG SRC="' + printMapURL + '" WIDTH=450 HEIGHT=650 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('		<TD HEIGHT="150" ALIGN="CENTER">');
		Win1.document.writeln('			<IMG SRC="' + printOVURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP" rowspan="3">');
		Win1.document.writeln('			<IMG SRC="' + printLegURL + '" WIDTH=170 height=470 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('		<TD height="500" valign="top"></TD>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
		if (group=="") {
		Win1.document.writeln('<P>Map created on ');
		Win1.document.writeln(getDateStr());
		Win1.document.writeln('</P>');
		} else {
		Win1.document.writeln('<P>Map created by ' + group + ' on ');
		Win1.document.writeln(getDateStr());
		Win1.document.writeln('</P>');
		}
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
		Win1.document.writeln('			<IMG SRC="http://' + hostName + appDir + 'images/NPWS.jpg" WIDTH=78 HEIGHT=77 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
		Win1.document.writeln('		</TD>');
		Win1.document.writeln('<TD ALIGN="CENTER" VALIGN="TOP">&nbsp; </TD>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('	<TR>');
		Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="2"><FONT FACE="Arial" size="-2">' + msgList[124] + msgList[125] + '</FONT>');
		Win1.document.writeln('	</TR>');
		Win1.document.writeln('</TABLE>');
		Win1.document.writeln('</B></FONT>');
		//Win1.document.writeln("<SCRIPT language=Javascript>window.print();</SCRIPT>");
		//Win1.document.writeln('</body></html>');
		Win1.document.close();
	
		legendVisible=legVis2;
		Win1=null;
		PrintMode="";
		hideRetrieveMap();
	break;
		
	default : 
	//alert("no print mode set");
	}
}