function populatecoords(p)
	{
	var lat = document.getElementById('lat');
	if (lat)
		{
		lat.value = p.coords.latitude.toFixed(4);
		}
	var lon = document.getElementById('lon');
	if (lon)
		{
		lon.value = p.coords.longitude.toFixed(4);
		}
	var frm = document.getElementById('lform');
	if (lat && lon && frm)
		{
		frm.submit();
		}
	}
	
function error_callback(p)
	{
		alert('We were unable to accurately determine your location. Please enter your information manually. Sorry!');
	}

function isCoordinate(str)
{
	// coordinate is of form: N 34° 00.138
	if (str.match(/^[nsew+-]?\s*\d+[d\u00B0]?\s+\d+\.\d+$/i))
		return true;
	
	// coordinate is of form: N 34° 0' 08.2800"
	if (str.match(/^[nsew+-]?\s*\d+[d\u00B0]?\s+\d+\'?\s+\d+\.\d+\"?$/i))
		return true;
	
	// coordinate is of form: 34.0023
	if (str.match(/^[+-]?\d+\.\d+$/))
		return true;
	
	return false;
}

function cvalidate(form)
{

	var flds = {'postalcode':'', 'city':'', 'state':'', 'lat':'', 'lon':''};

	for (var i = 0; i < form.length; i++)
    {
        var f = form.elements[i];
		for (var t in flds)
		{
			if (f.name == t)
				{
				flds[t] = f.value;
				}
		}	
	}

	if (flds['lat'] != '' && !isCoordinate(flds['lat']))
		{
		alert("Latitude coordinates must be in one of the following formats:\n34 00.138,\nN 34 00.138,\nN 34\u00B0 0' 08.2800\",\nor 34.0023\n(if not specified, system assumes N)");
		return false;
		}
	if (flds['lon'] != '' && !isCoordinate(flds['lon']))
		{
		alert("Longitude coordinates must be in one of the following formats:\n118 26.466,\nW 118 26.466,\nW 118\u00B0 26' 27.9600\",\nor -118.4411\n(if not specified, system assumes W)");
		return false;		
		}
	if ((flds['lat'] != '' && flds['lon'] == '') ||
		(flds['lat'] != '' && flds['lon'] == ''))
		{
		alert("You must provide both latitude and longitude if entering GPS coordinates.");
		return false;
		}
	if (flds['lat']!='' && flds['lon']!='')
		{
		return true;
		}
	if (flds['city'] == '' && flds['state'] == '' && flds['postalcode'] == '')
		{
		alert("You must enter at least a city and state, or a postal code, or GPS coordinates");
		return false;
		}
	if (flds['postalcode'] != '' && flds['postalcode'].match(/[^\d]/))
		{
		alert('Postal code is not valid.');
		return false;
		}
	if (flds['address'] != '' && flds['city']=='' && flds['state'] == '' && flds['postalcode']=='')
		{
		alert('You need to provide city and state or postal code');
		return false;
		}
	if (flds['postalcode'] != '')
		{
		return true;
		}
	if (flds['city'] != '' && flds['state'] == '')
		{
		alert('When specifying a city, you must also specify a state.');
		return false;
		}
	if (flds['city'] == '' && flds['state'] != '')
		{
		alert('When specifying a state, you must also specify a city or postal code.');
		return false;
		}
	return true;
}
