// eLabs Server Time functions --- START ---   **********************************************************************

// This script implements runnign clock and the status of the office.
// Script had been modified from the original version located at http://www.stuarthomfray.co.uk/testing/whatsmytime.php

var servergmtime = new Date("<%=sOfficeTime%>");
var timecorrection = (new Date() - servergmtime);
var dst = 2;
var gmtOffset = (0 * 3600000);

timeID = window.setTimeout("timeUpdate();", 10);

function timeUpdate() {

  //Get current date
  timeNow1 = new Date();

  //Apply the correction
  var expdate = timeNow1.getTime();
  expdate -= timecorrection;
  timeNow1.setTime(expdate);

  if (dst != 0) {
  	dstinfo=new WhatDates(dst,timeNow1.getFullYear());

	with (dstinfo) {
  		currentYear = timeNow1.getFullYear();

  		var startDST = new Date(currentYear,dstMonth1-1,dstMonth1Date,1,0);
  		var endDST   = new Date(currentYear,dstMonth2-1,dstMonth2Date,1,0);

  		if ((timeNow1.getTime() > startDST.getTime()) && (timeNow1.getTime() < endDST.getTime())) {
			var timeNowMs = timeNow1.getTime();
			timeNowMs = timeNowMs + 3600000;
			timeNow1.setTime(timeNowMs);
  		}
	}
  }

  var timeNowGMT = timeNow1.getTime();
  timeNowGMT = timeNowGMT + (gmtOffset);
  timeNow1.setTime(timeNowGMT);

  var ampm = " AM";
  hours = timeNow1.getHours();
  
  day = timeNow1.getDay();
  
  displayStatus = "OPEN";
  if ((hours < 9) || (hours > 16) || (day == 6) || (day == 7)) {displayStatus = "CLOSED";};  // Monday - Friday, 9am - 5pm
  
  if (hours > 11) 
  {
  	ampm = " PM";
  	if (hours > 12) { hours = hours - 12; };
  };
   
  minutes = timeNow1.getMinutes();
  if (minutes < 10) {minutes = "0" + minutes};

  seconds = timeNow1.getSeconds();

  displayTime = hours+":"+minutes+":";
  if (seconds < 10) {seconds = "0" + seconds};
  displayTime+=seconds + ampm;
    
  if (document.getElementById('showTime')!=null) document.getElementById('showTime').innerText=displayTime;
  if (document.getElementById('showStatus')!=null) 
  {
  	document.getElementById('showStatus').innerText=displayStatus;
  	if (displayStatus == "OPEN") document.getElementById('showStatus').style.color="#00CC00";
  	if (displayStatus == "CLOSED") document.getElementById('showStatus').style.color="red";
  }
  
  timeID=window.setTimeout("timeUpdate();",1000);
}

function WhatDates(dsavingt,thisYear) {
  var dsavingt, thisYear;
  thisYear = Math.round(parseInt(thisYear));

  // USA
  if (dsavingt == 2) {
  	this.dstMonth1 = 4;
  	this.dstMonth2 = 10;
  	this.dstTime = 2;
  	this.dstMonth1Date = (2+6 * thisYear - Math.floor (thisYear / 4) ) % 7 + 1;
  	this.dstMonth2Date = (31-( Math.floor (thisYear * 5 / 4) + 1) % 7);
  }

}


// eLabs Server Time functions --- END ---  **********************************************************************
