﻿function ElapsedTime(inFromDate, inToDate)
{
	var inFromDate = (arguments.length == 0) ? new Date() : arguments[0];
	var inToDate = (arguments.length == 1) ? new Date() : arguments[1];

	// if (arguments.length == 0) var inFromDate = new Date(); // IE4 has a bug in constructors, 
	// if (arguments.length == 1) var inToDate = new Date(); // so use above method. 

	var fromDate = new Date(inFromDate);
	var toDate = new Date(inToDate);

	var tempDate = new Date();
	if (fromDate.getTime() > toDate.getTime())
	{
		tempDate = new Date(fromDate);
		fromDate = new Date(toDate);
		toDate = new Date(tempDate);
	}
	var totMonths = 12 * toDate.getFullYear() + toDate.getMonth() +
-12 * fromDate.getFullYear() - fromDate.getMonth()
	var years = Math.floor(totMonths / 12)
	var months = totMonths - 12 * years
	if (dateAsNumber(toDate, "D") < dateAsNumber(fromDate, "D")) months -= 1
	if (months < 0)
	{
		months = 0
		if (years > 0) years -= 1
	}

	var yearsOff = years + fromDate.getFullYear()
	var monthsOff = months + fromDate.getMonth()
	if (monthsOff >= 12)
	{
		monthsOff -= 12
		yearsOff += 1
	}
	var tempDate = new Date(fromDate);
	tempDate.setFullYear(yearsOff);
	tempDate.setMonth(monthsOff); // might push us into early next month, so... 
	while (tempDate.getDate() < fromDate.getDate() && tempDate.getDate() < 9)
		tempDate.setTime(tempDate.getTime() - 1000 * 60 * 60 * 24); // Feb 29 etc. 

	var milliSecs = toDate.getTime() - tempDate.getTime();
	var oneSecond = 1000;
	var oneMinute = 60 * 1000;
	var oneHour = 60 * oneMinute;
	var oneDay = 24 * oneHour;
	var oneWeek = 7 * oneDay;
	var weeks = Math.floor(milliSecs / oneWeek);
	milliSecs -= weeks * oneWeek;
	var days = Math.floor(milliSecs / oneDay);
	milliSecs -= days * oneDay;
	var hours = Math.floor(milliSecs / oneHour);
	milliSecs -= hours * oneHour;
	var minutes = Math.floor(milliSecs / oneMinute);
	milliSecs -= minutes * oneMinute;
	var seconds = Math.floor(milliSecs / oneSecond);

	var timeValue = "";
	if (years) timeValue += years + ((years == 1) ? " år, " : " år, ");
	if (months) timeValue += months + ((months == 1) ? " måned, " : " mdr, ");
	if (weeks) timeValue += weeks + ((weeks == 1) ? " uge, " : " uger, ");
	if (days) timeValue += days + ((days == 1) ? " dag, " : " dage, ");
	var timeValueDays = timeValue.substring(0, timeValue.length - 2);
	timeValue += hours + ((hours == 1) ? " time, " : " timer, ");
	timeValue += minutes + ((minutes == 1) ? " minut og " : " minutter og ");
	timeValue += seconds + ((seconds == 1) ? " sekund" : " sekunder");

	this.years = years;
	this.months = months;
	this.weeks = weeks;
	this.days = days;
	this.hours = hours;
	this.minutes = minutes;
	this.seconds = seconds;
	this.text = timeValue;
	this.textDays = timeValueDays;
}

function dateAsNumber(inDate, inWhat)
{
	var what = "", yearBit = 0, monthBit = 0
	if (typeof (inWhat) == "undefined" || inWhat.toString() == "" || inWhat.toString() == null) inWhat = ""
	what = inWhat.toString().toUpperCase()
	if (what != "M" && what != "D") // we want yyyy bit 
		yearBit = inDate.getFullYear() * Math.pow(10, 13);
	if (what != "D") // we want month bit 
		monthBit = inDate.getMonth() * Math.pow(10, 11);
	return yearBit +
monthBit +
inDate.getDate() * Math.pow(10, 09) +
inDate.getHours() * Math.pow(10, 07) +
inDate.getMinutes() * Math.pow(10, 05) +
inDate.getSeconds() * Math.pow(10, 03) +
inDate.getMilliseconds()
}

//  *****  SET the DATE and TIME HERE  ***** 
//  ( Remembering that in Java Dates, 
//       months go from 0 (Jan) to 11 (Dec) ) 

function ageClock()
{
	var leaveDate = new Date(1969, 03, 19, 7, 00) // for August 01, 2004 7:31am 
	var now = new Date();
	var elapsed = new ElapsedTime(leaveDate, now);
	return elapsed.text;
}

function getElement(id)
{
	return document.all ? document.all(id) :
document.getElementById ? document.getElementById(id) :
document.layers ? document.layers[id] :
null;
}

function centerShowIt(id)
{
	var winMid, aD = getElement('ageDisplay');
	if (!aD) return;
	if (window.innerWidth) winMid = innerWidth / 2;
	else if (document.body) winMid = document.body.clientWidth / 2;
	if (!document.layers)
	{
		aD.style.left = winMid - aD.offsetWidth / 2;
		aD.style.visibility = 'visible';
	} else
	{
		aD.pageX = winMid - aD.clip.width / 2;
		aD.visibility = 'show';
	}
}

function update()
{
	var text = ageClock();
	var aD = getElement('ageDisplay');
	if (!aD) return;
	if (!document.layers)
	{
		aD.innerHTML = text + ' ';
	} else
	{
		aD.document.write('<SPAN class="bodytext">' + text + ';</SPAN>');
		aD.document.close();
	}
	setTimeout('update()', 1000);
}

/* NS4 resize bug fix from webreference.com */
if (document.layers)
{
	origWidth = innerWidth;
	origHeight = innerHeight;
}
if (document.layers) window.onresize = function ()
{
	if (innerWidth != origWidth || innerHeight != origHeight)
		location.reload();
}
/********************************************/

window.onload = update;
