//Calendar copyright by Constantine Roussos, www.roussosweb.com
function selDateFn(month, day, year) {
//When you click on a date this function is activated.  Insert here whatever code you
//want executed.
	document.getElementById("selDateTxt").innerHTML=month+" "+day+", "+year;
}//end selDateFn

function makeNewCalendar(specMonth, specYear, calWidth, calHeight) {
	createCalendar(specMonth, specYear, calWidth, calHeight);
}// end makeNewCalendar

	var arMonths = new Array("January","February","March",
			"April","May","June","July","August","September",
			"October","November","December");
	var shortMonths = new Array("Jan","Feb","Mar",
			"Apr","May","Jun","Jul","Aug","Sep",
			"Oct","Nov","Dec");
			//Note: 0=jan ... 11=dec
	var daysOfWeek = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

function createCalendar(month, year, calWidth, calHeight) {
	var argv = createCalendar.arguments;
	//get the number of arguments
	var argc = createCalendar.arguments.length;
	var scrWidth = screen.width; //3rd arg is screen width 
		//(use browser width, instead-> document.body.clientHeight
	if (argc > 2) scrWidth=argv[2];
    var scrHeight = screen.height;
	if (argc > 3) scrHeight=argv[3];
	//alert("width="+scrWidth+", "+"height="+scrHeight);
    var cellWidth = scrWidth/9; 
    var cellHeight = scrHeight/11;

    //Get first day of month (day of week (0-6))
    var firstDayOfMonth = getFirstDayOfMonth(month, year);
    
    //Get last date of month (28-31)
    var lastDateOfMonth = getLastDateOfMonth(month, year);

	//get the number of rows required for this month
	var numRows = getNumRows(firstDayOfMonth, lastDateOfMonth);
	numRows=6;		//always use 6 rows instead

	//write the calendar table
	//document.write('<table border=1 cellspacing=2 cellpadding=2>' + "\n ");
	document.write('<table border=1 width='+calWidth+' height='+calHeight+' cellspacing=2 cellpadding=2>' + "\n ");
	
	//write the Month name and Year on the 1st row (spanning 4 cols)
	document.write('<tr>');
	var monthYear='<td style="FONT-FAMILY:Ariel;FONT-SIZE:16px;FONT-WEIGHT: bold" colspan=4 align=center>' + 
	' <span id=monthHead>' + arMonths[month] +  ' ' + year + '</span></td>'
	document.write(monthYear);

	// Write the Date selected on upper right section of Calendar
	var selDateSection='<td id=selDateTxt style="FONT-FAMILY:Ariel;FONT-SIZE:14px;" colspan=3 align=center>Date Selected' + 
	'</td>'
	document.write(selDateSection); document.write('</tr>');
	
	//Write the names of the days of the week
	document.write('<tr>');
	for(i = 0; i<=6; i++) {
		daysTD = '<td align=center bgColor="#ccccff" ' +  
		' style="FONT-FAMILY:Courier;FONT-SIZE:9px;FONT-WEIGHT: normal">' + 
		daysOfWeek[i] + "</td>";
		document.write(daysTD);
	}
	document.write("</tr>"); //end of month-name row
 	
	//start writing calendar cells
	document.write('<tr>');
	//write first row
	var dayNum = new String("");  //number to go into cal cell
	var tdSpec= new String("");
	 //command to change bg for cells prior to 1st day of month
	var newBG= new String("");
	
	var lastDatePrevMonth = getLastDateOfPrevMonth(month, year);

	//write 1st row of dates, orange bg indicates prev month
    for(i=1; i<=7; i++) { 
		if(i<=firstDayOfMonth) { dayNum = lastDatePrevMonth-firstDayOfMonth+i;
			tdSpec = '<td id=td0' + i + ' align=right bgColor="orange" ' + 
			' style="FONT-FAMILY:Arial;FONT-SIZE:12px;FONT-WEIGHT: bold">' + dayNum + 
			'</td>';
		}//end if
		else { dayNum = (i-firstDayOfMonth).toString();
			tdSpec = '<td id=td0' + i + ' align=right bgColor="#ffffff" ' + 
			' style="FONT-FAMILY:Arial;FONT-SIZE:12px;FONT-WEIGHT: bold" ' + 
			' onMouseover=this.bgColor="green" ' + 
			' onMouseout=this.bgColor="white" ' +
			' onclick=selDateFn("'+shortMonths[month]+'",' + dayNum +',' + year + ') >' + dayNum + '</td>'
		}//end else
		document.write(tdSpec);
	  }//end for
	document.write('</tr>');

	//write rest of rows
	for(j=1; j<=numRows-1; j++) { 
	  for(i=1; i<=7; i++) { 
		dayNum = (j*7 + i - firstDayOfMonth);
		if(dayNum>lastDateOfMonth) {  //if into next month
			dayNum = dayNum-lastDateOfMonth;
			tdSpec = '<td id=td' + j + i + ' align=right bgColor="orange" ' + 
			'style="FONT-FAMILY:Arial;FONT-SIZE:12px;FONT-WEIGHT:bold">' + dayNum + 
			'</td>';
		}//end if
		else {  //day is in this month
			tdSpec = '<td id=td' + j + i + ' align=right bgColor="#ffffff" ' + 
			'style="FONT-FAMILY:Arial;FONT-SIZE:12px;FONT-WEIGHT:bold" ' + 
			' onMouseover=this.bgColor="green"' + 
			' onMouseout=this.bgColor="white"' +
			' onclick=selDateFn("'+shortMonths[month]+'",' + dayNum +',' + year + ') >' + dayNum + '</td>';
		}//end else
		document.write(tdSpec);
	  }//end for
	  document.write('</tr>');
	 }//end for
	document.write('</table>');
}//end createCalendar

function updateCalendar(month, year) {
    //Get first day of month
    var firstDayOfMonth = getFirstDayOfMonth(month, year);
    //Get last date of month
    var lastDateOfMonth = getLastDateOfMonth(month, year);
	var numRows = getNumRows(firstDayOfMonth, lastDateOfMonth);
	numRows=6;
   	//write the Month name
	document.getElementById("monthHead").innerHTML = arMonths[month] + ' ' + year;
	var dayNum=1;
	document.getElementById("selDateTxt").innerHTML="Selected Date";

	var lastDatePrevMonth = getLastDateOfPrevMonth(month, year);
    //write the first row.  orange bg indicates prev month
    for(i=1; i<=7; i++) { 
		if(i<=firstDayOfMonth) { dayNum = lastDatePrevMonth-firstDayOfMonth+i;
			var newTDDef='document.getElementById("td0' + i + '").style.backgroundColor="orange";'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").innerHTML=' + '"' + dayNum + '";'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onmouseover=function () {return false};'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onmouseout=function () {return false;};'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onclick=function () {return false};'; eval(newTDDef);
		}//end then
		else { dayNum = (i-firstDayOfMonth).toString();
			var newTDDef='document.getElementById("td0' + i + '").style.backgroundColor="white"'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").innerHTML=' + '"' + dayNum + '";'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onmouseover=function () {document.getElementById("td0' + i + '").style.backgroundColor="green"};'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onmouseout=function () {document.getElementById("td0' + i + '").style.backgroundColor="white"};'; eval(newTDDef);
			var newTDDef='document.getElementById("td0' + i + '").onclick=function () {selDateFn("'+shortMonths[month]+'",' + dayNum + ',' + year + ')}'; eval(newTDDef);
		}//end else
	}//end for

	//write rest of rows
	var nxtMoDayNum=0;  //init day number for following month
	for(j=1; j<=numRows-1; j++) { 
	  for(i=1; i<=7; i++) { 
		if(dayNum<lastDateOfMonth) {
			dayNum = (j*7 + i - firstDayOfMonth);
			var newTDDef='document.getElementById("td' +j + i + '").innerHTML="' + dayNum + '";'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j + i + '").style.backgroundColor="white";'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onmouseover=function () {document.getElementById("td' + j + i + '").style.backgroundColor="green";}'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onmouseout=function () {document.getElementById("td' + j + i + '").style.backgroundColor="white";}'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onclick=function () {selDateFn("'+shortMonths[month]+'",' + dayNum + ',' + year + ');}'; eval(newTDDef);
		}//end then
		else {	//fill out last row
			nxtMoDayNum++;
			var newTDDef='document.getElementById("td' +j + i + '").innerHTML="' + nxtMoDayNum + '";'; eval(newTDDef);
			newTDDef = 'document.getElementById("td' +j+ i + '").style.backgroundColor="orange"'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onmouseover=function () {return false;}'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onmouseout=function () {return false;}'; eval(newTDDef);
			newTDDef='document.getElementById("td' +j+ i + '").onclick=function () {return false;}'; eval(newTDDef);
		}//end else
	  }//end for
	}//end for
}//end updateCalendar

function getFirstDayOfMonth(month, year) {
			var thisDay=new Date();
			thisDay.setYear(year); thisDay.setMonth(month);
			thisDay.setDate(1);
			return thisDay.getDay()
}//end getFirstDayOfMonth

function getLastDateOfMonth(month, year) {
			var thisDay=new Date();
			thisDay.setYear(year); thisDay.setMonth(month);
			for(i=28; i<=31; i++) {
				thisDay.setDate(i);
				if (thisDay.getDate()==1) {
					return (i-1);
				}//end if
			}//end for
			return(31);
}// end getLastDateOfMonth

function getLastDateOfPrevMonth(month, year) {
			if (month>0) prevMonth=month-1;
			else prevMonth=11;
			return getLastDateOfMonth(prevMonth, year)
}// end getLastDateOfPrevMonth

function isLeapYear(year) {
	var thisDay=new Date();
	thisDay.setYear(year);
	thisDay.setMonth(2);	//set month to March
	thisDay.setDate(1);	//set Date to 1st
	//subtract one day
	thisDay.setTime(thisDay.getTime() - 24*60*60*1000);
	//if date is 29 then this is a leap year
	if (thisDay.getDate == 29) {
		return true;}
	//else this is not leap year
	else { return false;}
}//end isLeapYear

function getNumRows(firstDayOfMonth, lastDateOfMonth) {
			//compute the number of rows on the calendar for month
			//firstDayOfMonth is day of week (0-6)
			//lastDateOfMonth is last date of month 28-31
			return Math.floor(1 + (lastDateOfMonth + firstDayOfMonth - 1)/7);
}//end getNumRows

