// ##########################
// World Time clock.
// Written: Simon Schmidt
// Date: 23 January 2001
// Bugs: y3k problem see below
// The clock works by taking the current timem off the users system,
// and converting it to GMT. A modifer is then calculated from the
// GMT offset (part of JavaScript) for the local time and 
// the offset for the area you want the time dispalyed in.
// Also daylight savings is taken into account, by entering the date
// it starts and ends.
// ##########################

// this function returns the input date as millisceonds since 1/1/1970.
// it is used to calculate the number for comparsion for the DST swap overs.
function showMillisec(n) {
	// n must be in the form "yy,mm,dd,hh,mm,ss" (with "'s)
	var findmillsec = new Date(n)
	return findmillsec.getTime()
}

// this function helps when creatign arrays.
function MakeArray(n) {
	this.length = n
	return this
}
// creating an array to make the months more friendly.
months = new MakeArray(12)
months[0] = "Jan"
months[1] = "Feb"
months[2] = "Mar"
months[3] = "Apr"
months[4] = "May"
months[5] = "Jun"
months[6] = "Jul"
months[7] = "Aug"
months[8] = "Sep"
months[9] = "Oct"
months[10] = "Nov"
months[11] = "Dec"

function showTime() {
	var now = new Date() // get the date off the users computer.
	var offset = now.getTimezoneOffset() // get the timezone offset off the users computer
	var MilliSecondsSince = now.getTime() // get the current time in millisceonds since 1/1/1970
	
	var Modifier // Must be in milliseconds
	// showMillisec must have the date string as "yy,mm,dd,hh,mm,ss" (with "'s)
	
	var DSTChangeTo = showMillisec("March 26, 2006 01:00:00") // the date daylight savings starts 
	var DSTChangeback = showMillisec("October 29, 2006 02:00:00") // the date daylight savings ends 

	// this if statement figures out the modifier for the time zone difference and DST
	if (MilliSecondsSince >= DSTChangeTo && MilliSecondsSince <= DSTChangeback) {
		// hours difference to GMT * milliseconds in a day
		Modifier = 1 * 3600000 
	} else {
		// hours difference to GMT * milliseconds in a day
		Modifier = 0 * 3600000 
	}
	
	// the offset brings the clock to GMT, adding the modifier brings it to the actual time.
	offset = (offset * 60000) + Modifier
	now.setTime(MilliSecondsSince + offset)
	
	var ampm = "am" // for ease of reading
	var theHour = now.getHours(); if (theHour >= 12) ampm = "pm" // see if it is the afternoon
	var theMin = now.getMinutes()
	var theDate = now.getDate()
	var theMonth = now.getMonth(); theMonth = months[theMonth] // turn the month into a friendly word.
	var theYear = now.getYear()
	var theSecond = now.getSeconds()
	// the following if statement has a bug, that will cause chaos in the year 3000.
	// it fix it change the 1100 to a yesr less than the current and 
	// greater than the difference between 1900 and the current year.
	if (theYear < 1100) theYear += 1900 // 1900 needs to be added cause of the way JS works.
	// put the string to be displayed together
	var currentTime = ((theHour > 12) ? theHour - 12 : theHour) + ((theMin < 10) ? ":0" : ":") + theMin + " " + theSecond + " " + ampm
	var theTime = theDate + " " + theMonth + " " + theYear  + " " + " - " + currentTime 
	
	// theTime += ((theHour > 12) ? theHour - 12 : theHour) + ((theMin < 10) ? ":0" : ":") + theMin + ampm
	// show the time in an input box in a form on the page.
	document.JSClock.CurrentTime.value = theTime;
	// set the timeout so it refreshes the clock.
	timerID = setTimeout("showTime()", 1000)
}
