/* external_date_functions.js
*
* Purpose: Sample external date functions for JavaScript calendar
* Author: Richard Pike)
* Website: http://calendar.pikesys.com
*
* my_date returns an alternate date string corresponding to the given Gregorian date 
* my_holiday returns an appropriate holiday text string for the given Gregorian date
* my_alt_holiday returns an appropriate alternate holiday string for the given Gregorian date
*
*/

function makeArray() {
	this[0] = makeArray.arguments.length;
	for (i = 0; i < makeArray.arguments.length; i = i + 1)
		this[i+1] = makeArray.arguments[i];
}

var altMonthNames = new makeArray("Red", "Yellow", "Blue","Green","Purple","Orange","Cyan","Magenta","Goldenrod","Brown","Black","White");
var altMonths = new Array;

// altMonths constructor
function altMonth(startdate, monthnumber) {
	this.startdate = startdate;
	this.month = monthnumber;
}

function DefineAltMonth(startdate, monthnumber) {
	altMonths[altMonths.length] = new altMonth(startdate, monthnumber);
}

// these could be defined in external JavaScript such as altdates.js (the last altMonth defined will not show because endate is unknown!)
DefineAltMonth(20040404, 1);
DefineAltMonth(20040505, 2);
DefineAltMonth(20040606, 3);
DefineAltMonth(20040707, 4);
DefineAltMonth(20040808, 5);
DefineAltMonth(20040909, 6);
DefineAltMonth(20041010, 7);
DefineAltMonth(20041111, 8);
DefineAltMonth(20041212, 9);
DefineAltMonth(20050113, 10);
DefineAltMonth(20050214, 11);
DefineAltMonth(20050315, 12);
DefineAltMonth(20050416, 1);

function my_date(day, month, year, style) {
	if (!style) style = 0;
	var mo = 0;
	var dy, yr;
	var dateString = "";

	ymd = day + month*100 + year*10000;
	for (i=1; i<altMonths.length-1; i++) {
		if (ymd >= altMonths[i-1].startdate && ymd < altMonths[i].startdate) {
			mo = altMonths[i-1].month;
			startDay = altMonths[i-1].startdate%100;
			startMonth = ((altMonths[i-1].startdate-startDay)/100)%100;
			if (month == startMonth) {
				dy = day - startDay +1;
			} else {
				ym = month + year*100;
				prevYM = PrevYearMonth(ym);
				prevM = prevYM%100;
				prevY = (prevYM - prevM)/100;
				eom = NumDaysIn(prevM, prevY);
				dy = day + (eom - startDay +1);
			}
			yr = year - 1970;
			break;
		}
	}
	if (mo > 0) {
		if (style == 0) { 		// day monthnum year
			dateString = dy + ' ' + mo + ' ' + yr;
		} else if (style == 1) { 	// monthname year
			dateString = altMonthNames[mo] + ' ' + yr;
		} else if (style == 2) { 	// day monthname
			dateString = dy + ' ' + altMonthNames[mo];
		} else if (style == 3) { 	// monthname day
			dateString = altMonthNames[mo] + ' ' + dy;
		} else if (style == 4) { 	// day monthname year
			dateString = dy + ' ' + altMonthNames[mo] + ' ' + yr;
		}
	}
	return dateString;
}

function my_holiday(day, month, year) {
//alert("my_holiday("+day+","+month+","+year+")");
	var holString = "";
	// some American civil holidays
	if (month == 1 && day == 1) {
		holString = "New Year's Day";
	} else if (month == 6 && day == 14) {
		holString = "Flag Day";
	} else if (month == 7 && day == 4) {
		holString = "Independence Day";
	}
	return holString;
}

function my_alt_holiday(day, month, year) {
	var aday, amonth;
	var holString = "";
	
	// calculate alternate date from given Gregorian date
	var altDate = my_date(day, month, year);
	var altMonthYear = altDate.substring(altDate.indexOf(' ')+1, altDate.length);
	aday = eval(altDate.substring(0, altDate.indexOf(' ')));
	amonth = eval(altMonthYear.substring(0, altMonthYear.indexOf(' ')));
	
	if (aday == 1 ) {
		holString = "Happy " + altMonthNames[amonth];
	}
	return holString;
}
