
/*
Name:  			LeapYear
Parameters: 	intYear -- Integer representing the year
Purpose:		Returns true if year is a leap year and false otherwise
*/
function LeapYear(intYear) 
{
	if (intYear % 100 == 0) { if (intYear % 400 == 0) { return true; }}
	else { if ((intYear % 4) == 0) { return true; }}
	return false;
}

/*
Name:  			IsValidDate
Parameters: 	date -- String representation of a number
Purpose:		Returns true if string is a date and false otherwise
*/
function IsValidDate(strDate)
{
	// Supports: mm/dd/yy or mm/dd/yyyy date formats
	var intDay=1;
	var intMonth=1;
	var intYear=1900;
	var intDelimIndex_1 = -1;
	var intDelimIndex_2 = -1;
	var chrNextChar = "";
	
	for (var i=0; i<strDate.length; ++i)
	{
		chrNextChar = strDate.charAt(i);
		if ( chrNextChar == '/' ) 
		{ 
			if ( intDelimIndex_1 == -1 ) { intDelimIndex_1 = i; }
			else { intDelimIndex_2 = i; }
		}
	}
	if ( (intDelimIndex_1 == -1) || (intDelimIndex_2 == -1) ) return false;
	else
	{
		intMonth = strDate.slice(0,intDelimIndex_1);
		intDay = strDate.slice(intDelimIndex_1+1,intDelimIndex_2);
		intYear = strDate.slice(intDelimIndex_2+1,strDate.length);
		if ( intDay < 1 || intDay > 31) { return false; }
		if (intMonth>12 || intMonth<1) { return false; }
		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31))  { return false; }
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) { return false; }
		if (intMonth == 2) { if (LeapYear(intYear) == true)	{ if (intDay > 29) 	{ return false; } } else{ if (intDay > 28) {return false;	}	} 	}
		return true;
	}		
}


/*
Name:  			IsNumeric
Parameters: 	strNumber -- String representation of a number
Purpose:		Returns true if string is a number and false otherwise
*/
function IsNumeric(strString)
//  check for valid numeric strings	
{
   	var strValidChars = "0123456789.-";
   	var strChar;
   	var blnResult = true;
	var decFound = false;

   	//  test strString consists of valid characters listed above
   	for (i = 0; i < strString.length && blnResult == true; i++)
	{
      	strChar = strString.charAt(i);
		if (strChar == '.') 
		{ 
			if ( decFound == true ) { blnResult = false; break; }
			else { decFound = true; }
		}
      	if (strValidChars.indexOf(strChar) == -1)
      	{
        	blnResult = false;
    	}
	}
	return blnResult;
}

function validate()
{
	var blnResult = true;
	var msg = "The following fields are either required or have invalid data: \n"
	with (document.all['formWeedEntry'])
	{
		if (txtSurveyDate.value == "" || IsValidDate(txtSurveyDate.value) == false)
		{
			blnResult = false;
			msg = msg +" Survey Date \n";
		}
		if (txtObservers.value == "" )
		{
			blnResult = false;
			msg = msg +" Observers \n"
		}
		if (txtLatitude.value == "" || IsNumeric(txtLatitude.value)== false || txtLatitude.value < 50 || txtLatitude.value > 75 || txtLatitude.value.length < 8)
		{
			blnResult = false;
			msg = msg +" Latitude must be decimal degrees, between 50 and 75 with 5 significant digits \n"
		}
		if (txtLongitude.value == "" || IsNumeric(txtLongitude.value)== false || txtLongitude.value > -130 || txtLongitude.value < -190 || txtLongitude.value.length < 10)
		{
			blnResult = false;
			msg = msg +" Longitude must be decimal degrees, between -130 and -190 with 5 signficant digits \n"
		}
		if (IsNumeric(txtElevation.value) == false)
		{
			blnResult = false;
			msg = msg +" Elevation must be a valid number \n "
		}
		var now = new Date();
		var yr = now.getYear();
		
		if (IsNumeric(txtMapDate.value) == false || (txtMapDate.value != "" && txtMapDate.value < 1900 || txtMapDate.value > yr))
		{
			blnResult = false;
			msg = msg +" Map Date must be a valid year between 1900 and present \n"
		}

		var numSpeciesRow = document.all['tblSpecies'].rows.length -2;
		var field1Result = true;
		var field2Result = true;
		var field3Result = true;
		var field5Result = true;
		var field6Result = true;

		for (var x=1; x<= numSpeciesRow; x++)
		{
			var field1 = eval("selEPCode"+x+".value");
			var field2 = eval("txtInfested"+x+".value");
			var field3 = eval("txtCanopy"+x+".value");
			var field5 = eval("txtAge"+x+".value");
			var field6 = eval("selLocation"+x+".value");
			if (field1 == "" ){	field1Result = false;		}
			if (field2 == "" || IsNumeric(field2) == false)	{	field2Result = false;			}
			if (field3 == "" || IsNumeric(field3) == false || field3 > 100 || field3 < 0)	{	field3Result = false;}
			if (IsNumeric(field5) == false)	{	field5Result = false;	}
			if (field6 == "" ){	field6Result = false;		}
		}
		if (field1Result == false) {blnResult = false; msg = msg +" Exotic Plant Species Code \n"; }
		if (field2Result == false) {blnResult = false; msg = msg +" Infested Acres must be a valid number \n"	; }
		if (field3Result == false) {blnResult = false; msg = msg +" Canopy Cover must be a percentage between 0 and 100 \n"		; }
		if (field5Result == false) {blnResult = false; msg = msg +" Disturbance Age must be a valid number \n"			; }
		if (field6Result == false) {blnResult = false; msg = msg +" Collection Information \n"; }
	}	
	if (blnResult == false)
	{	alert(msg);
		return false;
	}
	else
	{	document.all['formWeedEntry'].numRows.value = numSpeciesRow;
		document.all['formWeedEntry'].submit();	}
}