/*
 *	Copyright Daniel J. Berger
 *	These functions may be used or modified freely, with the following conditions:
 *	1. The functions may not be sold or otherwise used to generate revenue in any way.
 *	2. The functions, if used as-is, must be include this copyright block.
 */

// these are common functions for syllabus pages
// Any page that uses these functions MUST define a global array "meetings" that contains the meeting dates as new Date objects, in reverse order. 

var debug = false; // set to true to see diagnostic alerts in this script

// this function finds the course meeting (from the "meetings" array) that comes next after the given date, input as a date object.
function FindToday(date) {
var modDate = 0; // 1000*60*60*24; // modify dates by thus-and-so many milliseconds (24 hours at present)
if (debug) alert("date = " + date);
var the_url = "#";

if (date <= meetings[0])  // check to see whether the date is before the last meeting
		{
		for (var dummy = 0; dummy < meetings.length; dummy++) 
			{
			if (meetings[dummy] < date) 
				{
				dummy = meetings.length - dummy + 1;
				the_url = the_url + dummy;
				if (debug) alert("dummy = " + dummy + "\nthe_url = " + the_url);
				dummy = meetings.length;
				}
			if (dummy == meetings.length-1)
				{ 
				the_url = "#1";
				if (debug) alert("dummy == meetings.length - 1");
				}
			}
		}
else alert("The semester is over!");
if (debug) alert("the_url = " + the_url);
return the_url;
}

// this function finds today's date and puts it into "mdd" format, then returns an internal bookmark from the FindToday function
function TodayDate() {
	var the_date = new Date();
	the_date = FindToday(the_date);
	return the_date;
}

// this function adds 12 hours to a blank date, so that we don't overshoot!
function adjustTime(a_date) {
	var myYear = the_date.getFullYear();
	var myMonth = the_date.getMonth();
	var myDay = the_date.getDate();
	var myDate = new Date(myYear, myMonth, myDay, 18);
	return myDate;
}

// This is a masking function for the text box "the_date". It excludes all characters other than digits and slashes "/". It is copied from Powell & Schneider "JavaScript: the Complete Reference", pp. 495-497 with modified output.
function checkValidity(field, event) {
	var key, keychar;
	// checks to see whether there has been some sort of event in the window, using both DOM 2 and NN4 methodology
	if (window.event) key = window.event.keyCode;
	else if (event) key = event.which;
	else return true;
	
	keychar = String.fromCharCode(key);
	// check for special characters like backspace; then check for numbers and slashes
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27) || (("0123456789/").indexOf(keychar) > -1)) return true;
	else {
		alert("Field accepts numbers or slashes \"/\" only.");
		field.focus();
		return false;
	}
}

// we start by defining a function that returns an error message
function returnError() {
	alert("Please check your entry and try again. \nEnter dates as \"m/d\" \nfor example, June 5 as \"6/5\"");
	return "#form";
}
// end error function

// This function is called if the month is February and checks whether the current year is a leap year. It returns a Boolean.
function LeapYearCheck() {
	var theYear = new Date();
	theYear = theYear.getFullYear();
	theYear = theYear/4;
	var leapYear_int = parseInt(theYear);
	// the current year is a leap year if it is evenly divisible by 4
	if (leapYear_int == theYear) return true;
	else return false;
}

// This function checks to see that a month and date (Number objects) are valid dates. It returns true for a valid date and false for an invalid date. After the first, "if" statements depend on the date not yet having been ruled invalid!
function validDate(month,day) {
	var validity = true;
	// is the month 1-12?
	if ((month < 1) || (month > 12)) validity = false;
	// is the date 1-31?
	if ( (validity) && ( (day < 1) || (day > 31) ) ) validity = false;
	// does the date fit into the correct range for the specified month?
	if (validity) {
		switch (month) {
		// 30 days hath September, April, June and November
			case 4:			
			case 6:			
			case 9:			
			case 11:		if (day > 30) validity = false;
							break;
		// February and Leap Years
			case 2:			if (day > 29) validity = false;
							else if ((!LeapYearCheck()) && (day > 28)) validity = false;
							break;
			default:		validity = validity;
		}
	}
	return validity;
}

// FindDate() takes a date string in mm/dd format and returns an internal bookmark from the FindToday function after converting the date string to a date object. Because we use a masking function to control the input field, we do not need to check for characters other than digits and "/".
function FindDate(date_string) {
	// checks to see that date_string is in #/# format:
	if ( (date_string.indexOf("/") <= 0) || (date_string.indexOf("/") == date_string.length-1) ) return returnError();
	else {
		var toDay = new Date(); // we get a new date to use below for year and hour
		var date_array = date_string.split("/"); // split the date string into its substrings
		// make sure there are either two or three date fields
		if (debug) alert("date_array = " + date_array);
		if ((date_array.length < 2) || (date_array.length > 3)) return returnError();
		// does the date string specify a real date? (e.g. the month is 1-12 and the date is 
		// within the correct range for the specified month)
		else if (!validDate(eval(date_array[0]),eval(date_array[1]))) return returnError();
		// error checking completed

		else {	
			// now we convert date_string into a date object and pass it to FindToday
			// we get the current year and generate a new date object
			if (date_array.length < 3) {
				var theDate = toDay.getFullYear();
				if (debug) alert("toDay.getFullYear() = " + theDate);
				theDate = date_string + "/" + theDate; // attach the current year to the date string
			}
			else var theDate = date_array[0] + "/" + date_array[1] + "/" + date_array[2];
			if (debug) alert("theDate = " + theDate);
			var myHours = toDay.getHours();
			if (debug) alert("myHours = " + myHours);
			theDate = new Date(theDate);
			theDate.setHours(myHours);
			if (debug) alert("theDate = " + theDate);
			theDate = FindToday(theDate); // get the proper URL from FindToday
			return theDate;
		}
	}
}
// end FindDate()
