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/
  // A space before single-digit dates is essential, otherwise 21 January 2008
  // will compare with 1 January 2008. (And it did. :()
  // 2010
  "25 December 2010",	"Christmas Day",
  "27 December 2010",	"the first weekday after Christmas Day",
  " 1 January 2011",    "the first day of January",
  " 3 February 2011",	"Lunar New Year's Day",
  " 4 February 2011",	"the second day of the Lunar New Year",
  " 5 February 2011",	"the third day of the Lunar New Year",
  " 5 April 2011",		"Ching Ming Festival",
  "22 April 2011",		"Good Friday",
  "23 April 2011",		"the day following Good Friday",
  "25 April 2011",		"Easter Monday",
  " 2 May 2011",		"the day following Labour Day",
  "10 May 2011",		"the Buddha's Birthday",
  " 6 June 2011",		"Tuen Ng Festival",
  " 1 July 2011",		"Hong Kong Special Administrative Region Establishment Day",
  "13 September 2011",	"the day following Chinese Mid-Autumn Festival",
  " 1 October 2011",	"National Day",
  " 5 October 2011",	"Chung Yeung Festival",
  "26 December 2011",	"the first weekday after Christmas Day",
  "27 December 2011",	"the second 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)
}

