/*in order to use this script, you need to create a span in the body of the HTML 
document. The span needs an ID = clock. You also need to create a style class called 
clock, which will position the clock-2-be absolutly or relatively, and give it top and 
left coords. The function 'calcDateTime', needs to be called when the document is 
loaded (or after x event). */

function calcDateTime() {
  Days = new Array('Sun', 'Mon', 'Tue', 'Wed',
    'Thu', 'Fri', 'Sat');
  Months = new Array('January', 'February', 'March', 'April', 'May', 'June',
    'July', 'August', 'September', 'October', 'November', 'December');
  Ending = new Array(' ', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 
    'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 
    'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st');
  DaysInMonth = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', 
    '31', '30', '31');

  var now = new Date();
  var year = now.getYear();
  var month = now.getMonth();
  var day = now.getDay();
  var date = now.getDate();
  var hour = now.getHours();
  var minute = now.getMinutes();

  //calculate the difference in local time and gmt
  var hourOffset = parseInt(now.getTimezoneOffset()/60);
  var minuteOffset = now.getTimezoneOffset()%60;   

  var minuteGMT = minute + minuteOffset;
  var hourGMT = hour + hourOffset;
  var dayGMT = 0;
  var dateGMT = 0;
  var monthGMT = 0;
  var yearGMT = 0;
  var sameday = 0; // used as a boolean test value
    
  // sort out the minutes and hours and days and date 
  if (minuteGMT < 0)
  {
    minuteGMT += 60;
    hourGMT -= 1;
  }
  if (minuteGMT >= 60)
  {
    minuteGMT -= 60;
    hourGMT += 1;
  }

  // GMT and local on a different day   
  if (hourGMT >=24 || hourGMT < 0)
  { 
    // GMT behind local
    if (hourGMT < 0)
    // correct the hour and go back one day
    {
      hourGMT += 24;
      if (day == 0)
        dayGMT = 6;
      else
        dayGMT = day - 1;
      // then correct the date
      // case first day of month
      if (date == 1)
      {
        //case first month of year
        if (month == 0)
        {
          monthGMT = 11;
          dateGMT = DaysInMonth[monthGMT];
          yearGMT = year - 1;
        }
        //case other months
        else
        {
          monthGMT = month - 1;
          dateGMT = DaysInMonth[monthGMT];
          yearGMT = year;
        }
      }
      // case other days of month
      else
      {
        dateGMT = date - 1;
        monthGMT = month;
        yearGMT = year;
      }
    }
    // GMT ahead of local   
    else if (hourGMT >= 24)
    // correct the hour and go forward one day
    {
      hourGMT -= 24;
      if (day == 6)
        dayGMT = 0;
      else
        dayGMT = day + 1;
      // then correct the date
      // case last day of month
      if(date == DaysInMonth[month])
      {
        //case last month of year
        if(month == 11)
        {
          monthGMT = 0;
          dateGMT = 1;
          yearGMT = year + 1;
        }
        // case other months
        else
        {
          dateGMT = 1;
          monthGMT = month + 1;
          yearGMT = year;
        }
      }
      // case other days
      else
      {
        dateGMT = date + 1;
        monthGMT = month;
        yearGMT = year;
      }     
    }
  }
  else
  // GMT and local on the same day
  {
    dayGMT = day;
    dateGMT = date;
    monthGMT = month;
    yearGMT = year;
    sameday = 1;
  }
 
  var timeLocal = ( addZero(hour) + ":" + addZero(minute) );
  var timeGMT = ( addZero(hourGMT) + ":" + addZero(minuteGMT) );
  var calLocal = ( (Days[day]) );
  var calGMT = ( (Days[dayGMT]) );

  output(timeLocal, timeGMT, calLocal, calGMT, sameday);
  
  setTimeout("calcDateTime()", 1000);
} /* end calcDateTime */

function addZero(value) {
  if (value <=9)
    value = '0' + value;
  return value;
}

function output(timeLocal, timeGMT, calLocal, calGMT, sameday)
{
  //this is the section which formats how the time/date is displayed
  if ( sameday==1 )
	{
    var timeAndDate = 
      "<SPAN><DIV CLASS='clocktext'>Local: "  
        + timeLocal + 
          "&nbsp;&nbsp;GMT: " 
            + timeGMT + "</div></span>";
  }
  else
  {
    var timeAndDate = 
      "<SPAN><DIV CLASS='clocktext'>Local: "  
        + timeLocal + 
          "&nbsp;&nbsp;GMT: " 
            + timeGMT + "&nbsp;(" + calGMT +	")"+"</div></span>";
  }
        
	var docProp;
  
	if ( document.getElementById )
		docProp = document.getElementById("clock");
	else if ( document.layers )
		docProp = document.layers["clock"];
	else if ( document.all )
		docProp = document.all.item("clock");
  
	if (docProp.innerHTML)
  {    
    docProp.innerHTML = timeAndDate;
	}
	else
  {
	  docProp.document.write(timeAndDate);
  	docProp.document.close();
  }
} /* end output */

