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.select.value = "select distinct ";

	if( theForm.cvnet.checked)
             theForm.select.value = theForm.select.value + "virtualresolved.name,";

	if( theForm.cnet.checked )
		theForm.select.value = theForm.select.value + "station.network,";
	if( theForm.csta.checked )
		theForm.select.value = theForm.select.value + "station.station,";

	if( theForm.cstm.checked ) {
		//if (theForm.vnet.value != "")
			//theForm.select.value = theForm.select.value + "virtualresolved.starttime,";
		//else
			theForm.select.value = theForm.select.value + "station.starttime,";
	}

	if( theForm.cetm.checked ) {
		//if (theForm.vnet.value != "")
			//theForm.select.value = theForm.select.value + "virtualresolved.endtime,";
		//else 
			theForm.select.value = theForm.select.value + "station.endtime,";
	}
	if( theForm.clat.checked )
		theForm.select.value = theForm.select.value + "to_char(station.latitude,'S00.999999') \"LAT\",";
	if( theForm.clon.checked )
		theForm.select.value = theForm.select.value + "to_char(station.longitude,'S000.999999') \"LON\",";
	if( theForm.cele.checked )
		theForm.select.value = theForm.select.value + "station.elevation,";
	if( theForm.csite.checked )
		theForm.select.value = theForm.select.value + "station.site,";
	if( theForm.cnetaff.checked )
		theForm.select.value = theForm.select.value + "b33.text,";

	if( theForm.select.value == "select distinct " )
	{
		alert("You have not checked any select fields.");
		return false;
	}

	var strlen = theForm.select.value.length - 1;
	theForm.select.value = theForm.select.value.substring(0,strlen);

	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.network.value;
	theForm.network.value = str.toUpperCase();
	str = theForm.station.value;
	theForm.station.value = str.toUpperCase();

////added by Deb 4/9/99

//	var str = theForm.site.value;
// 	theForm.site.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 + 
			  "station.starttime <= to_date('" + endtime + "','MM/DD/YYYY') and "
			+ "station.endtime > to_date('" + starttime + "','MM/DD/YYYY')";
		conjunct = " and ";

		if (theForm.cvnet.checked || theForm.vnet.value != "")
                        theForm.parms.value += conjunct +
		"virtualresolved.starttime <= to_date('" + endtime + "','MM/DD/YYYY') and "
		+ "virtualresolved.endtime > to_date('" + starttime + "','MM/DD/YYYY')";

	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + 
			"station.endtime > to_date('" + starttime + "','MM/DD/YYYY')";
		conjunct = " and ";
           if (theForm.cvnet.checked || theForm.vnet.value != "")
                        theForm.parms.value += conjunct +
              "virtualresolved.endtime > to_date('" + starttime + "','MM/DD/YYYY')";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + 
			"station.starttime <= to_date('" + endtime + "','MM/DD/YYYY')";
		conjunct = " and ";
		if (theForm.cvnet.checked || theForm.vnet.value != "")
			theForm.parms.value += conjunct +
			"virtualresolved.starttime <= to_date('" + endtime + "','MM/DD/YYYY')";


	}

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

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

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

		conjunct = " and ";
	}
	else if( theForm.lon1.value != "" ) 
	{
		theForm.parms.value += conjunct + "station.longitude >= " + theForm.lon1.value;
		conjunct = " and ";
	}
	else if( theForm.lon2.value != "" ) 
	{
		theForm.parms.value += conjunct + "station.longitude <= " + theForm.lon2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any elevation constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.elev1.value != "") && (theForm.elev2.value != "") )
	{
		theForm.parms.value += conjunct + "station.elevation between " + 
			theForm.elev1.value + " and " + theForm.elev2.value;
		conjunct = " and ";
	}
	else if( theForm.elev1.value != "" ) 
	{
		theForm.parms.value += conjunct + "station.elevation >= " + theForm.elev1.value;
		conjunct = " and ";
	}
	else if( theForm.elev2.value != "" ) 
	{
		theForm.parms.value += conjunct + "station.elevation <= " + theForm.elev2.value;
		conjunct = " and ";
	}
	/////////////////////////////////////////////////////
	//
	// Format any site constraints.
	//
	/////////////////////////////////////////////////////
	if( theForm.site.value != "" ) 
	{
		theForm.parms.value += conjunct +
			" upper(station.site) like '%" + theForm.site.value.toUpperCase() + "%'";
                        conjunct = " and ";
	}
	// mod sue 4/22/04 was preventing stations with null sites from printing
	//else
	//{
		//theForm.parms.value += conjunct +
			//" site like '%'";
                        //conjunct = " and ";
	//}
	/////////////////////////////////////////////////////
	//
	// Format any network affiliation constraints.
	//
	/////////////////////////////////////////////////////
	if( theForm.netaff.value != "" ) 
	{
		theForm.parms.value += conjunct +
			" upper(b33.text) like '%" + theForm.netaff.value.toUpperCase() + "%'";
	}

	if( ( theForm.parms.value == "" ) 
 	 && ( theForm.vnet.value == "" )
	 && ( theForm.network.value == "" ) 
	 && ( theForm.station.value == "" ) 
	)
	{
		alert("You have not supplied enough search criteria." );
		return false;
	}
	
	theForm.submit();
	return true;
}
