// ************************************************ //
// *** Start of advanced print quality rountine *** //
// ************************************************ //
/*
	To use of this, you need to:
	
	1) Make a reference to this file in MapFrame.htm, by pasting the single line of code below

		<!-- DCC improved printing -->
		<script type="text/javascript" language="JavaScript" src="javascript/aimsGoodPrint.js"></script>

	2) Change the onClick event handler function on the print map image in printform.htm, so that it looks like:

		<input type="image" src="images/printtext.gif" name="submit" value="Create" onclick="parent.MapFrame.submitHighQualityPrint()" alt="Create print layout">

	3) At the very top of the writeXML() function in aimsXML.js, delete everything above " var visString = "" ", then paste this code:

		//****************************
		// ** High qualtiy printing **
		//****************************
		//--------------------------------------------------------------
		//Get the quality of the map - better quality for print outs ...
		
		var theQualityXML = parent.MapFrame.calculateQuality()	
		var theString = ""
		theString = theString + theQualityXML
		//--------------------------------------------------------------

	4) In the writeXML() function of aimsXML.js, replce the "if (drawCopyright){ }" condition, paste this code

		//Add copyright text
		if (drawCopyright) 
		{
			//****************************
			// ** High qualtiy printing **
			//****************************
			//------------------------------------------------------------------------------------
			//If we are priting off a good quality map, we need to change the coords of copyright.
			adjustCoordsForHighQualityPrint()
			//------------------------------------------------------------------------------------
		
			// draw text on the map
			theString += '<LAYER type="ACETATE" name="theCopyright">\n';		
			theString += '<OBJECT units="PIXEL">\n<TEXT coords="' + coord1 + '" label="' + CopyrightText + '">\n';
			theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
			theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
			if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
			theString += '/>\n</TEXT>\n</OBJECT>\n';
			theString += '</LAYER>\n';

			theString += '<LAYER type="ACETATE" name="theCopyright2">\n';
			theString += '<OBJECT units="PIXEL">\n<TEXT coords="' + coord2 + '" label="' + CopyrightText2 + '">\n';
			theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
			theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
			if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
			theString += '/>\n</TEXT>\n</OBJECT>\n';
			theString += '</LAYER>\n';
			
			theString += '<LAYER type="ACETATE" name="theCopyright3">\n';
			theString += '<OBJECT units="PIXEL">\n<TEXT coords="' + coord3 + '" label="' + CopyrightText3 + '">\n';
			theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
			theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
			if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
			theString += '/>\n</TEXT>\n</OBJECT>\n';
			theString += '</LAYER>\n';

			theString += '<LAYER type="ACETATE" name="theCopyright4">\n';
			theString += '<OBJECT units="PIXEL">\n<TEXT coords="' + coord4 + '" label="' + CopyrightText4 + '">\n';
			theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
			theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
			if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
			theString += '/>\n</TEXT>\n</OBJECT>\n';
			theString += '</LAYER>\n';

			theString += '<LAYER type="ACETATE" name="theCopyright5">\n';
			theString += '<OBJECT units="PIXEL">\n<TEXT coords="' + coord5 + '" label="' + CopyrightText5 + '">\n';
			theString += '<TEXTMARKERSYMBOL fontstyle="' + CopyrightStyle + '" fontsize="' + CopyrightSize + '" ';
			theString += 'font="' + CopyrightFont + '" fontcolor="' + CopyrightColor + '" antialiasing="True" ';
			if (CopyrightGlow.toUpperCase()=="TRUE") theString += ' glowing="' + CopyrightGlowColor + '" ';
			theString += '/>\n</TEXT>\n</OBJECT>\n';
			theString += '</LAYER>\n';
		}
	
	5) All done, but make sure that this JavaScript file is located inside the JavaScript directory of the live folder.
*/

//**********************************
//*** Declare standard variables ***
//**********************************
//Is the map being produced for a print out or just normal browsing display ?
var userISPrinting = false;

//What is the quality of the best print out - this is the default
var bestPrintQuality = 4; //ie, four times better than normal.

//What other levels of quality do we have.
var goodPrintQuality = 2.5;
var mediumPrintQuality = 1.7;
var normalPrintQuality = 1.5;

//What is the quality of the vector data on the print out - this works for everything below 1:5,000 scale, even photos
var vectorPrintQuality = 1.5; //ie, one and a half times better than normal.

//What is the size of the printout - used to determine the quality, ie the bigger the size, the worse the quality.
//This is because ArcIMS returns an Image too big message, and will not create the map
var printSize=""

//Coords of the copyright text - need to change them if we produce a good quality map
var coord1 = "";
var coord2 = "";
var coord3 = "";
var coord4 = "";
var coord5 = "";

//---------------------------------------------------------------------------------------------------------
function submitHighQualityPrint()
{
	
	//This sets a variable to tell ArcIMS that we the next map we produce will be for a 
	//print out, so make it a better quality.	
	parent.MapFrame.userISPrinting=true
	
	// TK - for print north arrow correctly
	parent.MapFrame.usePrintNthCoords=true
	

	//Submit the print form that is displayed in the TOCFrame
	parent.LeftFrame.printer.submit();
}
//---------------------------------------------------------------------------------------------------------
function calculateQuality()
{
	//create variables to store the height and width of the printed map - 
	//the bigger these no's, often the better the print quality
	var pWidth = 0 ;
	var pHeight = 0;

	//If the user is printing a map, rather than drawing on screen
	if (parent.MapFrame.userISPrinting == true)
	{
		//Get the current map scale
		var CurrentScale = parseInt(parent.ToolFrame.document.forms[0].txtScale.value);

		//A variable to hold the print quality, assume we want best quality
		var pq = parent.MapFrame.bestPrintQuality;

		//Depening upon the dimensions of the map the user wants to create, set the quality
		//so that we don't get ArcIMS "image too big" message. This will apply to every map above 1:5000 - see later
		if (printSize !="") //user has set a page size, and wants to print the map
		{	
			//Set the quality, depending upon the size of the images ArcIMS has to produce.
			switch (printSize)
			{
				case "a4l":
					//alert("A4l")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a4p":
					//alert("A4p")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a3l":
					//alert("A3l")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a3p":
					//alert("A3p")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a2l":
					//alert("A2l")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a2p":
					//alert("A2p")
					pq = parent.MapFrame.bestPrintQuality;
					break
				case "a1l":
					//alert("A1l")
					pq = parent.MapFrame.goodPrintQuality;
					break
				case "a1p":
					//alert("A1p")
					pq = parent.MapFrame.goodPrintQuality;
					break
				case "a0l":
					//alert("A0l")
					pq = parent.MapFrame.mediumPrintQuality;
					break
				case "a0p":
					//alert("A0p")
					pq = parent.MapFrame.mediumPrintQuality;
					break
			}
		}
		
		//FORCE QUALITY FOR THE LANDLINE DATA
		//If we are below 1:5,000, then FORCE the print quality to 1.5 times as good as the on screen map.
		//This seems to be the best for the land-line / Master map data.
		if (CurrentScale <= 5050)
		{
			//increase image size by half again.
			pq = parent.MapFrame.vectorPrintQuality; 
		}

		//Apply this to the pWidth and pHeight variables. 
		pWidth = iWidth	* pq;
		pHeight = iHeight * pq;

		//alert("The print quality is set to : " + pq)
		
		// Remember ... we need to set a printing flag to false, so that we don't change the quality of the on-screen maps. This
		// happens after we draw the copyright, as we need to make adjustments for the copyright Y coordinate.
	}
	else
	{	
		//The user is not printing a map, so just use the standard quality for drawing on-screen maps.
		pWidth = iWidth
		pHeight = iHeight
	}

	//This is the important bit! Pass in the printwidth and printheight variables, to increase the quality of the image.
	var theXML = '<ARCXML version="1.1">\n<REQUEST>\n<GET_IMAGE>\n<PROPERTIES>\n<ENVELOPE minx="' + forceComma(eLeft) + '" miny="' + forceComma(eBottom) + '" maxx="' + forceComma(eRight) + '" maxy="' + forceComma(eTop) + '" />\n';
	theXML += '<IMAGESIZE height="' + iHeight + '" width="' + iWidth + '" printwidth="' + pWidth + '" printheight="' + pHeight + '" scalesymbols="true" />\n';
	
	//Return the XML
	return theXML;
}
//---------------------------------------------------------------------------------------------------------
function adjustCoordsForHighQualityPrint()
{			
	//We need to change the Y coordinate of the copyright for the print-out, as they overlap when we
	//produce larger images.

	//User is printing a map off, so change accordingly
	if (parent.MapFrame.userISPrinting == true)
	{
		//Get the current map scale
		var CurrentScale = parseInt(parent.ToolFrame.document.forms[0].txtScale.value);

		//If we are below 1:5,000, then set the copyright coords to different values, as we are using different quality factors
		if (CurrentScale <= 5050)
		{
			//When we are using a slightly larger print scale factor - for vector data (< 1:5,000)
			coord1 = "70 70";
			coord2 = "70 55";
			coord3 = "70 40";
			coord4 = "70 25";
			coord5 = "70 10";
		}
		else
		{
			//When we are using the largest print scale factor - for raster data (>= 1:5,000)
			coord1 = "70 130";
			coord2 = "70 100";
			coord3 = "70 70";
			coord4 = "70 40";
			coord5 = "70 10";
		}
		
		//Set flag to false - finished printing - very important, otherwise we will increase the size of the
		//on-screen maps as well.
		parent.MapFrame.userISPrinting = false;
	}
	else //User is not printing maps off, so use standard coords for copyright for on-screen output
	{
		coord1 = "70 40";
		coord2 = "70 32";
		coord3 = "70 24";
		coord4 = "70 16";
		coord5 = "70 8";


	}
}
//---------------------------------------------------------------------------------------------------------
function userPrintInstructions()
{
	//Depening upon the dimensions of the map the user has created, inform them
	if (printSize !="") //user has set a page size, and wants to print the map
	{	
		//Depending upon the size of the image, give relevant message to the user
		var str=""
		switch (printSize)
		{
			case "a4l":
				str= "You have produced an A4 landscape layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a4p":
				str= "You have produced an A4 portrait layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a3l":
				str= "You have produced an A3 landscape layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a3p":
				str= "You have produced an A3 portrait layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a2l":
				str= "You have produced an A2 landscape layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a2p":
				str= "You have produced an A2 portrait layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a1l":
				str= "You have produced an A1 landscape layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a1p":
				str= "You have produced an A1 portrait layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a0l":
				str= "You have produced an A0 landscape layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			case "a0p":
				str= "You have produced an A0 portrait layout. Please set the printer, page size and orientation before printing the map"
				
				//Set page size flag to ""
				parent.MapFrame.printSize = "";
				return str;
			default: 
				str="";
				return str;
		}
	}
}
//---------------------------------------------------------------------------------------------------------
function printToScale(theBoxHeight, theBoxWidth, theScale, loadMap) 
{
	//take a backup copy of eBottom etc
	parent.MapFrame.eBottomBAK = eBottom
	parent.MapFrame.eTopBAK = eTop
	parent.MapFrame.eLeftBAK = eLeft
	parent.MapFrame.eRightBAK = eRight

	var ratio = theBoxWidth / theBoxHeight;
	var screenWidM = theBoxWidth / 96 / 39.37;

	var newWid = screenWidM * theScale;
	var cenx = (eLeft + eRight)/2;
	var ceny = (eBottom + eTop)/2;

	if (loadMap)
	{
		saveLastExtent();
	}

	eLeft = cenx - (newWid/2);
	eRight = cenx + (newWid/2);

	var newHigh = newWid/ratio;

	eBottom = ceny - (newHigh/2);
	eTop = ceny + (newHigh/2);

	if (loadMap) 
	{
		sendMapXML();
	}
}
//---------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------

// ************************************************ //
// **** End of advanced print quality rountine **** //
// ************************************************ //
