function selectJurisdiction(result, jurisType, zoomToCounty)
{    
	removePolygon(); //remove the current overlay for the previous jurisdiction        
	var overlayOptions = {
        strokeColor:"#0000FF",
        strokeWeight:2,
        strokeOpacity:0.85,
        fillOpacity:0.50,
        fillColor: "#0000FF"
	};
	
	 //if (result.layerName == "County")
     //{
	 //	 overlayOptions.strokeOpacity = 0.0;
     //	 overlayOptions.fillOpacity = 0.0;
     //}    
	
	var shapes = [];
	var encoder = new PolylineEncoder();
	// check to see if an array of features were returned
	var features = (result.features.length ? result.features : [result.feature]);
	var jurisdictionToSelect = 0;
	for (var k=0; k < features.length; k++)
	{
		// find the jurisdiction that isn't a splinter (no ';' allowed in the label)
		if (features[k].attributes[GIS_LABEL_FIELD].indexOf(';') == -1)
		{
			jurisdictionToSelect = k;
		}
		
		var geometry = features[k].geometry;
		for (var i=0; i < geometry.length; i++)
		{
			var points = [];
			var vertexCount = geometry[i].getVertexCount();
			for (var j=0; j < vertexCount; j++)
				points.push(geometry[i].getVertex(j));
			shapes.push(encoder.dpEncode(points));
		}
	}
	
	var polygonData = {
			polylines: [],
			fill: true,
			color: overlayOptions.fillColor,
			opacity: overlayOptions.fillOpacity,
			outline: true
	}
	
	for (var i=0; i < shapes.length; i++)
	{
		var polyline = {
			points: shapes[i].encodedPoints,
			levels: shapes[i].encodedLevels,
			color: overlayOptions.strokeColor,
			opacity: overlayOptions.strokeOpacity,
			weight: overlayOptions.strokeWeight,
			numLebels: 18,
			zoomFactor: 2
		};
		polygonData.polylines.push(polyline);
	}
	var polygon = new GPolygon.fromEncoded(polygonData);
	GEvent.addListener(polygon, "click", openInfoWindow);
	gmap.addOverlay(polygon);
	
	var selectedJurisdictionFeatures = result.features[jurisdictionToSelect];
	selectedJurisdiction.sguid = selectedJurisdictionFeatures.attributes[GIS_SGUID_FIELD];
	selectedJurisdiction.label = selectedJurisdictionFeatures.attributes[GIS_LABEL_FIELD];
	selectedJurisdiction.type = result.layerName;
	selectedJurisdiction.polygon = polygon;
	selectedJurisdiction.infoWindow.html = genInfoWindowData();
	
	if (zoomToCounty == zoomTypes.always || ( zoomToCounty == zoomTypes.conditional && gmap.getZoom() < 11))
    	adjustZoom(selectedJurisdiction.polygon)
	
	openInfoWindow();
}

function removePolygon() 
{
   // if (selectedJurisdiction.polygon)
   // {
   //	gmap.removeOverlay(selectedJurisdiction.overlay);
   // }
    
    selectedJurisdiction.type = null;
	selectedJurisdiction.label = null;    	
	selectedJurisdiction.sguids = [];
	selectedJurisdiction.mainSguid = null;
	selectedJurisdiction.polygon = null;
	selectedJurisdiction.overlay = null;
	selectedJurisdiction.infoWindow.html = null;
	selectedJurisdiction.selectInfo = null;
	
	PrintObject.list = null;
	PrintObject.jurisInfo = null;
	PrintObject.bubbleInfo = null;
	PrintObject.selectedPolygon = null;    
    
    removeInfoWindow();
    removeMarkerAtPoint();
    removeCircleHint();
    redrawDynamicTiles();
}
function removeRadiusPolygon() 
{
    if (radiusSearchCircle.overlay  != null) 
    {
        gmap.removeOverlay(radiusSearchCircle.overlay);
        gmap.removeOverlay(radiusSearchCircle.crossHair);
        radiusSearchCircle.overlay  = null;
        radiusSearchCircle.points  = null;
        radiusSearchCircle.crossHair  = null;
        radiusSearchCircle.crossHairPoints  = null;
        removeInfoWindow();
    }
    for (var i=0; i < circleOverlays.length; i++)
    	gmap.removeOverlay(circleOverlays[i]);
 
    removeCircleHint();
    circleOverlays =[];
    clearListSearch();
    adjustListResultDiv();
    if (getSelectedTab()== "#list")
		switchTab("#search");
}
function removeInfoWindow()
{
    var infoWindow = gmap.getInfoWindow();
    infoWindow.hide();
}
function drawCircleForSearch(cPoint, radius, isNewSearch)
{	
	var color = radiusColorKey[radius];
	var width = 3;  
	var radians;
	if (isMiles)
		radians = radius/3963;  //for miles
	else
		radians = radius/6378.8;  // for kilo
		
	var lat = (Math.PI/180)*cPoint.lat();  
	var lng =(Math.PI/180)*cPoint.lng(); 
	var points = []; 
	
	for (var i=0; i < 361; i+=15) 
	{ 
	   var theta = (Math.PI/180)*i; 
	   var pLat = Math.asin(Math.sin(lat)*Math.cos(radians)+Math.cos(lat)*Math.sin(radians)*Math.cos(theta));
	   var dLng =  Math.atan2(Math.sin(theta)*Math.sin(radians)*Math.cos(lat),Math.cos(radians)-Math.sin(lat)*Math.sin(pLat));
	   var pLng = ((lng-dLng+Math.PI) % (2*Math.PI)) - Math.PI; 
	   var point = new GLatLng(parseFloat(pLat*(180/Math.PI)), parseFloat(pLng*(180/Math.PI)));
	   points.push(point); 
	}
	var overlay = new GPolygon(points, color, width, 0.5, color, 0.3);
	GEvent.addListener(overlay, "click", openCircleInfoWindow);
	GEvent.addListener(overlay, "mouseover", function(){showCircleHint(cPoint);});
	GEvent.addListener(overlay, "mouseout", removeCircleHint);
	
	if (isNewSearch) //leave other circles if this isn't a new search
	{
		removeRadiusPolygon(); 
		radiusSearchCircle.polyClickPoint = cPoint;
		radiusSearchCircle.points  = points;
		radiusSearchCircle.overlay = overlay;	
	}
	radiusSearchCircle.radius = radius; //store the current radius size
	circleOverlays.push(overlay);
	gmap.addOverlay(overlay);
	
	addCrossHair();
	gmap.setCenter(cPoint);
	switch (radius)
	{
		case 15:
		case 10:
			gmap.setZoom(10);
			break;
		case 0.25:
		case 0.5:
			gmap.setZoom(15);
			break;
		case 1:
			gmap.setZoom(14);
			break;
		case 3:
			gmap.setZoom(12);
			break;
		default:
			gmap.setZoom(11);
	}
	return overlay;
}
function addCrossHair()
{
	if (radiusSearchCircle.crossHair != null)
		gmap.removeOverlay(radiusSearchCircle.crossHair);
	var topLat = radiusSearchCircle.points[0].lat();
	var botLat = radiusSearchCircle.points[12].lat();
	var leftLng = radiusSearchCircle.points[6].lng();
	var rightLng = radiusSearchCircle.points[18].lng();	
	var centerLat = (topLat + botLat)/2;
	var centerLng = (leftLng + rightLng)/2; 
	
	topLat = (centerLat + topLat)/2;
	botLat = (centerLat + botLat)/2;
	rightLng = (centerLng + rightLng)/2;
	leftLng = (centerLng + leftLng)/2;
	
	var topPoint = new GLatLng((centerLat + topLat)/2, centerLng);
	var bottomPoint = new GLatLng((centerLat + botLat)/2, centerLng);
	var leftPoint = new GLatLng(centerLat, (centerLng + leftLng)/2);	
	var rightPoint = new GLatLng(centerLat, (centerLng + rightLng)/2);
	var centerPoint = new GLatLng(centerLat, centerLng);
	var points = [];
	
	points.push(topPoint);
	points.push(centerPoint);
	points.push(bottomPoint);
	points.push(centerPoint);
	points.push(leftPoint);
	points.push(centerPoint);
	points.push(rightPoint);
	points.push(centerPoint);
	points.push(topPoint);
	
	var color = "#000000";
	var width = 1;
	var opacity = 0.8;
	var crossHair = new GPolyline(points, color, width, opacity);
	radiusSearchCircle.crossHair = crossHair;
	radiusSearchCircle.crossHairPoints = points;
	GEvent.addListener(radiusSearchCircle.crossHair, "click", openCircleInfoWindow);
	gmap.addOverlay(crossHair);
}
function initMoveSearchRadius()
{
	clearSearch();
	isMoveCircle = true;
	removeInfoWindow();
	for (var i=1; i < circleOverlays.length; i++)
    	gmap.removeOverlay(circleOverlays[i]);
   	
   	var smallestSize = 15;
   	for (var i=0; i < circleListDB.length; i++)
  		if (circleListDB[i].size < smallestSize)
  			smallestSize = circleListDB[i].size;
    radiusSearchCircle.radius = smallestSize;
	gmap.getDragObject().setDraggableCursor("crosshair");
}

function moveSearchRadius(point)
{
	if (isMoveCircle)
	{
		if (radiusSearchCircle.overlay != null)
		 	gmap.removeOverlay(radiusSearchCircle.overlay);

		var points = [];
		var color = radiusColorKey[radiusSearchCircle.radius];
		var width = 3;  
		for (var i=0; i < radiusSearchCircle.points.length; i++) 
		{ 
			var newLat = radiusSearchCircle.points[i].lat() + (point.lat() - radiusSearchCircle.polyClickPoint.lat());
			var newLng = radiusSearchCircle.points[i].lng() + (point.lng() - radiusSearchCircle.polyClickPoint.lng());
			points.push(new GLatLng(newLat, newLng));
		}
		radiusSearchCircle.overlay = new GPolygon(points, color, width, 0.8, color, 0.6);
		GEvent.addListener(radiusSearchCircle.overlay, "click",  placeSearchRadius);
		gmap.addOverlay(radiusSearchCircle.overlay);
		moveCrossHair(point);
	}
}
function moveCrossHair(point)
{
	 if (radiusSearchCircle.crossHair != null)
		 	gmap.removeOverlay(radiusSearchCircle.crossHair);
		 	
	var points = [];
	for (var i=0; i < radiusSearchCircle.crossHairPoints.length; i++)
	{
		var newLat = radiusSearchCircle.crossHairPoints[i].lat() + (point.lat() - radiusSearchCircle.polyClickPoint.lat());
		var newLng = radiusSearchCircle.crossHairPoints[i].lng() + (point.lng() - radiusSearchCircle.polyClickPoint.lng());
		points.push(new GLatLng(newLat, newLng));
	}
	var color = "#000000";
	var width = 1;
	var opacity = 0.8;
	var crossHair = new GPolyline(points, color, width, opacity);
	radiusSearchCircle.crossHair = crossHair;
	GEvent.addListener(radiusSearchCircle.crossHair, "click",  placeSearchRadius);
	gmap.addOverlay(crossHair);
}
function placeSearchRadius(point)
{	
	if (radiusSearchCircle.overlay != null)
		gmap.removeOverlay(radiusSearchCircle.overlay);
	var color = radiusColorKey[radiusSearchCircle.radius];
	var width = 3;  
	var points = [];
	isMoveCircle = false;
	for (var i=0; i < 25; i++) 
	{ 
		var newLat = radiusSearchCircle.points[i].lat() + (point.lat() - radiusSearchCircle.polyClickPoint.lat());
		var newLng = radiusSearchCircle.points[i].lng() + (point.lng() - radiusSearchCircle.polyClickPoint.lng());
		points.push(new GLatLng(newLat, newLng));
	}
	radiusSearchCircle.points  = points;
	radiusSearchCircle.overlay = new GPolygon(points, color, width, 0.5, color, 0.3);
	GEvent.addListener(radiusSearchCircle.overlay, "click", openCircleInfoWindow);
	gmap.addOverlay(radiusSearchCircle.overlay);
	circleOverlays = [radiusSearchCircle.overlay];
	radiusSearchCircle.polyClickPoint = point;
	initRadialSearch(radiusSearchCircle.radius, point, true);
	addCrossHair();
	gmap.getDragObject().setDraggableCursor("url(http://maps.google.com/intl/en_us/mapfiles/openhand.cur),default");
}
function showCircleHint(center)
{
	if (circleHint)
		removeCirclehint();
	var marker = new GIcon();
	marker.image = "images/click.png";
	marker.iconSize = new GSize(200, 50);
	marker.iconAnchor = new GPoint(100, 100);
	circleHint = new GMarker(center,{icon: marker});
	GEvent.addListener(circleHint, "click", openCircleInfoWindow);
	gmap.addOverlay(circleHint);
}
function removeCircleHint()
{
	if (circleHint)
		gmap.removeOverlay(circleHint);
	circleHint = null;
}
//=====================================================================
//                             Info Window
//=====================================================================
function handleWindowOpen()
{
	if (!isCircleInfoWindow)
	{
		var infoWindow = gmap.getInfoWindow();
		infoWindow.supressMapPan = infoWindowOptions.suppressMapPan;
		if (selectedJurisdiction.type == "Parish")
		{
			if (selectedJurisdiction.infoWindow.html == null || selectedJurisdiction.infoWindow.html[0].name == "Loading")
				getParishInfo(selectedJurisdiction.mainSguid);
			else
			{
				var tab = infoWindow.getTabs();
				tab[0] = new google.maps.InfoWindowTab("Info", infoWindowHtml[0].contentElem.innerHTML);
				
				if (infoWindowHtml.length > 1)
				{
					tab[1] = new google.maps.InfoWindowTab("Jurisdictions", selectedJurisdiction.infoWindow.html[1].contentElem.innerHTML);
					tab[2] = new google.maps.InfoWindowTab("Options", selectedJurisdiction.infoWindow.html[2].contentElem.innerHTML);
				}							
				gmap.updateInfoWindow(tab);
			}	
		}
	}
	else
		isCircleInfoWindow = false;
}
function openInfoWindow(center)
{
	if (selectedJurisdiction.infoWindow.html && selectedJurisdiction.polygon)
	{
		if (!center)
		{
			if (selectedJurisdiction.polygon[0].getBounds)
			{
				var bounds = selectedJurisdiction.polygon[0].getBounds();
				center = bounds.getCenter();
			}
		}
		gmap.openInfoWindowTabs(center, selectedJurisdiction.infoWindow.html, infoWindowOptions);
	}
}
function openCircleInfoWindow(clickPoint)
{
	var html;      
	isCircleInfoWindow = true;
    html = "<div><div> What would you like to do?</div>";
    html += "<div class='infoWindowAction'><span onclick='initMoveSearchRadius()'>Move radius</span><br />";
    html += "<span onclick='doIdentify(null, "+clickPoint.y+", "+clickPoint.x+")'>Search here</span><br />";
 	html += "Add another radius search of <select name='ddlRadius' onChange='initRadialSearch(this, \"add\", false)'>";
   	html += "<option value=null>";
   	if (isMiles)
   		html += "miles";
	else
		html += "km";
	html += "</option>";
    html += "<option value=0.25>1/4</option>";
    html += "<option value=0.5>1/2</option>";
    html += "<option value=1>1</option>";
    html += "<option value=3>3</option>";
    html += "<option value=5>5</option>";
    html += "<option value=10>10</option>";
    html += "<option value=15>15</option>";
    html += "</select><br /> ";
    html += "<span onclick='removeRadiusPolygon()'>Remove radius search</span><br /></div></div>";

	gmap.openInfoWindow(clickPoint, html);
}
function genInfoWindowData()
{
	if (selectedJurisdiction.type)
	{
		if (selectedJurisdiction.type == "Parish")
			return parishInfoWindow();
		else
			return jurisInfoWindow();
	}
}
function jurisInfoWindow()
{    
    var html;  
    var label = selectedJurisdiction.label;
    var sguid = selectedJurisdiction.mainSguid;
    var type = getDisplayName(selectedJurisdiction.type);
    var pluralNameOfType = getPluralOfJurisdiction(type);
    var attributes = getJurisActionAttributes(type);
    html = "<table";
    html += "<tr><td>"+type+": </td>";
    html += "<td>"+label+"</td></tr>";
    html += "</table><br />";
    
    if (/*label.indexOf("shire") != -1 &&*/ type == "County")
    	type = "";
    html += "<div><div> What would you like to do?</div>";
    html += "<div class='infoWindowAction'>";
    if (attributes.listParishes)
    	html += "<span onclick='retrieveParishes(\""+sguid+"\")'>List all parishes in "+label+" "+type+"</span><br />";
    if (attributes.counties)
    	html += "<span onclick='getChildren(\"County\")'>List all counties in "+label+" "+type+"</span><br />";
    if (attributes.diocese)
    	html += "<span onclick='getChildren(\"Diocese\")'>List all diocese in "+label+" "+type+"</span><br />";
    if (attributes.ruralDeaneries)
    	html += "<span onclick='getChildren(\"Rural Deanery\")'>List all rural deaneries in "+label+" "+type+"</span><br />";
    if (attributes.probateCourts)
    	html += "<span onclick='getChildren(\"Probate Court\")'>List all probate courts in "+label+" "+type+"</span><br />";
    if (attributes.contiguousSearch)
    	html += "<span onclick='initContiguousSearch()'>List all "+pluralNameOfType.toLowerCase()+" contiguous to "+label+"</span><br />";
    if (attributes.catalogLink)
    	html += "<span onclick='famHistCatalogLink(\""+label+"\")'>Search the Family History Library Catalog</span><br />";
    if (attributes.wikiLink)
    	html += "<span onclick='famHistWikiLink(\""+label+"\")'>Search the FamilySearch Research Wiki</span><br />";
    html += "<span onclick='removePolygon()'>Remove selection</span><br /></div></div>";
    var tab1 = new google.maps.InfoWindowTab("Basic", html);
    html = [tab1];
    return html;
}
function getJurisActionAttributes(type)
{
	var attributes = { listParishes: true,
					   contiguousSearch: true,
					   catalogLink:  true,
					   wikiLink: true,
					   counties: false,
					   diocese: false,
					   ruralDeaneries: false,
					   probateCourts: false};
	if (type == "Province")
	{
		attributes.listParishes = false;
		attributes.contiguousSearch = false;
		attributes.catalogLink = false;
		attributes.counties = true;
		attributes.diocese = true;
	}
	else if (type == "Diocese")
	{
		attributes.counties = true;
		attributes.ruralDeaneries = true;
		attributes.probateCourts = true;
	}
	
	return attributes;
}
function parishInfoWindow()
{
    var html = "<div id='parishWindow'>";
    html += "<center>Loading contents<br />";
    html += "<img src='images/ajax-loader.gif'></center>";
    html += "</div>"; 	
    var tab1 = new google.maps.InfoWindowTab("Loading", html);
    html = [tab1];
	return html;
}
function getParishInfo(sguid)
{
	if (sguid == "Null")
	{
		var infoWindow = gmap.getInfoWindow();
		var tab = infoWindow.getTabs();
		var tab0 = new google.maps.InfoWindowTab("Info", "No Information available.");
		
		tab[0] = tab0;
		gmap.updateInfoWindow(tab);
	}
	else
		$.getJSON('api-parish-info.action', {
			sGuid: sguid
			},formatParishInfo);
}
function formatParishJurisInfo(result)
{
    var html;  
    html = "<div class='infoWindowDiv'><table class='infoWindowTbl' > ";
	html += "<tr><td><b>Place:</b> </td><td>"+result.jurisGuid.JURISDICTION+"</td></tr>";
	
	result = result.parentResult.results;
	var tableOrder = ["County", "Civil Registration District", "Probate Court", "Diocese", "Rural Deanery", "Poor Law Union", "Hundred", "Province"];
	for (var i=0; i < tableOrder.length; i++)
	{
		html +="<tr><td style='width: 50%'><b>"+tableOrder[i]+":</b> </td><td>";
		for (var j=0; j < result.length; j++)
			if (result[j].layer == tableOrder[i])
			{
				if (tableOrder[i] == "Probate Court")
				{
					html += "<span>";
					var names = "";
					for (var k=0; k < result[j].jurisdictions[0].names.length; k++)
					{
						if (k > 0)
							names += ", ";
						names += result[j].jurisdictions[0].names[k];
					}
					html += names + "</span>";
				}
				else
				{
					var nameArray = [];
					for (var k=0; k < result[j].jurisdictions.length; k++)
					{
						var name = result[j].jurisdictions[k].names[0];
						if (result[j].jurisdictions[k].names.length <= 1 && !isInArray(nameArray, name))
						{							
							if (nameArray.length > 0)
							{
								html += ", ";
							}
							nameArray.push(name);
							html += "<span class='jurisSelect' onclick='selectPossibleSplitByList(\""+tableOrder[i]+"\"";
							html += ", \""+result[j].jurisdictions[k].SGUID+"\"";
//							html += ", null";
							html += ", \""+name+"\")'>";
							html += name+"</span>";
						}
					}
					/*html += "<span class='jurisSelect' onclick='selectByList(\""+tableOrder[i]+"\"";
					html += ", \""+result[j].jurisdictions[0].SGUID+"\"";
					html += ", null";
					var namesDisplay = "";
					var names = "";
					for (var k=0; k < result[j].jurisdictions[0].names.length; k++)
					{
						if (k > 0)
						{
							namesDisplay += ", ";
							names += ",<br>";
						}
						namesDisplay += result[j].jurisdictions[0].names[k];
						names += result[j].jurisdictions[0].names[k];
					}
					html += ", \""+names+"\")'>";
					html += namesDisplay + "</span>";*/
				}				
			}
		html += "</td></tr>";
	}
	
	PrintObject.jurisInfo = html;
    var tab2 = new google.maps.InfoWindowTab("Jurisdictions", html);
	var infoWindow = gmap.getInfoWindow();
	var tab = infoWindow.getTabs();
	tab[1] = tab2;
	if (infoWindowHtml)
	{
		gmap.updateInfoWindow(tab);
		infoWindowHtml[1] = tab2;
	}
}
function formatParishInfo(result)
{
	var html;   
    result = result.result.results[0].jurisdictions[0];
    var parish = result.label;
    var placeType = result.placeType;
    var sguid = result.SGUID;
    var place = result.place;
    
    if (place &&  place.indexOf("[") != -1)
    {
    	parish = place;
    	result.label = place;
    }
    else if (place && place.indexOf("(") != -1)
    {
    	parish += " " + place.substring(place.indexOf("("), place.indexOf(")")+1);
    	result.label = parish;
    }
    html = "<div class='infoWindowDiv'><div> What would you like to do?</div><br />";
    html += "<div class='infoWindowAction'>";
    html += "<span onclick='initContiguousSearch()'>List contiguous parishes</span><br />";
    html += "<label id='radiusSearchOptions'><span onclick='showRadiusSearchOptions()'>Radius search</span><br /></label>";
    html += "<span onclick='famHistCatalogLink(\""+parish+"\")'>Search the Family History Library Catalog</span><br />";
    html += "<span onclick='famHistWikiLink(\""+parish+"\")'>Search the FamilySearch Research Wiki</span><br />";
    html += "<span onclick='removePolygon()'>Remove selection </span><br /></div></div>";
	var tab3 = new google.maps.InfoWindowTab("Options", html);
    
    
    html = "<center>Loading contents<br />";
    html += "<img src='images/ajax-loader.gif'></center>";
    PrintObject.jurisInfo = html;
    var tab2 = new google.maps.InfoWindowTab("Jurisdictions", html);
   
    html = "<div class='infoWindowDiv'>";
    if (placeType == "Ancient Parish")
   		html += genAncientParishInfo(result);
	else if (placeType == "Ecclesiastical Parish")
		html += genEcclesiasticalParishInfo(result);
	else if (placeType == "Chapelry")
		html += genChapelryInfo(result);
	else if (placeType == "Cathedral")
		html += genCathedralInfo(result);
	else if (placeType == "Common Land")
		html += genCommonLandInfo(result);
	else if (placeType == "Extra-parochial")
		html += genExtraParochialInfo(result);
	else 
		html += "This parish has no retrievable data.";
   	html +="</div>";
   	PrintObject.bubbleInfo = html;
    var tab1 = new google.maps.InfoWindowTab("Info", html);
    
    html = [tab1, tab2, tab3];
	infoWindowHtml = html;
	var infoWindow = gmap.getInfoWindow();
	var tab = infoWindow.getTabs();
	tab[0] = tab1;
	tab[1] = tab2;
	tab[2] = tab3;
	//gmap.updateInfoWindow(tab);
	
	$.getJSON('api-jurisdiction-parent.action', {
		sGuid: sguid
		},formatParishJurisInfo);	
}
function showRadiusSearchOptions()
{
	var html = "Radius search of <select name='ddlRadius'>";
    html += "<option value=0.25>1/4</option>";
    html += "<option value=0.5>1/2</option>";
    html += "<option value=1>1</option>";
    html += "<option value=3>3</option>";
    html += "<option value=5 selected='selected'>5</option>";
    html += "<option value=10>10</option>";
    html += "<option value=15>15</option>";
    html += "</select>";
    html += "<select name='ddlMiles' onChange='toggleMiles(this)' style='margin-left: 3px'>";
    if (isMiles)
    {
	    html += "<option value='miles' selected='selected'>miles</option>";
	    html += "<option value='km'>km</option>";
    }
    else
    {
    	html += "<option value='miles'>miles</option>";
    	html += "<option value='km' selected='selected'>km</option>";
    }
    html += "</select>";
    html += "<input type='button' style='margin-left: 2px' value='Go' onClick='initRadialSearch(this, false, true)'>";
    html += "<br />";
    	
    document.getElementById("radiusSearchOptions").innerHTML = html;
}
function genAncientParishInfo(result, isEcclesiastical)
{
	var parish = result.label;
    var placeType = result.placeType;
    var marketTown = result.marketTown;
    var county = result.county;
    var associatedChapelries = result.associatedChapelries;
    var notes = result.note;    
    
    var html =  "<span class='keyInfo'>"+parish+"</span> is an <span class='keyInfo'>"+placeType+"</span> ";
	if (marketTown != null)
		html +="and a <span class='keyInfo'>market town</span> ";
    html +="in the county of <span class='keyInfo'>"+county+"</span>";    
    if (isEcclesiastical && notes != null)
    {
		notes = lowercaseWord(wordsToLower, notes); 
    	html += ", "+notes;
   	}    	
   	html += ". ";
    if (associatedChapelries.length == 0)
		html += "As of 1851, "+ parish+" had <span class='keyInfo'>no chapelries</span>. "; 
	else if (associatedChapelries.length == 1) //only one 
	{
		var onClick = " onclick='selectByList(" +
		  "\"Parish\", " +
		  "\"" + associatedChapelries[0].sguid + "\", " +
		  "0, " +
		  "\"" + associatedChapelries[0].name + "\", " + 
		  "null, null, " + 
		  "\"" + result.SGUID + "\")'";
		html += "<span class='keyInfoLink'"+onClick+">"+associatedChapelries[0].name+"</span> is a chapelry of "+parish+". ";
	}
	else
	{
		for (var i=0; i < associatedChapelries.length; i++)
		{
			var onClick = " onclick='selectByList(" +
						  "\"Parish\", " +
						  "\"" + associatedChapelries[i].sguid + "\", " +
						  "0, " +
						  "\"" + associatedChapelries[i].name + "\", " + 
						  "null, null, " + 
						  "\"" + result.SGUID + "\")'";
			html += "<span class='keyInfoLink'"+onClick+">"+associatedChapelries[i].name+"</span>";
			if (i == associatedChapelries.length-1)
				html += " are chapelries of "+parish+". ";
			else if (i == associatedChapelries.length -2) //second to last
			{
				if (associatedChapelries.length == 2)
					html += " and ";
				else
					html += ", and ";
			}
			else
				html += ", "; 
		}
	}   	 
    
    html += genCommonEndInfo(result);
   
    return html;
}
function genEcclesiasticalParishInfo(result)
{       
    var html = "";
    html += genAncientParishInfo(result, true);       
   
    return html;
}
function genChapelryInfo(result)
{
	var html = formatNotes(result, true);   
    html += genCommonEndInfo(result);
   
    return html;
}
function formatNotes(result, isChapelry)
{
    var parish = result.label;
    var county = result.county;
    var notes = result.note;
    notes = lowercaseWord(wordsToLower, notes); 
    
    var notes1 = notes;
    var notes2 = null;
    var notes3 = "";
	if (notes.indexOf(";") != -1)
	{
		var split = notes.split(";");
		for (var j=0; j < split.length; j++)
		{
			if (split[j].indexOf("partly") != -1)
				notes2 = split[j];
			else if (split[j].indexOf("chapelry") != -1)
				notes1 = split[j];
			else 
				notes3 += split[j];
		}
		
	}
    var html = "";
    if (notes1 != null) 
    {
    	html += "<span class='keyInfo'>"+parish+"</span> is a ";
    	if (isChapelry)
    	{
    		var isLastLink = false;
    		html += notes.substring(0, notes.indexOf(" of ")+4);
    		notes = notes.substring(notes.indexOf(" of ")+4); 
    		while (!isLastLink)
    		{
    			
        		var link;
        		if (notes.indexOf("Ancient") != -1)
        		{
        			link = notes.substring(0, notes.indexOf(" Ancient"));
        			notes = notes.substring(notes.indexOf("Ancient"));
        		}
        		else if (notes.indexOf("Ecclesiastical") != -1)
        		{
        			link = notes.substring(0, notes.indexOf(" Ecclesiastical"));
        			notes = notes.substring(notes.indexOf("Ecclesiastical"));
        		}
        		else
        		{
        			link = notes;
        		}        		
        		var parentSguid = "";
        		for (var i=0; i < result.ac_parent_sguid.length; i++)
        			if (link == result.ac_parent_sguid[i].name)
        				parentSguid = result.ac_parent_sguid[i].sguid;
        		var onClick = " onclick='selectByList(" +
    			  "\"Parish\", " +
    			  "\"" + parentSguid + "\", " +
    			  "0, " + 
    			  "\""+link+"\"" +
    			  ", null, null, " + 
    			  "\""+result.SGUID+"\")'";
        		html += "<span class='keyInfoLink'"+onClick+">";
        		html += link + "</span> ";
        		       		
        		if (notes.indexOf(",")!=-1 || notes.indexOf(" and ") !=-1)
        		{
        			if ((notes.indexOf(", and ") != -1 && notes.indexOf(",") >= notes.indexOf(", and "))
        				|| (notes.indexOf(" and ") != -1 && notes.indexOf(",") == -1))
        			{
        				html += notes.substring(0, notes.indexOf(" and ")+5);
        				notes = notes.substring(notes.indexOf(" and ")+5);
        			}
        			else
        			{
        				html += notes.substring(0, notes.indexOf(",")+2);
        				notes = notes.substring(notes.indexOf(",")+2);
        			}
        		}
        		else 
        		{
        			html += notes + " ";
        			isLastLink = true;
        		}        		
    		}    		
    	}
    	else
    		html += notes1+" ";
	    html += "in <span class='keyInfo'>"+county+"</span>. ";
    }
   	if (notes2 != null)
   	{
   		var span =	html.length > 0 ?  "" :  "class='keyInfo'";
   		html += "<span "+span+">"+parish + " is " + notes2 + ". ";   		
   	}
   	if (html.length == 0)
   	{
   		 html += "<span class='keyInfo'>"+parish+"</span> is ";
   		 html += "in <span class='keyInfo'>"+county+"</span>. ";
   	}
   	if (notes3.length > 0)
   		html += notes3 + ". ";   
   		
	return html; 
}
function genCommonLandInfo(result)
{
	var placeName = result.place;
	
	var html = placeName + ". For more information see "+placename.substring(14); //14 for 'Land Common To'
	
	return html;
}
function genExtraParochialInfo(result)
{
	var parish = result.label;
	
	var html = "<span class='keyInfo'>"+parish+"</span> is ";
	html += "an extra-parochial place. Search surrounding parishes for records and information. ";
	
	return html;
}
function genCathedralInfo(result)
{
	var parish = result.label;
    var county = result.county;
    
    var html = "<span class='keyInfo'>"+parish+"</span> is ";
   	html += "in <span class='keyInfo'>"+county+"</span> county. ";
    html += genCommonEndInfo(result);
   
    return html;
}
function genCommonEndInfo(result)
{
	var parish = result.label; 
    var placeType = result.placeType;
    var nonCEDenominations = result.nonCEDenom;
    var otherPlaces = result.otherPlaces;
    var CEParishRegisters = result.ceParishRegBegin;
    var CEParishRegistersNotes = result.ceParishRegBeginNotes;
    var CEBishopTranscripts = result.ceBishopTransBegin;
    var CEBishopTranscriptsNotes = result.ceBishopTransBeginNotes;
   var html = "<br /><br />";

		
	if (otherPlaces.length > 0)
	{
		if (otherPlaces.length == 1)
			html += otherPlaces[0].name+" is a hamlet or village in the parish. ";
		else 
		{
			for  (var i=0; i < otherPlaces.length; i++)
			{
				if (i == 0)
					html += "Other places in the parish include: ";
					
				html += otherPlaces[i].name;
				if (i == otherPlaces.length-1)
					html += ". ";
					//html += " are other places in the parish. ";
				else if (i == otherPlaces.length -2) //second to last
				{
					if (otherPlaces.length == 2)
						html += " and ";
					else
						html += ", and ";
				}
				else
					html += ", "; 
			}
		}
		html += "<br /><br />";
	}
	
	if (CEParishRegisters != null && CEParishRegisters != "None")
		html += "Church of England parish registers begin in <span class='keyInfo'>"+CEParishRegisters+"</span>. ";
	else
		html += "There are no parish registers for "+parish+". ";
	
	if (CEParishRegistersNotes != null)
		html += CEParishRegistersNotes + ". ";
	
	if (CEBishopTranscripts != null && CEBishopTranscripts != "None")
		html += "Church of England Bishops' transcripts exist from <span class='keyInfo'>"+CEBishopTranscripts+"</span>.";
	else
		html += "There are no Bishops' transcripts for "+parish+". ";
	
	if (CEBishopTranscriptsNotes != null)
		html += CEBishopTranscriptsNotes + ". ";
		
	//if (CEParishRegisters != null || CEBishopTranscripts != null) //we need an extra space
	html += "<br /><br />";
				
	if (nonCEDenominations.length == 0)
		html += "Non-Church of England denominations have not yet been added for "+parish+".";
	else if (nonCEDenominations[0].name == "None Identified")
		html += "No non-Church of England denominations have been identified for "+parish+". ";
	else if (nonCEDenominations.length == 1) //only one 
	{
		html += nonCEDenominations[0].name+" is the only identified ";
		html += "non-Church of England denomination in "+parish+". ";
	}
	else
	{
		html +="Non-Church of England denominations identified in "+parish+" include: ";
		for (var i=0; i < nonCEDenominations.length; i++)
		{
			html += nonCEDenominations[i].name;
			if (i == nonCEDenominations.length-1)
				html += ". ";
			else if (i == nonCEDenominations.length -2) //second to last
			{
				if (nonCEDenominations.length == 2)
					html += " and ";
				else
					html += ", and ";
			}
			else
				html += ", "; 
		}
	}
	
	return html;
}