///////////////////////////////////////
//                                   //
//                                   //
//         Calendar.js               //
//                                   //
//         By Terrell Harris         //
//                                   //
//                                   //
///////////////////////////////////////

	/* To display the calendar write this out inside of the div you want to contain the calendar
			<script type="text/javascript">
			var cal = new Calendar();
				cal.generateHTML();
				document.getElementById('calendar').innerHTML = cal.getHTML();
			</script>
			
			
		To display the time  create a div called 'time' anywhere on the page
		
		To display the current date create a div called 'day' anywhere on the page
		
	*/

//global variables
var calDays = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
var calMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var calDaysMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var curDate = new Date();
	
	function Calendar(month, year) {
		this.month = (isNaN(month) || month === null) ? curDate.getMonth() : month;
		this.year = (isNaN(year) || year === null) ? curDate.getFullYear() : year;
		this.html = '';
	}

	Calendar.prototype.generateHTML = function() {
		var firstDay = new Date(this.year, this.month, 1);
		
		var startingDay = firstDay.getDay();
		
		var monthLength = calDaysMonth[this.month];
		
				
		if (this.month == 1) { // February only!
		  if ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0){
			monthLength = 29;
		  }
		}
	
		var monthName = calMonths[this.month];
		var html = '<table cellpadding="0" cellspacing="1">';
		html +=  '<th colspan="7">';
		html += '<span style="text-align:center">' + monthName + '&nbsp;' + this.year + '</span>';
		html += '</th>';
		html += '<tr>';
		for (var i=0; i <= 6; i++) {
			html += '<th scope="col" ><span class="weekDays">';
			html += calDays[i];
			html += '</span></th>';
		}
		html += '</tr>';
		
		  // fill in the days
		  var day = 1;

		  // this loop is for is weeks (rows)
		  for (var i = 0; i < 9; i++) {
			// this loop is for weekdays (cells)
			for (var j = 0; j <= 6; j++) { 
			  html += '<td class="calendar-day">';
				if (day <= monthLength && (i > 0 || j >= startingDay)) {
					if (day == curDate.getDate()) {
						html += '<span class="cur">' + day + '</span>';
						day++;
					} else {
						html += day;
						day++;
					}
				} 
			  html += '</td>';
			}
			
			
			  
			// stop making rows if we've run out of days
			if (day > monthLength) {
			  break;
			} else {
			  html += '</tr><tr>';
			}
		  }
		  html += '</tr></table>';
				
		  this.html = html;
	}
	
	Calendar.prototype.getHTML = function() {
	  return this.html;
	}
	
	function writeDay() {
		var day = new Date();
		
			var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
			var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
			
			document.getElementById('day').innerHTML = weekday[day.getDay()] + '&nbsp;' + month[day.getMonth()] + '&nbsp;' + day.getDate() + ',' + '&nbsp;' + day.getFullYear();
	}
		
	function startTime() {
		var today = new Date();
		var h = today.getHours();
		var m = today.getMinutes();
		var s = today.getSeconds();
		var day = today.getDay();
		// add a zero in front of numbers<10
		m = checkTime(m);
		s = checkTime(s);
	
		document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
		writeDay();
		setTimeout('startTime()',500);
	}
	
	function checkTime(i) {
		if (i<10) {
			i="0" + i;
		}
		  return i;
	}