// 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 UseAimsPrintLegend = false;
var printTitle = titleList[4];
var thePreparedby = "";
var printMapURL="";
var printOVURL="";
var printLegURL="";
var printlegend = true;
var printsize="";
var printres="";
var printlayout="";
var mmWidth;
var mmHeight;	
var movWidth;
var movHeight;
var mlgWidth;
var txtScaleText = "";
var curDate = new Date();
var cmonth = (curDate.getMonth()+1);
var cday = curDate.getDate();
var cyear = curDate.getFullYear();
var cDate ="";
cDate = (cmonth + "/" + cday + "/" + cyear);
var legVis2=false;

/*
***************************************************************************************

Print functions 

***************************************************************************************
*/


// display print form
function printIt() {
	hideLayer("measureBox");
	if (useTextFrame) {
		parent.TextFrame.document.location = "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,preparedby,showlegend,pagesize, resolution, layout,txtMapScale) {
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	var tempUseAimsPrintLegend
	var temphasOVMap = hasOVMap
	var tempLeft = eLeft;
	var tempRight = eRight;
	var tempTop = eTop;
	var tempBottom = eBottom;
	txtScaleText = "";
	showRetrieveMap();
	thePreparedby = preparedby;
	printTitle = title;
	printlegend = showlegend;
	printlayout = layout;
	printres = resolution;
	printsize = pagesize;	
	legVis2=legendVisible;
	if (printlegend) {
		tempUseAimsPrintLegend = UseAimsPrintLegend;
		LegendListOff();
		UseAimsPrintLegend = true;
		if (aimsLegendPresent) legendVisible=true;	
	}else
	{
		legendVisible=false;
		temphasOVMap=false;
	}
	showRetrieveMap();		
	if (txtMapScale != "") 
	{
		
		if (printsize == "small"  && printlayout == "Portrait")
		{
			mmWidth = 615;
			mmHeight = 610;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
						
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "large"  && printlayout == "Portrait")
		{
			mmWidth = 855;
			mmHeight = 1185;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "D" && printlayout == "Portrait")
		{
			mmWidth = 1900;
			mmHeight = 2700;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "E"  && printlayout == "Portrait")
		{
			mmWidth = 3000;
			mmHeight = 3850;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		
		else if(printsize == "small" && printlayout == "Landscape")
		{
			//iWidth=660;
			//iHeight=660;
			mmWidth = 855;
			mmHeight = 380;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
	
		else if(printsize == "large" && printlayout == "Landscape")
		{
			mmWidth = 1430;
			mmHeight = 615;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}
		else if(printsize == "D" && printlayout == "Landscape")
		{
			mmWidth = 3000;
			mmHeight = 1620
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}
		
		else if(printsize == "E" && printlayout == "Landscape")
		{
			mmWidth = 3960;
			mmHeight = 2720;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}		
		ZoomToScale(parseInt(txtMapScale));	
		
	}
	else
	{
		
		if (printsize == "small" && printres == "Low" && printlayout == "Portrait")
		{
			mmWidth = 615;
			mmHeight = 610;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
						
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "small" && printres == "Normal" && printlayout == "Portrait")
		{
			mmWidth = 615;
			mmHeight = 610;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;
		}
		else if(printsize == "small" && printres == "High" && printlayout == "Portrait")
		{
			mmWidth = 615;
			mmHeight = 610;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
		else if(printsize == "large" && printres == "Low" && printlayout == "Portrait")
		{
			mmWidth = 855;
			mmHeight = 1185;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "large" && printres == "Normal" && printlayout == "Portrait")
		{
			mmWidth = 855;
			mmHeight = 1185;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;	
		}
		else if(printsize == "large" && printres == "High" && printlayout == "Portrait")
		{
			mmWidth = 855;
			mmHeight = 1185;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
			else if(printsize == "D" && printres == "Low" && printlayout == "Portrait")
		{
			mmWidth = 1900;
			mmHeight = 2700;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "D" && printres == "Normal" && printlayout == "Portrait")
		{
			mmWidth = 1903;//1950;//1900;
			mmHeight = 2834;//2880;//2750;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;	
		}
		else if(printsize == "D" && printres == "High" && printlayout == "Portrait")
		{
			mmWidth = 1900;
			mmHeight = 2750;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
		else if(printsize == "E" && printres == "Low" && printlayout == "Portrait")
		{
			mmWidth = 3000;
			mmHeight = 3850;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "E" && printres == "Normal" && printlayout == "Portrait")
		{
			mmWidth = 3000;
			mmHeight = 3850;		
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;	
		}
		else if(printsize == "E" && printres == "High" && printlayout == "Portrait")
		{
			mmWidth = 3000;
			mmHeight = 3850;	
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}	
		else if(printsize == "small" && printres == "Low" && printlayout == "Landscape")
		{
			mmWidth = 855;
			mmHeight = 380;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth;
			iHeight=mmHeight;
		}
		else if(printsize == "small" && printres == "Normal" && printlayout == "Landscape")
		{
			mmWidth = 855;
	
			mmHeight = 380;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight* 1.5;
		}
		else if(printsize == "small" && printres == "High" && printlayout == "Landscape")
		{
			mmWidth = 855;
			mmHeight = 380;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
		else if(printsize == "large" && printres == "Low" && printlayout == "Landscape")
		{
			mmWidth = 1430;
			mmHeight = 615;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}
		else if(printsize == "large" && printres == "Normal" && printlayout == "Landscape")
		{
			mmWidth = 1430;
			mmHeight = 615;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;
		}
		else if(printsize == "large" && printres == "High" && printlayout == "Landscape")
		{
			mmWidth = 1430;
			mmHeight = 615;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
		else if(printsize == "D" && printres == "Low" && printlayout == "Landscape")
		{
			mmWidth = 3000;
			mmHeight = 1620
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}
		else if(printsize == "D" && printres == "Normal" && printlayout == "Landscape")
		{
			mmWidth = 3000;
			mmHeight = 1620
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;
		}
		else if(printsize == "D" && printres == "High" && printlayout == "Landscape")
		{
			mmWidth = 3000;
			mmHeight = 1620
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}
		else if(printsize == "E" && printres == "Low" && printlayout == "Landscape")
		{
			mmWidth = 3960;
			mmHeight = 2720;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			iWidth=mmWidth ;
			iHeight=mmHeight ;
		}
		else if(printsize == "E" && printres == "Normal" && printlayout == "Landscape")
		{
			mmWidth = 3960;
			mmHeight = 2720;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			
			iWidth=mmWidth * 1.5;
			iHeight=mmHeight * 1.5;
		}
		else if(printsize == "E" && printres == "High" && printlayout == "Landscape")
		{
			mmWidth = 3960;
			mmHeight = 2720;
			movWidth = 250;
			movHeight = 235;
			mlgWidth = mmWidth - movWidth;
			if (!printlegend)mmHeight +=movHeight-15
			iWidth=mmWidth * 2;
			iHeight=mmHeight * 2;
		}	
		
	}
		
	var theString ;	
	if (printlegend) {		
		theString= writeXML();
		sendToServer(imsURL,theString,101);	
		//txtScaleText = "1 inch = " + parseInt(((eRight-eLeft)/iWidth)*97.69230769) + " feet";
		legendVisible = legVis2;
		hasOVMap=temphasOVMap 
		iWidth = tempWidth;
		iHeight = tempHeight;
		UseAimsPrintLegend = tempUseAimsPrintLegend;
		LegendListOn();
	}
	else
	{
		
		theString= writeXML();		
		sendToServer(imsURL,theString,101);	
		//txtScaleText = "1 inch = " + parseInt(((eRight-eLeft)/iWidth)*97.69230769) + " feet";
		legendVisible = legVis2;
		hasOVMap=temphasOVMap 
		iWidth=tempWidth;
		iHeight = tempHeight;
	}
	
	eLeft = tempLeft  ;
	eRight=tempRight  ;
	eTop=tempTop ;
	eBottom=tempBottom  ;	
	tempWidth=null;
	tempHeight=null;
	theString=null;
	
	
}
function ZoomToScale(txtMapScale){  
  
   txtScaleText = "1 inch = " + txtMapScale + " feet";
  
   var txtMapScale = txtMapScale*12;
   var txtMapWidth;
   if ( iHeight < iWidth) {
      txtMapWidth = iHeight
   } else {
      txtMapWidth = iWidth
   }

   var midX = ((eLeft + eRight) / 2);
   var midY = ((eTop + eBottom) / 2);
   var txtDeltaScale = (txtMapScale * ((txtMapWidth/97.69230769)/12)) * .5;
   eLeft = midX - txtDeltaScale ;
   eRight =  midX + txtDeltaScale ;
   eBottom = midY - txtDeltaScale ;
   eTop =  midY + txtDeltaScale ;  
   
   

 }
// second, get OVMap
function getPrintOV() {
	var tempWidth = i2Width;
	var tempHeight = i2Height;
	i2Width=130;
	i2Height=110;
	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();
}
// fourth, write the web page

function addPrintLegendToMap() {
	var tmptitle = printTitle  
	var legString = '<LEGEND   font="' + legFont + '" width="' + mlgWidth + '" height="' + movHeight + '" ';
	legString += 'autoextend="false"  reverseorder="true" cansplit="true" splittext =" " cellspacing="2" columns="3" backgroundcolor="255,255,255"';
	//layerfontsize="8" valuefontsize ="8"
	if (hideLayersFromList) {
		legString += '>\n<LAYERS>\n';
		for (var legvar=0;legvar<noListLayer.length;legvar++) {
			if (noListLayer[legvar]) legString += '<LAYER id="' + LayerID[legvar] + '" />\n';
		}
		legString += '</LAYERS>\n';
		legString += '</LEGEND>\n';
	} else {
		legString += ' />\n';
	}
	if (drawLegendOnly) legString = legString + '<DRAW map="false" />\n';
	
	return legString;
	
}


function writePrintPage() 
{
	//alert(printOVURL);
var Win1 = open("","PrintPage");
			Win1.document.writeln('<html><meta http-equiv="Content-Type" content="text/html; charset=' + charSet + '"><head>');
			Win1.document.writeln('<link REL="stylesheet" Type="text/css" HREF="' + appDir + 'sss.css" >');
			Win1.document.writeln('<title>Print Preview</title>');
			Win1.document.writeln('</head>');
			Win1.document.writeln('<body BGCOLOR="White" TEXT="#003366" LEFTMARGIN="0" TOPMARGIN="0" RIGHTMARGIN="0" BOTTOMMARGIN="0">');
			//Win1.document.writeln('<div align="center"><input  type="button" value=" Print Map " name="button1" onClick="print()" class="hide_button"></div>');
			Win1.document.writeln('<TABLE WIDTH="' + mmWidth + '" BORDER="0" CELLSPACING="0" CELLPADDING="0" NOWRAP style="border: 2px groove white">');
			Win1.document.writeln('	<TR>');
			Win1.document.write('		<TD BGCOLOR="white" WIDTH="' + mmWidth + '" HEIGHT="' + mmHeight + '" COLSPAN="2"');
		
			Win1.document.writeln('>');
			Win1.document.writeln('			<IMG SRC="' + printMapURL + '" WIDTH="' + mmWidth+ '" HEIGHT="' + mmHeight + '" HSPACE=0 VSPACE=0 BORDER=0>');
			Win1.document.writeln('		</TD>');			
			Win1.document.writeln('	</TR>');
			if (printlegend) {
				Win1.document.writeln('<TR>');
				Win1.document.writeln('<TD VALIGN="TOP">');
				Win1.document.writeln('<TABLE WIDTH="' + movWidth + '" HEIGHT="' + movHeight + '"  BORDER="0" CELLSPACING="0" CELLPADDING="0" style="border: 2px groove white">');
				Win1.document.writeln('<TR  height="120"><TD valign="top">');
				if (hasOVMap)Win1.document.writeln('<IMG  SRC="' + printOVURL + '"  HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
				Win1.document.writeln('</TD>'); 
				Win1.document.writeln('<TD valign="top" ><img src="' + appDir + 'images/ncseal.gif"   HSPACE=0 VSPACE=0 BORDER=0 alt=""></TD></TR>');
				Win1.document.writeln('<TR height="15"><TD colspan="2" valign="top"><b>' + printTitle + '</b></td></tr>');
				Win1.document.writeln('<TR height="15"><TD colspan="2" valign="top">' + txtScaleText + '<br>Prepared By: ' + thePreparedby + '<br>');
				Win1.document.writeln('Print Date: ' + cDate  + '</td></tr>');
				Win1.document.writeln('</TABLE></TD>');			
				Win1.document.writeln('	<TD VALIGN="TOP"><TABLE WIDTH="' + mlgWidth + '" HEIGHT="' + movHeight + '" BORDER="0" CELLSPACING="0" CELLPADDING="0" style="border: 2px groove white"><TR><TD>');
				Win1.document.writeln('			<IMG SRC="' + printLegURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
				Win1.document.writeln('		</TD>');
				Win1.document.writeln('	</TR>');
			}else
			{
			Win1.document.writeln('<TR ><TD colspan="2" valign="top"><b>' + printTitle + '</b>');
			Win1.document.writeln('&nbsp;&nbsp;&nbsp;' + txtScaleText + '&nbsp;&nbsp;&nbsp;Prepared By: ' + thePreparedby + '&nbsp;&nbsp;&nbsp;Print Date: ' + cDate  + '</td></tr>');
			}
			Win1.document.writeln('</table>');
			Win1.document.close();
	
	legendVisible=legVis2;
	Win1=null;
	hideRetrieveMap();
}

function LegendListOff(){
	for (var i=0;i<layerCount;i++) {
		for (var j=0;j<noLegendListArray.length;j++){
			if (LayerName[i]==noLegendListArray[j] ) {
				noListLayer[i] = true;
				
			}	
		}
	}	
}


function LegendListOn(){
	for (var i=0;i<layerCount;i++) {
		for (var j=0;j<noLegendListArray.length;j++){
			if (LayerName[i]==noLegendListArray[j]) {
				noListLayer[i] = false;
				
			}	
		}
	}	
}
