// 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 printHeight = 450;	// default print dimensions Dnyanada 11/7/2005
var printWidth = 450;	//
var RFScale; //Dnyanada 4/7/2006 Display RFScale on printed map


aimsPrintPresent=true;

var printTitle = titleList[4];
var printMapURL="";
var printOVURL="";
var printLegURL="";

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=250,scrollbars=yes,resizable=yes");
		var Win1 = open("printform.htm","PrintFormWindow","width=645,height=365,scrollbars=yes,resizable=yes");
	
	}
}


//Dnyanada 4/7/2006 start
function GetRFScale(){
		
	if ( iHeight < iWidth) {
		 RFScale = (((eTop - eBottom)* 12 )/(iHeight/97.69231)); 
	} else {
		RFScale = (((eRight - eLeft)* 12 )/(iWidth/97.69231)); 
	} 
	RFScale = parseInt(RFScale);
}
//Dnyanada end


// create web page for printing
	// first get Map
function getPrintMap(title, w, h) {
	var tLeft, tRight, tBottom, tTop;
	hasOVMap = true;
	showRetrieveMap();
	printTitle=title;
	
	legVis2=legendVisible;
	if (aimsLegendPresent) legendVisible=true;
	
	// BB: set print size to input values
	if (w != "") printWidth = parseInt(w);	
	if (h != "") printHeight = parseInt(h);
	
	// BB: set map extent if scale specified
	tLeft = eLeft;
	tRight = eRight;
	tBottom = eBottom;
	tTop = eTop;
	
	//BB: set XMLMode so get print res.
	XMLMode = 101;
	// BB: enlarge legend in proportion to map
	var tempLegW = legWidth;
	var tempLegH = legHeight;
	//legWidth = parseInt(printWidth/4);  DJ
	legWidth = parseInt(printWidth/5);
	legHeight = parseInt(printWidth/3);
	// BB
	 
	//Dnyanada added following 2 lines to save current height and width 3/23/2006
	var TempWidth = iWidth; 
	var TempHeight = iHeight; 
	
	//Dnyanada added following 2 lines to send new height and width from Printform to server 3/23/2006
	iWidth = w; 
	iHeight = h; 
	
	GetRFScale();//added by dnyanada 4/7/2006
	
	ShowDisClaimer = true; //Dnyanada 4/17/2006
	
	var theString = writeXML();
	
	ShowDisClaimer = false; //Dnyanada 4/17/2006
	
	eLeft = tLeft;
	eRight = tRight;
	eBottom = tBottom;
	eTop = tTop;
	legWidth = tempLegW;	// BB: restore default leg vars
	legHeight = tempLegH;	// BB
	legendVisible = legVis2;
	
	//Dnyanada added following 2 lines to restore default height and width  3/23/2006
	
	iWidth = TempWidth; 
	iHeight = TempHeight; 
	
	
	sendToServer(imsURL,theString,101);
	theString=null;
}

// second, get OVMap
function getPrintOV() {
	var tempWidth = i2Width;
	var tempHeight = i2Height;
	//i2Width=parseInt(printWidth/4);		//BB: scale to print   - DJ
	i2Width=parseInt(printWidth/5);		//BB: scale to print 
	i2Height=parseInt(printHeight/4);	// BB: scale to print
	var tempDraw=drawOVExtentBox;
	drawOVExtentBox=true;
	XMLMode = 102;
	
	//following line is changed by Dnyanada 3/10/2006 so Over View map will be displayed when 
	//print button is clicked
	//var theString = writeOVXML();
	var theString = writePrintOVXML();
	
	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 writePrintPage() {
	var Win1 = open("","PrintPage");
	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 BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP ALIGN="CENTER">'); //BB: omit width
	Win1.document.writeln('	<TR>');
	Win1.document.writeln('		<TH COLSPAN="2" style="font-size:' + parseInt(printWidth/30) + 'px">' + printTitle + '</TH>');	// BB: scale title
	Win1.document.writeln('	</TR>');
	Win1.document.writeln('	<TR>');
	Win1.document.write('		<TD');
	if (hasOVMap) Win1.document.write(' ROWSPAN="3"'); //changed from 2 to 3 4/7/2006 to add a row about RFScale
	Win1.document.writeln('>');
	Win1.document.writeln('			<IMG SRC="' + printMapURL + '" WIDTH=' + printWidth + ' HEIGHT=' + printHeight + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="Main map">'); //BB: set size
	Win1.document.writeln('		</TD>');
	if (hasOVMap) {
		Win1.document.writeln('		<TD height="' + parseInt(printHeight/4) + '" valign="center">'); //BB: set size changed from top to center and 3 to 4 in this line Dnyanada 3/27/2006 
		//Changed HEIGHT=' + parseInt(printHeight/3) from 3 to 4 in the following line Dnyanada 3/10/2006
		Win1.document.writeln('			<IMG SRC="' + printOVURL + '" WIDTH=' + parseInt(printWidth/5) + ' HEIGHT=' + parseInt(printHeight/4) + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="Overview">'); // BB: set size
		Win1.document.writeln('		</TD>');
	}
	Win1.document.writeln('	</TR>');

	//Dnyanada start 4/7/2006 Display RF Scale
	Win1.document.writeln('	<TR>');
	Win1.document.writeln('		<TD ALIGN="CENTER" style="font-size:' + parseInt(printWidth/60) + 'px">'+ "Scale 1:" + RFScale + '</TD>');
	Win1.document.writeln('	</TR>'); 

	//Dnyanada end 4/7/2006

	Win1.document.writeln('	<TR>');
	Win1.document.writeln('		<TD ALIGN="CENTER" VALIGN="TOP">');
	Win1.document.write('         <span style="font-size:' + parseInt(printWidth/40) + 'px">');
	Win1.document.writeln(legTitle + '</span><br><br> '); // BB: add Legend title, set size
	Win1.document.write('			<IMG SRC="' + printLegURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="Legend"');
	Win1.document.writeln('		</TD>');
	Win1.document.writeln('	</TR>');
	
	Win1.document.writeln('</TABLE>');
	Win1.document.writeln('</B></FONT>');
	Win1.document.writeln('</body></html>');
	Win1.document.close();
	
	legendVisible=legVis2;
	Win1=null;
	hasOVMap = false; //Dnyanada 3/10/2006 OverView is only displayed when Print Page command is given
	hideRetrieveMap();
}








// prepare the request in xml format for overview map
function writePrintOVXML() {
	var extentRatio = xDistance/fullWidth;
	var midX = eLeft + xHalf;
	var midY = eBottom + yHalf;
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n';
	theString += '<ENVELOPE minx="' + forceComma(ovlimitLeft) + '" miny="' + forceComma(ovlimitBottom) + '" maxx="' + forceComma(ovlimitRight) + '" maxy="' + forceComma(ovlimitTop) + '" />\n';
	theString += '<IMAGESIZE height="' + i2Height + '" width="' + i2Width + '" />\n';
	theString += '<LAYERLIST >\n';
	for (var i=0;i<layerCount;i++) {
		theString += '<LAYERDEF name="' + LayerName[i] +'"';
       if ((LayerName[i] == "Mid-Atlantic States") ||(LayerName[i] == "Counties")) {
		// if (LayerName[i] == "Mid-Atlantic States"){
			theString += ' visible="true"';
			 if (LayerName[i] == "Counties" ) {
                 theString += ' >\n';
                 theString += '<SIMPLERENDERER>\n';
                 theString += '<SIMPLEPOLYGONSYMBOL boundarytransparency="1.0" filltransparency="1.0" fillcolor="255,255,230" filltype="solid" boundarytype="solid" boundarywidth="1" boundarycaptype="round" boundaryjointype="round" boundarycolor="0,0,0" />\n';
                 theString += '</SIMPLERENDERER>\n';
                 theString += '</LAYERDEF>\n';
            } else {
            //Dnyanada added following lines  3/10/2006
				 theString += ' >\n';
				 theString += '<SIMPLERENDERER>\n';
                 theString += '<SIMPLEPOLYGONSYMBOL boundarytransparency="0.01" filltransparency="1.0" fillcolor="220,220,220" filltype="solid" boundarytype="solid" boundarywidth="0.01" boundarycaptype="round" boundaryjointype="round" boundarycolor="0,0,0" />\n';
                 theString += '</SIMPLERENDERER>\n';					 
				 theString += '</LAYERDEF> \n'; 
             
           }
            
            //end
            
             
		} else {
			theString += ' visible="false" />\n';
		}
	}
	theString += '</LAYERLIST>\n';
	if (mapBackColor!="") {
		theString += '<BACKGROUND color="' + mapBackColor + '" />\n';
	}
	theString += '</PROPERTIES>\n';
	if (drawOVExtentBox) {
		theString += '<LAYER type="acetate" name="ACETATE">\n';
		if (extentRatio<(1/100)) {
		//changed coord to coords 12/29/2005 Dnyanada
			theString += '<OBJECT units="database">\n<POINT coords="' + forceOVComma(midX) + coordsDelimiter + forceOVComma(midY) + '">\n';
			theString += '<SIMPLEMARKERSYMBOL  type="Square" color="255,0,0" width="4" />\n</POINT></OBJECT>\n';
		} else {
			theString += '<OBJECT units="database">\n<LINE coords="' + forceOVComma(eLeft) + coordsDelimiter + forceOVComma(eBottom) + pairsDelimiter + forceOVComma(eLeft) + coordsDelimiter + forceOVComma(eTop) + pairsDelimiter;
			theString += forceOVComma(eRight) + coordsDelimiter + forceOVComma(eTop) + pairsDelimiter + forceOVComma(eRight) + coordsDelimiter + forceOVComma(eBottom) + pairsDelimiter + forceOVComma(eLeft) + coordsDelimiter + forceOVComma(eBottom) + '">\n';
			theString += '<SIMPLELINESYMBOL type="solid" color="255,0,0" width="3" />\n</LINE></OBJECT>\n';
		}
		theString += '</LAYER>\n';
	}
	theString += '</GET_IMAGE>\n</REQUEST>\n</ARCXML>';
	//alert(theString);
	return theString;
}



