// 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="";
var printMapScale = 0;
var tmpDrawScalebar = true;

var aerialDate = "";

/*
***************************************************************************************

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=940;
    pHeight=690;
    //pWidth=3000;
    //pHeight=2200;  
  } else if (pageSize==2) {			//Halfsize, 11x17
	pWidth=1405;
    pHeight=865;  
  } 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;
  tmpDrawScalebar = drawScaleBar;
  drawScaleBar = false;
  legVis2=legendVisible;
  if (aimsLegendPresent) legendVisible=true;
  var theString = writeXML();
  printMapScale = (eLeft - eRight)/printMapImageWidth;
  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() {
  drawScaleBar = tmpDrawScalebar;
  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="160">');
  Win1.document.writeln('          <br><img src=\"\/' + SiteName + '\/images\/Color_Seal.gif" 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="290">');
  // Display either the Overview Map or the data table
  if (showDataTable=='true') {
    Win1.document.writeln('      <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=1 ALT="">'); 
  }
  Win1.document.writeln('		  <br><FONT face="Arial Narrow" size=2><b>MapScale:</b><br> 1 inch = ' + Math.round(printMapScale * 96) + ' Feet </FONT>');
  Win1.document.writeln('          <br><img src=\"\/' + SiteName + '\/images\/NorthArrow.gif" border=0 ALT="northArrow"><br>');

  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>');
  var dislaimer = '<FONT face="Arial Narrow" size=1>The information contained in this cadastral map is used to locate, identify ';
  dislaimer += 'and inventory parcels of land in Washtenaw County for appraisal and taxing purposes';
  dislaimer += 'only and is not to be construed as a &quot;survey description&quot;.  ';
  dislaimer += 'The data depicted is from a variety of sources, thus the information is provided ';
  dislaimer += 'with the understanding that the conclusions drawn from such information are solely'; 
  dislaimer += 'the responsibility of the user.<BR>';
  dislaimer += 'Any assumption of legal status of this data is hereby disclaimed.<BR>';
  dislaimer += '<b>NOTE: PARCELS MAY NOT BE TO SCALE</b><br>Copyright © 2000 by Washtenaw County, MI.</FONT><p>';
  Win1.document.writeln(dislaimer);
  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(mapSize, mapScale, title1,title2) {
//alert (scaleFactor);
    
	//var scale = scaleFactor;
	mapTitle1=title1;
  	mapTitle2=title2;
	//alert(mapTitle1 + "  " + mapTitle2);
    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 = "150"/>\n';
    theString += '<OUTPUT type = "PDF"/>\n';
	var dFrame = 'Layers';
	var URL = imsURL;
	//theString += '<ENVELOPE minx="0" miny="0" maxx="8.5" maxy="11" />\n';
	//URL=serverURL + "Washtenaw_AMS";
	switch(mapSize) {
		case "8x11":
			theString += '<ENVELOPE minx="0" miny="0" maxx="8.5" maxy="11" />\n';
			dFrame = 'Layers';
			URL=serverURL + "mapwashtenaw_portrait";
			break
		case "11x8":
			theString += '<ENVELOPE minx="0" miny="0" maxx="11" maxy="8.5" />\n';
			dFrame = 'Layers';
			URL=serverURL + "mapwashtenaw_landscape";
			break
	//	case "11x17":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="11" maxy="17" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ1117Portrait_AMS";
	//		break		
        //	case "17x11":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="17" maxy="11" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ1117Landscape_AMS";
	//		break		
        //	case "18x24":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="18" maxy="24" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ1824Portrait_AMS";
	//		break
        //	case "24x18":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="24" maxy="18" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ1824Landscape_AMS";
	//		break
        //	case "36x36":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="36" maxy="36" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ3636_AMS";
	//		break
        //	case "36x48":
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="36" maxy="48" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ3648Portrait_AMS";
	//		break
        //	case "48x36": 
	//		theString += '<ENVELOPE minx="0" miny="0" maxx="48" maxy="36" />\n';
	//		dFrame = 'Layers';
	//		URL=serverURL + "Equ3648Landscape_AMS";
	//		break		
		default:
			theString += '<ENVELOPE minx="0" miny="0" maxx="8.5" maxy="11" />\n';
			dFrame = 'Layers';
			URL=imsURL;
			break
	}
	theString += '</PROPERTIES>\n';
	theString += '<DATAFRAME id="' + dFrame + '" >\n';
	if (mapScale > 0){
		mapScale = mapScale * 12;
		theString += '<SCALE rf="' + mapScale + '" x="' + mapCenterX + '" y="' + mapCenterY + '"/>\n';
	}else{
		theString += '<ENVELOPE minx="' + mapLeft + '" miny="' + mapBottom + '" maxx="' + mapRight + '" maxy="' + mapTop + '" />\n';
	}
	theString += '<LAYERLIST order="true">\n';
	var Aerial2002Vis = false;
	aerialDate = "None";
	var isVis = false;
	for (i=0;i<layerCount;++i){
		if (LayerVisible[i] == 1){
			isVis = true;
			//alert(i + " " + LayerName[i] + " " + LayerIsFeature[i] + " " + LayerType[i]);
			if ((LayerName[i].indexOf("Aerial") > -1) && (LayerName[i].indexOf("2002") > -1)){
				Aerial2002Vis = true;
				aerialDate = "2002";
			}
			if ((LayerName[i].indexOf("Aerial") > -1) && (LayerName[i].indexOf("1998") > -1)){
				aerialDate = "1998";
			}
			if ((LayerName[i].indexOf("Aerial") > -1) && (LayerName[i].indexOf("1998") > -1) && (Aerial2002Vis)){
				isVis = false;
				aerialDate = "2002";
			}
		}else
			isVis = false;
		
		theString += '<LAYERDEF id="' + i + '" visible="' + isVis + '" />\n';
		//alert('<LAYERDEF id="' + i + '" name="' + LayerName[i] + '" visible="' + isVis + '" />');
	}

	theString += '</LAYERLIST>\n';
	theString += '</DATAFRAME>\n';
	theString += '</GET_LAYOUT>\n</REQUEST>\n</ARCXML>\n';
	showRetrieveMap();
	//alert(URL + "\n" + theString);
	sendToServer(URL,theString,104);	
	//XMLMode = 104;
	//processXML("asdfhklhl");
// 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;

}