/* Javascript date check by Justin Klein Keane  Copyright (C) 2004 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

function IsValidDate(thisField) {
	var myRegex = new RegExp("^[0-9]{4}\-([0][0-9]|[1][0-2])\-([0-2][0-9]|[3][0-1])$");
	if (thisField.value.match(myRegex)) {
		var theDay = Math.round(thisField.value.substr(8,2));
		var theMonth = Math.round(thisField.value.substr(5,2));
		var theYear = Math.round(thisField.value.substr(0,4));
		
		if ((theYear%4 == 0) && (theDay > 29) && (theMonth == 2)) {
			//alert ("Not a valid date.");
			return false;
		}
		else if ((theYear%4 != 0) && (theDay > 28) && (theMonth == 2)) {
			//alert ("Not a valid date.");
			return false;
		}
		else if ((theDay > 30) && (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11)) {
			//alert ("Not a valid date.");
			return false;
		}
		else if (theYear == 0 || theMonth == 0 || theDay == 0){
			//alert ("Not a valid date.");
			return false;
		}		
		else {
			return true;
		}
	}
	else {
		//alert ("Date not in correct yyyy-mm-dd format or the date specified does not exist.");
		return false;
	}
}

function addDays(myDate,days) {
	return new Date(myDate.getTime() + days*24*60*60*1000);
}

function StrToDate(valDate) {
	var d1;
	aryDate = valDate.split('-');
	d1=new Date(aryDate[0],aryDate[1]-1,aryDate[2]);
	return d1;
}

function IsDateWithinFiscalYear(valDate, valFiscalYear){
	var varFiscalYearStart 	= 	valFiscalYear 	+ '-' + '04' + '-' + '01';
	var varFiscalYearEnd 	= 	parseInt(valFiscalYear)+1 + '-' + '03' + '-' + '31';
	
	var datDate = StrToDate(valDate);
	var datFiscalYearStart = StrToDate(varFiscalYearStart);
	var datFiscalYearEnd = StrToDate(varFiscalYearEnd);
	
	if ((datDate >= datFiscalYearStart) && (datDate <= datFiscalYearEnd)) {
		return true;
	}
	else {
		return false;
	}
}