//JavaScript functions for creating a print page

var tempeLeft;
var tempeRight;
var tempeTop;
var tempeBottom;
var intLayoutChoice = 0;
var strTitle="";
var strSubtitle="";
var t;
var s;
var tName = "";
	if (opener) {
		if (opener.name=="MapFrame") {
			t = opener.parent.MapFrame;
			tName = "opener.parent.MapFrame.";
		} else {
			t = opener;
			tName = "opener.";
		}
	} else {
			
		if (parent.MapFrame!=null) {
			t = parent.MapFrame;
				tName = "parent.MapFrame.";
		} else {
			t=document;
		}
	}
	if (opener){
		//alert(opener.name);
		s = opener.parent.TOCFrame;
	} else {
		//alert("Something else opened it");
		s = parent.TOCFrame;
	}

function trim(strText) { 
	//Description - removes any leading and/or trailing spaces from a string value that is passed in.
	//Parameters passed to function –strText
	//Return value of function – strText
	//Function called by – checkTitle
	//Functions called – none
	//Global variables used - none

    while (strText.substring(0,1) == ' '){ 
        strText = strText.substring(1, strText.length);
	}
    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' '){
        strText = strText.substring(0, strText.length-1);
	}
	return strText;
}

function createPrintMap(theForm) {
	//Description – creates a user customized map.
	//Parameters passed to function –theForm
	//Return value of function – 
	//Function called by – submit button of theForm in customPrint.asp 
	//Functions called – checkTitle, setLayoutChoice, setImageSize, setLegendSize, setExtent.
	//Global variables used - 

	//alert("Create Print Map function called");
	var strScale;
	var intImageHeight;
	var intImageWidth;
	var intLegendHeight;
	var intLegendWidth;
	var tempWidth = t.iWidth;
    var tempHeight = t.iHeight;
    var tempLegWidth = t.legWidth;
    var tempLegHeight = t.legHeight;
    tempeLeft = t.eLeft;
    tempeRight = t.eRight;
    tempeTop = t.eTop;
    tempeBottom = t.eBottom;
    
    showRetrieveMap();
	checkTitle(theForm);
	intLayoutChoice = setLayoutChoice(theForm);
	t.imageSizeArray = setImageSize();
	legVis2=legendVisible;
	if (aimsLegendPresent) legendVisible=true;
	t.legendSizeArray = setLegendSize();
	setExtent(theForm);
	var theString = writeXML();
	t.iWidth=tempWidth;
	t.iHeight = tempHeight;
	t.legWidth = tempLegWidth;
	t.legHeight = tempLegHeight;
	legendVisible = legVis2;
	sendToServer(imsURL,theString,1001);
	
	tempWidth=null;
	tempHeight=null;
	tempLegWidth = null;
	tempLegHeight = null;
	theString=null;	
}

function checkTitle(theForm){
	//Description – checks and sets the value of strTitle and strSubtitle.
	//Parameters passed to function –theForm
	//Return value of function – 
	//Function called by – createPrintMap
	//Functions called – trim
	//Global variables used - 

	//var theForm=document.forms[0];
	strTitle=trim(theForm.title.value);
	strSubtitle=theForm.subtitle.value;
	if ((strTitle=="Enter title here") || (strTitle.length==0)){
		strTitle="Indianapolis Custom Map";
	}
	if (strSubtitle=="Enter subtitle here"){
		strSubtitle="";
	}
}

function setLayoutChoice(theForm) {
	//Description – sets the value of intLayoutChoice based on user input values.
	//Parameters passed to function –theForm
	//Return value of function –  intLayoutChoice
	//Function called by – createPrintMap
	//Functions called – 
	//Global variables used - 

	//var theForm=document.forms[0];
	
	for (var i=0; i<theForm.R1.length; i++){
		if (theForm.R1[i].checked){
			break
		}
	}
	strPaper=theForm.R1[i].value;
	//alert(strPaper);
	
	for (var i=0; i<theForm.R2.length; i++){
		if (theForm.R2[i].checked){
			break
		}
	}
	strOrientation=theForm.R2[i].value;
	//alert(strOrientation);
	
	for (var i=0; i<theForm.R3.length; i++){
		if (theForm.R3[i].checked){
			break
		}
	}
	bolOverview=theForm.R3[i].value;
	//alert(bolOverview);
	
	if (strPaper=="Letter"){
		intLayoutChoice = intLayoutChoice + 1;
	} else if (strPaper=="Legal"){
		intLayoutChoice = intLayoutChoice + 2;
	} else {
		intLayoutChoice = intLayoutChoice + 4;
	}
	
	if (strOrientation=="Portrait"){
		intLayoutChoice = intLayoutChoice + 8;
	} else {
		intLayoutChoice = intLayoutChoice + 16;
	}
	
	if (bolOverview=="True") {
		intLayoutChoice = intLayoutChoice + 32;
	} else {
		intLayoutChoice = intLayoutChoice + 64;
	}
	//alert(intLayoutChoice);
	return intLayoutChoice;
}

function setImageSize() {
	//sets the size of the map image based on the user input choices.
	//Description – sets the value of intImageHeight and intImageWidth and returns them in an array (imageSizeArray).
	//Parameters passed to function – intLayoutChoice
	//Return value of function – imageSizeArray 
	//Function called by – createPrintMap
	//Functions called – 
	//Global variables used - 

	switch (intLayoutChoice) {
		case 41:
			//alert ("Letter, Portrait, OV");
			intImageHeight = 542;
			intImageWidth = 662;
			break
		case 42:
			//alert ("Legal, Portrait, OV");
			intImageHeight = 732;
			intImageWidth = 642;
			break
		case 44:
			//alert ("Ledger, Portrait, OV");
			intImageHeight = 1022; 
			intImageWidth = 902;
			break
		case 49:
			//alert ("Letter, Landscape, OV");
			intImageHeight = 590;
			intImageWidth = 700;
			break
		case 50:
			//alert ("Legal, Landscape, OV");
			intImageHeight =580; 
			intImageWidth = 854;
			break
		case 52:
			//alert ("Ledger, Landscape, OV");
			intImageHeight = 772;
			intImageWidth = 1084;
			break
		case 73:
			//alert ("Letter, Portrait, no OV");
			intImageHeight = 542;
			intImageWidth = 662;
			break
		case 74:
			//alert ("Legal, Portrait, no OV");
			intImageHeight = 734;
			intImageWidth = 662;
			break
		case 76:
			//alert ("Ledger, Portrait, no OV");
			intImageHeight = 1022;
			intImageWidth = 902;
			break
		case 81:
			//alert ("Letter, Landscape, no OV");
			intImageHeight = 590;
			intImageWidth = 700;
			break
		case 82:
			//alert("Legal, Landscape, no OV");
			intImageHeight = 590;
			intImageWidth = 854;
			break
		case 84:
			//alert ("Ledger, Landscape, no OV");
			intImageHeight = 782;
			intImageWidth = 1094;
			break										
	}
	
	if(isNav){
		intImageHeight = intImageHeight * 1.25;
		intImageWidth = intImageWidth * 1.25;
	}
		
	t.iHeight = intImageHeight;
	t.iWidth = intImageWidth;
	var imageSizeArray = new Array(1);
		imageSizeArray[0] = intImageHeight;
		imageSizeArray[1] = intImageWidth;
	return imageSizeArray
}

function setLegendSize() {
	//sets the size of the legend image based on the user input choices.
	//Description – sets the value of intLegendHeight and intLegendWidth and returns them in an array (legendSizeArray).
	//Parameters passed to function – intLayoutChoice
	//Return value of function – legendSizeArray 
	//Function called by – createPrintMap
	//Functions called – 
	//Global variables used - 

	switch (intLayoutChoice) {
		case 41:
			//alert ("Letter, Portrait, OV");
			intLegendHeight = 286; 
			intLegendWidth = 190;
			break
		case 42:
			//alert ("Legal, Portrait, OV");
			intLegendHeight = 326;
			intLegendWidth = 306;
			break
		case 44:
			//alert ("Ledger, Portrait, OV");
			intLegendHeight = 334;
			intLegendWidth = 286;
			break
		case 49:
			//alert ("Letter, Landscape, OV");
			intLegendHeight = 224;
			intLegendWidth = 190;
			break
		case 50:
			//alert ("Legal, Landscape, OV");
			intLegendHeight = 262;
			intLegendWidth = 334;
			break
		case 52:
			//alert ("Ledger, Landscape, OV");
			intLegendHeight = 286;
			intLegendWidth = 382;
			break
		case 73:
			//alert ("Letter, Portrait, no OV");
			intLegendHeight = 286;
			intLegendWidth = 334;
			break
		case 74:
			//alert ("Legal, Portrait, no OV");
			intLegendHeight = 334;
			intLegendWidth = 334;
			break
		case 76:
			//alert ("Ledger, Portrait, no OV");
			intLegendHeight = 334;
			intLegendWidth = 454;
			break
		case 81:
			//alert ("Letter, Landscape, no OV");
			intLegendHeight = 398;
			intLegendWidth = 182;
			break
		case 82:
			//alert("Legal, Landscape, no OV");
			intLegendHeight = 406;
			intLegendWidth = 334;
			break
		case 84:
			//alert ("Ledger, Landscape, no OV");
			intLegendHeight = 502;
			intLegendWidth = 382;
			break										
	}
	
	if(isNav){
		intLegendHeight = intLegendHeight * 1.25;
		intLegendWidth = intLegendWidth * 1.25;
	}
		
	t.legHeight = intLegendHeight;
	t.legWidth = intLegendWidth;
	var legendSizeArray = new Array(1);
		legendSizeArray[0] = intLegendHeight;
		legendSizeArray[1] = intLegendWidth;
	return legendSizeArray	
}

function setExtent(theForm) {
	//Description – Take the extent chosen by the user, determine the map extent, reset global variables to be used in sending the mapXML.
	//Parameters passed to function – theForm
	//Return value of function – 
	//Function called by – createPrintMap
	//Functions called – 
	//Global variables used – eTop, eBottom, eLeft, eRight
	
	var centerPoint;
	//var theForm=document.forms[0];
	var strScale = "";
	var scaleDenominator;
	
	//calculate the center point
	centerPointX = (t.eRight + t.eLeft)/2;
	centerPointY = (t.eTop + t.eBottom)/2;
	
	
	for (var i=0; i<theForm.R4.length; i++){
		if (theForm.R4[i].checked){
			break
		}
	}
	strScale = theForm.R4[i].value;
	//alert(strScale);
	
	switch (strScale) {
		case "CurrentScale":
			//intExtentWidth=intImageWidth*t.mapScaleFactor;
			//intExtentHeight=intImageHeight*t.mapScaleFactor;
			intExtentWidth=t.iWidth*t.mapScaleFactor;
			intExtentHeight=t.iHeight*t.mapScaleFactor;
			//Envelope
			t.eLeft = centerPointX - (intExtentWidth/2);
			t.eRight = centerPointX + (intExtentWidth/2);
			t.eBottom = centerPointY - (intExtentHeight/2);
			t.eTop = centerPointY + (intExtentHeight/2);
			//alert("eLeft " + t.eLeft + "\neRight " + t.eRight + "\neTop " + t.eTop + "\neBottom " + t.eBottom);
			break
		case "CurrentExtent":
			//use eLeft, eRight, eTop, eBottom from current extent
			break
		default:
			if (strScale == "1:1200") {
				scaleDenominator = 1200;
			} else if (strScale == "1:6000"){
				scaleDenominator = 6000;	
			} else if (strScale == "1:12000"){
				scaleDenominator = 12000;	
			} else if (strScale == "1:24000"){
				scaleDenominator = 24000;	
			}
			
			doZoomScale(scaleDenominator);
	}		
}
function doZoomScale(zval){
	saveLastExtent();
	var msize;
	if (t.iHeight < t.iWidth){
		msize = t.iHeight;
	} else {
		msize = t.iWidth;
	}
	var midX =((t.eLeft + t.eRight)/2);
	var midY = ((t.eTop + t.eBottom)/2);
	var mscale = (zval * ((msize/96)/12)) * .5;
	t.eLeft = ((midX) - (mscale));
	t.eRight = ((midX) + (mscale));
	t.eBottom = ((midY) - (mscale));
	t.eTop = ((midY) + (mscale));
	
}

function writeCustomPrintPage() {
//Description - selects the appropriate HTML layout based on the user input options.
//Parameters passed to function –intLayoutChoice
//Return value of function – None
//Function called by – getPrintLegend()
//Functions called – none
//Global variables used - t.printURL, t.appName - set in ArcIMSparam.js
	
	//based on the appropriate case switch and open the corresponding layout print page
	switch (intLayoutChoice) {
		case 41:
			//alert ("Letter, Portrait, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/41.htm"
			break
		case 42:
			//alert ("Legal, Portrait, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/42.htm"
			break
		case 44:
			//alert ("Ledger, Portrait, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/44.htm"
			break
		case 49:
			//alert ("Letter, Landscape, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/49.htm"
			break
		case 50:
			//alert ("Legal, Landscape, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/50.htm"
			break
		case 52:
			//alert ("Ledger, Landscape, OV");
			hasOVMap = true;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/52.htm"
			break
		case 73:
			//alert ("Letter, Portrait, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/73.htm"
			break
		case 74:
			//alert ("Legal, Portrait, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/74.htm"
			break
		case 76:
			//alert ("Ledger, Portrait, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/76.htm"
			break
		case 81:
			//alert ("Letter, Landscape, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/81.htm"
			break
		case 82:
			//alert("Legal, Landscape, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/82.htm"
			break
		case 84:
			//alert ("Ledger, Landscape, no OV");
			hasOVMap = false;
			var Win1 = open("","PrintPage");
			Win1.location.href = t.printURL + t.appName + "printLayouts/84.htm"
			break											
	} 
	//reset eTop, eBottom, eLeft, eRight to original extent
	t.eLeft = tempeLeft;
	t.eRight = tempeRight;
	t.eTop = tempeTop;
	t.eBottom = tempeBottom;
	//refresh the main map
	t.sendMapXML();
	intLayoutChoice = 0;
	hideRetrieveMap();
}