/*
 * Functions for customizing the calendar (http://dynarch.com/mishoo/calendar.epl)
 * CareerCross 2007
 * Include this after the form element where you want the calendar to popup
 * Reference: www.irt.org/articles/js050/index.htm
 */

// TODO (will be perfect):
// equinox function (accurately calculate equinox for the year).
// makeup holiday function (e.g. April 29 (Showa day) is Sun, so Mon is pub hol)
// 'sandwich' function (e.g. sept 2009: 21 (aged day), 23 (equinox) - 22 is tues, so made public holiday.)

/*
var SPECIAL_DAYS = {
	// this is static. need a better method
	0 : [ 1 ],					// special days in January
	2 : [ 1, 6, 8, 12, 18 ],	// special days in March
	8 : [ 21, 11 ],				// special days in September
	11: [ 23 ]
};
*/

// note Sun: 1 - Sat: 7.
// get day of week
function DayOfWeek(day,month,year) {
	var a = Math.floor((14 - month)/12);
	var y = year - a;
	var m = month + 12*a - 2;
	var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
			 Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
	return d + 1;
}

// get nth day of month
function NthDay(nth,weekday,month,year) {
	return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
}

function dateIsSpecial(year, month, day) {
	// note Sun: 1 - Sat: 7.
	// equinoxes: 20/21 March, 23/24 Sept - make function?
	// special holidays in japan (note this month starts at 0. NthDay function months start at 1)
	var special = [];
	switch(month){
		case 0:		// Jan
			special.push(1);						// NY day
			special.push(NthDay(2,2,1,year));		// 2nd Mon of Jan; Coming of age day
		break;
		case 1:
			special.push(11);						// national foundation day
		break;
		case 2:
			special.push(20);						// or 21: equinox... MAKE FUNCTION ?
		break;
		case 3:
			special.push(29);						// Showa day
		break;
		case 4:
			special.push(3,4,5);					// golden week
		break;
		case 6:
			special.push(NthDay(3,2,7,year));		// 3rd M of Jul; Marine Day
		break;
		case 8:
			special.push(NthDay(3,2,9,year));		// 3rd M of Sep; Respect aged day
			special.push(20);						// or 21: equinox... MAKE FUNCTION ?
		break;
		case 9:
			special.push(NthDay(2,2,10,year));		// Health sports day
		break;
		case 10:
			special.push(3);						// Culture day
			special.push(23);						// Labour thanks giving day
		break;
		case 11:
			special.push(23);						// Emperors bday
		break;
	}
	
	//var m = SPECIAL_DAYS[month];
	var m = special;
	
	if (!m) return false;
	for (var i in m) if (m[i] == day) return true;
	return false;
};

function ourDateStatusFunc(date, y, m, d) {
	if (dateIsSpecial(y, m, d))
		return "special";
	else
		return false; // other dates are enabled
		// return true if you want to disable other dates
};
