function checkyear( year )
{
	if ( year.value == "") {
		alert("Blank year." );
		return false;
	} 
	yr = parseInt( year.value, 10 );
	if( isNaN( yr ) == true )
	{
		alert("Invalid year entered." );
		return false;
	} 
	else if (yr < 1900)
	{
		alert("Invalid year entered (YYYY)." );
		return false;
	}

	return true;
}

function checkmonth( month )
{
	if ( month.value == "") {
		alert("Blank month." );
		return false;
	}
	mon = parseInt( month.value, 10 );
	if( isNaN( mon ) == true )
	{
		alert("Invalid month entered." );
		return false;
	}
	if ( (mon < 1) || (mon > 12) )
	{
		alert("The month must be between 1 and 12." );
		return false;
	}
	if( month.value.length == 1 )
	{
		month.value = "0" + month.value;
	}

	return true;
}

function checkday( day )
{
	if ( day.value == "") {
		alert("Blank day." );
		return false;
	}

	dy = parseInt( day.value, 10 );
	if( isNaN( dy ) == true )
	{
		alert("Invalid day entered." );
		return false;
	}
	else if ( (dy < 1) || (dy > 31) )
	{
		alert("The day must be between 1 and 31." );
		return false;
	}
	if( day.value.length == 1 )
		day.value = "0" + day.value;

	return true;
}

function checkjday( day )
{
	if ( day.value == "") {
		alert("Invalid Julian day entered." );
		return false;
	}

	dy = parseInt( day.value, 10 );
	if( isNaN( dy ) == true )
	{
		alert("Invalid Julian day entered." );
		return false;
	}
	else if ( (dy < 1) || (dy > 366) )
	{
		alert("The day must be between 1 and 366." );
		return false;
	}

	if( day.value.length == 1 )
	{
		day.value = "00" + day.value;
	}
	else if( day.value.length == 2 )
	{
		day.value = "0" + day.value;
	}

	return true;
}

function setjdate2() {

	if (document.forms[0].DAY2.value == "" ||
		document.forms[0].MONTH2.value == "" ||
		document.forms[0].YEAR2.value == "") { 
		return false; 
	}

	var day = parseInt(document.forms[0].DAY2.value, 10);
	var month = parseInt(document.forms[0].MONTH2.value, 10);
	var year = parseInt(document.forms[0].YEAR2.value, 10);

	if (isNaN(day)) {
		alert("Non-Numeric in End Day field"); 
		return false;
	}

	if (isNaN(month)) {
		alert("Non-Numeric in End Month field"); 
		return false;
	}

	var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	if (isNaN(year)) {
		alert("Non-Numeric in End Year field"); 
		return false;
	}

        if (year%4 == 0) {
                isLeapYear = 1;
                if (year%100 == 0) {
                        isLeapYear = 0;
                        if (year%400 == 0) {
                                isLeapYear = 1;
                        }
                }
        }
        else { isLeapYear = 0; }

	if (isLeapYear)  {
		days_in_month[1] = 29;
	}

	i = 0;
	jday = 0;
	while (i < month-1) {
			jday += days_in_month[i];
			i++;
	}
	jday += day;

	document.forms[0].JDAY2.value = String(jday);

	if (document.forms[0].JDAY2.value.length == 1) {
		document.forms[0].JDAY2.value= "00" + document.forms[0].JDAY2.value; 
	} else if (document.forms[0].JDAY2.value.length == 2) {
		document.forms[0].JDAY2.value= "0" + document.forms[0].JDAY2.value; 
	}

	document.forms[0].YEAR2.value = String(year);

	delete days_in_month;
	
        return true;

}

function checklat( lat )
{
	if (lat.value == "") return true;

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

	return true;
}
function checklon( lon )
{
	if (lon.value == "") return true;

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

	return true;
}

function checkaz( az )
{
	if (az.value == "") return true;

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

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

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

	return true;
}


function setmmdd2() {

	if (document.forms[0].JDAY2.value == "" ||
		document.forms[0].YEAR2.value == "") {
		return false;
	}

	var jday = parseInt(document.forms[0].JDAY2.value, 10);
	var jyear = parseInt(document.forms[0].YEAR2.value, 10);

	if (isNaN(jday)) {
		alert("Non-Numeric in Julian End Day field"); 
		return false;
	}

	if (isNaN(jyear)) {
		alert("Non-Numeric in Julian End Year field"); 
		return false;
	}

	var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        if (jyear%4 == 0) {
                isLeapYear = 1;
                if (jyear%100 == 0) {
                        isLeapYear = 0;
                        if (jyear%400 == 0) {
                                isLeapYear = 1;
                        }
                }
        }

        else { isLeapYear = 0; }

	if (isLeapYear)  {
		days_in_month[1] = 29;
	}

	i = 0;
	month = 1;

	while (jday - days_in_month[i] > 0) {
			jday -= days_in_month[i];
			month++;
			i++;
	}
	document.forms[0].MONTH2.value = String(month);
	if (document.forms[0].MONTH2.value.length == 1)
		document.forms[0].MONTH2.value= "0" + document.forms[0].MONTH2.value; 
	document.forms[0].DAY2.value = String(jday);
	if (document.forms[0].DAY2.value.length == 1)
		document.forms[0].DAY2.value= "0" + document.forms[0].DAY2.value; 

	document.forms[0].YEAR2.value = String(jyear);

        return true;

}

function setjdate1() {

	if (document.forms[0].DAY1.value == "" ||
		document.forms[0].MONTH1.value == "" ||
		document.forms[0].YEAR1.value == "") { 
		// can't set jdate1
		return false; 
	}

	var day = parseInt(document.forms[0].DAY1.value, 10);
	var month = parseInt(document.forms[0].MONTH1.value, 10);
	var year = parseInt(document.forms[0].YEAR1.value, 10);

	if (isNaN(day)) {
		alert("Non-Numeric in End Day field"); 
		return false;
	}

	if (isNaN(month)) {
		alert("Non-Numeric in End Month field"); 
		return false;
	}

	if (isNaN(year)) {
		alert("Non-Numeric in End Year field"); 
		return false;
	}

	var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        if (year%4 == 0) {
                isLeapYear = 1;
                if (year%100 == 0) {
                        isLeapYear = 0;
                        if (year%400 == 0) {
                                isLeapYear = 1;
                        }
                }
        }

        else { isLeapYear = 0; }

	if (isLeapYear)  {
		days_in_month[1] = 29;
	}

	i = 0;
	jday = 0;
	while (i < month-1) {
			jday += days_in_month[i];
			i++;
	}
	jday += day;

	document.forms[0].JDAY1.value = String(jday);

	if (document.forms[0].JDAY1.value.length == 1) {
		document.forms[0].JDAY1.value= "00" + document.forms[0].JDAY1.value; 
	} else if (document.forms[0].JDAY1.value.length == 2) {
		document.forms[0].JDAY1.value= "0" + document.forms[0].JDAY1.value; 
	}

	document.forms[0].YEAR1.value = String(year);

	delete days_in_month;
	
        return true;

}

function setmmdd1() {

	if (document.forms[0].JDAY1.value == "" ||
		document.forms[0].YEAR1.value == "") {
		return false;
	}

	var jday = parseInt(document.forms[0].JDAY1.value, 10);
	var jyear = parseInt(document.forms[0].YEAR1.value, 10);

	if (isNaN(jday)) {
		alert("Non-Numeric in Julian Start Day field"); 
		return false;
	}

	if (isNaN(jyear)) {
		alert("Non-Numeric in Julian Start Year field"); 
		return false;
	}

	var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        if (jyear%4 == 0) {
                isLeapYear = 1;
                if (jyear%100 == 0) {
                        isLeapYear = 0;
                        if (jyear%400 == 0) {
                                isLeapYear = 1;
                        }
                }
        }

        else { isLeapYear = 0; }

	if (isLeapYear)  {
		days_in_month[1] = 29;
	}

	i = 0;
	month = 1;

	while (jday - days_in_month[i] > 0) {
			jday -= days_in_month[i];
			month++;
			i++;
	}


	document.forms[0].MONTH1.value = String(month);
	if (document.forms[0].MONTH1.value.length == 1)
		document.forms[0].MONTH1.value= "0" + document.forms[0].MONTH1.value; 
	document.forms[0].DAY1.value = String(jday);
	if (document.forms[0].DAY1.value.length == 1)
		document.forms[0].DAY1.value= "0" + document.forms[0].DAY1.value; 

	document.forms[0].YEAR1.value = String(jyear);

	delete days_in_month;
	
        return true;

}

function checkstarthhmmss( hhmmss )
{

        if( hhmmss.value == "") hhmmss.value = "000000";
        var v=parseInt(hhmmss.value, 10);
        if (isNaN(v)) {
                alert("Non-numeric start time: hhmmss" );
                return false;
        }
        else {
                if (hhmmss.value.length != 6) {
                        alert("Invalid start time: hhmmss" );
        		hhmmss.value = "000000";
                        return false;
                }
                if (hhmmss.value.substring(0,2) > "24" || hhmmss.value.substring(0,2) < "00")  {
                        alert("The start hour must be between 00 and 23." );
        		hhmmss.value = "000000";
                        return false;
                }
                if (hhmmss.value.substring(2,4) > "59" || hhmmss.value.substring(2,4) < "00") {
                        alert("The start minute must be between 0 and 59." );
        		hhmmss.value = "000000";
                        return false;
                }
                if (hhmmss.value.substring(4,6) > "59" || hhmmss.value.substring(4,6) < "00") {
                        alert("The start second must be between 0 and 59." );
        		hhmmss.value = "000000";
                        return false;
                }
        }

        return true;
}
function checkendhhmmss( hhmmss )
{
        if( hhmmss.value == "") hhmmss.value = "235959";
        var v=parseInt(hhmmss.value, 10);
        if (isNaN(v)) {
                alert("Non-numeric end time: hhmmss" );
                return false;
        }
        else {
                if (hhmmss.value.length != 6) {
                        alert("Invalid end time: hhmmss" );
        		hhmmss.value = "235959";
                        return false;
                }
                if (hhmmss.value.substring(0,2) > "24" || hhmmss.value.substring(0,2) < "00")  {
                        alert("The end hour must be between 00 and 23." );
        		hhmmss.value = "235959";
                        return false;
                }
                if (hhmmss.value.substring(2,4) > "59" || hhmmss.value.substring(2,4) < "00") {
                        alert("The end minute must be between 0 and 59." );
        		hhmmss.value = "235959";
                        return false;
                }
                if (hhmmss.value.substring(4,6) > "59" || hhmmss.value.substring(4,6) < "00") {
                        alert("The end second must be between 0 and 59." );
        		hhmmss.value = "235959";
                        return false;
                }
        }

        return true;
}



function validateAndSubmit( theForm )

{
//alert ('validate and submit running');

	// Check for starttime parameters
	if( checkyear( theForm.YEAR1 ) == false )          return false;
	else if( checkjday( theForm.JDAY1 ) == false )       return false;


	// Check for endtime parameters
	if( checkyear( theForm.YEAR2 ) == false )          return false;
	// checkstart functions set to 1/1, which is what we want here
	else if( checkjday( theForm.JDAY2 ) == false )       return false;

	
	if (checklat(theForm.lat1) == false) return false;
	if (checklat(theForm.lat2) == false) return false;
	// return true;

	if (checklon(theForm.lon1) == false) return false;
	if (checklon(theForm.lon2) == false) return false;
	if (checkaz(theForm.az1) == false) return false;
	if (checkaz(theForm.az2) == false) return false;
	if (checkdip(theForm.dip1) == false) return false;
	if (checkdip(theForm.dip2) == false) return false;

	var conjunct = "";

	var str = theForm.vnet.value;
	theForm.vnet.value = str.toUpperCase();
	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.b33.value;
	theForm.b33.value = str.toUpperCase();
	str = theForm.site.value;
	theForm.site.value = str.toUpperCase();
	str = theForm.flags.value;
	theForm.flags.value = str.toUpperCase();

	/////////////////////////////////////////////////////
	//
	// Format any start and end time constraints.
	//
	/////////////////////////////////////////////////////

	//if( ( 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,location, OR channel box" );
		//return false;
	//}
	
	theForm.submit();
	return true;
}
