var g_bPlaceShape = false;
var pickedPoint = new Array(); //records the spatial query polygon, or measuring area polygon
var pickedPointNum = 0;
var g_nMaxPoints = 100;

var g_strPhotoOpacity = "0.6";
var g_strGeotiffOpacity = "0.6";

var g_aryCities = new Array();
var g_aryWindows = new Array();

// menu options
var MENU_DOC_SEARCH = 0;
var MENU_PARCEL_SEARCH = 1;
var MENU_GEODETIC_VIEW = 2;
var MENU_GET_VIEW = 3;
var MENU_MEASURE = 4;
var MENU_MAP_ARCHIVE = 5;
var MENU_HELP = 6;
var MENU_SHOPPING_CART = 7;
var MENU_DOWNLOAD = 8;
var MENU_ECOMMERCE = 9;
var MENU_HBP_PARCELS = 10;
var MENU_ROWE_PARCELDOC = 11;

// spatial search shapes
var RectangleShape = null;
var CircleShape = null;
var CircleRadius = 0;
var PolygonShape = null;
var bPolygonShapeBegin = true;
var closeShapeCircle = null;
var PolygonShapeSX;
var PolygonShapeSY;

// measuring
var shpMeaLine = null;
var aryMeaPointObjs = new Array();		// store measuring point objects
var g_nLastMeaPointX = 0;
var g_nLastMeaPointY = 0;
var g_nMeaPointCount = 0;
var g_nLastTotalDistance = 0;
var g_nTotalDistance = 0;
var g_nLastTotalArea = 0;
var g_nTotalArea = 0;
var g_sLastAreaUnit = "ft<sup>2</sup>";

var g_strGeodeticIsVertical = 'horizontal'; // geodetic view
var strDrawGeodetic = 0;
var	strGeodeticX = 0;
var	strGeodeticY = 0;

var g_strGeometryType = 'rectangle';
var g_strSpatialMode = 'o';		// overlap

var g_strImageID = null;
var g_aryOrigin = new Array();
var g_aryMultiplier = new Array();
var g_arySize = new Array();
var g_strTiffTemplate = "TiffClip";

var g_strHiliteParcelID = null;
var g_strParcelLayerID = "13030";
var g_strROWEParcelLayerID = "";

var g_selectedPhotoId = "countyap1";

function escapeSpecialChar4SQLServer(strValue)
{
	strResult = strValue;
	strResult = strResult.replace(/\\/g, "\\\\");
	strResult = strResult.replace(/%/g, "\\%");
	strResult = strResult.replace(/_/g, "\\_");
	strResult = strResult.replace(/\[/g, "\\[");
	strResult = strResult.replace(/\]/g, "\\]");
	return strResult;
}

function ParcelSearch(form)
{
	var strLastName = form.txtLastName ? form.txtLastName.value : "";
	var strAPN = form.txtAPN ? form.txtAPN.value : "";
	var strAddressNo = form.txtAddress ? form.txtAddressNo.value : "";
	var strAddress = form.txtAddress ? form.txtAddress.value : "";
	var strCity = form.txtCity ? form.txtCity.value : "";
	var strZip = form.txtZip ? form.txtZip.value : "";
	
	if (!(strLastName || strAPN || strAddressNo || strAddress || strCity || strZip))
	{
		alert("Please input search value.");
		return;
	}

	var queryStr = "";
	queryStr += "txtLastName=" + escape(strLastName);
	queryStr += "&txtAPN=" + escape(strAPN);
	queryStr += "&txtAddressNo=" + escape(strAddressNo);
	queryStr += "&txtAddress=" + escape(strAddress);
	queryStr += "&txtCity=" + escape(strCity);
	queryStr += "&txtZip=" + escape(strZip);
	
	top.openSearchingWin();
	
		var win = window.open("ParcelSearch.aspx?" + queryStr, "ParcelSearch", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function ClearParcelSearchFields(form)
{
	if (form.txtFirstName) form.txtFirstName.value = "";
	if (form.txtLastName) form.txtLastName.value = "";
	if (form.txtAPN) form.txtAPN.value = "";
	if (form.txtAddressNo) form.txtAddressNo.value = "";
	if (form.txtAddress) form.txtAddress.value = "";
	if (form.txtCity) form.txtCity.value = "";
	if (form.txtZip) form.txtZip.value = "";
}

function HBPParcelSearch(form)
{
	var strParkName = form.txtParkName ? form.txtParkName.value : "";
	var strFacilityNum = form.txtFacilityNum ? form.txtFacilityNum.value : "";
	var strParcelNum = form.txtParcelNum ? form.txtParcelNum.value : "";
	
	if (!(strParkName || strFacilityNum || strParcelNum))
	{
		alert("Please input search value.");
		return;
	}

	var queryStr = "";
	queryStr += "txtParkName=" + escape(strParkName);
	queryStr += "&txtFacilityNum=" + escape(strFacilityNum);
	queryStr += "&txtParcelNum=" + escape(strParcelNum);
	
	top.openSearchingWin();
	
	var win = window.open("HBPParcelSearch.aspx?" + queryStr, "HBPParcelSearch", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function ClearHBPParcelSearchFields(form)
{
	if (form.txtParkName) form.txtParkName.value = "";
	if (form.txtFacilityNum) form.txtFacilityNum.value = "";
	if (form.txtParcelNum) form.txtParcelNum.value = "";
}

function HBPParcelDetail(strMSLINK)
{
	var queryStr = "";
	queryStr += "fentnum=" + 10000236;
	queryStr += "&fmslink=" + strMSLINK;
	top.openSearchingWin();
	
	var win = window.open("ShowAttribute.aspx?" + queryStr, "HBPParcelDetail", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function HBPReport0(form)
{
	var strStartDate = form.txtStartDate ? form.txtStartDate.value : "";
	var strEndDate = form.txtEndDate ? form.txtEndDate.value : "";
	
	var queryStr = "";
	queryStr += "txtStartDate=" + escape(strStartDate);
	queryStr += "&txtEndDate=" + escape(strEndDate);

	top.openSearchingWin();
	
	var win = window.open("HBPReport0.aspx?" + queryStr, "HBPReport0", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function SelectDocType()
{
	var win;
	if (top.g_bIntranet)
		win = window.open("SelectDocType.htm", "SelectDocType", "width=300,height=630,scrollbars=1,resizable=1");
	else
		win = window.open("SelectDocType.htm", "SelectDocType", "width=320,height=610,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function SelectROWEParcelType()
{
	var win;
	if (top.g_bIntranet)
		win = window.open("SelectROWEParcelType.htm", "SelectROWEParcelType", "width=300,height=630,scrollbars=1,resizable=1");
	else
		win = window.open("SelectROWEParcelType.htm", "SelectROWEParcelType", "width=320,height=610,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function UpdateDocTypeArray(aryFlags)
{
	for (var i = 0; i < gaDocType.length; i++)
		gaDocType[i].bSelected = aryFlags[i];
}

function UpdateParcelTypeArray(aryFlags)
{
	for (var i = 0; i < gaParcelType.length; i++)
		gaParcelType[i].bSelected = aryFlags[i];
}

function prepareShape(oSVGRoot)
{
	// reset it each time the map is refreshed because we don't keep the old shape
	pickedPointNum = 0;

	var shapeLayer = oSVGRoot.getElementById(SEARCH_SHAPE_ID);
	RectangleShape = _newRectangle( oSVGRoot,0, 0 ,"1","1","red","red",0.5);
	RectangleShape.getStyle().setProperty("display", "none");
	shapeLayer.appendChild(RectangleShape);
	
	CircleShape = _newcircle( oSVGRoot,0, 0 ,"1","red","red",0.5);
	CircleShape.getStyle().setProperty("display", "none");
	shapeLayer.appendChild(CircleShape);
	
	PolygonShape = _newpath(oSVGRoot,0,0,0.5);
	PolygonShape.getStyle().setProperty("display", "none");
	shapeLayer.appendChild(PolygonShape);

	closeShapeCircle = _newcircle( oSVGRoot,0, 0 ,3,"black","black",1);
	closeShapeCircle.getStyle().setProperty("display", "none");
	shapeLayer.appendChild(closeShapeCircle);
	
	// measuring shape
	shpMeaLine = _newpolyline(oSVGRoot, "red", 1, "red", 0.5);
	shpMeaLine.getStyle().setProperty("display", "none");
	shapeLayer.appendChild(shpMeaLine);

	ZoomInBox = _newrect(SVGDocument, 0, 0, 0, 0, "red", 1);	
	with (ZoomInBox.getStyle())
	{	
		setProperty("stroke-dasharray", "4 2");	
		setProperty("display",   "none");
	}
	shapeLayer.appendChild(ZoomInBox);
}

function PlaceShape()
{
	if (!bFullyloaded)
	{
		alert("Map is still loading. Please try later.");
		return;
	}
	
	DeleteShape();
	g_bPlaceShape = true;
}

function DeleteShape()
{
	RectangleShape.getStyle().setProperty("display", "none");
	CircleShape.getStyle().setProperty("display", "none");
	PolygonShape.getStyle().setProperty("display", "none");
	closeShapeCircle.getStyle().setProperty("display", "none");

	g_bPlaceShape = false;
	bPolygonShapeBegin = true;
	pickedPoint = new Array();
	pickedPointNum = 0;
}

function closeToEnd(MouseX,MouseY,PolygonShapeSX,PolygonShapeSY)
{
    var bReturn;
	with (Math){
	if(abs(MouseX-PolygonShapeSX)<=4 && abs(MouseY-PolygonShapeSY)<=4 )
	{
		bPolygonShapeBegin = true;
		bReturn =  true;
	}
	else
		bReturn =  false; 
	}
	return (bReturn);
}

function ViewParcel(nMSLink, nXLow, nXHigh, nYLow, nYHigh,layerid)
{
	vCenterLong = (nXLow + nXHigh) * 0.5;
	vCenterLat = (nYLow + nYHigh) * 0.5;
	var currentW = Math.abs(vMapWidth / mul_x);
	var currentH = Math.abs(vMapHeight / mul_y);
	var newW = 2 * Math.abs(nXHigh - nXLow);
	var newH = 2 * Math.abs(nYHigh - nYLow);
	var factor = Math.max(newW / currentW, newH / currentH);
	var scale = Math.round(vScale * factor);
	if (scale > g_nMaxViewParcelScale)
		scale = g_nMaxViewParcelScale;
	else if (scale < g_nMinViewParcelScale)
		scale = g_nMinViewParcelScale;
	vScale = scale;
	
	g_strHiliteParcelID = "" + nMSLink;
	if (layerid && layerid.length > 0)
	    g_strROWEParcelLayerID = layerid;  
	var vSVGImage = getSvgURL(SVG_IMAGE_URL, SVG_TEMPLATE, vMapWidth, vMapHeight, vCenterLong, vCenterLat, vScale, g_strVisibleLayers);
    
	RefreshSVG(parent.MainMapFrame.MapImageID, vSVGImage);
	window.focus();
}

function SearchDocByShape()
{
	var strDocTypes = "";
	for (var i = 0; i < gaDocType.length; i++)
	{
		if (gaDocType[i].bSelected)
		{
			if (strDocTypes == "")
				strDocTypes = gaDocType[i].DocName;
			else
				strDocTypes += ";" + gaDocType[i].DocName;
		}
	}
	
	if (strDocTypes == "")
	{
		alert("Please choose at least one document type to search.");
		SelectDocType();
		return;
	}
	
	if (pickedPoint.length < 1)
	{
		alert("Please draw a shape to search.");
		return;
	}
	
	var dblArea = 0.0;
	var nGeometryType = 0;
	var strGeoValue = "";
	var factor = 1.0;
	if (!bUseMeter)
	    factor = 0.30480061;
	if (g_strGeometryType.match(/rectangle/i) || g_strGeometryType.match(/polygon/i))
	{
		nGeometryType = 1;		// polygon
		
		for (var i = 0; i < pickedPoint.length; i++)
		{
			if (i != 0)
				strGeoValue += ";"

			strGeoValue += pickedPoint[i].x * factor + "," + pickedPoint[i].y * factor ;
		}
		
		dblArea = CalculateArea(pickedPoint) * factor  * factor ;
	}
	else if (g_strGeometryType.match(/circle/i))
	{
		nGeometryType = 2;		// circle
		//haiw090714
		//strGeoValue = pickedPoint[0].x * factor  + "," + pickedPoint[0].y * factor  + ";" + (Math.round((CircleRadius * factor  * 10)) / 10);
		strGeoValue = pickedPoint[0].x * factor  + "," + pickedPoint[0].y * factor  + ";" + (Math.round((CircleRadius * 1  * 10)) / 10); //get rid of factor fro radius
		//>>
		dblArea = Math.PI * CircleRadius * CircleRadius * factor  * factor ;
	}
	else
	{
		alert("Invalid shape type: " + g_strGeometryType);
		return;
	}

	var url = "SearchDocByShape.aspx?documentTypes=" + strDocTypes + "&geometryValue=" + strGeoValue + "&geometryType=" + nGeometryType + "&spatialOperator=" + g_strSpatialMode;

	top.openSearchingWin();

	var win = window.open(url, "SearchDocByShape", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function SearchRoweParcelByShape()
{
	var strParcelTypes = "";
	var factor = 1.0;
	if (!bUseMeter)
	    factor = 0.30480061;
	
	for (var i = 0; i < gaParcelType.length; i++)
	{
		if (gaParcelType[i].bSelected)
		{
			if (strParcelTypes == "")
				strParcelTypes = gaParcelType[i].ParcelTypeName;
			else
				strParcelTypes += ";" + gaParcelType[i].ParcelTypeName;
		}
	}
	
	if (strParcelTypes == "")
	{
		alert("Please choose at least one Parcelument type to search.");
		SelectROWEParcelType();
		return;
	}
	
	if (pickedPoint.length < 1)
	{
		alert("Please draw a shape to search.");
		return;
	}
	
	var dblArea = 0.0;
	var nGeometryType = 0;
	var strGeoValue = "";
	if (g_strGeometryType.match(/rectangle/i) || g_strGeometryType.match(/polygon/i))
	{
		nGeometryType = 1;		// polygon
		
		for (var i = 0; i < pickedPoint.length; i++)
		{
			if (i != 0)
				strGeoValue += ";"

			strGeoValue += pickedPoint[i].x*factor + "," + pickedPoint[i].y*factor;
		}
		
		dblArea = CalculateArea(pickedPoint) * factor  * factor ;
	}
	else if (g_strGeometryType.match(/circle/i))
	{
		nGeometryType = 2;		// circle
		//haiw090714
		//strGeoValue = pickedPoint[0].x*factor + "," + pickedPoint[0].y*factor + ";" + (Math.round((CircleRadius * 10*factor)) / 10);
		strGeoValue = pickedPoint[0].x*factor + "," + pickedPoint[0].y*factor + ";" + (Math.round((CircleRadius * 10 * 1)) / 10); //get rid of factor fro radius
		//>>
		dblArea = Math.PI * CircleRadius * CircleRadius * factor  * factor ;
	}
	else
	{
		alert("Invalid shape type: " + g_strGeometryType);
		return;
	}

	var url = "SearchRoweParcelByShape.aspx?ParcelTypes=" + strParcelTypes + "&geometryValue=" + strGeoValue + "&geometryType=" + nGeometryType + "&spatialOperator=" + g_strSpatialMode;

	top.openSearchingWin();

	var win = window.open(url, "SearchRoweParcelByShape", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function SelectFeatures()
{
	var url = "SelectFeatures.aspx";
	var rtnVal = showModalDialog(url,[window, g_selectedPhotoId],"dialogWidth:550px; dialogHeight:400px; scrollbars:1; center:yes; help:no; status:no");
	if(typeof(rtnVal) != "object" ) 
		rtnVal = "noupd";
	else {
		var aryFeatures = rtnVal[0];
		g_selectedPhotoId = rtnVal[1];
		for (var i = 0 ; i < gFeatureGroups.length; i++)
		{
			// ignore geoTiff
			if (gFeatureGroups[i].szName == GEO_TIFF_GROUP_NAME)
				continue;

			var bVisible = false;
			for(var a = 0; a < aryFeatures.length; a++)
			{
				if (gFeatureGroups[i].szName == aryFeatures[a])
				{
					bVisible = true;
					break;
				}
			}

			if (gFeatureGroups[i].bVisible)
			{
				if (!bVisible)
				{
					setGroupVisible(i, false);
				}
			}
			else
			{
				if (bVisible)
				{
					setGroupVisible(i, true);
				}
			}
		}

		reloadSVGMap();
	}
}

function ToggleImageDisplay()
{
	if (g_strImageID != null)
	{
		var group = FindGroupByName(GEO_TIFF_GROUP_NAME);
		if (group != null)
		{
			var image = SVGDocument.getElementById(GEO_TIFF_LAYER_ID);

			setGroupVisible(group.nGroupIndex, !group.bVisible)

			if (group.bVisible)
			{
				if (image == null || bReloadGImage)
				{
					var vSVGImage = getSvgURL(SVG_IMAGE_URL, SVG_TEMPLATE, vMapWidth, vMapHeight, vCenterLong, vCenterLat, vScale, g_strVisibleLayers);
					RefreshSVG(top.MainMapFrame.MapImageID, vSVGImage);

					// this must be set after "RefreshSVG"
					bReloadGImage = false;
				}
				else
					image.getStyle().setProperty("display", "inline");
			}
			else
			{
				if (image != null)
					image.getStyle().setProperty("display", "none");
			}

			window.focus();
		}
	}
}


function SetMeasuringTool(vTool)
{
	top.SelectMenu(MENU_MEASURE);		// measuring tool panel
	
	SetTool(vTool);
	
	switch (vTool)
	{
		case GET_COORDINATE:
			top.fraLeftFrame.document.all("lblMeaTool").innerHTML = "[Get Coordinates]";
			break;
		
		case MEASURE_DISTANCE:
			top.fraLeftFrame.document.all("lblMeaTool").innerHTML = "[Measure Distance]";
			break;
			
		case MEASURE_AREA:
			top.fraLeftFrame.document.all("lblMeaTool").innerHTML = "[Measure Area]";
			break;
		
		default:
			break;
	}
}

function ShowCoordinate(x, y)
{
	if (!bUseMeter)
	{
		x = x * FOOT_PER_METER;
		y = y * FOOT_PER_METER;
	}

	var table = top.fraLeftFrame.document.all("tblMeasuringDisplay");
	ClearTable(table);
	var row = table.insertRow();
	var cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "<B>Northing:</B>");
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(y * 10) / 10);
	row = table.insertRow();
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "<B>Easting:</B>");
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(x * 10) / 10);
}

function ClearTable(table)
{
	while (table.rows.length > 0)
		table.deleteRow(0);
}

function trim(strInput)
{
	if (strInput == null)
		return null;

	strInput = strInput.replace(/^\s*/, "");
	strInput = strInput.replace(/\s*$/, "");
	
	return strInput;
}

function GeodeticShowDetail(strControlPointN, strViewRadius)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strViewRadius != null && strViewRadius.length > 0) strViewRadius = trim(strViewRadius);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	if (strViewRadius == null || strViewRadius.length < 1)
	{
		var factor1 = 2 * mul_x / vMapWidth;
		var factor2 = 2 * mul_y / vMapHeight;
		with (Math) 
		{
			var factor = max(abs(factor1), abs(factor2));
			var nRadius = 1. / factor;
		}
		strViewRadius = nRadius.toString();
	}
	else {
		if (bUseMeter)
			strViewRadius = (strViewRadius * FOOT_PER_METER).toString();
		else
			strViewRadius = strViewRadius.toString();
	}
	
	var queryStr = "txtControlPointN=" + escape(strControlPointN);
	queryStr += "&txtViewRadius=" + escape(strViewRadius);
	queryStr += "&txtIsVertical=" + escape(g_strGeodeticIsVertical);
	queryStr += "&method=0";
	queryStr += "&nMaxRecords=" + escape(top.g_nMaxSearchByTypeRecords);
	top.openSearchingWin();
	var win = window.open("GeodeticView.aspx?" + queryStr, "GeodeticControl", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function GeodeticShowPhoto(strControlPointN, strViewRadius)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strViewRadius != null && strViewRadius.length > 0) strViewRadius = trim(strViewRadius);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	if (strViewRadius == null || strViewRadius.length < 1)
	{
		var factor1 = 2 * mul_x / vMapWidth;
		var factor2 = 2 * mul_y / vMapHeight;
		with (Math) 
		{
			var factor = max(abs(factor1), abs(factor2));
			var nRadius = 1. / factor;
		}
		strViewRadius = nRadius.toString();
	}
	else {
		if (bUseMeter)
			strViewRadius = (strViewRadius * FOOT_PER_METER).toString();
		else
			strViewRadius = strViewRadius.toString();
	}
	
	var queryStr = "txtControlPointN=" + escape(strControlPointN);
	queryStr += "&txtViewRadius=" + escape(strViewRadius);
	queryStr += "&txtIsVertical=" + escape(g_strGeodeticIsVertical);
	queryStr += "&method=1";
	queryStr += "&nMaxRecords=" + escape(top.g_nMaxSearchByTypeRecords);
	top.openSearchingWin();
	var win = window.open("GeodeticView.aspx?" + queryStr, "GeodeticControl", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function GeodeticShowMonumentRecord(strControlPointN, strViewRadius)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strViewRadius != null && strViewRadius.length > 0) strViewRadius = trim(strViewRadius);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	if (strViewRadius == null || strViewRadius.length < 1)
	{
		var factor1 = 2 * mul_x / vMapWidth;
		var factor2 = 2 * mul_y / vMapHeight;
		with (Math) 
		{
			var factor = max(abs(factor1), abs(factor2));
			var nRadius = 1. / factor;
		}
		strViewRadius = nRadius.toString();
	}
	else {
		if (bUseMeter)
			strViewRadius = (strViewRadius * FOOT_PER_METER).toString();
		else
			strViewRadius = strViewRadius.toString();
	}

	var queryStr = "txtControlPointN=" + escape(strControlPointN);
	queryStr += "&txtViewRadius=" + escape(strViewRadius);
	queryStr += "&txtIsVertical=" + escape(g_strGeodeticIsVertical);
	queryStr += "&method=2";
	queryStr += "&nMaxRecords=" + escape(top.g_nMaxSearchByTypeRecords);
	top.openSearchingWin();
	var win = window.open("GeodeticView.aspx?" + queryStr, "GeodeticControl", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function GeodeticCenterMap(strControlPointN, strViewRadius)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strViewRadius != null && strViewRadius.length > 0) strViewRadius = trim(strViewRadius);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	if (strViewRadius != null && strViewRadius.length > 0) {
		if (bUseMeter) 
			strViewRadius = strViewRadius.toString();
		else
			strViewRadius = (strViewRadius / FOOT_PER_METER).toString();
	}
	var queryStr = "&txtControlPointN=" + escape(strControlPointN) + "&txtViewRadius=" + escape(strViewRadius);
	queryStr += "&txtIsVertical=" + escape(g_strGeodeticIsVertical);
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	var url = "GetView.aspx?t=" + SVG_TEMPLATE + "&h=" + vMapHeight + "&w=" + vMapWidth + "&hg=" + g_strVisibleLayers + queryStr;
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Failed to locate the position.\n\n");
	    delete xmlDoc;
		return "";
	}

	xmlDoc.loadXML(xmlDoc.xml);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting center point coordinates.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	var nodes = xmlDoc.getElementsByTagName("x");
	if (nodes == null || nodes.length < 1)
	{
		alert("Error in getting X value of coordinates.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}
	
	var strX = node.firstChild.nodeValue;

	var nodes = xmlDoc.getElementsByTagName("y");
	if (nodes == null || nodes.length < 1)
	{
		alert("Error in getting Y value of coordinates.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}
	
	var strY = node.firstChild.nodeValue;

	strGeodeticX = strX;
	strGeodeticY = strY;
	strDrawGeodetic = 1;

	DrawViewMap(xmlDoc.xml);
	
	window.focus();
}

function GeodeticShowDetailReport(strControlPointN, strIsVertical)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strIsVertical != null && strIsVertical.length > 0) strIsVertical = trim(strIsVertical);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	var queryStr = "txtControlPointN=" + escape(strControlPointN);
	queryStr += "&txtIsVertical=" + escape(strIsVertical);
	var win = window.open("GeodeticDetail.aspx?" + queryStr, "GeodeticDetail", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function GeodeticShowDetailPhoto(strControlPointN, strIsVertical)
{
	if (strControlPointN != null && strControlPointN.length > 0) strControlPointN = trim(strControlPointN);
	if (strIsVertical != null && strIsVertical.length > 0) strIsVertical = trim(strIsVertical);
	if (strControlPointN == null || strControlPointN.length < 1)
	{
		alert("Input Control Point.");
		return "";
	}

	var queryStr = "txtControlPointN=" + escape(strControlPointN);
	queryStr += "&txtIsVertical=" + escape(strIsVertical);
	var win = window.open("GeodeticImage.aspx?" + queryStr, "GeodeticImage", "width=400,height=400,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function GeodeticShowDetailMonument(strDocID)
{
	if (strDocID != null && strDocID.length > 0) strDocID = trim(strDocID);
	var win = window.open("SVGDocView.aspx?docID=" + strDocID, "DocumentViewer", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function DrawViewMap(xmlStr)
{
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.loadXML(xmlStr);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Failed to locate the position.\n\n");
	    delete xmlDoc;
		return "";
	}

	var nodes = xmlDoc.getElementsByTagName("x");
	if (nodes == null || nodes.length < 1)
	{
		alert("Failed to locate the position.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}
	
	var strX = node.firstChild.nodeValue;

	var nodes = xmlDoc.getElementsByTagName("y");
	if (nodes == null || nodes.length < 1)
	{
		alert("Failed to locate the position.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}
	
	var strY = node.firstChild.nodeValue;

	var nodes = xmlDoc.getElementsByTagName("r");
	if (nodes == null || nodes.length < 1)
	{
		alert("Failed to locate the position.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}

	if (node.firstChild) 
	{	
		var strR = node.firstChild.nodeValue;

		if (strR != "") 
		{
			var factor1 = 2 * strR * mul_x / vMapWidth;
			var factor2 = 2 * strR * mul_y / vMapHeight;
			with (Math)
			{
				vScale = ceil(vScale * max(abs(factor1), abs(factor2)));
			}
		}
	}
	vCenterLong = 1 * strX;
	vCenterLat = 1 * strY;
	var vSVGImage = getSvgURL(SVG_IMAGE_URL, SVG_TEMPLATE, vMapWidth, vMapHeight, vCenterLong, vCenterLat, vScale, g_strVisibleLayers);
	RefreshSVG(top.MainMapFrame.MapImageID, vSVGImage);
}

function isDigit(c)
{
  return "0123456789.".indexOf(c) == -1 ? 0 : 1
}

function GetViewByPageAndGrid(txtGVPage,txtGVGrid)
{
	if (txtGVGrid != null && txtGVGrid.length > 0) txtGVGrid= trim(txtGVGrid);
	if (txtGVPage != null && txtGVPage.length > 0) txtGVPage = trim(txtGVPage);
	if (txtGVPage == null || txtGVPage.length < 1)
	{
		alert("Input Page.");
		return "";
	}

/*2002.7.29
	if (txtGVGrid == null || txtGVGrid.length < 1)
	{
		alert("Input Grid.");
		return "";
	}
*/	

	if (txtGVGrid.length != 0)

	{
//		var c1 = txtGVGrid.substr(0, 1).toLowerCase();
//		var c2 = txtGVGrid.substr(1, 1).toLowerCase();
//		if ( txtGVGrid.length != 2 || c1 < 'a' || c1 > 'z' || isDigit(c2) == 0 )
		if (!txtGVGrid.match(/^[a-hj][1-7]$/i))
		{
			alert("Grid format is 2 characters like 'C2'.\n\nThe first character must be a letter between 'A' and 'J', not including 'I'.\n\nThe second character must be a digit between 1 and 7.");
			return "";
		}
	}

	var queryStr = "&fppage=" + txtGVPage + "&fpgrid=" + txtGVGrid;

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	var url = "GetView.aspx?t=" + SVG_TEMPLATE + "&h=" + vMapHeight + "&w=" + vMapWidth + "&hg=" + g_strVisibleLayers + queryStr;
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Failed to locate the position.\n\n");
	    delete xmlDoc;
		return "";
	}

	DrawViewMap(xmlDoc.xml);
}

function GetViewByCity(nCityIndex)
{
	var queryStr = "&fcCity=" + g_aryCities[nCityIndex].strName;

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	var url = "GetView.aspx?t=" + SVG_TEMPLATE + "&h=" + vMapHeight + "&w=" + vMapWidth + "&hg=" + g_strVisibleLayers + queryStr;
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting response from web service in GetView.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	DrawViewMap(xmlDoc.xml);
}

function GetViewByIntersection(txtGVStreet1,txtGVStreet2,txtGVInterCity,txtGVRadius)
{
	if (txtGVStreet1 != null && txtGVStreet1.length > 0) txtGVStreet1 = trim(txtGVStreet1);
	if (txtGVStreet2 != null && txtGVStreet2.length > 0) txtGVStreet2 =trim(txtGVStreet2);
	if (txtGVInterCity != null && txtGVInterCity.length > 0) txtGVInterCity = trim(txtGVInterCity);
	if (txtGVRadius != null && txtGVRadius.length > 0) txtGVRadius = trim(txtGVRadius);
			
	if (txtGVStreet1 == null || txtGVStreet1.length < 1)
	{
		alert("Input #1 Street Name.");
		return "";
	}

	if (txtGVStreet2 == null || txtGVStreet2.length < 1)
	{
		alert("Input #2 street name.");
		return "";
	}

	if (txtGVRadius != null && txtGVRadius.length > 0)
	{
		if (isNaN(txtGVRadius))
		{
			alert("Invalid radius.");
			return "";
		}
		if (bUseMeter)
			txtGVRadius = txtGVRadius.toString();
		else
			txtGVRadius = (txtGVRadius / FOOT_PER_METER).toString();
	}

	var queryStr = "&fss1=" + txtGVStreet1 + "&fss2=" + txtGVStreet2 + "&fscity=" + txtGVInterCity + "&fsr=" + txtGVRadius;

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	var url = "GetView.aspx?t=" + SVG_TEMPLATE + "&h=" + vMapHeight + "&w=" + vMapWidth + "&hg=" + g_strVisibleLayers + queryStr;
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Failed to locate the position.\n\n");
	    delete xmlDoc;
		return "";
	}

	DrawViewMap(xmlDoc.xml);
}

function GetViewByCoordinate(txtGVCoordinateX,txtGVCoordinateY,txtGVRadius)
{
	if (txtGVCoordinateX != null && txtGVCoordinateX.length > 0) txtGVCoordinateX = trim(txtGVCoordinateX);
	if (txtGVCoordinateY != null && txtGVCoordinateY.length > 0) txtGVCoordinateY = trim(txtGVCoordinateY);
	if (txtGVRadius != null && txtGVRadius.length > 0) txtGVRadius = trim(txtGVRadius);

	if (txtGVCoordinateX == "") 
	{
		alert("Please input Easting value of coordinates.");
		return;
	}
	if (isNaN(1 * txtGVCoordinateX))
	{
		alert("The format of Easting value of coordinates is wrong.");
		return;
	}

	if (txtGVCoordinateY == "") 
	{
		alert("Please input Northing value of coordinates.");
		return;
	}
	if (isNaN(1 * txtGVCoordinateY))
	{
		alert("The format of Northing value of coordinates is wrong.");
		return;
	}

	if (txtGVRadius != null && txtGVRadius.length > 0)
	{
		if (isNaN(1 * txtGVRadius))
		{
			alert("Radius format is wrong.");
			return;
		}
		if (bUseMeter)
			txtGVRadius = txtGVRadius.toString();
		else
			txtGVRadius = (txtGVRadius / FOOT_PER_METER).toString();
		var factor1 = 2 * txtGVRadius * mul_x / vMapWidth;
		var factor2 = 2 * txtGVRadius * mul_y / vMapHeight;
		with (Math)
		{
			vScale = ceil(vScale * max(abs(factor1), abs(factor2)));
		}
	}

	if (bUseMeter) {
		vCenterLong = 1 * txtGVCoordinateX;
		vCenterLat = 1 * txtGVCoordinateY;
	}
	else {
		vCenterLong = 1 * txtGVCoordinateX / FOOT_PER_METER;
		vCenterLat = 1 * txtGVCoordinateY / FOOT_PER_METER;
	}
	var vSVGImage = getSvgURL(SVG_IMAGE_URL, SVG_TEMPLATE, vMapWidth, vMapHeight, vCenterLong, vCenterLat, vScale, g_strVisibleLayers);
	RefreshSVG(top.MainMapFrame.MapImageID, vSVGImage);
}

function MapArchiveDetail(method)
{
	var queryStr = "method=" + escape(method);
	var win = window.open("MapArchive.aspx?" + queryStr, "MapArchive", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function MapArchiveShow(strDocID)
{
	var queryStr = "docID=" + escape(strDocID);
	var win = window.open("SVGDocView.aspx?" + queryStr, "MapArchiveShow", "width=800,height=600,scrollbars=1,resizable=1");
	win.focus();
	AddWindow(win);
}

function RestartMeasuring()
{
	// reset variables
	g_nMeaPointCount = 0;
	g_nTotalDistance = 0;
	g_nTotalArea = 0
}

function EndMeasuring()
{
	top.fraLeftFrame.document.all("lblMeaTool").innerHTML = "[No Measuring Tool Selected]";

	RestartMeasuring();
}

function ClearMeasuringShapes()
{
	if (aryMeaPointObjs.length)
	{
		for (var i = 0; i < aryMeaPointObjs.length; i++)
		{
			aryMeaPointObjs[i][0].getStyle().setProperty("display", "none");
			aryMeaPointObjs[i][1].getStyle().setProperty("display", "none");
		}
	}
		
	shpMeaLine.getStyle().setProperty("display", "none");
		
	// reset variables
	g_nMeaPointCount = 0;
	g_nTotalDistance = 0;
	g_nTotalArea = 0
	
	aryMeaPointObjs = new Array();

	// clear searching shape
	DeleteShape();
	pickedPoint = new Array();
	pickedPointNum = 0;
}

function AddMeaDisPoint(sx, sy, nLong, nLat)
{
	var table = top.fraLeftFrame.document.all("tblMeasuringDisplay");
	var row, cell;
	if (g_nMeaPointCount == 0)		// first point
	{
		ClearTable(table);
		// insert table header
		row = table.insertRow();
		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>ID</b>");

		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>Northing</b>");
		
		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>Easting</b>");
		
		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>Distance (" + MAP_UNIT + ")</b>");

		// display total distance
		row = table.insertRow();
		cell = row.insertCell();
		cell.colSpan = 3;
		cell.align = "right";
		cell.insertAdjacentHTML("afterBegin", "Total: ");

		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", g_nTotalDistance);

		ClearMeasuringShapes();
		
		shpMeaLine.setAttribute("points", sx + "," + sy);
		shpMeaLine.getStyle().setProperty("display", "inline");
		shpMeaLine.getStyle().setProperty("fill", "none");
	}
	else
	{
		var params = shpMeaLine.getAttribute("points");
		shpMeaLine.setAttribute("points", params + " " + sx + "," + sy);
	}

	g_nMeaPointCount++;
	
	// display point and line on map
	var shapeLayer = SVGDocument.getElementById(SEARCH_SHAPE_ID);
	var circle = _newcircle(SVGDocument, 0, 0, 3, "black", "black", 1);
	var text = SVGDocument.createTextNode(g_nMeaPointCount);
	var textNode = SVGDocument.createElement("text");
	textNode.setAttribute("x", 5);
	textNode.setAttribute("y", 15);
	textNode.getStyle().setProperty("stroke", "blue");
	textNode.getStyle().setProperty("fill", "blue");
	textNode.appendChild(text);
	circle.setAttribute("transform", "translate(" + sx + "," + sy + ")");
	textNode.setAttribute("transform", "translate(" + sx + "," + sy + ")");
	shapeLayer.appendChild(circle);
	shapeLayer.appendChild(textNode);
	aryMeaPointObjs[aryMeaPointObjs.length] = [circle, textNode];
	
	// display point info
	row = table.insertRow(table.rows.length - 1);
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + g_nMeaPointCount);

	cell = row.insertCell();
	if (!bUseMeter) {
		nLat = nLat * FOOT_PER_METER;
		nLong = nLong * FOOT_PER_METER;
	}
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(nLat));

	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(nLong));

	var nDistance = 0;
	if (g_nMeaPointCount != 1)	// not first point
	{
		var wDeltaX = (sx - g_nLastMeaPointX) / mul_x;
		var wDeltaY = (sy - g_nLastMeaPointY) / mul_y;
		nDistance = Math.sqrt(wDeltaX * wDeltaX + wDeltaY * wDeltaY);
		if (bUseMeter)
			nDistance = Math.round(nDistance * 10) / 10;
		else
			nDistance = Math.round(nDistance * FOOT_PER_METER * 10) / 10;
		
		g_nLastTotalDistance = g_nTotalDistance;
		g_nTotalDistance += nDistance;
		g_nTotalDistance = Math.round(g_nTotalDistance * 10) / 10;
		table.rows[table.rows.length - 1].cells[1].innerHTML = FormatNumber(g_nTotalDistance);
	}
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", FormatNumber(nDistance));

    g_nLastMeaPointX = sx;
    g_nLastMeaPointY = sy;
}

function AddMeaAreaPoint(sx, sy, nLong, nLat)
{
	var table = top.fraLeftFrame.document.all("tblMeasuringDisplay");
	var row, cell;
	if (g_nMeaPointCount == 0)		// first point
	{
		ClearTable(table);
		// insert table header
		row = table.insertRow();
		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>ID</b>");

		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>Northing</b>");
		
		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", "<b>Easting</b>");
		
		cell = row.insertCell();
		       
		cell.insertAdjacentHTML("afterBegin", "<b>Area</b>");

		// display total distance
		row = table.insertRow();
		cell = row.insertCell();
		cell.colSpan = 3;
		cell.align = "right";
		cell.insertAdjacentHTML("afterBegin", "Total: ");

		cell = row.insertCell();
		cell.insertAdjacentHTML("afterBegin", g_nTotalArea);

		ClearMeasuringShapes();
		
		shpMeaLine.setAttribute("points", sx + "," + sy);
		shpMeaLine.getStyle().setProperty("display", "inline");
		shpMeaLine.getStyle().setProperty("fill", "red");
		
		// add first point
		pickedPoint[pickedPoint.length] = new PointCoor(nLong, nLat);
		// add last point the same as first point
		pickedPoint[pickedPoint.length] = pickedPoint[0];
	}
	else
	{
		var params = shpMeaLine.getAttribute("points");
		shpMeaLine.setAttribute("points", params + " " + sx + "," + sy);

		// move down the last point, which is always the same as the first point
		pickedPoint[pickedPoint.length] = pickedPoint[0];
		// add the new point
		pickedPoint[pickedPoint.length - 2] = new PointCoor(nLong, nLat);
	}

	g_nMeaPointCount++;
	
	// display point and line on map
	var shapeLayer = SVGDocument.getElementById(SEARCH_SHAPE_ID);
	var circle = _newcircle(SVGDocument, 0, 0, 3, "black", "black", 1);
	var text = SVGDocument.createTextNode(g_nMeaPointCount);
	var textNode = SVGDocument.createElement("text");
	textNode.setAttribute("x", 5);
	textNode.setAttribute("y", 15);
	textNode.getStyle().setProperty("stroke", "blue");
	textNode.getStyle().setProperty("fill", "blue");
	textNode.appendChild(text);
	circle.setAttribute("transform", "translate(" + sx + "," + sy + ")");
	textNode.setAttribute("transform", "translate(" + sx + "," + sy + ")");
	shapeLayer.appendChild(circle);
	shapeLayer.appendChild(textNode);
	aryMeaPointObjs[aryMeaPointObjs.length] = [circle, textNode];
	
	// display point info
	row = table.insertRow(table.rows.length - 1);
	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + g_nMeaPointCount);

	cell = row.insertCell();
	if (!bUseMeter) {
		nLat = nLat * FOOT_PER_METER;
		nLong = nLong * FOOT_PER_METER;
	}
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(nLat));

	cell = row.insertCell();
	cell.insertAdjacentHTML("afterBegin", "" + Math.round(nLong));

	var nArea = 0;
	if (g_nMeaPointCount > 2)	// after the 3rd point
	{
		nArea = CalculateArea(pickedPoint);
		if (!bUseMeter)
			nArea = Math.round(nArea * FOOT_PER_METER * FOOT_PER_METER * 10) / 10;
		g_nLastTotalArea = g_nTotalArea;
		g_sLastAreaUnit = AREA_UNIT;
		g_nTotalArea = nArea;
		g_nTotalArea = Math.round(g_nTotalArea * 10) / 10;
		if (bUseMeter) {
		   if (g_nTotalArea < 1000000) {
			   AREA_UNIT = "m<sup>2</sup>";
		   }
		   else if (g_nTotalArea < 100000000) {
			   AREA_UNIT = "km<sup>2</sup>";
			   g_nTotalArea /= 1000000.0;
		   }
		   else {
			   AREA_UNIT = "hectares";
			   g_nTotalArea /= 100000000.0;
		   }
		}
		else {
		   if (g_nTotalArea < 43559) {
			   AREA_UNIT = "ft<sup>2</sup>";
		   }
		   else if (g_nTotalArea < 27878313) {
			   AREA_UNIT = "acres";
			   g_nTotalArea /= 43559.8641;
		   }
		   else {
			   AREA_UNIT = "mi<sup>2</sup>";
			   g_nTotalArea /= 27878313.024;
		   }
		}
		// round the total area
		g_nTotalArea = Math.round(g_nTotalArea*10.0)/10.0;
		table.rows[table.rows.length - 1].cells[1].innerHTML = FormatNumber(g_nTotalArea)+" "+AREA_UNIT;
	}
}

function RemoveLastMeaPoint()
{
	// remove the last point on map
	aryMeaPointObjs[aryMeaPointObjs.length - 1][0].getStyle().setProperty("display", "none");
	aryMeaPointObjs[aryMeaPointObjs.length - 1][1].getStyle().setProperty("display", "none");
	var params = shpMeaLine.getAttribute("points");
	params = params.substring(0, params.lastIndexOf(" "));
	shpMeaLine.setAttribute("points", params);
	
	var table = top.fraLeftFrame.document.all("tblMeasuringDisplay");
	table.deleteRow(table.rows.length - 2);
	switch (vTool)
	{
		case MEASURE_DISTANCE:		// measure distance
			// restore total distance
			g_nTotalDistance = g_nLastTotalDistance;
			table.rows[table.rows.length - 1].cells[1].innerHTML = FormatNumber(g_nTotalDistance);
			break;

		case MEASURE_AREA:	// measure area
			// restore total area
			g_nTotalArea = g_nLastTotalArea;
			AREA_UNIT = g_sLastAreaUnit;
			table.rows[table.rows.length - 1].cells[1].innerHTML = FormatNumber(g_nTotalArea)+" "+AREA_UNIT;
			//table.rows[table.rows.length - 1].cells[1].innerHTML = g_nTotalArea;
			break;
		
		default:
	}
	g_nMeaPointCount--;
}

function FormatNumber(dblNum)
{
	var tmpStr = String(dblNum);
	var dotPos = tmpStr.indexOf(".");
	var front = tmpStr;
	var tail = "";
	if (dotPos > 0) {
		front = tmpStr.substr(0,dotPos);
		tail = tmpStr.substr(dotPos,tmpStr.length-dotPos);
	}
	var numComma = Math.floor((front.length-1)/3);
	var rtnStr = tail;
	for (var i = 0; i < numComma; i++)
		rtnStr = ","+front.substr(front.length-(i+1)*3, 3)+rtnStr;
	rtnStr = front.substr(0, front.length-numComma*3)+rtnStr;
		
	return rtnStr;
}

function ClearPoints()
{
	var table = top.fraLeftFrame.document.all("tblMeasuringDisplay");
	ClearTable(table);

	ClearMeasuringShapes();
}

function CalculateArea(aryPoints)
{
	var nArea = 0;

	if (aryPoints.length < 3 + 1)	// +1 because the last point is always added as the same as the first one.
		return 0;
	
	for (var i = 0; i < aryPoints.length - 1; i++)
	{
		nArea += aryPoints[i].x * aryPoints[i + 1].y - aryPoints[i + 1].x * aryPoints[i].y;
	}
	nArea = nArea * 0.5;
	
	return Math.abs(nArea);
}

function ShowGeoPage(strImageID)
{
	g_strImageID = strImageID;
	if (strImageID == null || strImageID == "")
		return;

	// get image spec
	var url = "TiffSpec.aspx?i=" + strImageID;
	if (GetImageSpec(url, g_aryOrigin, g_aryMultiplier, g_arySize))
	{
		// re-center map
		vCenterLong = g_aryOrigin[0] + g_arySize[0] * g_aryMultiplier[0] * 0.5;
		vCenterLat = g_aryOrigin[1] + g_arySize[1] * g_aryMultiplier[1] * 0.5;
		
		var factor1 = g_arySize[0] * g_aryMultiplier[0] * mul_x / vMapWidth;
		var factor2 = g_arySize[1] * g_aryMultiplier[1] * mul_y / vMapHeight;
		vScale = Math.ceil(vScale * Math.max(factor1, factor2));
		
		// reset this flag any way
		var group = FindGroupByName(GEO_TIFF_GROUP_NAME);
		if (group != null)
			setGroupVisible(group.nGroupIndex, true)

		var vSVGImage = getSvgURL(SVG_IMAGE_URL, SVG_TEMPLATE, vMapWidth, vMapHeight, vCenterLong, vCenterLat, vScale, g_strVisibleLayers);
		RefreshSVG(top.MainMapFrame.MapImageID, vSVGImage);
		// this must be set after "RefreshSVG"
		bReloadGImage = false;
		
		window.focus();
	}
}

function GetImageSpec(ImageSpecURL,ImageOriginAry,ImageMultiplierAry,ImageSizeAry,ImageUnitAry)
{
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false"
	xmlDoc.load(ImageSpecURL);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting response from web service.");
	    delete xmlDoc;
		return false;
	}

	var strResponse = xmlDoc.xml.replace(/&lt;/g, "<");
	strResponse = strResponse.replace(/&gt;/g, ">");
	
	xmlDoc.loadXML(strResponse);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting image specs.");		
	    delete xmlDoc;
		return false;
	}

	EleOrigin = xmlDoc.getElementsByTagName("origin").item(0);
    if(EleOrigin == null)
	{
		alert("Error in getting image origin.");		
	    delete xmlDoc;
		return false;
	}

    ImageOriginAry[0] = EleOrigin.getAttribute("x")*1.0;
    ImageOriginAry[1] = EleOrigin.getAttribute("y")*1.0;

    EleMul = xmlDoc.getElementsByTagName("scale").item(0);
    if(EleMul == null)
	{
		alert("Error in getting image multiplier.");		
	    delete xmlDoc;
		return false;
	}

    ImageMultiplierAry[0] = EleMul.getAttribute("x")*1.0;
    ImageMultiplierAry[1] = EleMul.getAttribute("y")*1.0;    

    EleSize = xmlDoc.getElementsByTagName("size").item(0);
    if(EleSize == null)
	{
		alert("Error in getting image clip size.");		
	    delete xmlDoc;
		return false;
	}

    ImageSizeAry[0] = EleSize.getAttribute("x")*1.0;
    ImageSizeAry[1] = EleSize.getAttribute("y")*1.0;

    if (ImageMultiplierAry[1] > 0)	// origin is bottom-left corner
    {
		// move the origin to top-left corner and change the multiplier to negative
		ImageMultiplierAry[1] *= -1;
	}
    
    delete xmlDoc;
    
    return true;
}

function GetImageAsText(strImageURL)
{
	var strImageData = "";
	
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false"
	xmlDoc.load(strImageURL);

	if (xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting image data.");
		delete xmlDoc;
		return "";
	}
	
	strImageData = xmlDoc.xml.replace(/&lt;/g, "<");
	strImageData = strImageData.replace(/&gt;/g, ">");
	
	xmlDoc.loadXML(strImageData);
	if (xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting image data.\n\n" + xmlDoc.parseError.reason);
		delete xmlDoc;
		return "";
	}
	
	var nodes = xmlDoc.getElementsByTagName("TiffClip");
	if (nodes == null || nodes.length < 1)
	{
		alert("Error in getting image data.");
		delete xmlDoc;
		return "";
	}
	
	var node = nodes.item(0);
	if (node.getAttribute("error-desc") != null && node.getAttribute("error-desc").match(/\S/))
	{
		alert(node.getAttribute("error-desc"));
		delete xmlDoc;
		return "";
	}
	
	node = node.firstChild;
	if (node == null || node.length < 1)
	{
		delete xmlDoc;
		return "";
	}

	strImageData = node.nodeValue;
	
	delete xmlDoc;
	return strImageData;
}

function getClipURL(imageTemplate,fileName,w, h, ox, oy, sw, sh)
{   
	return pagePath + "/TiffClip.aspx?t="+imageTemplate+"&i="+fileName+"&w=" + w + "&h=" + h + "&ox=" + ox + "&oy=" + oy + "&sw=" + sw + "&sh=" + sh;
}

function InsertImage(objImage, x, y, width, height, strImageURL)
{  
	if(strImageURL == null || SVGDocument == null)
	  return false;
	
	var strImageData = GetImageAsText(strImageURL);
	if (strImageData == "")
		return false;
	
	strImageData = "data:;base64," + strImageData;

	objImage.setAttribute("x", x);
	objImage.setAttribute("y", y);
	objImage.setAttribute("width", width);
	objImage.setAttribute("height", height);
	objImage.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', strImageData);
	alert(strImageData);
	return true;
}

function ViewDoc(strDocID)
{
	var win = window.open("SVGDocView.aspx?docID=" + strDocID, "DocumentViewer", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function HiliteParcel(svgRoot)
{
	HiliteParcel_Layer(svgRoot, "13030");
	HiliteParcel_Layer(svgRoot, "13031");
	HiliteParcel_Layer(svgRoot, "13032");
	HiliteParcel_Layer(svgRoot, "13033");
	HiliteParcel_Layer(svgRoot, "13034");
	HiliteParcel_Layer(svgRoot, "13035");
	HiliteParcel_Layer(svgRoot, "13036");
	HiliteParcel_Layer(svgRoot, "13037");
	HiliteParcel_Layer(svgRoot, "13038");
	HiliteParcel_Layer(svgRoot, "13039");
	HiliteParcel_Layer(svgRoot, "13040");
	HiliteParcel_Layer(svgRoot, "13041");
	HiliteParcel_Layer(svgRoot, "13042");
	HiliteParcel_Layer(svgRoot, "13043");
	HiliteParcel_Layer(svgRoot, "13044");
	HiliteParcel_Layer(svgRoot, "13045");
	HiliteParcel_Layer(svgRoot, "13046");
	HiliteParcel_Layer(svgRoot, "13047");
}
function HiliteParcel_Layer(svgRoot, asLayer)
{
	//g_strROWEParcelLayerID is 502
	if (g_strHiliteParcelID == null)
		return;

	//var parcelLayer = svgRoot.getElementById(g_strParcelLayerID);
	var parcelLayer = svgRoot.getElementById(asLayer);
	
    if (g_strROWEParcelLayerID.length >0 )
    {
        parcelLayer = svgRoot.getElementById(g_strROWEParcelLayerID);
        g_strROWEParcelLayerID = "";
    }	
	
	if (parcelLayer == null)
	{
	    return;
	}
	
	var parcels = parcelLayer.getElementsByTagName("path");
	if (parcels == null)
		return;
	
	var parcel = null;
	for (var i = 0; i < parcels.length; i++)
	{
		parcel = parcels.item(i);

		if (parcel.getAttribute("id") == g_strHiliteParcelID)
		{
			parcel.getStyle().setProperty("fill", HIGHLIGHT_COLOR); //090914 moved here
			break;
		}
	}
	//if (parcel == null)
	//	return;
	//parcel.getStyle().setProperty("fill", HIGHLIGHT_COLOR);
}

function city(strName, minx, maxx, miny, maxy)
{
	this.strName = strName;
	this.minx = minx;
	this.maxx = maxx;
	this.miny = miny;
	this.maxy = maxy;
}

function GetCityList()
{
	var strCityList = "";

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false"
	var url = "GetCityList.aspx";
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting response from web service in GetCityList.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	var strResponse = xmlDoc.xml.replace(/&lt;/g, "<");
	strResponse = strResponse.replace(/&gt;/g, ">");
	
	xmlDoc.loadXML(strResponse);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("[GetCityList] Error in getting response from web service.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	var nodeList = xmlDoc.getElementsByTagName("SQLQuery");
    if(nodeList != null && nodeList.length > 0)
	{
		if (nodeList.item(0).getAttribute("error-desc") != null && nodeList.item(0).getAttribute("error-desc").match(/\S/))
		{
			alert("[GetCityList] " + nodeList.item(0).getAttribute("error-desc"));
			delete xmlDoc;
			return "";
		}
	}
	
	nodeList = xmlDoc.getElementsByTagName("row");
    if(nodeList == null || nodeList.length < 1)
	{
		alert("Error in getting city list: No city found.");		
	    delete xmlDoc;
		return "";
	}
	
	for (var i = 0; i < nodeList.length; i++)
	{
		var strCityName = nodeList.item(i).getAttribute("JURISDICTION");
		var strCityOption = "<option value=\"" + i + "\">" + strCityName + "</option>";
		
		strCityList += strCityOption;
		
		g_aryCities[i] = new city(strCityName, 1.0 * nodeList.item(i).getAttribute("minx"), 1.0 * nodeList.item(i).getAttribute("maxx"), 1.0 * nodeList.item(i).getAttribute("miny"), 1.0 * nodeList.item(i).getAttribute("maxy"));
	}

	delete xmlDoc;
	
	return strCityList;
}

function AddWindow(win)
{
	var nWinIndex = 0;
	for (nWinIndex = 0; nWinIndex < g_aryWindows.length; nWinIndex++)
	{
		if (!g_aryWindows[nWinIndex] || g_aryWindows[nWinIndex].closed || g_aryWindows[nWinIndex] == win)	// not occupied, or closed, or already in the array.
			break;
	}
	
	g_aryWindows[nWinIndex] = win;
}

function CloseAllWindows()
{
	for (var nWinIndex = 0; nWinIndex < g_aryWindows.length; nWinIndex++)
	{
		if (g_aryWindows[nWinIndex] && !g_aryWindows[nWinIndex].closed)
			g_aryWindows[nWinIndex].close();
	}
}

function DownloadSave(strDocid, objWin)
{
	var queryStr = "DownloadWait.aspx?strDocid=" + escape(strDocid);
	if (!objWin || !objWin.document)
		objWin = top;

	var win = objWin.open(queryStr, "DownloadWait", "width=700,height=500,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

function HelpContent(nIndex)
{
	if (!top.fraLeftFrame || !top.fraLeftFrame.document.all("helpContent")) return;
	
	if (nIndex == MENU_DOC_SEARCH) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Find and locate Documents based on the spatial filter or key words of their attributes.";
	}
	else if (nIndex == MENU_PARCEL_SEARCH) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Find and locate Parcels based on the parcel key fields.";
	}
	else if (nIndex == MENU_GEODETIC_VIEW) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "View and locate GPS related data set.";
	}
	else if (nIndex == MENU_GET_VIEW) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Locate a window area based the searching criteria (Page and Grid, City name, Street intersection and center point.";
	}
	else if (nIndex == MENU_MAP_ARCHIVE) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Map Archive.";
	}
	else if (nIndex == MENU_HELP) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "How to use help document.";
	}
	else if (nIndex == MENU_SHOPPING_CART) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Shopping Cart.";
	}
	else if (nIndex == MENU_DOWNLOAD) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Downloading help document.";
	}
	else if (nIndex == 11) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Find and locate ROWE parcels based on the spatial filter or key words of their attributes.";
	}
	else if (nIndex == 12) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Zoom Out: Click the mouse on the map and the image will appear half as large. Drag the mouse over a selected area of the map to place a rectangular outline. The outlined selection will fit into the chosen area. The center point of the map will be at the center of the selected area.";
	}
	else if (nIndex == 13) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Pan: Click the mouse over the map to re-center the display at the clicked point. Hold down the left mouse button to drag the map.";
	}
	else if (nIndex == 14) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Show Attributes: When the Show Attributes icon is selected, clicking the left mouse button over the boundary of certain linear features (like Local Street Centerline), or the inside of certain area features (like Parcel Polygon), will show attributes.";
	}
	else if (nIndex == 15) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Get Coordinates: When the Get Coordinates icon is selected, clicking the Detail Map will show the coordinates below in the Menu frame.";
	}
	else if (nIndex == 16) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Measure Distance: When the Measure Distance icon is selected, clicking two points on the Detail Map will measure the distance between them.";
	}
	else if (nIndex == 17) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Measure Area: When the Measure Area icon is selected, clicking three or more points on the Detail Map will measure the area between them.";
	}
	else if (nIndex == 19) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Pan West: Selecting this icon moves the Detail Map view east.";
	}
	else if (nIndex == 20) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Pan East: Selecting this icon moves the Detail Map view west.";
	}
	else if (nIndex == 21) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Pan North: Selecting this icon moves the Detail Map view south.";
	}
	else if (nIndex == 22) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Pan South: Selecting this icon moves the Detail Map view north.";
	}
	else if (nIndex == 23) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Reload Base Map: Clicking this icon returns to the initial Detail Map view.";
	}
	else if (nIndex == 24) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Select Features: Clicking this icon will bring up the 'Select Features' dialog box.";
	}
	else if (nIndex == 25) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Toggle Image Display: Clicking this icon will toggle the loaded image display on and off.";
	}
	else if (nIndex == 26) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Map Tips: Pressing this icon will display map tips when the cursor moves over the Detail Map.";
	}
	else if (nIndex == 27) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Highlight: Pressing this icon will toggle the feature highlighting on or off.";
	}
	else if (nIndex == 28) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Depress to use meters: Depress this icon for meters, release it for feet.";
	}
	else if (nIndex == 29) {
		top.fraLeftFrame.document.all("helpContent").innerHTML = "Online Help: Clicking this icon takes the user to the E-Commerce System Help menu.";
	}
}

function help()
{
	openWinown = window.open("Help/index.htm","helpWin",'height=600,width=900 scrollbars=1 resizable=1');
	openWinown.focus();
	//window.showModelessDialog("Help/index.asp", "helpWin", "dialogLeft:10;dialogTop:10;dialogWidth:700px;dialogHeight:600px;resizable:yes;status:no;help:no");
}

function DetailedOCFCDParcelReport(MSLINK, FCLT_NO, FCLT_NAME, MAP_NO, MAP_TITLE)
{
	var winURL = "ROWParcel.aspx?mslink=" + MSLINK + "&FCLT_NO=" + escape(FCLT_NO) + "&FCLT_NAME=" + escape(FCLT_NAME) + "&MAP_NO=" + escape(MAP_NO) + "&MAP_TITLE=" + escape(MAP_TITLE);
	var win = window.open(winURL,"OCFCDParcelReport",'height=600,width=500 scrollbars=1 resizable=1');
	win.focus();
	AddWindow(win);
}
/*
function GetParcelTypeList()
{     var strCityList = "";
	  var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".","rootMicrosoftSQLServer");
      var properties = service.ExecQuery("SELECT * FROM MSSQL_DatabaseFile where DatabaseName='Northwind'");
      var e = new Enumerator (properties);
      strCityList +="<table border=1>";
      for (;!e.atEnd();e.moveNext ())
      {
            var p = e.item ();
            strCityList += "<tr>";
            strCityList +="<td>" + p.Name + "</td>";
            strCityList +="<td>" + p.PhysicalName + "</td>";
            strCityList += "</tr>";
      }
      strCityList += "</table>";
	  return strCityList;
}
*/

function GetParcelTypeList()
{
	var strParcelList = "";

	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false"
	var url = "GetParcelTypeList.aspx";
	xmlDoc.load(url);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("Error in getting response from web service in GetParcelTypeList.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	var strResponse = xmlDoc.xml.replace(/&lt;/g, "<");
	strResponse = strResponse.replace(/&gt;/g, ">");
	
	xmlDoc.loadXML(strResponse);

	if ( xmlDoc.parseError.errorCode != 0 )
	{
		alert("[GetParcelTypeList] Error in getting response from web service.\n\n" + xmlDoc.parseError.reason);
	    delete xmlDoc;
		return "";
	}

	var nodeList = xmlDoc.getElementsByTagName("SQLQuery");
    if(nodeList != null && nodeList.length > 0)
	{
		if (nodeList.item(0).getAttribute("error-desc") != null && nodeList.item(0).getAttribute("error-desc").match(/\S/))
		{
			alert("[GetParcelTypeList] " + nodeList.item(0).getAttribute("error-desc"));
			delete xmlDoc;
			return "";
		}
	}
	
	nodeList = xmlDoc.getElementsByTagName("row");
    if(nodeList == null || nodeList.length < 1)
	{
		alert("Error in getting parcel type list: No parcel type found.");		
	    delete xmlDoc;
		return "";
	}
	
	for (var i = 0; i < nodeList.length; i++)
	{
		var strParcelName = nodeList.item(i).getAttribute("DocType");
		var strParcelOption = "<option value=\"" + i + "\">" + strParcelName.substr(0,20) + "</option>";
		
		strParcelList += strParcelOption;
		
//		g_aryCities[i] = new city(strCityName, 1.0 * nodeList.item(i).getAttribute("minx"), 1.0 * nodeList.item(i).getAttribute("maxx"), 1.0 * nodeList.item(i).getAttribute("miny"), 1.0 * nodeList.item(i).getAttribute("maxy"));
	}

	delete xmlDoc;
	
	return strParcelList;
}

function ViewParcelDetail( parcelType, ID )
{
	var win = window.open("ViewRoweParcelDetail.aspx?type=" + parcelType+"&id="+ID, "ParcelViewer", "width=800,height=600,scrollbars=1,resizable=1,status=1");
	win.focus();
	AddWindow(win);
}

