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 )
{
	if( theForm.choice[1].checked ) theForm.comment_type.value = "channel";
	else theForm.comment_type.value = "station";

	theForm.select.value = "select distinct network, station, ";
	if( theForm.comment_type.value == "channel" )
	{
		theForm.select.value += "channel,";
	}
	theForm.select.value += "starttime, endtime, b31.text ";
	if( theForm.comment_type.value == "station" )
	{
		theForm.select.value += "from station_comments, ";
	}
	else
	{
		theForm.select.value += "from channel_comments, ";
	}
	theForm.select.value += "b31 where b31_id = b31.id and ";

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


	/////////////////////////////////////////////////////
	//
	// Format any comment constraints.
	//
	/////////////////////////////////////////////////////
	if( theForm.text.value != "" ) 
	{
		theForm.parms.value += conjunct +
			" upper(b31.text) like '%" + theForm.text.value.toUpperCase() + "%'";
	}

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