
function clearForm()
{
  document.getElementById('herbarium').selectedIndex = 0;
  document.getElementById('accession').value = "";
  document.getElementById('family').value = "";
  document.getElementById('genus').value = "";
  document.getElementById('species').value = "";
  document.getElementById('subtaxon').value = "";
  document.getElementById('collector').value = "";
  document.getElementById('collnum').value = "";
  document.getElementById('day').value = "";
  document.getElementById('month').selectedIndex = 0;
  document.getElementById('year').value = "";
  document.getElementById('country').value = "";
  document.getElementById('state').value = "";
  document.getElementById('county').value = "";
  document.getElementById('locality').value = "";
  document.getElementById('minelev').value = "";
  document.getElementById('maxelev').value = "";
  document.getElementById('sort1').selectedIndex = 4;
  //document.searchform.reset();
  resetPolygon();
}

// Code to display search map:

var isEditing = false;
var polygon = null;
var map = null;
var isCompatible = GBrowserIsCompatible();
var markers = [];
	
function editClick()
{
	isEditing = !isEditing;

	if(isEditing && polygon != null)
	{
		document.getElementById('edit_polygon').innerHTML = 'Stop Editing';
		document.getElementById('reset_polygon').style.color = '#999999';
		document.getElementById('reset_polygon').style.cursor = 'default';
		document.getElementById('maphelp').innerHTML = '<b>Map Search:</b> Click your mouse button to add points to the polygon.';
		polygon.enableDrawing();
	}
	else
	{
		document.getElementById('edit_polygon').innerHTML = 'Edit Polygon';
		document.getElementById('reset_polygon').style.color = '#000000';
		document.getElementById('reset_polygon').style.cursor = 'pointer';
		document.getElementById('maphelp').innerHTML = '<b>Map Search:</b> Click "Edit Polygon" to modify the boundary of your polygon.';
		polygon.disableEditing();
		setVertexList();
	}
}

// JavaScript doesn't have a function to round a number to a specified number of decimal points.
// Arguments: rnum = number to round, rlength = number of decimal places
function roundNumber(rnum, rlength)
{
  return Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
}

function setVertexList()
{
	if(polygon != null)
	{
		var vertexCount = polygon.getVertexCount();
		var vertexString = "";
		for (var i = 0; i < vertexCount; i++)
		{
			var vertexCoord = polygon.getVertex(i);
			vertexString += roundNumber(vertexCoord.lat(), 6) + "," + roundNumber(vertexCoord.lng(), 6) + ";";
		}
		document.getElementById('polygon').value = vertexString;
		document.getElementById('zoom').value = map.getZoom() + 1;
	}
}

function resetPolygon()
{
	if(polygon != null)
	{
		map.removeOverlay(polygon);
		document.getElementById('edit_polygon').innerHTML = 'Create Polygon';
		document.getElementById('reset_polygon').style.color = '#999999';
		document.getElementById('reset_polygon').style.cursor = 'default';
		//document.getElementById('reset_polygon').
		document.getElementById('maphelp').innerHTML = '<b>Map Search:</b> Click "Create Polygon" to define an area to search.';
		document.getElementById('polygon').value = "";
	}

	polygon = new GPolygon([], "#000000", 1, 1, "#336699", 0.3);
	map.addOverlay(polygon);
/*
	GEvent.addListener(polygon, "lineupdated", function() {
		setTimeout(updateBoundingBox, 50);
	});
*/
	GEvent.addListener(polygon, "endline", function() {
		setTimeout(editClick, 50);
	});
}

function load()
{
	if (isCompatible)
	{
		// Create Map
		map = new GMap2(document.getElementById("searchmap"));

		// Center and zoom the map:
		if(searchpoly.polygonZoom < 1) searchpoly.polygonZoom = 4;
		if(searchpoly.polygonCenterLat != 0 && searchpoly.polygonCenterLon != 0) map.setCenter(new GLatLng(searchpoly.polygonCenterLat, searchpoly.polygonCenterLon), searchpoly.polygonZoom);
		else tmap.setCenter(new GLatLng(41.5, -112.0), 4);

		// Add controls
		map.addMapType(G_PHYSICAL_MAP);
		var mapControl = new GHierarchicalMapTypeControl();
		mapControl.clearRelationships();
		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
		map.addControl(mapControl);
		map.addControl(new GLargeMapControl());
		map.setMapType(G_PHYSICAL_MAP);

		resetPolygon();
		if(searchpoly.polygonVertexCount > 0) showPolygon();
	}
}

function showPolygon()
{
	if(searchpoly.polygonVertexCount > 0)
	{
		var latlonArray = new Array();
		var i = 0;
		for(i = 0; i < searchpoly.polygonVertexCount; i++)
		{
		  latlonArray[i] = new GLatLng(searchpoly.polygon[i][0], searchpoly.polygon[i][1]);
		}
		polygon = new GPolygon(latlonArray, "#000000", 1, 1, "#336699", 0.3);
		map.addOverlay(polygon);

		document.getElementById('edit_polygon').innerHTML = 'Edit Polygon';
		document.getElementById('reset_polygon').style.color = '#000000';
		document.getElementById('reset_polygon').style.cursor = 'pointer';
		document.getElementById('maphelp').innerHTML = '<b>Map Search:</b> Click "Edit Polygon" to modify the boundary of your polygon.';
		polygon.disableEditing();
		setVertexList();
	}
}


function showMessage(message)
{
  if(message != "") window.alert(message);
}
