//  datefunction.js
//
//  include using the following line in your HTML:
//  <SCRIPT LANGUAGE="JavaScript" src="datefunctions.js"></SCRIPT>
//
//  to use the calendar popup, add the following code:
//  <a href="javascript: openCalWin(document.frmName.fldName.value,'frmName','fldName');">
//  <img border="0" src="/includes/cal.gif" alt="click for calendar entry" width="16" height="15"></a>
//
//  to validate dates, include the following in your validation routine:
//  	strError = DateValidate(theForm.fldName.value);
//	if ( strError.length != 0)
//	{	alert(strError);
//		theForm.fldName.focus();
//		return (false);
//	}
//
//
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var totalDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
var curForm
var curField

function openCalWin(inDate, inForm, inField) { 
var y, m, calYear, calMonth
var today = new Date();
	stats='toolbar=no,location=no,directories=no,status=no,menubar=no,';
	stats += 'scrollbars=no,resizable=no,width=225,height=200';
	CalWin = window.open ("","Calendar",stats);
	y = parseInt(inDate.substr(0,4),10);
	m = parseInt(inDate.substr(5,2),10);
	if (isNaN(y) || y <= 0) calYear = today.getFullYear() 
		else calYear = y;
	if (isNaN(m) || m > 12 || m <= 0) calMonth = today.getMonth()
		else calMonth = m - 1;

	curForm = inForm
	curField = inField
	theDate = new Date(calYear, calMonth, 1);

	buildCal_date(theDate);
}

function buildCal_date(theDate) {
	
	var startDay = theDate.getDay()
	var printDays = false
	var currDay = 1
	var rowsNeeded = 5
	// check for leapyear
	if (theDate.getFullYear() % 4 == 0) totalDays[1] = 29
	 else totalDays[1] = 28;
	// check for rows needed
	if (startDay + totalDays[theDate.getMonth()] > 35)
		rowsNeeded++
	// write page for chosen month/year
	CalWin.document.write('<html><head><Title>Click a Date</title>')
	CalWin.document.write('<STYLE TYPE="text/css">')
	CalWin.document.write('A { color: #000000; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:10pt; font-weight: bold; text-decoration: none}')
	CalWin.document.write('body { font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:10pt;}')
	CalWin.document.write('A:hover { color: red; }')
	CalWin.document.write('</STYLE></head>')
	CalWin.document.write('<body topmargin=0 leftmargin=0><a name="this"></a>')
	CalWin.document.write('<form method=post><table align=center height=100% width=100% border=1 bordercolor=Black cellpadding=0 cellspacing=0>')
	CalWin.document.write('\n<tr><th colspan=2 bgcolor=#3C3A8F><input type="Submit" size="2" name="Back_Y" value="<<" onClick="opener.getNewCal_date(-12)">&nbsp;<input type="Submit" size="2" name="Back_M" value="&lt;" onClick="opener.getNewCal_date(-1)"></th>\n<th bgcolor=#3C3A8F colspan=3><font color=white>' + months[theDate.getMonth()] + ' ' + theDate.getFullYear() + '</font></th>\n<th colspan=2 bgcolor=#3C3A8F><input type="Submit" size="2" name="Fwd_M" value="&gt;" onClick="opener.getNewCal_date(1)">&nbsp;<input type="Submit" size="2" name="Fwd_Y" value="&gt;&gt;" onClick="opener.getNewCal_date(12)"></th></tr>')
	CalWin.document.write('\n<tr bgcolor="#c0c0c0"><th><font face=Arial color=white>Su</font></th><th><font face=Arial color=white>Mo</font></th><th><font face=Arial color=white>Tu</font></th><th><font face=Arial color=white>We</font></th><th><font face=Arial color=white>Th</font></th><th><font face=Arial color=white>Fr</font></th><th><font face=Arial color=white>Sa</font></th></tr>')
	for (x=1; x<=rowsNeeded; x++){
		CalWin.document.write('\n<tr>')
		for (y=0; y<=6; y++){
			if (currDay == 1 && !printDays && startDay == y)
				printDays = true;
			CalWin.document.write('<td align="center" width=14.28%>');
			if (printDays){
        		CalWin.document.write('<a href="javascript:opener.Dret_date(' + theDate.getMonth() + ',' +  currDay + ',' + theDate.getFullYear() + ')">' + currDay++ + '</a></td>')
				if (currDay > totalDays[theDate.getMonth()])
					printDays = false
			}
			else
				CalWin.document.write('&nbsp;</td>')
		}		
		CalWin.document.write('</tr>')
	}	
	CalWin.document.write('<tr bgcolor="#3C3A8F"><td align="center" colspan=7><font face=Arial color=white size="1"> &lt;&nbsp;&gt; - months<br> &lt;&lt; &gt;&gt; - years.</font></td></tr>') 
	CalWin.document.write('</table></form></body></html>')
	CalWin.document.close()
	
}

function getNewCal_date(newDir) {
	if (newDir == -1){
		theDate.setMonth(theDate.getMonth() - 1)
		if (theDate.getMonth() == 0){
			theDate.setMonth(12)
			theDate.setFullYear(theDate.getFullYear() - 1)
		}
	}
	else if (newDir == 1){
		theDate.setMonth(theDate.getMonth() + 1)
		if (theDate.getMonth() == 13){
			theDate.setMonth(1)
			theDate.setFullYear(theDate.getFullYear() + 1)
		}
	}
	else if (newDir == -12)
			theDate.setMonth(theDate.getMonth() - 12)
	else if (newDir == 12)
			theDate.setMonth(theDate.getMonth() + 12)
	
		
		
	CalWin.document.clear();
	buildCal_date(theDate);

}

function Dret_date(monthNum, dayNum, yearNum){
	var strMonth
	var strDay
	if (monthNum + 1 <10)
		strMonth = "0" + (monthNum + 1);
	else
		strMonth = (monthNum + 1);
	if (dayNum < 10)
		strDay = "0" + dayNum;
	else
		strDay = dayNum;

	var dateString = yearNum + '/' + strMonth + '/' + strDay

	document.forms[curForm].elements[curField].value = dateString
	dirty=true;
	CalWin.close()
}

// function to validate a date - return message (blank if valid.)
function DateValidate(strDate)
{
	// check for valid date.

	var y, m, d, strError
	strError = "";
	if (strDate.length != 0)
	{
		if (strDate.length != 10)
			strError = "Date must be in format 'YYYY/MM/DD' (eg 2002/05/17).";
		else {
			y = parseInt(strDate.substr(0,4),10);
			m = parseInt(strDate.substr(5,2),10);
			d = parseInt(strDate.substr(8,2),10);
			if (isNaN(y) || isNaN(m) || isNaN(d)) strError = "Date must be in format 'YYYY/MM/DD' (eg 2002/05/17).";
			else {
				if (m > 12 || m < 01) strError = "Invalid Month.  Please use format 'YYYY/MM/DD'.";
				if (m == 2)
				{	if (y % 4 != 0)
						{ if (d > 28) strError = "Invalid Date. (only 28 days in February)"; }
					else
					  	{ if (d > 29) strError = "Invalid Date. (only 29 days in February)"; }
				}
				if ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) strError = "Invalid Date. (only 30 days in this month)";
				if (d > 31 || d < 1) strError = "Invalid Date. (use days from 1 - 31)";
			}
		}
	}
	return strError;
}