function openWin(){
aWindow=window.open("wildcards.html","thewindow","toolbar=no,width=350,height=400,status=no,scrollbars=yes,resize=yes,menubar=no");
}
function checkaz( az )
{
        if (az.value == "") return true;

        a = parseFloat( az.value );
        if( isNaN( a ) == true )
        {
                alert("Invalid azimuth entered." );
                az.value = "";
                return false;
        }
        else if ( (a < 0) || (a > 360) )
        {
                alert("The azimuth must be between 0 and 360." );
                az.value = ""; 
		return false;
        }

        return true;
}
function checkdip( dip )
{
        if (dip.value == "") return true;

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

        return true;
}


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());
        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 + "channel.network,";
	if( theForm.csta.checked )
		theForm.select.value = theForm.select.value + "channel.station,";
	if( theForm.cloc.checked )
		theForm.select.value = theForm.select.value + "channel.location,";
	if( theForm.ccha.checked )
		theForm.select.value = theForm.select.value + "channel.channel,";
	if( theForm.cstm.checked ) {
                //if (theForm.vnet.value != "")
                        //theForm.select.value = theForm.select.value + "virtualresolved.starttime,";
                //else
                        theForm.select.value = theForm.select.value + "channel.starttime,";
        }
        if( theForm.cetm.checked ) {
                //if (theForm.vnet.value != "")
                        //theForm.select.value = theForm.select.value + "virtualresolved.endtime,";
                //else 
                        theForm.select.value = theForm.select.value + "channel.endtime,";
        }
	if( theForm.clat.checked )
		theForm.select.value = theForm.select.value + "to_char(channel.latitude,'S00.999999') \"LAT\",";
	if( theForm.clon.checked )
		theForm.select.value = theForm.select.value + "to_char(channel.longitude,'S000.999999') \"LON\",";
	if( theForm.cele.checked )
		theForm.select.value = theForm.select.value + "channel.elevation,";
	if( theForm.cdep.checked )
		theForm.select.value = theForm.select.value + "channel.depth,";
	if( theForm.cazi.checked )
		theForm.select.value = theForm.select.value + "channel.azimuth,";
	if( theForm.cdip.checked )
		theForm.select.value = theForm.select.value + "channel.dip,";
	if( theForm.csam.checked )
		theForm.select.value = theForm.select.value + "channel.sample_rate,";
	if( theForm.csen.checked )
		theForm.select.value = theForm.select.value + "b33.text,";
	if( theForm.cflag.checked )
		theForm.select.value = theForm.select.value + "channel.flags,";

	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();
	str = theForm.location.value;
	theForm.location.value = str.toUpperCase();
	str = theForm.channel.value;
	theForm.channel.value = str.toUpperCase();
	str = theForm.flags.value;
	theForm.flags.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 + 
			  "channel.starttime <= to_date('" + endtime + "','MM/DD/YYYY') and "
			+ "channel.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 + 
			"channel.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 + 
			"channel.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 + "latitude between " + 
			theForm.lat1.value + " and " + theForm.lat2.value;
		conjunct = " and ";
	}
	else if( theForm.lat1.value != "" ) 
	{
		theForm.parms.value += conjunct + "latitude >= " + theForm.lat1.value;
		conjunct = " and ";
	}
	else if( theForm.lat2.value != "" ) 
	{
		theForm.parms.value += conjunct + "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 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 ";
	}
	else if( theForm.lon1.value != "" ) 
	{
		theForm.parms.value += conjunct + "longitude >= " + theForm.lon1.value;
		conjunct = " and ";
	}
	else if( theForm.lon2.value != "" ) 
	{
		theForm.parms.value += conjunct + "longitude <= " + theForm.lon2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any elevation constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.elev1.value != "") && (theForm.elev2.value != "") )
	{
		theForm.parms.value += conjunct + "elevation between " + 
			theForm.elev1.value + " and " + theForm.elev2.value;
		conjunct = " and ";
	}
	else if( theForm.elev1.value != "" ) 
	{
		theForm.parms.value += conjunct + "elevation >= " + theForm.elev1.value;
		conjunct = " and ";
	}
	else if( theForm.elev2.value != "" ) 
	{
		theForm.parms.value += conjunct + "elevation <= " + theForm.elev2.value;
		conjunct = " and ";
	}

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

	/////////////////////////////////////////////////////
	//
	// Format any azimuth constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.az1.value != "") && (theForm.az2.value != "") )
	{
		theForm.parms.value += conjunct + "azimuth between " + 
			theForm.az1.value + " and " + theForm.az2.value;
		conjunct = " and ";
	}
	else if( theForm.az1.value != "" ) 
	{
		theForm.parms.value += conjunct + "azimuth >= " + theForm.az1.value;
		conjunct = " and ";
	}
	else if( theForm.az2.value != "" ) 
	{
		theForm.parms.value += conjunct + "azimuth <= " + theForm.az2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any dip constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.dip1.value != "") && (theForm.dip2.value != "") )
	{
		theForm.parms.value += conjunct + "dip between " +
			theForm.dip1.value + " and " + theForm.dip2.value;
		conjunct = " and ";
	}
	else if( theForm.dip1.value != "" ) 
	{
		theForm.parms.value += conjunct + "dip >= " + theForm.dip1.value;
		conjunct = " and ";
	}
	else if( theForm.dip2.value != "" ) 
	{
		theForm.parms.value += conjunct + "dip <= " + theForm.dip2.value;
		conjunct = " and ";
	}

	/////////////////////////////////////////////////////
	//
	// Format any sample rate constraints.
	//
	/////////////////////////////////////////////////////
	if( (theForm.sam1.value != "") && (theForm.sam2.value != "") )
	{
		theForm.parms.value += conjunct + "sample_rate between " +
			theForm.sam1.value + " and " + theForm.sam2.value;
		conjunct = " and ";
	}
	else if( theForm.sam1.value != "" ) 
	{
		theForm.parms.value += conjunct + "sample_rate >= " +theForm.sam1.value;
		conjunct = " and ";
	}
	else if( theForm.sam2.value != "" ) 
	{
		theForm.parms.value += conjunct + "sample_rate <= " +theForm.sam2.value;
		conjunct = " and ";
	}

	if( theForm.sens.value != "" ) 
	{
		theForm.parms.value += conjunct +
			" upper(b33.text) like '%" + theForm.sens.value.toUpperCase() + "%'";
	}

	if( theForm.flags.value != "" )
	{
		theForm.parms.value += conjunct +
			" upper(flags) like '%" + theForm.flags.value.toUpperCase() + "%'";
	}	

	if( ( theForm.parms.value == "" ) 
	 && ( theForm.vnet.value == "" ) 
	 && ( theForm.network.value == "" ) 
	 && ( theForm.station.value == "" ) 
	 && ( theForm.location.value == "" ) 
	 && ( theForm.channel.value == "" ) 
	)
	{
		alert("You have not supplied enough search criteria. You must set at least one parameter in the network, station OR channel box" );
		return false;
	}
	
	theForm.submit();
	return true;
}
