// Array of day names
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var dayCount = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

var thisDate= new Date();
var theMonth=thisDate.getMonth();	// 0...11
var theDay=thisDate.getDate();		// 1...31
var theYear=thisDate.getFullYear();	// 2003, etc.

var timerID = null;
var timerRunning = false;

function startClock()
{
	if (timerRunning) {
		clearTimeout(timerID);
	}
    timerID = setTimeout("showTime()",100);
    timerRunning = true;
}

function showTime()
{
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    var timeValue = "" + ((hours > 12) ? hours - 12 : hours);
    timeValue  += ((minutes < 10) ? ":0" : ":") + minutes;
    timeValue  += ((seconds < 10) ? ":0" : ":") + seconds;
    timeValue  += (hours >= 12) ? " PM" : " AM";

    timerID = setTimeout("showTime()",1000);
    timerRunning = true;
    timeDiv = document.getElementById("tod");
    timeDiv.innerHTML = timeValue;
}

function monthStr()
{
	var lastDay=dayCount[theMonth];
	if (theMonth == 1 && theYear % 4 == 0) {
		var lastDay = 29;
	}

	var firstDay = new Date(theYear,theMonth,1);
	var aDay = 1 - firstDay.getDay();

	var outStr = "<b>" + monthNames[theMonth] + " " + theYear + "</b>";
	outStr += "<table align='center' border='0' cellpadding='0' cellspacing='0' >";
	while (aDay<=lastDay) {
		outStr += "<tr>";
		pad = "";
		for (col=0; col<7 && aDay<=lastDay; col++) {
			if (aDay < 1) {
				outStr += "<td align='right'>&nbsp;</td>";
			}
			else if (aDay==theDay) { // today's day in bold
				outStr += "<td align='right'><span id='today'>" + aDay + "</span></td>";
			} else { // other days normal
				outStr += "<td align='right'>" + pad + aDay + "</td>";
			}
			pad = "&nbsp;";
			aDay++;
		}
		outStr += "</tr>";
	}
	outStr += "</table>";
	
	return outStr;
}

function newCal()
{
	calStr = "" + dayNames[thisDate.getDay()] + " <span id='tod'>12:59:59 am</span>";
	calStr += "<br>" + theDay + " " + monthNames[theMonth] + " " + theYear;
	calStr += "<hr width='90%'>";
	
	calStr += monthStr();
	calStr += "<br><a href='calendar/index.html'>Any year calendar</a>";

    calDiv = document.getElementById("newCal");
    calDiv.innerHTML = calStr;

	startClock();
}

// open today's chapter from Abide In Christ, by Andrew Murray
function dailyAbide() {
	if (theDay < 10) {
		file = "aic0" + theDay;
	} else {
		file = "aic" + theDay;
	}
	window.open(url="life/am_aic/" + file + ".html");
}