today = new Date();
//set the minimum year for calendar, based on current year - 2
var min_year = today.getFullYear()-2;
//set the maximum year for calendar, based on minimum year + 10
var max_year = min_year + 10;
var controlName;

function getElem(elem){
	if (document.all)
		return document.all(elem);
	else
		return document.getElementById(elem);
}

//validates the forms date
function validateDate(controlName){
	curr_day = getElem(controlName+"day").value;
	curr_month = getElem(controlName+"month").value;
	curr_year = getElem(controlName+"year").value;
	
	//correct day being 31 in months with ony 30 days
	if ((curr_month == 4 || curr_month == 6 || curr_month == 9 || curr_month == 11) && curr_day == 31){
		getElem(controlName+"day").value = 30;
	}
	//check if month is feb
	if (curr_month == 2){
		if (leapYear(curr_year)){
			if (curr_day > 29){
				getElem(controlName+"day").value = 29;
			}
		}
		else if(curr_day > 28){
			getElem(controlName+"day").value = 28;
		}
	}
}

//calculates whether a given year is a leap year
function leapYear(curr_year)
{
	if (curr_year % 100 == 0)
	{
		if (curr_year % 400 == 0) { return true; }
	}
	else
	{
		if ((curr_year % 4) == 0) { return true; }
	}
	return false;
}

//get the number of days in a given month
function getNumDays(year, month){
	num_days = false;
	
	//correct day being 31 in months with ony 30 days
	if ((month == 4 || month == 6 || month == 9 || month == 11)){
		num_days = 30;
	}
	//check if month is feb
	else if (month == 2){
		if (leapYear(year)){
			num_days = 29;
		}
		else {
			num_days = 28;
		}
	}
	//else must have 31 days
	else {
		num_days = 31;
	}
	return(num_days);
}

var cal_is_hidden = new Array();
//shows \ hides the calendar
function showCalendar(cal_num,cName){
	controlName=cName;

	//check if calendar is hidden
	if (cal_is_hidden[cal_num]){
		//set start value for calendar from form date
		getElem("cal_month["+cal_num+"]").value = getElem(controlName+"month").value;
		getElem("cal_year["+cal_num+"]").value = getElem(controlName+"year").value;

		//update the calendar display
		updateCal(cal_num);
		
		//show calendar
		getElem("cal_shell["+cal_num+"]").style.visibility="visible";
	}
	//else hide calendar
	else{
		getElem("cal_shell["+cal_num+"]").style.visibility="hidden";
	}
	cal_is_hidden[cal_num] = !cal_is_hidden[cal_num];
}

//changes the month of the calendar
function changeMonth(cal_num, month_dir){
	new_month = false;
	new_year = false;
	curr_month = parseInt(getElem("cal_month["+cal_num+"]").value);
	curr_year = parseInt(getElem("cal_year["+cal_num+"]").value);
	//check if we are going to next month
	if (month_dir == 1){
		//check if we are going into next year
		if (curr_month == 12){
			//only change if current year is not last
			if (curr_year < max_year){
				new_month = 1;
				new_year = curr_year + 1;
			}
		}
		//just choose next month
		else{
			new_month = curr_month + 1;
		}
	}
	//must be previous month
	else{
		//check if we are going into previous year
		if (curr_month == 1){
			//only change if current year is not first
			if (curr_year > min_year){
				new_month = 12;
				new_year = curr_year - 1;
			}
		}
		//just choose previous month
		else{
			new_month = curr_month - 1;
		}
	}
	
	//only update if necessary
	if (new_month){
		getElem("cal_month["+cal_num+"]").value = new_month;
		
		if (new_year){
			getElem("cal_year["+cal_num+"]").value = new_year;
		}
		
		//now update the calendar
		updateCal(cal_num);
	}
}

var days_of_week = new Array("Su","Mo","Tu","We","Th","Fr","Sa");

//updates the month on the calendar
function updateCal(cal_num){
	curr_month = getElem("cal_month["+cal_num+"]").value;
	curr_year = getElem("cal_year["+cal_num+"]").value;

	//make a timestamp for the first day on the selected month and year 
	time_stamp = curr_year+"/"+curr_month+"/"+1;
	start_date = new Date(time_stamp);
	//start_date = new Date(curr_year, curr_month, 1, 12);
	
	//find out which week day this day falls on
	start_day = start_date.getDay();
	
	//get the number of days in this month
	num_days = getNumDays(curr_year, curr_month);
	
	//calculate the number of rows needed to display all days
	cal_rows = Math.ceil((num_days+start_day)/7);
	
	//build the table to display the calendar days
	cal_html = "<table cellpadding='2' cellspacing='1' border='0'>";
	cal_html += "<tr>"
	for(col_count = 0; col_count < 7; col_count++){
		cal_html += "<td class='calendar_weekdays'>"+days_of_week[col_count]+"</td>";
	}
	cal_html += "</tr>";
	curr_day = 1;
	for(row_count = 0; row_count < cal_rows; row_count++){
		cal_html += "<tr class='calendar_cell1'>";
		for(col_count = 0; col_count < 7; col_count++){
			cal_html += "<td";
			if (col_count == 0 || col_count == 6){
				cal_html += " class='calendar_cell2'";
			}
			//else{
			//	cal_html += " class='calendar_days'";
			//}
			cal_html += ">";
			if (col_count >= start_day && curr_day <= num_days){
				cal_html += "<a href='javascript:void(0)' onclick='selectDate("+cal_num+","+curr_year+","+curr_month+","+curr_day+")'>"+curr_day+"</a>";
				curr_day++;
				start_day = 0;
			}
			else{
				cal_html += "&nbsp;";
			}
			cal_html += "</td>";
		}
		cal_html += "</tr>";
	}
	cal_html += "</table>";

	//update the calendar with the month table
	getElem("calendar["+cal_num+"]").innerHTML = cal_html;
}

//update the form with the given date
function selectDate(cal_num, year, month, day){
	//update the form 
	getElem(controlName+"day").value = day;
	getElem(controlName+"month").value = month;
	getElem(controlName+"year").value = year;
	getElem(controlName+"day").onchange();
	//hide the calendar
	showCalendar(cal_num);
}
