﻿/***************************************************************************************
	Calendar JavaScript
	
		// Varibales
			thisDate: Tracks current date being written in calendar
			wordMonth: Array that stores name of month
			today: Date object to store the current date
			todaysDay: Stores the current day number 1-7
			todaysDate: Stores the current numeric date within the month
			todaysMonth: Stores the current month 1-12
			todaysYear: Stores the current year
			monthNum: Tracks the current month being displayed
			yearNum: Tracks the current year being displayed
			firstDate: Object Storing the first day of the current month
			firstDay: Tracks the day number 1-7 of the first day of the current month  
			lastDate: Tracks the last date of the current month
			direction: Inputs the direction (next, prev, or return) the calendar should flip
		
		// Functions
			changeMonth(): Flips to next, previous, or current month depending on "direction" input
			createCalendar(): Renders the calander into the page with links for each to fill the date form filds above.
			setDate(): Sets the form date fields based on data passed to it from createCalendar()
			
***************************************************************************************/


	var thisDate = 1;
	var wordMonth = new Array("january","february","march","april","may","june","july","august","september","october","november","december");
	var today = new Date();
	var todaysDay = today.getDay() + 1;
	var todaysDate = today.getDate();
	var todaysMonth = today.getUTCMonth() + 1;
	var todaysYear = today.getFullYear();
	var monthNum = todaysMonth;
	var yearNum = todaysYear;
	var firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));
	var firstDay = firstDate.getUTCDay();
	var lastDate = new Date(String(monthNum+1)+"/0/"+String(yearNum));
	

	function changeMonth(direction) {
		if (direction == "prev") monthNum--;
		else if (direction == "next") monthNum++;
		else  if (direction == "return") { 
			monthNum = todaysMonth;
			yearNum = todaysYear;
		}
		if (monthNum == 0) {
			monthNum = 12;
			yearNum--;
		}
		else if (monthNum == 13) {
			monthNum = 1;
			yearNum++
		}
		lastDate = new Date(String(monthNum+1)+"/0/"+String(yearNum));
		numbDays = lastDate.getDate();
		firstDate = new Date(String(monthNum)+"/1/"+String(yearNum));
		firstDay = firstDate.getDay() + 1;
		createCalendar();
		return;
	}
	
	function createCalendar() {
		var writeCalendar = '';	
		
		writeCalendar += '<a class="calControl" onmouseover="window.status=\'Previous Month\';return true" onmouseout="window.status=\'\';return true" href="javascript:changeMonth(\'prev\')">3</a>'
		writeCalendar += '<a class="calControl" href="javascript:changeMonth(\'return\')">=</a>';
		writeCalendar += '<a class="calControl" onmouseover="window.status=\'Next Month\';return true" onmouseout="window.status=\'\';return true" href="javascript:changeMonth(\'next\')">4</a>'
		
		writeCalendar += '<span class="monthShow">';
		writeCalendar += wordMonth[monthNum-1] + '&nbsp;&nbsp;';
		writeCalendar += yearNum;
		writeCalendar += '</span><br style="clear:both" />';
		
		writeCalendar += '<span class="calDay">M</span><span class="calDay">T</span><span class="calDay">W</span><span class="calDay">Th</span><span class="calDay">F</span><span class="calDay">S</span><span class="calDay">S</span>'
		
		
		for (var i = 1; i <= 42; i++) {
			if ((i==1)|| (i==8)|| (i==15)|| (i==22)|| (i==29)|| (i==36)) 
				writeCalendar +=  '<br style="clear:both" />';
			if ((thisDate <= numbDays) && (i >= (firstDay-1))) {
				if ((thisDate == todaysDate) && (todaysMonth == monthNum) && (todaysYear == yearNum)) {
					 
						writeCalendar += '<a class="calDateToday" href="javascript:setDate(' + thisDate + ',' + monthNum + ',' + yearNum + ')">' + thisDate + '</a>';
				}
				
				else 
					writeCalendar += '<a class="calDate" href="javascript:setDate(' + thisDate + ',' + monthNum + ',' + yearNum + ')">' + thisDate + '</a>';
				thisDate++;
			}
			else writeCalendar += '<span class="calEmpty">&nbsp;</span>';
	}
	var object=document.getElementById('calendar');
	object.innerHTML= writeCalendar;
	thisDate = 1;
	}
	
	function setDate(dayVal,monthVal,yearVal) {
		document.forms.dateInput.dayVal.value = dayVal;
		document.forms.dateInput.monthVal.value = monthVal;
		document.forms.dateInput.yearVal.value = yearVal;
	
	}
	
