function checklat( lat )
{
	if (lat.value == "") return true;

	lt = parseFloat( lat.value );
	if( isNaN( lt ) == true )
	{
		alert("Invalid latitude entered." );
		lat.value = "";
		return false;
	}
	else if ( (lt < -90) || (lt > 90) )
	{
		alert("The latitude must be between -90 and 90." );
		lat.value = "";
		return false;
	}

	return true;
}
function checklon( lon )
{
	if (lon.value == "") return true;

	ln = parseFloat( lon.value );
	if( isNaN( ln ) == true )
	{
		alert("Invalid longitude entered." );
		lon.value = "";
		return false;
	}
	else if ( (ln < -180) || (ln > 180) )
	{
		alert("The longitude must be between -180 and 180." );
		lon.value = "";
		return false;
	}

	return true;
}

function viewResults( theForm )
{
	// need this function so that email is reset.
	theForm.email.value = "none";
        theForm.count.value = "0";
	validateAndSubmit( theForm );
	return true;
}

function emailResults( theForm )
{
	var email_addr = prompt("Email address to send results to:","");

	email_addr = email_addr.replace(/[`;]/g, "");
	if( email_addr == ""  || email_addr.match(/[@]/) == null) {
		alert("You must enter a valid email address to receive results.");
		return false;
	}
	if( email_addr == null ) return false;
	theForm.email.value = email_addr;
        theForm.count.value = "0";
	validateAndSubmit( theForm );
	return true;
}

function todaysDate () {

        var now = new Date();

        now.setTime(now.getTime());
        // format date MM/DD/YYYY
        var month = now.getMonth() + 1;
        return(month+'/'+now.getDate()+'/'+now.getFullYear());
}

function validateAndSubmit( theForm )
{
	theForm.label.value = "Event selection criteria";

       // set the select statement

	theForm.select.value = "select (rownum)\"GROUP_\", event.id, event.time, (event.latitude)\"LAT\", (event.longitude)\"LON\", event.depth, to_char(mag.magnitude,'9.9')\"MAG\", mag.type, (event.catalog)\"CATALOG\", (event.contributor)\"EVENT_CONTRIBUTOR\", (mag.contributor)\"MAG_CONTRIBUTOR\", to_char(region.code)\"CODE\", to_char(region.seismic_region)\"REGION\", region.region_text, event.id, time_ms from mag, event, region where rownum <= 50000 and event.region = region.code and event.id = mag.id(+)";

        theForm.starttime.value = theForm.startdate.value;
        theForm.endtime.value = theForm.enddate.value;
        if (theForm.endtime.value == todaysDate()) {theForm.endtime.value="none";}

	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 + " 23:59:59','MM/DD/YYYY HH24:MI:SS') and "
			+ "time > to_date('" + starttime + "','MM/DD/YYYY')";
		conjunct = " and ";
	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + 
			"time > to_date('" + starttime + "','MM/DD/YYYY')";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + 
			"time <= to_date('" + endtime + " 23:59:59','MM/DD/YYYY HH24:MI:SS')";
		conjunct = " and ";
	}



	/////////////////////////////////////////////////////
	//
	// Format any depth constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.depth1.value != "") && (theForm.depth2.value != "") )
	{
		theForm.parms.value += conjunct + "depth between " + 
			theForm.depth1.value + " and " + theForm.depth2.value;
		theForm.label.value += ":depth between " +
			theForm.depth1.value + " and " + theForm.depth2.value;
		conjunct = " and ";

	}
	else if( theForm.depth1.value != "" ) 
	{
		theForm.parms.value += conjunct + "depth >= " + theForm.depth1.value;
		theForm.label.value += ":depth >= " + theForm.depth1.value;
		conjunct = " and ";
	}
	else if( theForm.depth2.value != "" ) 
	{
		theForm.parms.value += conjunct + "depth <= " + theForm.depth2.value;
		theForm.label.value += ":depth <= " + theForm.depth1.value;
		conjunct = " and ";
	}

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

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

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


                theForm.label.value += ":longitude between " +
                        theForm.lon1.value + " and " + theForm.lon2.value;
                conjunct = " and ";
        }
        else if( theForm.lon1.value != "" )
        {
                theForm.parms.value += conjunct + "longitude >= " + theForm.lon1
                theForm.label.value += ":longitude >= " + theForm.lon1
.value;
                conjunct = " and ";
        }
        else if( theForm.lon2.value != "" )
        {
                theForm.parms.value += conjunct + "longitude <= " + theForm.lon2
                theForm.label.value += ":longitude <= " + theForm.lon2
.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;
		theForm.label.value += ":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;
		theForm.label.value += ":magnitude >= " + theForm.mag1.value;
		conjunct = " and ";
	}
	else if( theForm.mag2.value != "" ) 
	{
		theForm.parms.value += conjunct + "mag.magnitude <= " + theForm.mag2.value;
		theForm.label.value += ":magnitude <= " + theForm.mag2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format mag type parameter
	//
	/////////////////////////////////////////////////////
	var mag_string = "";
	if( theForm.magtype.options[0].selected == false )
	{ 
		for( var i=1; i < theForm.magtype.length; i++)
		{
			if( theForm.magtype.options[i].selected)
			{
				mag_string = theForm.magtype.options[i].text;
				break;
			}
		}
		// alert("mag_string = " + mag_string);
		theForm.parms.value += conjunct + " mag.type = '" + mag_string + "'";
		theForm.label.value += ":magnitude type(s) = '" + mag_string + "'";
		conjunct = " and ";
	}


	/////////////////////////////////////////////////////
	//
	// Format catalog parameter
	//
	/////////////////////////////////////////////////////
	var catalog = "";
        catconjunct = conjunct + " (";
	if ((theForm.catalog.options[0].selected) && (theForm.catalog.options[1].selected)) {
		alert("ERROR - both \"ALL\" and \"PREFERRED\" selected"); 
		return false;
	}	
	cataloglabel="";
	for( var i=2; i < theForm.catalog.length; i++)
	{
		if( theForm.catalog.options[i].selected)
		{
			if (theForm.catalog.options[0].selected) {
				alert("ERROR - both \"ALL\" and \""+theForm.catalog.options[i].text +"\" selected"); 
				return false;
			}	
			if (theForm.catalog.options[1].selected) {
				alert("ERROR - both \"PREFERRED\" and \""+theForm.catalog.options[i].text +"\" selected"); 
				return false;
			}	
	
			catalog += catconjunct + " event.catalog = '" + 
				theForm.catalog.options[i].text + "'";
			cataloglabel += ":catalog = "+theForm.catalog.options[i].text; 
                	catconjunct = " or ";
                	conjunct = " and ";
	
		}
	}
	if (catalog != "") {
		catalog += ")";
		theForm.parms.value += catalog ;
		theForm.label.value += cataloglabel;
	} else if (theForm.catalog.options[1].selected) {
		theForm.parms.value += catalog ;
		theForm.label.value += ":catalog PREFERRED";
                conjunct = " and ";
	}

	if (theForm.contributor.value != "") {
		var str = theForm.contributor.value;
                theForm.contributor.value = str.toUpperCase();
		var contributor =theForm.contributor.value.replace(/,\s+/g,",");
                contributor = contributor.replace(/\s+/g,",");

		var list = contributor.split(",");

		theForm.parms.value += conjunct + " (event.contributor in ('"+ list[0] + "'" ;
		theForm.label.value += ":contributor in '"+ list[0] + "'" ;
		for (i = 1; i < list.length; i++) {
			theForm.parms.value += ",'" + list[i] + "'" ;
			theForm.label.value += ",'" + list[i] + "'" ;
		}

		theForm.parms.value += ") or mag.contributor in ('" + list[0] + "'";
		for (i = 1; i < list.length; i++) {
			theForm.parms.value += ",'" + list[i] + "'" ;
			theForm.label.value += ",'" + list[i] + "'" ;
		}
		theForm.parms.value += ")) ";

		conjunct = " and ";

	}


	/////////////////////////////////////////////////////
	//
	// Format any region constraints. (REGION.REGION TEXT)
	//
	/////////////////////////////////////////////////////
	if( theForm.region.value != "" ) 
	{
		theForm.parms.value += conjunct +
	        " upper(region.region_text) like '%" + theForm.region.value.toUpperCase() + "%'";
                conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any region constraints. (REGION.CODE 1-729)
	//
	/////////////////////////////////////////////////////
	if( theForm.code.value != "" ) 
	{
		theForm.parms.value += conjunct + " region.code = " + theForm.code.value;
		theForm.label.value += ":code = " + theForm.code.value;
                conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any region constraints. (REGION.SEISMIC 1-50)
	//
	/////////////////////////////////////////////////////
	if( theForm.seismic.value != "" ) 
	{
		theForm.parms.value += conjunct + " region.seismic_region = " + theForm.seismic.value;
		theForm.label.value += ":region = " + theForm.seismic.value;
                conjunct = " and ";
	}
	if (theForm.parms.value == "") {	
		alert("Please restrict your query with some constraints.");
		return false;
	}

        theForm.parms.value += " order by event.time desc ";

	// if any delta's have been set, set both...
        if( (theForm.time_delta.value != "") && (theForm.dist_delta.value == "") ) theForm.dist_delta.value = 0;
        if( (theForm.dist_delta.value != "") && (theForm.time_delta.value == "") ) theForm.time_delta.value = 0;
	if (theForm.catalog.options[1].selected) {
		theForm.parms.value = theForm.parms.value.replace(/event/g,"preferred_event");
		theForm.select.value = theForm.select.value.replace(/event/g,"preferred_event");
	}

	theForm.submit();
	return true;

}
