function checkradius(radius)
{
	if (radius.value == "") return true;
	r = parseFloat( radius.value );
	if( isNaN( r ) == true || (r <= 0) )
	{
		alert("Radius must be a valid float > 0." );
		return false;
	}
}
function validateAndSubmit( theForm )
{       // set the select statement
	if( theForm.count.value == 1 )
	{
		theForm.select.value = "select distinct count(preferred_event.id) from preferred_event, mag where preferred_event.id = mag.id";
	}
	else
	{
		theForm.select.value = "select to_char(preferred_event.time,'YYYY/MM/DD HH24:MI:SS')\"TIME\", rpad(preferred_event.latitude, 8)\"LAT\", rpad(preferred_event.longitude, 9)\"LON\", rpad(preferred_event.depth,5)\"DEP\", to_char(mag.magnitude,'9.9')\"MAG\", mag.type, preferred_event.catalog, rpad(region.region_text,26)\"REGION\", preferred_event.time_ms from mag, preferred_event, region where preferred_event.region = region.code and preferred_event.id = mag.id(+) ";

	}
	if( checkradius( theForm.radius ) == false ) return false;

	// Check for starttime parameters
	if( (theForm.year1.value == "") || (theForm.year1.value == "YYYY") ) {
		alert("A start time is required.");
		return false;
	} else
	{
		if( checkyear( theForm.year1 ) == false ) return false;
		else if( checkstartmonth( theForm.month1 ) == false )   return false;
		else if( checkstartday( theForm.day1 ) == false )       return false;
		theForm.starttime.value = 
			theForm.month1.value + "/" + theForm.day1.value + "/" + theForm.year1.value;

		if ( checkstarthhmmss(theForm.hhmmss1) == false ) return false;
		else {
			theForm.starttime.value += theForm.hhmmss1.value; 
		}
	}
	// Check for endtime parameters
	if( (theForm.year2.value == "") || (theForm.year2.value == "YYYY") )
		theForm.endtime.value = "none";
	else
	{
		if( checkyear( theForm.year2 ) == false )          return false;
		else if( checkendmonth( theForm.month2 ) == false )   return false;
		else if( checkendday( theForm.day2 ) == false )       return false;
		theForm.endtime.value = 
			theForm.month2.value + "/" + theForm.day2.value + "/" + theForm.year2.value;
		if ( checkendhhmmss(theForm.hhmmss2 ) == false ) return false;
		else {
			theForm.endtime.value += theForm.hhmmss2.value; 
		}
	}
	
	var conjunct = "";

	 theForm.parms.value = "";

	// var str = theForm.region.value;
	// theForm.region.value = str.toUpperCase();

	/////////////////////////////////////////////////////
	//
	// Format any start and end time constraints.
	//
	/////////////////////////////////////////////////////
	var starttime = theForm.starttime.value;
	var endtime   = theForm.endtime.value;
	if( (starttime != "none") && (endtime != "none") )
	{
		theForm.parms.value += conjunct + 
			  "time <= to_date('" + endtime + "','MM/DD/YYYYHH24MISS') and "
			+ "time > to_date('" + starttime + "','MM/DD/YYYYHH24MISS')";
		conjunct = " and ";
	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + 
			"time > to_date('" + starttime + "','MM/DD/YYYYHH24MISS')";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + 
			"time <= to_date('" + endtime + "','MM/DD/YYYYHH24MISS')";
		conjunct = " and ";
	}

	theForm.SOUTH.value = "-90";
	theForm.NORTH.value = "90";
	theForm.WEST.value = "-180";
	theForm.EAST.value = "180";

	// adjust lat/lon if radius is defined
	if (theForm.lon1.value != "" && theForm.lat1.value != "" &&
			theForm.radius.value != "" ) {
		var lon1 = parseFloat(theForm.lon1.value);
		var lat1 = parseFloat(theForm.lat1.value);
		var radius = parseFloat(theForm.radius.value);

		if (lon1 < -180) lon1 = -180;
		if (lat1 < -90) lat1 = -90;

		theForm.lon1.value = String(lon1);

		theForm.lat1.value = String(lat1);

		theForm.parms.value += conjunct + 
			" (pdistGeoG(preferred_event.latitude,preferred_event.longitude," +
			theForm.lat1.value + "," + theForm.lon1.value + ",0," +
			String(radius) + ")=1)";

                conjunct = " and ";

	} else {

	/////////////////////////////////////////////////////
	//
	// Format any latitude constraints.
	//
	/////////////////////////////////////////////////////
	if  (theForm.lat1.value != "" && theForm.lat1.value != "-90"
		&& theForm.lat2.value != "" && theForm.lat2.value != "90")
	{
		theForm.parms.value += conjunct + "latitude between " + 
			theForm.lat1.value + " and " + theForm.lat2.value;
		conjunct = " and ";
		theForm.SOUTH.value = theForm.lat1.value;
		theForm.NORTH.value = theForm.lat2.value;
	}
	else if( theForm.lat1.value != "" && theForm.lat1.value != "-90")
	{
		theForm.parms.value += conjunct + "latitude >= " + theForm.lat1.value;
		conjunct = " and ";
		theForm.SOUTH.value = theForm.lat1.value;
	}
	else if( theForm.lat2.value != "" && theForm.lat2.value != "90")
	{
		theForm.parms.value += conjunct + "latitude <= " + theForm.lat2.value;
		conjunct = " and ";
		theForm.NORTH.value = theForm.lat2.value;
	}

	/////////////////////////////////////////////////////
	//
	// Format any longitude constraints.
	//
	/////////////////////////////////////////////////////
	if(theForm.lon1.value != "" && theForm.lon1.value != "-180"
		 && theForm.lon2.value != "" && theForm.lon2.value != "180")
	{
		//theForm.parms.value += conjunct + "longitude between " + 
			//theForm.lon1.value + " and " + theForm.lon2.value;

theForm.parms.value += conjunct +
"(("+theForm.lon1.value + "<=" + theForm.lon2.value +
" and longitude between "+theForm.lon1.value+" and "+theForm.lon2.value+
") or ("+theForm.lon1.value+" > "+theForm.lon2.value+" and ("+
theForm.lon1.value + "<= longitude or longitude <= "+theForm.lon2.value+")))";

		conjunct = " and ";

		theForm.WEST.value = theForm.lon1.value;
		theForm.EAST.value = theForm.lon2.value;
	}
	else if( theForm.lon1.value != "" && theForm.lon1.value != "-180")
	{
		theForm.parms.value += conjunct + "longitude >= " + theForm.lon1.value;
		conjunct = " and ";
		theForm.WEST.value = theForm.lon1.value;
	}
	else if( theForm.lon2.value != ""  && theForm.lon2.value != "180")
	{
		theForm.parms.value += conjunct + "longitude <= " + theForm.lon2.value;
		conjunct = " and ";
		theForm.EAST.value = theForm.lon2.value;
	}
}

	/////////////////////////////////////////////////////
	//
	// Format any depth constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.depth1.value != "") && (theForm.depth2.value != "") )
	{
		theForm.parms.value += conjunct + "depth between " + 
			theForm.depth1.value + " and " + theForm.depth2.value;
		conjunct = " and ";
	}
	else if( theForm.depth1.value != "" && theForm.depth1.value != "0") 
	{
		theForm.parms.value += conjunct + "depth >= " + theForm.depth1.value;
		conjunct = " and ";
	}
	else if( theForm.depth2.value != "" ) 
	{
		theForm.parms.value += conjunct + "depth <= " + theForm.depth2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any magnitude constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.mag1.value != "") && (theForm.mag2.value != "") )
	{
		theForm.parms.value += conjunct + "mag.magnitude between " + 
			theForm.mag1.value + " and " + theForm.mag2.value;
		conjunct = " and ";
	}
	else if( theForm.mag1.value != "" ) 
	{
		theForm.parms.value += conjunct + "mag.magnitude >= " + theForm.mag1.value;
		conjunct = " and ";
	}
	else if( theForm.mag2.value != "" ) 
	{
		theForm.parms.value += conjunct + "mag.magnitude <= " + theForm.mag2.value;
		conjunct = " and ";
	}

        conjunct = " and ";

	// get preferred preferred_event info

	if (theForm.parms.value == "") {       
		alert("Please restrict your query with some constraints.");
		return false;
	}

        theForm.parms.value += " order by preferred_event.time desc ";
	theForm.submit();
	return true;

}
