// Time functions
// by Greg Hartwig (greg at hartwig dot com)
// 2004-08

// serverTime.pl should be included BEFORE including this
// websiteOffset should be set BEFORE including this

 
// Check for missing serverTime variable, which should have come in from serverTime.pl
if (typeof(serverTime) == "undefined") {
	var serverTime = new Date();  // Not found; assume server is at same time as client
}
//serverTime.setTime(serverTime.getTime() /*+ 1000 * 60 * 60 * 1*/);  // Simulate New York for testing

// websiteOffset:  Hours east of server.  Set before including this file
if (typeof(websiteOffset) == "undefined") {
	var websiteOffset = 0;  // Not found; assume website is at same time as server
	document.write("<!-- websiteOffset not set -->\n");
}
websiteOffset *= 1000 * 60 * 60;  // Convert to miliseconds
//alert ("websiteOffset = " + websiteOffset);

var localTime = new Date();
var gmtOffset = localTime.getTimezoneOffset() / 60;  // Convert to hours

var timeDiff = localTime.getTime() - serverTime.getTime() - websiteOffset;
timeDiff = Math.round(timeDiff / 1000 / 60 / 15) * 15 * 60 * 1000;  // Round to the nearest 15 minutes
var timeDiffHours = timeDiff / 1000 / 60 / 60;   // Convert to hours
//alert ("timeDiffHours = " + timeDiffHours);

// Create the GMT time and instructions on how to convert to local time messages
var gmtMsg = "GMT" + (gmtOffset>0 ? "-" : "+")  + Math.abs(gmtOffset);
var convertMsg = ( (timeDiffHours<0) ? "subtract" : "add" ) + " " + Math.abs(timeDiffHours) + " hour" + ((Math.abs(timeDiffHours)==1) ? "" : "s");
convertMsg = "To convert to your local time (" + gmtMsg + "), " + convertMsg;
if (timeDiffHours==0) convertMsg = "Times are in your local time (" + gmtMsg + ")";
//alert("to convert, " + convertMsg);


// Format a time object into a 12-hour human-readable value
function formatTime(timeObj) {
	var hours   = timeObj.getHours();
	var minutes = timeObj.getMinutes();
	var ampm    = "am";
	if (hours >= 12) {
		hours -= 12;
		ampm = "pm";
	}
	if (hours == 0) hours = 12;
	if (minutes < 10) minutes = "0" + minutes;
	var timeval = hours + ":" + minutes + " " + ampm;
	
	// Handle special values
	if (timeval == "12:00 am") timeval = "midnight";
	if (timeval == "12:00 pm") timeval = "noon";	
	return timeval;
}


// Adjust a 12-hour human-readable server-based time to local time
function getLocalTime(timeval) {
	// Handle special values
	if (timeval == "midnight") timeval="0:00";
	if (timeval == "noon")     timeval="12:00";
	// Split off am/pm
	var timeWords = timeval.split(" ");
	timeval = timeWords[0];
	//alert("words=" + timeWords[0] + ", " + timeWords[1]);
	
	var timeParts = timeval.split(":");
	//alert("parts=" + timeParts[0] + ", " + timeParts[1]);
	if (timeWords[1]=="pm") timeParts[0] = parseInt(timeParts[0]) + 12;  // Convert to 24-hour clock
	//alert("parts=" + timeParts[0] + ", " + timeParts[1]);
	
	var ttime = new Date(2001, 6-1, 15, timeParts[0], timeParts[1], 0, 0);  // Desired time on a safe day (June 15, 2001)
	ttime.setTime(ttime.getTime() + timeDiff);
	return formatTime(ttime);
}


// Convert a 12-hour human-readable server-based time to local time and write it out
function showLocalTime(timeval) {
	document.write(getLocalTime(timeval));
}

