// 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 mapTitle1="";
var mapTitle2="";

var printMapURL="";
var printOVURL="";
var printLegURL="";
var tempWidth = 0;
var tempHeight = 0;
var printMapImageWidth=0;
var printMapImageHeight=0;
var mapBufferWidth=6;                  // supports the inner table buffer for the map image
var mapBufferHeight=4;

var printData="";
var showDataTable=false;

var pWidth=0;
var pHeight=0;
var pAnnoCol=190;

var legVis2=false;

var printSize="";

/*
***************************************************************************************

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=575height=250,scrollbars=yes,resizable=yes");
    }
  }

// create web page for printing
// first get Map
function getPrintMap(pageSize, title1, title2, showData) {
  showRetrieveMap();
  mapTitle1=title1;
  mapTitle2=title2;
  
  printSize = pageSize;
  showDataTable = showData;
  
  // Set the page size which in turn will define the size of hte map image requested
  if (pageSize==1) {				//Letter, 8.5x11
    pWidth=930;
    pHeight=680;
  } else if (pageSize==2) {			//Halfsize, 11x17
	pWidth=1395;
    pHeight=855;  
  } else if (pageSize==3) {			//D Size
	pWidth=2200;
    pHeight=1400;  
  } else if (pageSize==4) {			//E Size
	pWidth=3000;
    pHeight=2200;  
  }

  tempWidth = iWidth;
  tempHeight = iHeight;

  iWidth = Math.floor( parseFloat(pWidth) - pAnnoCol - (mapBufferWidth * 2));
  iHeight = Math.floor( parseFloat(pHeight) - (mapBufferWidth * 2));
  printMapImageWidth=iWidth;
  printMapImageHeight=iHeight;
  
  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();
}
// fourth, write the web page
// To improve teh printing, basically need to line up the following variables:
// iHeight, iWidth - these are the image ratios established by the size of the 
// map image at the time the browser loads (as currently implemented).  That is to say
// if the map display frame has a w=320 and h=100 - that is what iwidth and iheight will be
// pHeight, pWidth - these are numbers established in the printform.htm - which
// as currently implemented are the size of the image given the requested image size (small, medium large).
// these need to have the same ratio - in getPrintMap - the value of the iHeight and iWidth are
// established as a factor of the requested image size - ...

function writePrintPage() {
  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('<LINK REL=\"Stylesheet\" TYPE=\"text\/css\" TITLE=\"theStyles\" HREF=\"\/' + SiteName + '\/theStyles.css\">');
  Win1.document.writeln('</head>');
  Win1.document.writeln('<body BGCOLOR="White" TEXT="Black" LEFTMARGIN=0 TOPMARGIN=0>');
  Win1.document.writeln('<TABLE WIDTH="' + pWidth + '" HEIGHT="' + pHeight + '" BORDER="0" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
  Win1.document.writeln('  <TR>');
  Win1.document.writeln('    <TD class="otb">');
  Win1.document.writeln('     <TABLE WIDTH="' + pWidth + '" HEIGHT="' + pHeight + '" BORDER="0" CELLSPACING="0" CELLPADDING="0" NOWRAP>');
  Win1.document.writeln('      <TR>');
  Win1.document.writeln('          <TD align="center" rowspan=3 WIDTH="'+ (printMapImageWidth + (2 * mapBufferWidth)) + '" HEIGHT="'+ (printMapImageHeight + (2 * mapBufferWidth)) + '" valign="middle">');
  Win1.document.writeln('          <TABLE align="center" WIDTH="' + printMapImageWidth + '" HEIGHT="' + printMapImageHeight + '" BORDER="0" CELLSPACING="0" CELLPADDING="0" NOWRAP>');  
  Win1.document.writeln('            <TR>');
  Win1.document.writeln('              <TD class="imb">');
  Win1.document.writeln('                <IMG SRC="' + printMapURL + '" WIDTH="'+printMapImageWidth+'" HEIGHT="'+printMapImageHeight+'" HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
  Win1.document.writeln('              </TD>');
  Win1.document.writeln('            </TR>');
  Win1.document.writeln('          </TABLE>');
  Win1.document.writeln('        </TD>');

  // Display the Seal and Source Title.
  Win1.document.writeln('        <TD ALIGN="CENTER" VALIGN="top" WIDTH="'+pAnnoCol+'" height="170">');
//  Win1.document.writeln('          <br><img src=\"\/' + SiteName + '\/Images\/MarlSeal.gif" WIDTH="'+pAnnoCol+'" border=0 ALT="seal"><br>');
  Win1.document.writeln('          <br><img src=\"\/' + SiteName + '\/Images\/gis_logo_small_PRINT.jpg" border=0 ALT="seal"><br>');
 // Win1.document.writeln('          <span class="mp_h3"><b><i>Geographic Information System</i></b></span><br><br>');
  Win1.document.writeln('     <img src=\"\/' + SiteName + '\/Images\/lineonwhite.gif" width="150" border=0"><br><br>');
  //Win1.document.writeln('          <span class="mp_h1">' + mapTitle1 + ' ' + (printMapImageWidth + (2 * mapBufferWidth)) + '</span><br>');
  Win1.document.writeln('          <span class="mp_h1">' + mapTitle1 + '</span><br>');
  Win1.document.writeln('          <span class="mp_h2">' + mapTitle2 + '</span>');
  Win1.document.writeln('        </TD>');
  Win1.document.writeln('      </TR>');
  Win1.document.writeln('      <TR>');
  
  // THIS IS THE CENTER FRAME... WHERE EITHER THE LOCUS MAP OR THE PROPERTY DATA GOES	
  Win1.document.writeln('        <TD align="center" valign="top" height="390">');
  // Display either the Overview Map or the data table
  if (showDataTable=='true') {
    Win1.document.writeln('      <br><br>');
    Win1.document.writeln(printData);
  } else if (hasOVMap) { 
    Win1.document.writeln('      <br><br><FONT face="Arial Narrow" size=3><b>Locus Map</b><br>');
    Win1.document.writeln('      <IMG SRC="' + printOVURL + '" WIDTH=190 HEIGHT=150 HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
  }

  Win1.document.writeln('       </TD>');
  Win1.document.writeln('     </TR>');
  
  // THIS IS THE BOTTOM RIGHT FRAME IN THE PRINT SCREEN
  Win1.document.writeln('     <TR>');
  Win1.document.writeln('       <TD ALIGN="center" VALIGN="bottom" height="150">');
  Win1.document.writeln('     <img src=\"\/' + SiteName + '\/Images\/lineonwhite.gif" width="150" border=0"><br><br>');
  Win1.document.writeln('        <FONT size=1><b>Disclaimer</b></FONT><br>');
  Win1.document.writeln('        <FONT face="Arial Narrow" size=1>Town of Enfield, CT, makes no warranty or representation as to the accuracy, timeliness or completeness of any of the data.  The Town of Enfield, CT, shall have no liability for the data or lack thereof, or any decision made or action taken or not taken in reliance upon any of the data.</FONT><p>');
  Win1.document.writeln('       </TD>');
  Win1.document.writeln('     </TR>');  
  Win1.document.writeln('    </TABLE>');
  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;
  hideRetrieveMap();
}

function getPrintMapGL(mapstyle, mapScale) {

//alert ("Map Style : " + mapstyle + "Map Scale : " + mapScale);
    
	//var scale = scaleFactor;
    var halfWidth = fullWidth / 2;
	var halfHeight = fullHeight / 2
	var midX = eRight - (xDistance / 2);
	var midY = eTop - (yDistance / 2);

	var mapLeft=parent.MapFrame.eLeft;
    var mapRight=parent.MapFrame.eRight;
    var mapTop=parent.MapFrame.eTop;
	var mapBottom=parent.MapFrame.eBottom;
	var mapCenterX = mapLeft + ((mapRight - mapLeft)/2);
	var mapCenterY = mapBottom + ((mapTop - mapBottom)/2);
	
//	alert (parent.MapFrame.activeMapService);
//	alert (activeMapService);
	var theString = '<ARCXML version="1.1">\n<REQUEST>\n<GET_LAYOUT autoextent="true" >\n';
    theString += '<PROPERTIES>\n';
	theString += '<IMAGESIZE dpi = "300"/>\n';
    theString += '<OUTPUT type = "PDF"/>\n';
	var dFrame = 'EnfieldArcIMS';
	var URL = imsURL;

	switch(mapstyle) {
		//case "11x17":
		case "8x11":
		//alert ("8X11");
			//theString += '<ENVELOPE minx="0" miny="0" maxx="17" maxy="11" />\n';
			theString += '<ENVELOPE minx="0" miny="0" maxx="8.5" maxy="11" />\n';
			dFrame = 'Layers';
			URL=serverURL + "EnfieldCT_AMS";
			break
		//case "ortho1117":
		  case "11x17":
	       //alert ("11X17");
			theString += '<ENVELOPE minx="0" miny="0" maxx="11" maxy="17" />\n';
			dFrame = 'Layers';
			URL=serverURL + "EnfieldCT_AMS";
			break

		//case "ortho811":
		 case "24x36":
		 //alert ("24X36");
			theString += '<ENVELOPE minx="0" miny="0" maxx="36" maxy="24" />\n';
			dFrame = 'Layers';
			URL=serverURL + "EnfieldCT_AMS";
			break


		default:
			theString += '<ENVELOPE minx="0" miny="0" maxx="8.5" maxy="11" />\n';
			dFrame = 'Layers';
			URL=serverURL + "EnfieldCT_AMS";
			break
	}
	theString += '</PROPERTIES>\n';
	theString += '<DATAFRAME id="' + dFrame + '" >\n';
	if (mapScale > 0){
		mapScale = mapScale * 12;
		theString += '<SCALE rf="' + mapScale + '" x="' + mapCenterX + '" y="' + mapCenterY + '"/>\n';
		
		
		// Loop through the layers to see which ones are selected
	if (aimsLayersPresent) 
		{
		theString += '<LAYERLIST >\n';
			// permanent layers from mxd file
			for (var i=0;i<layerCount;i++) 
				{
				if (LayerVisible[i]==1) 
					{
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="true" />\n';
					
					}
				else 
					{
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
					}
				
				}
			theString += '</LAYERLIST>\n';
		}	
		
		
	}else{
		theString += '<ENVELOPE minx="' + mapLeft + '" miny="' + mapBottom + '" maxx="' + mapRight + '" maxy="' + mapTop + '" />\n';
		
		
		
		// Loop through the layers to see which ones are selected
	if (aimsLayersPresent) 
		{
		theString += '<LAYERLIST >\n';
			// permanent layers from mxd file
			for (var i=0;i<layerCount;i++) 
				{
				if (LayerVisible[i]==1) 
					{
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="true" />\n';
					
					}
				else 
					{
					theString += '<LAYERDEF id="' + LayerID[i] + '" visible="false" />\n';
					}
				
				}
			theString += '</LAYERLIST>\n';
		}
	
		
		
		
		
		
		
	}
	theString += '</DATAFRAME>\n';
	theString += '</GET_LAYOUT>\n</REQUEST>\n</ARCXML>\n';
	showRetrieveMap();
	//alert(URL + "\n" + theString);
		sendToServer(URL,theString,104);	
// End Function
}

function getPrintXYs(theString) {
	var tempStr = "";
	var smallStr = "";
	var startpos = 0;
	var endpos = 0;
	var theReply = theString
	var theXYs =  getEnvelopeXYs(theString, 0)
	epLeft = theXYs[0];
	epBottom = theXYs[1];
	epRight = theXYs[2];
	epTop = theXYs[3];
	xpDistance = Math.abs(epRight-epLeft);
	var spFactor = xpDistance / printMapImageWidth;
	printMapScale = spFactor;

}
