// http://www.isolani.co.uk/articles/simpleJavascriptAndForms.html was quite useful in creating this validation code
function validate(f)
{
	// Check to see if all fields are blank
	if (f.latitude.value == "" && f.longitude.value == "" && f.zone.value == "" && f.easting.value == "" && f.northing.value == "" && f.mgrs.value == "")
	{
		printerr("All fields are blank.");
		return(false);
	}
	
	// Let's check the latitude and longitude. Since all the supported locations
	// are in the US anyways I'm going to do a little data verification that you're
	// north of the equator and in the western hemisphere.	
	if (f.latitude.value != "")
	{
		var lat = parseFloat(f.latitude.value);
		if (isNaN(lat))
		{
			printerr("Latitude was not entered as a number.");
			return(false);
		}
		if (lat < 0.0 || lat > 90.0)
		{
			printerr("Invalid latitude (<i>expected 0.0 =< Latitude =< 90.0<\/i>).");
			return(false);
		}
	}
	
	if (f.longitude.value != "")
	{
		var lon = parseFloat(f.longitude.value);
		if (isNaN(lon))
		{
			printerr("Longitude was not entered as a number.");
			return(false);
		}
		if (lon > 0.0 || lon < -180.0)
		{
			printerr("Invalid longitude (<i>expected -180.0 =< Longitude =< 0.0<\/i>).");
			return(false);
		}
		else //if you've made it to here, that means both your latitude and longitude were fine and we're ready to submit.
		{
			return(true);
		}
	}
	
	// Now let's go through the UTM grids.
	if (f.zone.value != "" && f.easting.value != "" && f.northing.value != "")
	{
		// I know that's a bear of a conditional statement. Basically, should be three digits, first two numeric, third should be 'N' or 'S'.
		if (!(f.zone.value.length == 3 && (f.zone.value.charAt(0) >= '0' && f.zone.value.charAt(0) <= '9') && (f.zone.value.charAt(1) >= '0' && f.zone.value.charAt(1) <= '9') && (f.zone.value.charAt(2) == 'N' || f.zone.value.charAt(2) <= 'S')))
		{
			printerr("Invalid UTM zone (<i>expected two digits and 'N' or 'S', i.e. 17S<\/i>).");
			return(false);
		}
		var easting_v = parseFloat(f.easting.value);
		if (isNaN(easting_v))
		{
			printerr("Invalid UTM easting.");
			return(false);
		}
		var northing_v = parseFloat(f.northing.value);
		if (isNaN(northing_v))
		{
			printerr("Invalid UTM northing.");
			return(false);
		}
		else // like line 53 of this program - if you made it here you're good to go.
		{
			return(true);
		}
	}
	
	// special case - check if one or two UTM inputs are filled by not all three
	var utm_total = 0;
	if (f.zone.value == "")
	{
		utm_total++;
	}
	if (f.easting.value == "")
	{
		utm_total++;
	}
	if (f.northing.value == "")
	{
		utm_total++;
	}
	if (utm_total < 3 && utm_total > 0)
	{
		printerr("One or more UTM fields have been left blank.");
		return(false);
	}
	
	var tenDigitGrid_s = f.mgrs.value.substring(5, 14);
	var tenDigitGrid_f = parseInt(tenDigitGrid_s);
	
	// Last the MGRS
	if (f.mgrs.value != "" && f.mgrs.value.length != 15)
	{
		printerr("MGRS coordinate must be a 10-digit grid (<i>i.e. 17SPU4321098765<\/i>).");
		return(false);
	}
	// check the zone exactly the same way we checked the UTM zone
	else if (!((f.mgrs.value.charAt(0) >= '0' && f.mgrs.value.charAt(0) <= '9') && (f.mgrs.value.charAt(1) >= '0' && f.mgrs.value.charAt(1) <= '9') && (f.mgrs.value.charAt(2) == 'N' || f.mgrs.value.charAt(2) <= 'S')))
	{
		printerr("Invalid MGRS zone (<i>i.e. <b>17S<\/b>PU4321098765<\/i>).");
		return(false);
	}
	// check the two-letter prefix
	else if (!(((f.mgrs.value.charAt(3) >= 'A') && (f.mgrs.value.charAt(3) <= 'Z')) && ((f.mgrs.value.charAt(4) >= 'A') && (f.mgrs.value.charAt(4) <= 'Z'))))
	{
		printerr("Invalid MGRS prefix (<i>i.e. 17S<b>PU<\/b>4321098765<\/i>).");
		return(false);
	}
	// check the 10-digit grid
	else if (isNaN(tenDigitGrid_f))
	{
		printerr("Invalid MGRS grid (<i>i.e. 17SPU<b>4321098765<\/b><\/i>).");
		return(false);
	}
	else // MGRS coordinate made it all the way through validation and is ready to be submitted
	{
		return(true);
	}
	
	printerr("Input validation failed for an unknown reason. Please submit a bug report to the developer.");
	return(false);
}

function printerr (string)
{
	document.getElementById('errorData').innerHTML = "<font color='red'>" + string + "<\/font>";
}