// begin config variables
// Target Date: March 14, 2051
var targetYear = 2051;
var targetMonth = 03;
var targetDay = 14;
var targetHour = 0;
var targetMin = 0;
var targetSec = 0;

// Customize the display
var before_occasion = "<b>Monday, " + targetMonth + "/" + targetDay + "/" + targetYear + "</b><br>which occurs in:";
var on_occasion = "My time just expired!  Please check on me!";
var after_occasion = "I've passed on.  Maybe I left you something...";
var opentags = '<span class="main">';
var closetags = '</span>';

// end config variables

var crosscount = '';

if (document.all || document.getElementById) {
	document.write('<span id="countdownie"></span>')
}

window.onload = start_countdown();

function start_countdown(){
	if (document.layers) {
		document.countdownnsmain.visibility = "show";
	}
	else if (document.all || document.getElementById) {
		crosscount = document.getElementById && !document.all ? document.getElementById("countdownie") : countdownie;
	}
	countdown();
}

function countdown() {
	var today = new Date();
	var currentYear = today.getYear();
	if (currentYear < 1000) {
		currentYear += 1900;
	}
	var currentMonth = today.getMonth() + 1;
	var currentDay = today.getDate();
	var currentHour = today.getHours();
	var currentMinute = today.getMinutes();
	var currentSeconds = today.getSeconds();
	var currentDateString = (currentMonth-1) + " " + currentDay + ", " + currentYear + " " + currentHour + ":" + currentMinute + ":"+currentSeconds;
	var futureDateString = (targetMonth-1) + " " + targetDay + ", " + targetYear + " " + targetHour + ":" + targetMin + ":" + targetSec;
	var secondsLeft = Math.floor(Date.parse(futureDateString)/1000) - Math.floor(Date.parse(currentDateString)/1000);

	dsec = targetSec - currentSeconds; 	// 15 - 43 = -28
	if (dsec < 0) {
		dsec += 60;
		currentMinute += 1;
	}
	dmin = targetMin - currentMinute; 	// 03 - 35 = -32
	if (dmin < 0) {
		dmin += 60;
		currentHour += 1;
	}
	dhour = targetHour - currentHour; 	// 22 - 23 = -1
	if (dhour < 0) {
		dhour += 24;
		currentDay += 1;
	}
	dday = targetDay - currentDay; 	// 09 - 10 = -1
	if (dday < 0) {
		if (currentMonth == 01 || currentMonth == 03 || currentMonth == 05 || currentMonth == 07 || currentMonth == 08 || currentMonth == 10 || currentMonth == 12) {
			dday += 31;
		}
		else if (currentMonth == 04 || currentMonth == 06 || currentMonth == 09 || currentMonth == 11) {
			dday += 30;
		}
		else {
			// feb
			if (currentYear % 4 == 0) {
				// leap year every 4 years
				if (currentYear % 100 == 0) {
					// except for the century
					if (currentYear % 400 == 0) {
						// except for every 4 centuries
						dday += 29;
					}
					else {
						dday += 28;
					}
				}
				else {
					dday += 29;
				}
			}
		}
		currentMonth += 1;
	}
	dmonth = targetMonth - currentMonth;  	// 02 - 03 = -1
	if (dmonth < 0) {
		dmonth += 12;
		currentYear += 1;
	}
	
	dyear = targetYear - currentYear;  	// 2050 - 2006 = 44
	
	// if on day of occasion
	if(secondsLeft == 0) {
		if (document.layers){
			document.countdownnsmain.document.countdownnssub.document.write(opentags + on_occasion + closetags);
			document.countdownnsmain.document.countdownnssub.document.close();
		}
		else if (document.all||document.getElementById) {
			crosscount.innerHTML = opentags + on_occasion + closetags;
		}
		return;
	}
	//if passed day of occasion
	else if (secondsLeft < 0) {
		if (document.layers) {
			document.countdownnsmain.document.countdownnssub.document.write(opentags + after_occasion + closetags)
			document.countdownnsmain.document.countdownnssub.document.close();
		}
		else if (document.all || document.getElementById) {
			crosscount.innerHTML = opentags + after_occasion + closetags;
		}
		return;
	}
	//else, if not yet
	else {
		if (document.layers) {
			document.countdownnsmain.document.countdownnssub.document.write(before_occasion + "<br>" + opentags + "<b>" + dyear + ':' + dmonth + ':' + dday + ' ' + dhour + ':'+ dmin + ':' + dsec + '</b>' + closetags);
			document.countdownnsmain.document.countdownnssub.document.close();
		}
		else if (document.all || document.getElementById) {
			crosscount.innerHTML = before_occasion + "<br>" + opentags + "<b>" + dyear + ' years, ' + dmonth + ' months, ' + dday + ' days<br>' + dhour + ' hours, '+ dmin + ' minutes, ' + dsec + ' seconds</b>' + closetags;
		}
	}
	setTimeout("countdown()", 1000);
}