// aimsPublic.js
//contains functionality developed to work with the public map services

//variable declarations
var routesOn = false;		//indicates if the RouteList in BusFinder is showing or not
var areaQuery = false;		//if areaQuery functionality is running. Important for redirecting code flow
var areaQueryStage = -1;	//Redirects work flow. Stage 1 = searching for center parcel, Stage 2 = buffering around it
var areaQueryMode = "";		// 'geocode' or 'query': redirects code flow
var strTxtPpn = "";
var strTxtAddress = "";		//these two store textbox values from comparablesales.htm page. 
var areaQueryUnits = "";	//unit of measure used for the buffer. Set on the comparable sales page.
var measureWindow;		//stores reference to the measurement window
var measureWindowUp = true;		//blocks flag setting and distance measurement when "change units" is up
var parcelReportXMLMode = 16;		//redirects work flow during the parcel reporting process
var parcelReportNorth = 0;		//stores search tolerance for multiple parcel report queries
var parcelReportEast = 0;
var parcelReportWest = 0;
var parcelReportSouth = 0;
var searchValue;			//The name or ppn that was entered on the addmatch.htm page 
var searchType;				//whether a name or ppn was entered

//voting finder variables
var runningVoterSearch = false;		//helps to direct automated Voting Search functionality
var showPollingSite = false;		//indicates that a polling place should be labeled and indicated on the map
var storedSearchAddress = "";		//the address being searched for in the automated search
var addressX;				//map coords of the user's address
var addressY;
var pollingX;				//map coords of the polling place assigned to that precinct
var pollingY;
var pollingLabel;			//label to go with those map coords
var votingLayers = ["County Commissioner Districts","Intermediate School Districts","Library Districts","School Districts","State House Districts","State Senate Districts","US House Districts","Wards and Precincts","Polling Sites"];
var votingReplies = ["","","","","","","","",""];	//stores replies of the automated identify requests
var tempWest;
var tempNorth;
var tempEast;
var tempSouth;


//routeVisible tracks the visibility of each route in BusFinder. There are currently 23 routes (indexed 0-22) 
var routeVisible = [true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true]

//changes symbology of bus route layer
function addBusXML() {
	var busString;
	var busLabelSubString = "";
	busString = '<GROUPRENDERER>\n';
	busString += '<VALUEMAPRENDERER lookupfield="TRANSPORTATION.BusRoutes.ROUTE_NUMBER">\n';
	var theLength = routeVisible.length
	for (var i=0;i<theLength;i++) {
		if (routeVisible[i]) {
				switch(i) {
			case 0:
				busString += '<EXACT value="1" label="#1 Division">\n';
				busString += '<SIMPLELINESYMBOL color="241,85,254" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="1">\n'; 
				break;
			case 1:
				busString += '<EXACT value="2" label="#2 Kalamazoo">\n';
				busString += '<SIMPLELINESYMBOL color="229,0,0" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="2">\n'; 	
				break;
			case 2:
				busString += '<EXACT value="3" label="#3 Madison">\n';
				busString += '<SIMPLELINESYMBOL color="254,179,0" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="3">\n';
				break;
			case 3:
				busString += '<EXACT value="4" label="#4 Eastern">\n';
				busString += '<SIMPLELINESYMBOL color="0,66,135" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="4">\n';
				break;
			case 4:
				busString += '<EXACT value="5" label="#5 Wealthy">\n';
				busString += '<SIMPLELINESYMBOL color="72,118,46" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="5">\n';
				break;
			case 5:
				busString += '<EXACT value="6" label="#6 Cherry">\n';
				busString += '<SIMPLELINESYMBOL color="254,185,227" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="6">\n';
				break;
			case 6:
				busString += '<EXACT value="7E" label="#7E Leonard Crosstown East">\n';
				busString += '<SIMPLELINESYMBOL color="204,0,125" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="7E">\n';
				break;
			case 7:
				busString += '<EXACT value="7W" label="#7W Leonard Crosstown West">\n';
				busString += '<SIMPLELINESYMBOL color="204,0,125" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="7W">\n';
				break;
			case 8:
				busString += '<EXACT value="8" label="#8 Rivertown">\n';
				busString += '<SIMPLELINESYMBOL color="158,0,229" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="8">\n';
				break;
			case 9:
				busString += '<EXACT value="9" label="#9 Alpine">\n';
				busString += '<SIMPLELINESYMBOL color="0,167,199" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="9">\n';
				break;

			case 10:
				busString += '<EXACT value="10" label="#10 Clyde Park">\n';
				busString += '<SIMPLELINESYMBOL color="254,133,25" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="10">\n';
				break;
			case 11:
				busString += '<EXACT value="11" label="#11 Plainfield">\n';
				busString += '<SIMPLELINESYMBOL color="0,153,254" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="11">\n';
				break;
			case 12:
				busString += '<EXACT value="12E" label="#12E Fulton Crosstown East">\n';
				busString += '<SIMPLELINESYMBOL color="115,131,37" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="12E">\n';
				break;
			case 13:
				busString += '<EXACT value="12W" label="#12W Fulton Crosstown West">\n';
				busString += '<SIMPLELINESYMBOL color="115,131,37" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="12W">\n';
				break;
			case 14:
				busString += '<EXACT value="13" label="#13 Michigan">\n';
				busString += '<SIMPLELINESYMBOL color="175,254,230" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="13">\n';
				break;
			case 15:
				busString += '<EXACT value="14" label="#14 Burton Crosstown">\n';
				busString += '<SIMPLELINESYMBOL color="0,213,254" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="14">\n';
				break;
			case 16:
				busString += '<EXACT value="28" label="#28 28th St Crosstown">\n';
				busString += '<SIMPLELINESYMBOL color="199,0,168" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="28">\n';
				break;
			case 17:
				busString += '<EXACT value="44" label="#44 44th St Crosstown">\n';
				busString += '<SIMPLELINESYMBOL color="0,254,88" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="44">\n';
				break;
			case 18:
				busString += '<EXACT value="50" label="#50 GVSU Campus Connector">\n';
				busString += '<SIMPLELINESYMBOL color="90,115,131" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="50">\n';
				break;
			case 19:
				busString += '<EXACT value="50A" label="#50A GVSU Saturday Extension">\n';
				busString += '<SIMPLELINESYMBOL color="90,115,131" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="50A">\n';
				break;
			case 20:
				busString += '<EXACT value="100" label="#100 GVSU Off-Campus Shuttle">\n';
				busString += '<SIMPLELINESYMBOL color="254,90,229" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="100">\n';
				break; 
			case 21:
				busString += '<EXACT value="DS" label="DASH South">\n';
				busString += '<SIMPLELINESYMBOL color="86,0,254" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="DS">\n';
				break;
			case 22:
				busString += '<EXACT value="WD" label="DASH West">\n';
				busString += '<SIMPLELINESYMBOL color="86,0,254" transparency="1" width="3" type="solid" /></EXACT>\n';
				busLabelSubString = busLabelSubString += '<EXACT value="WD">\n';
				break;
				}
				busLabelSubString += '<TEXTSYMBOL antialiasing="true" glowing="255,255,255" font="Verdana" fontstyle="bold" fontsize="10" /></EXACT>\n';
			}
		} 	
	busString += '</VALUEMAPRENDERER>\n';
	busString += '<VALUEMAPLABELRENDERER lookupfield="TRANSPORTATION.BusRoutes.ROUTE_NUMBER" labelfield="TRANSPORTATION.BusRoutes.ROUTE_NUMBER" howmanylabels="one_label_per_name">\n';
	busString += busLabelSubString;
	busString += '</VALUEMAPLABELRENDERER>\n';
	busString += '</GROUPRENDERER>\n';
	return busString;
}

//changes symbology of various layers
function addSymbolXML(layerid) {
	var symbolString;
	switch(layerid) {
		case "parcels":
			symbolString += '<SIMPLERENDERER>\n';
                         symbolString += '<SIMPLEPOLYGONSYMBOL fillcolor="200,255,0" filltransparency="0" filltype="solid" fillinterval="6" boundarycolor="200,255,0" boundarytransparency="1" boundarywidth="3" boundarytype="solid" boundarycaptype="butt" boundaryjointype="round" />\n';
               		symbolString += '</SIMPLERENDERER>\n';
			break;
		case "parcelanno":
			symbolString += '<GROUPRENDERER>\n';
			symbolString += '<SIMPLERENDERER>\n';
                	symbolString += '<SIMPLELINESYMBOL color="0,0,0" transparency="0" width="1" type="solid" captype="round" jointype="round" />\n';
                 	symbolString += '</SIMPLERENDERER>\n';
                 	symbolString += '<SIMPLELABELRENDERER field="CADASTRAL.PARCEL_NUMBER_ANNO.ANNO_TEXT" linelabelposition="placeontop">\n';
                        symbolString += '<TEXTSYMBOL antialiasing="true" font="Arial" fontstyle="bold" fontsize="14" fontcolor="0,255,100"/>\n';
                 	symbolString += '</SIMPLELABELRENDERER>';
        		symbolString += '</GROUPRENDERER>';
			break;
	}			
	return symbolString;
}

function getPPN(GCX,GCY) {
	fullSetActiveLayer("Parcels");
	//var theX = mouseX;
	//var theY = mouseY;
	//getMapXY(theX,theY);
	//identifyPointX = GCX;
	//identifyPointY = GCY;
	//alert("GCX="+GCX);
	//alert("GCY="+GCY);
	//alert("xDistance="+xDistance);
	//alert("iWidth="+iWidth);
	//searchTolerance = (xDistance/iWidth) * 5;
	searchTolerance = 5;
	var tempWest = GCX - searchTolerance;
	var tempNorth = GCY + searchTolerance;
	var tempEast = GCX + searchTolerance;
	var tempSouth = GCY - searchTolerance;
	var theString = writeGetFeatures(tempWest,tempSouth,tempEast,tempNorth);
	showRetrieveData();
	sendToServer(imsQueryURL,theString,17);
}

//zf edited on 1/22/2009, parcel info link to kent county
function accessKentTransfer(thePPN) {
	//first, we have to parse the ppn to Access Kent's standards
	thePPN = thePPN.toString();
	//alert(thePPN);
	var partOne = thePPN.slice(0,2)
	var partTwo = thePPN.slice(2,4)
	var partThree = thePPN.slice(4,6)
	var partFour = thePPN.slice(6,9)
	var partFive = thePPN.slice(9,12)
	var AKPPN = partOne + "-" + partTwo+"-"+partThree+"-"+partFour+"-"+partFive;
	var theString = "https://www.accesskent.com/Property/PropertyTemplate.jsp?include=PropBasicParcel.jsp?parcelNumber="+thePPN;
	var Win1 = open("","AccessKent");
	Win1.document.location = theString;
	
}

function autoVotingFrontEnd(geoAdd,geoX,geoY) {
	/*once a user-specified address is found, this function governs a series of identify requests. 
	Once complete, the results are listed in a report and an appropriate map is generated*/
	//store the address's map points. Need them for map later
	addressX = geoX;
	addressY = geoY;
	storedSearchAddress = geoAdd;
	//alert(addressX+"   "+addressY+"   "+storedSearchAddress);
	//translate geocode points to an envelope for an identify request
	//searchTolerance = (xDistance/iWidth) * 5;
	searchTolerance = 1;
	tempWest = geoX - searchTolerance;
	tempNorth = geoY + searchTolerance;
	tempEast = geoX + searchTolerance;
	tempSouth = geoY - searchTolerance;
	showRetrieveData();
	autoVotingProgram("launch");
}

function autoVotingProgram(theReply) {
	//find the next empty reply and identify that layer
	for (var t=0;t<votingLayers.length;t++) {
		if (votingReplies[t] == "") {
			var theLayer;
			if (theReply != "launch") {
				if (justGetFeatureCount(theReply) > 0) votingReplies[t] = theReply;
				else votingReplies[t] = "No Match";
				theLayer = votingLayers[t+1];
			} else {
				theLayer = votingLayers[0];
			}
			if (t != votingLayers.length - 1) {
				if (theLayer == "Polling Sites") {
					//Last in the list: this one gets handled differently
					//First, we need the polling place tag no. from the wards and precincts reply
					var strWardsReply = "";
					for (var g=0;g<votingLayers.length;g++) {
						if (votingLayers[g] == "Wards and Precincts") strWardsReply = votingReplies[g];
					}
					var intStartIndex = strWardsReply.indexOf("TAG=") + 5;
					var intEndIndex = strWardsReply.indexOf('"',intStartIndex);
					var theTagNumber = strWardsReply.slice(intStartIndex,intEndIndex);
					var theQueryString = "PRECINCT_K = '" + theTagNumber + "'";
					fullSetActiveLayer(theLayer);
					var theXML = writeQueryXML(theQueryString);
					sendToServer(imsQueryURL,theXML,18);
					return;
				} else {
					fullSetActiveLayer(theLayer);
					var theString = writeGetFeatures(tempWest,tempSouth,tempEast,tempNorth);
					sendToServer(imsQueryURL,theString,18);
					return;
				}
			}
		}
	}
	//Get the precinct info now...need it for the map.
	var thePrecinctReply;
	for (var g=0;g<votingLayers.length;g++) {
		if (votingLayers[g] == "Wards and Precincts") thePrecinctReply = votingReplies[g];
	}
	if (thePrecinctReply != "No Match") {
		var featPos = thePrecinctReply.indexOf('<FEATURECOUNT');
		var intStartPos = thePrecinctReply.indexOf('minx="',featPos);
		var intNextPos = thePrecinctReply.indexOf('"',intStartPos+7);
		customLeft = thePrecinctReply.slice(intStartPos+6,intNextPos);
		intStartPos = thePrecinctReply.indexOf('miny="',featPos);
		intNextPos = thePrecinctReply.indexOf('"',intStartPos+7);
		customBottom = thePrecinctReply.slice(intStartPos+6,intNextPos);
		intStartPos = thePrecinctReply.indexOf('maxx="',featPos);
		intNextPos = thePrecinctReply.indexOf('"',intStartPos+7);
		customRight = thePrecinctReply.slice(intStartPos+6,intNextPos);
		intStartPos = thePrecinctReply.indexOf('maxy="',featPos);
		intNextPos = thePrecinctReply.indexOf('"',intStartPos+7);
		customTop = thePrecinctReply.slice(intStartPos+6,intNextPos);
		customLeft = Number(customLeft) - 500;
		customRight = Number(customRight) + 500;
		customTop = Number(customTop) + 500;
		customBottom = Number(customBottom) - 500;
		if (customLeft == customRight || customTop == customBottom) {
			//don't use 'em after all
		} else useCustomCoords = true;
	}

	//Now we need some polling place info
	//#1: label and points (envelope) to draw a point on the map
	//#2: double check the point in relation to the envelope. Expand the envelope if it falls outside
	var thePollingReply;
	for (var g=0;g<votingLayers.length;g++) {
		if (votingLayers[g] == "Polling Sites") thePollingReply = votingReplies[g];
	}
	if (thePollingReply != "No Match") {
		showPollingSite = true;
		var intStartIndex = thePollingReply.indexOf('minx=',0) + 6;
		var intEndIndex = thePollingReply.indexOf('"',intStartIndex);
		pollingX = thePollingReply.slice(intStartIndex,intEndIndex);
		intStartIndex = thePollingReply.indexOf('miny=',0) + 6;
		intEndIndex = thePollingReply.indexOf('"',intStartIndex);
		pollingY = thePollingReply.slice(intStartIndex,intEndIndex);
		intStartIndex = thePollingReply.indexOf('PLACE_NAME=',0) + 12;
		intEndIndex = thePollingReply.indexOf('"',intStartIndex);
		pollingLabel = thePollingReply.slice(intStartIndex,intEndIndex);
		//alert(pollingX+"  "+pollingY+"  "+pollingLabel);
	}
	//Double check the polling place coords: if they lie outside the extent of the custom coords, they'll have to be adjusted
	if (pollingX < customLeft) customLeft = pollingX - 500;
	if (pollingX > customRight) customRight = pollingX + 500;
	if (pollingY < customBottom) customBottom = pollingY - 500;
	if (pollingY > customTop) customTop = pollingY + 500; 
	sendMapXML();
	var Win1 = window.open("http://" + hostName + "/website/public/votingReport.htm","QueryWindow","left=120,top=400,width=700,height=200,scrollbars=yes,resizable=yes");
	parent.BottomFrame.document.location = "http://" + hostName + "/website/public/images/polling_places_legend.gif";
	Win1 = null;
}

