// CivXplorer PDF output
// Suzanne Fliege
// Plansight, LLC

//******************************************************************************
// 			client specific variables
//******************************************************************************
var PrintService = MasterService;
// PDF application location
var PDFParentDir = "http://" + hostName + "/CivXplorerPDFcreation/";
// is it a version that supports duplicate owner name filtering
var IsOwnerFilterSupport = true;

// output file locations (use ArcIMS Output directory, and Tasker for cleanup)
var outputURL = "http://" + hostName + "/output/";
var outputDir = "d:/arcims/output/";
var outputBasename = "Kalamazoo";

// layer specific mailing label info
//******************************************************************************
// master variables
var mailingPDFLayerID = new Array();			// layer ID from AXL
var mailingOwnerFieldName = new Array();		// field with owner name (for filtering duplicates)
var mailingOwnerFilterStatus = new Array();		// true or false, whether to filter for duplicate owner names
var mailingOwnerLabelLine1 = new Array();
var mailingOwnerLabelLine2 = new Array();
var mailingOwnerLabelLine3 = new Array();
var mailingOwnerLabelLine4 = new Array();
var mailingOwnerLabelLine5 = new Array();
var mailingResidentLabelLine1 = new Array();
var mailingResidentLabelLine2 = new Array();
var mailingResidentLabelLine3 = new Array();
var mailingResidentLabelLine4 = new Array();
var mailingResidentLabelLine5 = new Array();
//******************************************************************************
// list fields in the order you want them cocatenated for each line.  A space will be added between the field values as they are cocatenated.
// use text instead of a field in any line, it will be cocatenated with the fields on that line ("my city,st","ZIP") will come out as 'my city,st 000000' on label
// create empty array(s) for line(s) with no data!
// use lines 2,3,4 to center a 3-line label
// if arrays are not created for a layer, then PDF labels will not be available 
	//(a mailingOwnerFieldNameList or mailingResidentFieldNameList must be created as well, although fields to not need to match or correspond in any way)

// parcels
mailingPDFLayerID[0] = "optional-parcels";
mailingOwnerFieldName[0] = "TAX_NAME";
mailingOwnerFilterStatus[0] = false;
mailingOwnerLabelLine1[0] = new Array("PIN");
mailingOwnerLabelLine2[0] = new Array("TAX_NAME");
mailingOwnerLabelLine3[0] = new Array("TAX_CO","TAX_STREET");
mailingOwnerLabelLine4[0] = new Array("TAX_CITY",", ","TAX_ST","TAX_ZIP");
mailingOwnerLabelLine5[0] = new Array();
mailingResidentLabelLine1[0] = new Array("PIN");
mailingResidentLabelLine2[0] = new Array("CURRENT RESIDENT");
mailingResidentLabelLine3[0] = new Array("SITE_ADDRE","SITE_APT");
mailingResidentLabelLine4[0] = new Array("PROPERTY");
mailingResidentLabelLine5[0] = new Array();


//******************************************************************************
// do not edit below here
//******************************************************************************
// variables and functions to support PDF output of maps, labels and reports
pdfMapOutputAvailable = true;
pdfMailOutputAvailable = true;
var imsPrintURL = "http://" + IMShost + serverAlias + "?ServiceName=" + PrintService;

var PDFMapPrintWin = ""; // variable to save open PDF Map Print window
var PDFMailLabelWin = ""; // variable to save open PDF Mailing Label window
var savedTIFLocation = ""; // variable to save location of current TIF link

var PDFiHeight = 0;
var PDFiWidth = 0;
var PDFScaleFeet = 0;

var mailingPDFInfoIndex = new Array();
var mailingOwnerFieldIndex = new Array();
var mailingOwnerIndexLine1 = new Array();
var mailingOwnerIndexLine2 = new Array();
var mailingOwnerIndexLine3 = new Array();
var mailingOwnerIndexLine4 = new Array();
var mailingOwnerIndexLine5 = new Array();
var mailingResidentIndexLine1 = new Array();
var mailingResidentIndexLine2 = new Array();
var mailingResidentIndexLine3 = new Array();
var mailingResidentIndexLine4 = new Array();
var mailingResidentIndexLine5 = new Array();

function openPDFPrintMapForm() {
	clickFunction("nothing");
	if ((showReportsInFrame) && (isIE)) {
		var theString = '';
		theString += '<table width="100%" cellspacing="0" cellpadding="3">';
		theString += '<tr><td align="left" class="toolInfo1"><a href="#" onClick="parent.hideInlineReportDisplay();return false;" class="noPrint">Close Print Window</a></td></tr>';
		theString += '</table>';
		theString += '<iframe id="InlinePrintFrame" width="100%" height="' + (winHeight-headerHeight-25) + '" scrolling="auto" src="' + PDFParentDir + 'PDFMapUserForm.aspx"></iframe>';
		// show report/map layer
		document.getElementById('cxInlineReports').style.visibility = "visible";
		document.getElementById('ovHeader').style.visibility = "hidden";
		updateContent("cxInlineReports",theString);
		PDFMapPrintWin = parent['InlinePrintFrame'];
	} else {
		PDFMapPrintWin = window.open(PDFParentDir + "PDFMapUserForm.aspx","","scrollbars=yes,resizable=yes,menubar=yes");
	}
	return false;
}

function openPDFMailLabelForm() {
// format requested by user as mailFormatRequest global variable, for future use of multiple label formats
	var tempLabelURL = "";
	if (mailFormatRequest == "pdf-avery5160") tempLabelURL = PDFParentDir + "MailLbl_StartForm_Avery5160.aspx";
	if ((showReportsInFrame) && (isIE)) {
		var theString = '';
		theString += '<table width="100%" cellspacing="0" cellpadding="3">';
		theString += '<tr><td align="left" class="toolInfo1"><a href="#" onClick="parent.hideInlineReportDisplay();return false;" class="noPrint">Close Print Window</a></td></tr>';
		theString += '</table>';
		theString += '<iframe id="InlinePrintFrame" width="100%" height="' + (winHeight-headerHeight-25) + '" scrolling="auto" src="' + tempLabelURL + '"></iframe>';
		// show report/map layer
		document.getElementById('cxInlineReports').style.visibility = "visible";
		document.getElementById('ovHeader').style.visibility = "hidden";
		updateContent("cxInlineReports",theString);
		PDFMapPrintWin = parent['InlinePrintFrame'];
	} else {
		PDFMailLabelWin = window.open(tempLabelURL,"","scrollbars=yes,resizable=yes,menubar=yes");
	}
	return false;
}

function getPDFPrintMap(pdfMapWidth,pdfMapHeight,pdfLegendWidth,pdfLegendHeight) {
	//temporarily save MapFrame map dimensions
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	var tempLegendVisible = legendVisible;
	var tempLegWidth = legWidth;
	var tempLegHeight = legHeight;
	var tempLegColor = legendBackColor;
	var tempDPIscale = 100/pdfLegendWidth;
	saveLastExtent();
	iHeight = pdfMapHeight;
	iWidth = pdfMapWidth;
	legWidth = pdfLegendWidth;
	legHeight = pdfLegendHeight;
	legendBackColor = '';
	legendVisible = true;
	// calculate scale here
	if (PDFScaleFeet > 0) {
		var xCtr = eLeft + (eRight - eLeft)/2;
		var yCtr = eBottom + (eTop - eBottom)/2;
		var pixInch = 72 / tempDPIscale;
		var iWidthFeet = (iWidth / pixInch) * PDFScaleFeet;
		var iHeightFeet = (iHeight / pixInch) * PDFScaleFeet;
		if (MapUnits.toUpperCase() != "FEET") {
			var iWidthMapUnits = convertUnits(iWidthFeet,MapUnits.toUpperCase(),"FEET");
			var iHeightMapUnits = convertUnits(iHeightFeet,MapUnits.toUpperCase(),"FEET");
		} else {
			var iWidthMapUnits = iWidthFeet;
			var iHeightMapUnits = iHeightFeet;
		}
		eLeft = xCtr - iWidthMapUnits/2;
		eRight = xCtr + iWidthMapUnits/2;
		eTop = yCtr + iHeightMapUnits/2;
		eBottom = yCtr - iHeightMapUnits/2;
	}
	// write map request string
	var theString = writeMapXML();
	//reinstate MapFrame map dimensions
	setToLastExtent();
	iWidth = tempWidth;
	iHeight = tempHeight;
	legWidth = tempLegWidth;
	legHeight = tempLegHeight;
	legendBackColor = tempLegColor;
	legendVisible = tempLegendVisible;
	//specify output in XML string
	var newString = theString;
	sendToServer(imsPrintURL,newString,105);
	return false;
}

// get file location strings for map and legend
function getMapLoc(theReply) {
	var theLoc = "";
	var startpos = 0;
	var endpos = 0;
	var pos = theReply.indexOf("OUTPUT");
	if (pos != -1) {
		theLoc = getInsideString(theReply,'file="',dQuote,pos,0,false);
	}
	return theLoc;
}
function getLegendLoc(theReply) {
	var theLoc = "";
	var startpos = 0;
	var endpos = 0;
	var pos = theReply.indexOf("LEGEND");
	//alert(pos);
	if (pos != -1) {
		theLoc = getInsideString(theReply,'file="',dQuote,pos,0,false);
	}
	return theLoc;
}

function getMailingIndexes() {
	// get current layer data
	var tempFields = currentSelectionFields[ActiveLayerIndex];
	var tempMailLines = new Array();
	if (mailInfoRequest == "owner") {
		tempMailLines[0] = mailingOwnerLabelLine1[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[1] = mailingOwnerLabelLine2[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[2] = mailingOwnerLabelLine3[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[3] = mailingOwnerLabelLine4[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[4] = mailingOwnerLabelLine5[mailingPDFInfoIndex[ActiveLayerIndex]];
	} else {
		tempMailLines[0] = mailingResidentLabelLine1[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[1] = mailingResidentLabelLine2[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[2] = mailingResidentLabelLine3[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[3] = mailingResidentLabelLine4[mailingPDFInfoIndex[ActiveLayerIndex]];
		tempMailLines[4] = mailingResidentLabelLine5[mailingPDFInfoIndex[ActiveLayerIndex]];
	}
	var tempLblIndexes = new Array();
	for (var f=0;f<5;f++){ 
		var tempMailLine = tempMailLines[f];
		tempLblIndexes[f] = new Array();
		for (var f1=0;f1<tempMailLine.length;f1++) { // iterate through mailing fields for label line
			for (var f2=0;f2<tempFields.length;f2++) { // iterate through data fields and find match, save index
				if (tempFields[f2] == tempMailLine[f1]) tempLblIndexes[f][f1] = f2;
			}
			if (tempLblIndexes[f][f1] == null) tempLblIndexes[f][f1] = -1;
		}
	}
	// get index of owner field name
	for (var f2=0;f2<tempFields.length;f2++) {
		if (tempFields[f2] == mailingOwnerFieldName[mailingPDFInfoIndex[ActiveLayerIndex]]) mailingOwnerFieldIndex[mailingPDFInfoIndex[ActiveLayerIndex]] = f2;
	}
	if (mailInfoRequest == "owner") {
		mailingOwnerIndexLine1[ActiveLayerIndex] = tempLblIndexes[0];
		mailingOwnerIndexLine2[ActiveLayerIndex] = tempLblIndexes[1];
		mailingOwnerIndexLine3[ActiveLayerIndex] = tempLblIndexes[2];		
		mailingOwnerIndexLine4[ActiveLayerIndex] = tempLblIndexes[3];		
		mailingOwnerIndexLine5[ActiveLayerIndex] = tempLblIndexes[4];		
	} else {
		mailingResidentIndexLine1[ActiveLayerIndex] = tempLblIndexes[0];
		mailingResidentIndexLine2[ActiveLayerIndex] = tempLblIndexes[1];
		mailingResidentIndexLine3[ActiveLayerIndex] = tempLblIndexes[2];		
		mailingResidentIndexLine4[ActiveLayerIndex] = tempLblIndexes[3];		
		mailingResidentIndexLine5[ActiveLayerIndex] = tempLblIndexes[4];		
	}
	return false;
}

// toggle owner filter variable
function setOwnerLabelFilterStatus() {
	mailingOwnerFilterStatus[mailingPDFInfoIndex[ActiveLayerIndex]] = document.PrintReportStuff.FilterOwnerLabels.checked;
	return false;
}
