/*
   New Perspectives on JavaScript
   Tutorial 3
   Tutorial Case

   Author: 
   Date:   

   Function List:
	*/ 
	 
   function calendar(calendarDay){
     // Creates the calendar table for the month specified in the
      //calendarDay parameter. The current date is highlighted in 
     // the table.
		 
		if(calendarDay == null){calDate=new Date()} else{ calDate=new Date(calendarDay);}
		// var calDate= new Date("May 18, 2007");
		 document.write("<table id='calendar_table'>");
		 writeCalTitle(calDate);
		 writeDayNames();
		 writeCalDays(calDate);
		 document.write("</table>");
}
    function writeCalTitle(calendarDay){
		
		// setup array
		var monthName= new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		
		// get month and year
		var thisMonth= calendarDay.getMonth();
		var thisYear= calendarDay.getFullYear();
		
		
		document.write("<tr>");
		document.write("<th id='calendar_head' colspan='7'>");
		document.write(monthName[thisMonth]+ " "+ thisYear);
		document.write("</th>");
		document.write("</tr>");
		
		
		
		}
		function writeDayNames(){
		 
		 // day names array
		 var dayName= new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
		 // displays days accross the display..
		 document.write("<tr>");
		 for (var i=0;i<dayName.length;i++){
		 document.write("<th class='calendar_weekdays'>"+dayName[i]+"</th>");
		 }
		 document.write("</tr>");
		 }
		 function daysInMonth(calendarDay){
		 var thisYear=calendarDay.getFullYear();
		 var thisMonth=calendarDay.getMonth();
		 var dayCount= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		 return dayCount[thisMonth]; //returns the number of days in the month.
		 if(thisYear % 4 == 0){
		  if((thisYear % 100 !=0)||(thisYear % 400 == 0)){
		   dayCount[1]=29; //leap year
		  }
		 }
		 
		}
		
		function writeCalDays(calendarDay) {
		var currentDay=calendarDay.getDate();
		// determin the starting day of the week
			
			 var dayCount = 1;
			 var totalDays = daysInMonth(calendarDay);
			 calendarDay.setDate(1); // set date to the first
			 var weekDay = calendarDay.getDay(); // sets day ofthe week of the first
		    
		// write blank cells
		   document.write("<tr>");
			 for (var i=0; i<weekDay; i++){
			   document.write("<td></td>");
				 }
		//write cells for each day of the month
		
		   while(dayCount <= totalDays){
			 			if (weekDay == 0 ) document.write("<tr>");
						if (dayCount==currentDay){
						// highlight the current day
						document.write("<td  class='calendar_dates' id='calendar_today'>"+dayCount+"</td>");
						} else {
						// display as usual
						document.write("<td class='calendar_dates'>"+dayCount+"</td>");
						}
						if(weekDay == 6)document.write("</tr>");
						// move body to next day
						dayCount++;
						calendarDay.setDate(dayCount);
						weekDay = calendarDay.getDay();
			 }
			 document.write("</tr>");
		}
		
		
		
		
		
		