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";
	var conjunct = "";

	theForm.parms.value = "";

	var contributor = "";
        conconjunct = conjunct + " (";
	contributorlabel="";
	for( var i=1; i < theForm.contributor.length; i++)
	{
		if( theForm.contributor.options[i].selected)
		{
			contributor += conconjunct + " origin.contributor = '" + 
				theForm.contributor.options[i].text + "'";
			contributorlabel += ":contributor = "+theForm.contributor.options[i].text; 
                	conconjunct = " or ";
                	conjunct = " and ";
	
		}
	}
	if (contributor != "") {
		contributor += ")";
		theForm.parms.value += contributor ;
		theForm.label.value += contributorlabel;
	}

       // set the select statement
	if (theForm.primary.checked == true && contributor == "") {
        	theForm.label.value += ":primary";
		// primary only 
		theForm.select.value = "select (origin.eventid)\"EVENT\", (origin.id)\"ORIGIN\",(primevent.origin)\"PRIMARY\", (to_char(origin.time,'YYYY-MM-DD HH24:MI:SS'))\"TIME\",(origin.latitude)\"LAT\", (origin.longitude)\"LON\", origin.depth, to_char(originmag.magnitude,'9.9')\"MAG\", originmag.type, (origin.catalog)\"CATALOG\", (origin.contributor)\"ORIGIN_CONTRIBUTOR\", (originmag.author)\"MAG_AUTHOR\", to_char(region.code)\"CODE\", to_char(region.seismic_region)\"REGION\", region.region_text, origin.id, (to_char(origin.time,'FF'))\"MS\" from origin, originmag, primevent, region where primevent.origin=origin.id and origin.region = region.code and origin.id = originmag.originid(+)";
	} else if (theForm.primary.checked == true && theForm.contributor.value != "") {
        	theForm.label.value += ":primary";

		theForm.select.value = "select (origin.eventid)\"EVENT\", (origin.id)\"ORIGIN\",(primevent.origin)\"PRIMARY\",(to_char(origin.time,'YYYY-MM-DD HH24:MI:SS'))\"TIME\",(origin.latitude)\"LAT\", (origin.longitude)\"LON\", origin.depth, to_char(originmag.magnitude,'9.9')\"MAG\", originmag.type, (origin.catalog)\"CATALOG\", (origin.contributor)\"ORIGIN_CONTRIBUTOR\", (originmag.author)\"MAG_AUTHOR\", to_char(region.code)\"CODE\", to_char(region.seismic_region)\"REGION\", region.region_text, origin.id, (to_char(origin.time,'FF'))\"MS\" from origin, originmag, primevent, region where origin.primaryid_contributor=origin.id and origin.region = region.code and origin.id = originmag.originid(+)";
	} else {
        	theForm.label.value += ":all origins";
	theForm.select.value = "select (origin.eventid)\"EVENT\", (origin.id)\"ORIGIN\",(primevent.origin)\"PRIMARY\",(to_char(origin.time,'YYYY-MM-DD HH24:MI:SS'))\"TIME\",(origin.latitude)\"LAT\", (origin.longitude)\"LON\", origin.depth, to_char(originmag.magnitude,'9.9')\"MAG\", originmag.type, (origin.catalog)\"CATALOG\", (origin.contributor)\"ORIGIN_CONTRIBUTOR\", (originmag.author)\"MAG_AUTHOR\", to_char(region.code)\"CODE\", to_char(region.seismic_region)\"REGION\", region.region_text, origin.id, (to_char(origin.time,'FF'))\"MS\" from origin, originmag, primevent, region where origin.id=primevent.origin(+) and origin.region = region.code and origin.id = originmag.originid(+)";
	}

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

	 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.label.value += ":starttime " + starttime;
        theForm.label.value += ":endtime " + endtime;
		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.label.value += ":starttime " + starttime;
		theForm.parms.value += conjunct + 
			"time > to_date('" + starttime + "','MM/DD/YYYY')";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
        theForm.label.value += ":endtime " + endtime;
		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 origin.longitude between "+theForm.lon1.value+" and "+theForm.lon2.value+
") or ("+theForm.lon1.value+" > "+theForm.lon2.value+" and ("+
theForm.lon1.value + "<= origin.longitude or origin.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 + "originmag.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 + "originmag.magnitude >= " + theForm.mag1.value;
		theForm.label.value += ":magnitude >= " + theForm.mag1.value;
		conjunct = " and ";
	}
	else if( theForm.mag2.value != "" ) 
	{
		theForm.parms.value += conjunct + "originmag.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 + " upper(originmag.type) = '" + mag_string + "'";
		theForm.label.value += ":upper(magnitude type(s)) = '" + mag_string + "'";
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format catalog parameter
	//
	/////////////////////////////////////////////////////
	var catalog = "";
        catconjunct = conjunct + " (";
	cataloglabel="";
	for( var i=1; i < theForm.catalog.length; i++)
	{
		if( theForm.catalog.options[i].selected)
		{
			catalog += catconjunct + " origin.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;
	}



	/////////////////////////////////////////////////////
	//
	// 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;
	}

	if (theForm.orderby.checked == true)
	// order by origin.id to keep originmags together 
	{
        	theForm.parms.value += " order by origin.time desc, origin.id desc, origin.eventid";
		theForm.label.value += ":order by time desc ";
	} else {
        	theForm.parms.value += " order by origin.time asc, origin.id desc, origin.eventid";
		theForm.label.value += ":order by time asc ";
	}

	theForm.submit();
	return true;

}

