function printLitColor()
{
	today = new Date();
	navColor = getLitColor(today);
	document.writeln('<BR>The liturgical color today is <span class="litcolor">' +
		navColor + '</span>.');
}

function styleLitColor()
{

	today = new Date();
	todayLitColor = getLitColor(today);
	document.writeln('<style>');
	document.writeln('<!--');

	if (todayLitColor == "white") {
		document.writeln('h1, h2 { background: black; }');
		document.writeln('h3, h4 { color: black}');
		document.writeln('.litcolor { background: black; color: white; font-size: larger; font-weight: bold; }');
		document.writeln('a.navbar { background : black; }');
		document.writeln('td.button { background : black; }');
	}
	else {
		document.writeln('h1, h2 { background: ' + todayLitColor + '; }');
		document.writeln('h3, h4 { color: ' + todayLitColor + '; }');
		document.writeln('.litcolor { color: ' + todayLitColor + '; font-size: larger; font-weight: bold; }');
		document.writeln('a.navbar { background: ' + todayLitColor + '; }');
		document.writeln('td.button { background : ' + todayLitColor + ' }');
	}

	document.writeln('-->');
	document.writeln('</style>');

}

function getLitColor(today)
{
	jToday = cnv2JulianDate(today);
	mToday = today.getMonth();

	if (mToday == 0) {
		lColor = janColor(today,jToday);
	}
	else if ((mToday >= 6) && (mToday <= 9)) {
		lColor = "green";
	}
	else if (mToday >= 10) {
		lColor = adventColor(today,jToday);
	}
	else {
		lColor = easterColor(today,jToday);
	}

	return lColor;

}

function janColor(today,jToday) {

	Jan7 = new Date();
        Jan7.setMonth(0);
	Jan7.setDate(7);
	Jan7.setYear(today.getFullYear());

	if (today.getTime() < Jan7.getTime()) {
		return "white";
	}
	nextSunday = cnv2JulianDate(Jan7);
	if (Jan7.getDay() > 0) {
		nextSunday += (7 - Jan7.getDay());
	}

	if (jToday == nextSunday) {
		return "white";
	}
	else {
		return "green";
	}

}

function adventColor(today,jToday) {

	if ((today.getDate() == 1) && (today.getMonth() == 10)) {
		return "white";
	}
	firstAdvent = getFirstAdvent(today);

	if (jToday == (firstAdvent - 7)) {
		return "white";
	}
	if (jToday < firstAdvent) {
		return "green";
	}
	Dec24 = new Date();
        Dec24.setMonth(11);
	Dec24.setDate(24);
	Dec24.setYear(today.getFullYear());

	if (today.getTime() > Dec24.getTime()) {
		return "white";
	}
	return "purple";

}

function easterColor(today,jToday) {

	Easter = getEaster(today.getFullYear());

	if (jToday == (Easter - 7)) {
		return "red"
	}
	else if (jToday == (Easter - 49)) {
		return "white"
	}

	AshWednesday = Easter - 46;
	if (jToday < AshWednesday) {
		return "green"
	}
	else if (jToday <= (Easter - 3)) {
		return "purple"
	}

	Pentecost = Easter + 49;
	if ((jToday >= Pentecost) && (jToday <= (Pentecost + 6))) {
		return "red";
	}
	else if (jToday > (Pentecost + 7)) {
		return "green";
	}
	return "white";

}

function cnv2JulianDate(d) {

	if (Math.floor(d.getFullYear() % 4) > 0) {
		var numDayz = new Array(0,31,59,90,120,151,181,212,243,273,304,334);
	}
	else {
		var numDayz = new Array(0,31,60,91,121,152,182,213,244,274,305,335);
	}

	jd = Math.round(numDayz[d.getMonth()]) + Math.round(d.getDate());
	return jd;

}

function getFirstAdvent(d) {

	Dec25 = new Date();
        Dec25.setMonth(11);
	Dec25.setDate(25);
	Dec25.setYear(today.getFullYear());

	advent1st = cnv2JulianDate(Dec25);
	if (Dec25.getDay() > 0) {
		advent1st -= (21 + Dec25.getDay());
	}
	return advent1st;

}

function getEaster(year) {

	a = Math.round(year%19);
	b = Math.floor(year/100);
	c = Math.round(year%100);
	d = Math.floor(b/4);
	e = Math.round(b%4);
	f = Math.floor((b+8)/25);
	g = Math.floor((b-f+1)/3);
	h = Math.round((19*a+b-d-g+15)%30);
	i = Math.floor(c/4);
	k = Math.round(c%4);
	l = Math.round((32+2*e+2*i-h-k)%7);
	m = Math.floor((a+11*h+22*l)/451);
	easterMonth = Math.floor((h+l-7*m+114)/31);
	p = Math.round((h+l-7*m+114)%31);
	easterDate = p+1;

	easter = new Date();
        easter.setMonth(easterMonth-1);
	easter.setDate(easterDate);
        easter.setYear(year);

	jEaster = cnv2JulianDate(easter);

	return jEaster;
}

