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 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 validateAndSubmit( theForm )
{
	var vtable = "";
	var today = todaysDate();

	theForm.select.value = "select distinct ";

	comma = "";

	//vtable =  "virtualnet";
	//theForm.vtable.value =  "virtualnet";
	//if (theForm.resolvestation.checked || theForm.station.value != "") {
		//////if ( theForm.resolvestation.checked ){
			////vtable =  "virtualresolved";
			//theForm.vtable.value =  "virtualresolved";
		//}
	//}
			vtable =  "virtualresolved";
			theForm.vtable.value =  "virtualresolved";

	if ( theForm.vnetcheck.checked ) {
		theForm.select.value += comma+vtable+".name ";
		comma = ","; 
	}
	if ( theForm.vnetdcheck.checked ) {
		theForm.select.value += comma+"virtualdesc.description ";
		comma = ","; 
	}
	if ( theForm.networkcheck.checked ) {
		theForm.select.value += comma+vtable+".network ";
		comma = ","; 
	}
	if ( theForm.stationcheck.checked || theForm.resolvestation.checked ) {
		theForm.select.value += comma+vtable+".station ";
		comma = ","; 
	}
	if ( theForm.primarycheck.checked ) {
		theForm.select.value += comma+" primary_dc ";
		comma = ","; 
	}
	if ( theForm.secondarycheck.checked ) {
		theForm.select.value += comma+" secondary_dc ";
		comma = ","; 
	}
	if ( theForm.startcheck.checked ) {
		theForm.select.value += comma+vtable+".starttime ";
		comma = ","; 
	}
	if ( theForm.endcheck.checked ) {
		theForm.select.value += comma+vtable+".endtime ";
		comma = ","; 
	}
	if ( theForm.installcheck.checked ) {
		theForm.select.value += comma+vtable+".installdate ";
		comma = ","; 
	}
	if ( theForm.certcheck.checked ) {
		theForm.select.value += comma+vtable+".certdate ";
		comma = ","; 
	}
        if( theForm.sitecheck.checked ) {
                theForm.select.value += comma+ "station.site";
		comma = ","; 
	}


	if (comma != "") checked = true;
	else checked = 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";}

	theForm.installdate1.value = theForm.istartdate.value;
	theForm.installdate1.value = theForm.ienddate.value;
	if (theForm.ienddate.value == todaysDate()) {theForm.installdate2.value="none";}

	theForm.certdate1.value = theForm.cstartdate.value;
	theForm.certdate2.value = theForm.cenddate.value;
	if (theForm.cenddate.value == todaysDate()) {theForm.certdate2.value="none";}
	
	var conjunct = "";

	theForm.parms.value = "";
	var str = theForm.vnet.value;
	theForm.vnet.value = str.toUpperCase();
	var str = theForm.network.value;
	theForm.network.value = str.toUpperCase();
	str = theForm.station.value;
	theForm.station.value = str.toUpperCase();
	str = theForm.primarydc.value;
	theForm.primarydc.value = str.toUpperCase();
	str = theForm.secondarydc.value;
	theForm.secondarydc.value = str.toUpperCase();

	wherevnetd = "";
	fromvnetd = "";
	if ( theForm.vnetdcheck.checked ) {
		wherevnetd = " virtualdesc.name(+) = "+vtable+".name "
		fromvnetd = ",virtualdesc ";
	}

	if (theForm.site.value != "" || theForm.sitecheck.checked ) {
		// join with station 
		theForm.select.value += 
		" from "+vtable+", station "+fromvnetd;
		theForm.parms.value += "(station.station="+vtable+".station or "+vtable+".station='*') and station.network="+vtable+".network and station.starttime < "+vtable+".endtime and station.endtime > "+vtable+".starttime ";
		conjunct = " and ";
	} else 
		theForm.select.value += " from "+vtable + fromvnetd;


	/////////////////////////////////////////////////////
	//
	// 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 + "("+vtable+".starttime is null or "+
			  vtable+".starttime <= to_date('" + endtime + "','MM/DD/YYYY')) and ("
			+ vtable+".endtime is null or "+vtable+".endtime > to_date('" + starttime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + "("+vtable+".endtime is null or "+
			vtable+".endtime > to_date('" + starttime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + "("+".starttime is null or "+
			vtable+".starttime <= to_date('" + endtime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	starttime = theForm.installdate1.value;
	endtime   = theForm.installdate2.value;
	if( (starttime != "none") && (endtime != "none") )
	{
		theForm.parms.value += conjunct + 
			  "(installdate is null or (installdate <= to_date('" + endtime + "','MM/DD/YYYY') and "
			+ "installdate > to_date('" + starttime + "','MM/DD/YYYY')))";
		conjunct = " and ";
	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + 
			"(installdate is null or installdate > to_date('" + starttime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + 
			"(installdate is null or installdate <= to_date('" + endtime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	starttime = theForm.certdate1.value;
	endtime   = theForm.certdate2.value;
	if( (starttime != "none") && (endtime != "none") )
	{
		theForm.parms.value += conjunct + 
			  "(certdate is null or (certdate <= to_date('" + endtime + "','MM/DD/YYYY') and "
			+ "certdate > to_date('" + starttime + "','MM/DD/YYYY')))";
		conjunct = " and ";
	}
	else if( starttime != "none" )
	{
		theForm.parms.value += conjunct + 
			"(certdate is null or certdate > to_date('" + starttime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}
	else if( endtime != "none" )
	{
		theForm.parms.value += conjunct + 
			"(certdate is null or certdate <= to_date('" + endtime + "','MM/DD/YYYY'))";
		conjunct = " and ";
	}

        if( theForm.site.value != "" )
        {
                theForm.parms.value += conjunct +
                        " upper(station.site) like '%" + theForm.site.value.toUpperCase() + "%'";
                        conjunct = " and ";
        }
	if ( theForm.vnetd.value != "" ) {
		theForm.parms.value += conjunct +" upper(virtualdesc.description) like '%"+
			theForm.vnetd.value.toUpperCase() + "%'";
                        conjunct = " and ";
	}

	theForm.parms.value += conjunct + wherevnetd; 


	/////////////////////////////////////////////////////
	//
	// Format any constraints.
	//
	/////////////////////////////////////////////////////

	if (!checked )
	{
		alert("You have not checked a box to display." );
		return false;
	}

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