function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
	try {
	req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (err2) {
		try {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (err3) {
			req = false;
		}
	}
}
return req;
}

var http = getXMLHTTPRequest();

function getServerTime() {
	var myurl = 'telltimeXML.php';
	myRand = parseInt(Math.random()*999999999999999);
	var modurl = myurl+"?rand="+myRand;
	http.open("GET", modurl, true);
	http.onreadystatechange = useHttpResponse;
	http.send(null);
}

var holidays = new Array(
  // Dates come from http://www.gov.hk/en/about/abouthk/holiday/index.htm
  // A space before single-digit dates is essential, otherwise 21 January 2008
  // will compare with 1 January 2008. (And it did. :()
  " 1 October 2009",	"National Day",
  " 3 October 2009",	"Chinese Mid-Autumn Festival",
  "26 October 2009",	"Chung Yeung Festival",
  "25 December 2009",	"Christmas Day",
  "26 December 2009",	"the first weekday after Christmas Day",
  // 2010
  " 1 January 2010",    "the first day of January",
  "13 February 2010",	"the day preceding Lunar New Year’s Day",
  "15 February 2010",	"the second day of the Lunar New Year",
  "16 February 2010",	"the third day of the Lunar New Year",
  " 2 April 2010",		"Good Friday",
  " 3 April 2010",		"the day following Good Friday",
  " 5 April 2010",		"Easter Monday",
  " 6 April 2010",		"the day following Ching Ming Festival",
  " 1 May 2010",		"Labour Day",
  "21 May 2010",		"the Buddha’s Birthday",
  "16 June 2010",		"Tuen Ng Festival",
  " 1 July 2010",		"Hong Kong Special Administrative Region Establishment Day",
  "23 September 2010",	"the day following Chinese Mid-Autumn Festival",
  " 1 October 2010",	"National Day",
  "16 October 2010",	"Chung Yeung Festival",
  "25 December 2010",	"Christmas Day",
  "27 December 2010",	"the first weekday after Christmas Day"
);

function checkHolidays(today) 
{
	for (var i = 0; i < holidays.length; i += 2)
	{
		if (today.indexOf(holidays[i]) != -1)
		{
			return holidays[i+1];
		}
	}

	return null;
}

function useHttpResponse() 
{
	if (http.readyState == 4) 
	{
		if(http.status == 200) 
		{
			var timeValue = http.responseXML.getElementsByTagName("timenow")[0];

			if (document.getElementById('showtime'))
			{
				document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
			}

			if (document.getElementById('holiday'))
			{
				var holiday = checkHolidays(timeValue.childNodes[0].nodeValue);

				if (holiday == null)
					document.getElementById('holiday').innerHTML = "";
				else
				{
					var info = "<p>Today is " + holiday + ", a public holiday in Hong Kong. Our office is therefore closed all day.";
					document.getElementById('holiday').innerHTML = info;
				}
			}
		}
	}
}

var timerID = null;
var delay = 60000;

function Timeout()
{
  getServerTime();

  timerID = self.setTimeout("Timeout()", delay)
}
