var timer = null

function stop() {
  clearTimeout(timer)
}

function start() {
  var UThours, a, b, jd, jdfloor
  var time = new Date()
  var hours = time.getHours()
  var minutes = time.getMinutes()
  var seconds = time.getSeconds()
  var days = time.getDate()
  var months = time.getMonth()
  var year = time.getFullYear()
  var offset = time.getTimezoneOffset()/60
  var clock = "Eastern time: "
  var unixTime = Math.round(time.getTime()/1000)
  var starDate;

  clock = "According to your computer, the date and time is:\n"
  clock += year 
  clock += ((months < 9) ? "-0" : "-") + (months+1)
  clock += ((days < 10) ? "-0" : "-") + days + "  "
  clock += hours
  clock += ((minutes < 10) ? ":0" : ":") + minutes
  clock += ((seconds < 10) ? ":0" : ":") + seconds
  clock += " UT-" + offset + "hr"

  if (months < 2)
  {
    months += 12;
    year--;
  }
  a = Math.floor(year/100.)
  b = 2 - a + Math.floor(a/4.);
  UThours = hours + offset + minutes/60. + seconds/3600.
  days += UThours/24.;
  jd  = Math.floor(365.25*(year+4716)) + Math.floor(30.6001*(months+2)) + days + b - 1524.5;   
  jdfloor = 10000*Math.floor(jd)
  starDate = -294035.0+5*(jd-2451911.0);
  starDate *= 10000;
  keepjd = jd;  
  mjd = jd-2400000.5;
  jd = 100000.*jd;
  clock += "  DOY=" + time.getDOY();
  clock += "  Julian Date " + Math.round(jd)/100000;
  jd = Math.round(jd-jdfloor);
  mjd = 100000.*mjd;

  clock += (((jd % 100000) == 0)? "0" : "")
  clock += (((jd %  10000) == 0)? "0" : "")
  clock += (((jd %   1000) == 0)? "0" : "")
  clock += (((jd %    100) == 0)? "0" : "")
  clock += (((jd %     10) == 0)? "0" : "")
  mactime = unixTime + 2082844800;
  clock += "\nUnixTime " + unixTime + "  MacTime " + mactime;
  clock += "     MJD   " + Math.round(mjd)/100000;
  clock += "\nSeconds since Gregorian calendar began " + (unixTime+12219292800);
  clock += "\nStarDate " + Math.round(starDate)/10000;
  clock += " Mayan long count ";
  days = Math.floor(keepjd - 2454491.5 + 9);
  baktun = 12 + Math.floor(days/144000);
  r = days % 144000;
  katun = 19 + Math.floor(r/7200);
  r = days % 7200;
  tun = 15 + Math.floor(r/360);
  r = days % 360;
  uinal = Math.floor(r/20);
  kin = days % 20;
  clock += "0.0.0.0." + baktun + "." + katun + "." + tun + "." + uinal + "." + kin

  document.forms[0].display.value = clock;
  timer = setTimeout("start()",1000);
}

Date.prototype.getDOY = function() {
  var onejan = new Date(this.getFullYear(),0,1);
  return Math.ceil((this - onejan) / 86400000);
}
