/*--------------------------------------------------------------------------------------------------+
+   Dynamic calendar built by Jeremy Gonzalez for WSI Global web strategies                         +
+   This script generates a calendar for iuse with CSS                                              +
+--------------------------------------------------------------------------------------------------*/

   var d = new Date(); 									// Creates a new date object
   var m = d.getMonth(); 								// Apply this to the offset of month array
   var year = d.getFullYear();							// Current year
   var days_in_month = daysInMonth(m, year);			// Number of days in current month
   var today = d.getDate(); 							// Today's date 1-31

   var curDayOfWeek = d.getDay(); 						// Current day of the week 0 - 6 
   var dayOfWeek = startMonthOn(m, year); 				// The day the month starts on

//--------------------------------------------------------------------------------------------------//
//  Array for Textual represenation of the month                                                    //
//--------------------------------------------------------------------------------------------------//

	var month=new Array(7)	
	month[0] = "January"; month[1]  = "February";  month[2]  = "March";
	month[3] = "April";   month[4]  = "May";       month[5]  = "June";
	month[6] = "July";    month[7]  = "August";    month[8]  = "September";
	month[9] = "October"; month[10] = "November"; month[11] = "December";
	
//--------------------------------------------------------------------------------------------------//
//  Below are the methods used to generating a calendar                                             //
//--------------------------------------------------------------------------------------------------//

    function echo(string){						// function: allows keyword echo to print text
 		window.document.write(string);			//            instead of using window.document.write
    }
    function daysInMonth(month,year) {			// function: returns the number of days in a month 
		var dd = new Date(year, month, 0);      //           based on the month and year passed in
		return dd.getDate();
    }
    function startMonthOn(month,year){          // function: returns the first weekday of the month
		var dd = new Date(year, month, 1);      //          (0 - 6 represententing day of the week)
		return dd.getDay();                     //          based on the month and yeard passed in
    } 
	function getDayOfWeek(month,year,day) {			// function: returns the number of days in a month 
		var dd = new Date(year, month, day);      //           based on the month and year passed in
		return dd.getDay();
    }
 
//--------------------------------------------------------------------------------------------------//
//  Below are the methods of displaying calendar                                                    //
//--------------------------------------------------------------------------------------------------//

 function showCalendar(){
 	echo("<html>\n<head>\n");
	echo("\t<link href=\"generate_calendar.css\" rel=\"stylesheet\" type=\"text/css\">\n</head>\n");
	echo("<body>\n");
		generateCalendar();						// call to function for displaying full calendar
	echo("\n</body>\n</html>\n");
	window.document.close();
 }
 
//--------------------------------------------------------------------------------------------------//	 
 function generateCalendar(){
	echo("<div id=\"month\">\n");
		echo("<div id=\"month_year\">" + month[m] + " " +  year + "</div>\n");
	
		echo("<div id=\"days\">\n" +
			"<div class=\"day\">S</div>\n" +
			"<div class=\"day\">M</div>\n" +
			"<div class=\"day\">T</div>\n" +
			"<div class=\"day\">W</div>\n" +
			"<div class=\"day\">TH</div>\n" +
			"<div class=\"day\">F</div>\n" +
			"<div class=\"day\">S</div>\n" +
		"</div>\n");
		
//--------------------------------------------------------------------------------------------------//	
		
		echo("<div id=\"dates\">\n");

        for(i=0; i < dayOfWeek; i++){							//loop: prints empty days until first
			echo("<div class=\"day_off\">&nbsp;</div>\n");      //      day of the month is reached.
        }
   
//--------------------------------------------------------------------------------------------------//	
		for(i=1; i<= days_in_month; i++){
			var DOW = getDayOfWeek(m, year, i);
			if(i == today) 		
				echo("<div class=\"day_today\">" + i + "</div>\n");
			else if(i < today || DOW == 0 || DOW == 5 || DOW == 6 ) 	
				echo("<div class=\"day_off\">"   + i + "</div>\n");
			else {
				echo("<div class=\"day_on\"><a href=\"#\" " + 
				"onClick=\"select(" + m + "," + i + "," + year + ")\">" + i + "</a></div>\n");
			}
		}
        echo("</div>\n");
  
 echo("<div>");
 } 