// 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
*/

var aimsPrintPresent=true;

var printTitle = "ArcIMS 9.0 HTML Viewer Map";
var printMapURL="";
var printOVURL="";
var printLegURL="";
var printmode="";
var legVis2=false;
var mapwidth="";
var mapheight="";

/*
***************************************************************************************

Print functions 

***************************************************************************************
*/

// create web page for printing
	// first get Map
function getPrintMap(title,userwidth,userheight) {
	mapwidth = userwidth;
	mapheight = userheight;
	showRetrieveMap();
	printTitle=title;
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	iWidth=450;
	iHeight=450;
	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();
}


function writePlotPage(URL) {
	var plotURL = URL;
	var mWidth = getMapWidth();
	var mHeight = getMapHeight();
	var Win1 = open("","PrintPage");
	Win1.document.writeln('<html><head>');
	Win1.document.writeln('	<title></title>');
	Win1.document.writeln('</head>');
	Win1.document.writeln('<body BGCOLOR=WHITE TEXT="BLACK" LEFTMARGIN=0 TOPMARGIN=0>');
	Win1.document.writeln('<TABLE WIDTH="80%" BORDER="0" CELLSPACING="0" CELLPADDING="0" align="center">');
	Win1.document.writeln('	<TR border="0" >');
	Win1.document.writeln('	<TD width="25%" rowspan="3" ALIGN="CENTER" VALIGN="Center">');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	<TD width="50%" ALIGN="CENTER" VALIGN="Center"><FONT SIZE="5" FACE="Garamond">');
	Win1.document.writeln('	<img src="/ArcIMS/Reedsburg/images/logo.gif">');
	Win1.document.writeln('	<Strong>CITY OF REEDSBURG</Strong>');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	<TD width="25%" rowspan="3" ALIGN="CENTER" VALIGN="Center">');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	</TR>');
	
	Win1.document.writeln('	<TR >');
	Win1.document.writeln('	<TD ALIGN="CENTER" VALIGN="Center"><FONT SIZE="3" FACE="Garamond">');
	Win1.document.writeln('	<strong>134 South Locust - Reedsburg, Wisconsin</strong>');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	</TR>');

	Win1.document.writeln('	<TR>');
	Win1.document.writeln('	<TD ALIGN="CENTER" VALIGN="Center"><FONT SIZE="3" FACE="Garamond">');
	Win1.document.writeln('	<strong>Phone: (608)524-6404</Strong>');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	</TR>');
	
	Win1.document.writeln('</TABLE>');
	Win1.document.writeln('<TABLE WIDTH="45%" BORDER="1" CELLSPACING="2" CELLPADDING="0" align="center">');
	
	Win1.document.writeln('<TR>');
	Win1.document.writeln('<TD width="75%">');
	Win1.document.writeln('<img name="theImage" src="' + plotURL + '" hspace=0 vspace=0 border=0 ALT="Map">');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('		<TD width="25%" ALIGN="CENTER" VALIGN="top">');
	Win1.document.writeln('			<IMG SRC="' + legendImage + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="Legend">');
	Win1.document.writeln('		</TD>');
	Win1.document.writeln('	</TR>');
	
	Win1.document.writeln('	<TR border=1>');
	Win1.document.writeln('	<TD align="left" border=0 rowspan="2"><FONT SIZE="2" FACE="Garamond">');
	Win1.document.writeln(' DISCLAIMER: The information and depictions herein are for informational purposes and the City of Reedsburg specifically disclaims accuracy in this reproduction and specifically admonishes and advises that if specific and precise accuracy is required, the same should be determined by procurement of certified maps, surveys, plats, Flood Insurance Studies, or other official means.  The City of Reedsburg will not be responsible for any damages that result from third party use of the information and depictions herein or for use that ignores this warning.');
	Win1.document.writeln('	</TD>');

	Win1.document.writeln('		<TD ALIGN="center" border=1><FONT SIZE="3" FACE="Garamond">');
	Win1.document.writeln('Scale: 1" = ' + userscale + ' feet');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('	<TR>');
	Win1.document.writeln('	<TD ALIGN="center" border=1><FONT SIZE="3" FACE="Garamond">');
	Win1.document.writeln('' + printdate + '. ');
	Win1.document.writeln('	</TD>');
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('</TABLE>');
	Win1.document.writeln('</body></html>');
	Win1.document.close();
	left = templeft;
	right = tempright;
	top =  temptop;
	bottom = tempbottom;
	legHeight = 800;
	legendVisible=legVis2;
	Win1=null;
	hideRetrieveMap();
}

function get_Scale(){

	var ltop =top;
	var lbottom = bottom;
	var lleft = left;
	var lright = right;
	
	var u = Math.pow(10,0);
	var plotlr = 7.5;
	var plottb = 7.2;
	var offLR=(lright-lleft);
	var plotscale = offLR/plotlr;

	var num1 = 0;
	var num2 = plotscale;
	if (plotscale>10000000) {
		num1 = parseInt(plotscale/5000000);
		num2 = num1 * 5000000;
	} else if (plotscale>1000000) {
		num1 = parseInt(plotscale/500000);
		num2 = num1 * 500000;
	} else if (plotscale>100000) {
		num1 = parseInt(plotscale/50000);
		num2 = num1 * 50000;
	} else if (plotscale>10000) {
		num1 = parseInt(plotscale/5000);
		num2 = num1 * 5000;
	} else if (plotscale>1000) {
		num1 = parseInt(plotscale/500);
		num2 = num1 * 500;
	} else if (plotscale>100) {
		num1 = parseInt(plotscale/50);
		num2 = num1 * 50;
	} else if (plotscale>10) {
		num1 = parseInt(plotscale/5);
		num2 = num1 * 5;
	} else if (sbDistance>1) {
		num1 = parseInt(plotscale/0.25);
		num2 = num1 * 0.25;
	} else if (plotscale>0.1) {
		num1 = parseInt(plotscale/0.025);
		num2 = num1 * 0.025;
	}
	plotscale = num2;
		
		//plotscale = parseInt(plotscale * u + 0.5) / u;
		return plotscale;
		
}


function Get_Layout(scale){
	//showRetrieveMap();
	//alert(hasLayer("Printjob"))
	
	var pagesize = "A";
	var userscale=scale;
		
		if (pagesize=="A") {
			var mapHeight = "2550";
			var mapWidth="3300";
		}

	var ltop = top;
	var lbottom = bottom;
	var lleft = left;
	var lright = right;
	var xc=(lleft+lright)/2;
	var yc=(ltop+lbottom)/2;
	
	var u = Math.pow(10,0);
		var plotlr = 7.5;
		var plottb = 6.7;
		var offLR=(lright-lleft)/2;
		var offTB=(ltop-lbottom)/2;
		var plotscale = offLR/plotlr;
		while(userscale != plotscale){
		if (userscale > plotscale){
		//zoomout to printscale
		var changeLR = (userscale * plotlr)/2;
		changeLR = (changeLR - offLR);
		lleft = (lleft - changeLR);
		lright = (lright + changeLR);
		var changeTB = (userscale * plottb)/2;
		changeTB = (changeTB - offTB);
		ltop = (ltop + changeTB);
		lbottom = (lbottom - changeTB);
		}else{
		//zoomin to printscale
		var changeLR = (userscale * plotlr)/2;
		changeLR = (offLR - changeLR);
		lleft = lleft + changeLR;
		lright = lright - changeLR;
		var changeTB = (userscale * plottb)/2;
		changeTB = (offTB - changeTB);
		ltop = ltop - changeTB;
		lbottom = lbottom + changeTB;
		}
		offLR=lright-lleft;
		offTB=ltop-lbottom;
		plotscale = offLR/plotlr;
		plotscale = parseInt(plotscale * u + 0.5) / u;
		var scaleTB = offTB/plottb;
		}
		

	var layoutString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_LAYOUT>\n<PROPERTIES>\n';
		layoutString += '<IMAGESIZE  dpi="600" width="' + mapWidth + '" height="' + mapHeight + '"/>\n';
		layoutString += '<OUTPUT type="pdf" path="C:/arcims/output" baseurl="http://'+ hostName +'/output" /> \n';
		layoutString += '</PROPERTIES>\n<DATAFRAME id="Layers">'
		//layoutString +=	'<SCALE rf="500" x="'+xc+'" y="'+yc+'" />'
		layoutString += '<ENVELOPE minx="' + lleft + '" miny="' + lbottom + '" maxx="' + lright + '" maxy="' + ltop + '"/>\n';
		layoutString+='<LAYERLIST order="true">';
for (var i=0;i<layerCount;i++) {
	if (LayerVisible[i]==1) {
		layoutString+='<LAYERDEF id="' + LayerID[i] + '" visible="true" />';
	}			
}
		layoutString+='</LAYERLIST>';
		layoutString += '</DATAFRAME>\n</GET_LAYOUT>\n</REQUEST>\n</ARCXML>';
	//alert(layoutString);
	var layoutURL = 'http://'+ hostName +'/ReedsburgCity/ims';
		if (mapWidth=="3300") {
			layoutURL = layoutURL + '?ServiceName=Reedsburg';
		}
	layoutURL = layoutURL + '&ClientVersion=4.0';
	
	//alert(layoutURL);
	sendToServer(layoutURL, layoutString, 1002);
	
}



