// JavaScript by Peter Hayes http://www.peter-hayes.freeserve.co.uk/
// Copyright 2001-2003
// This code is made freely available but please keep this notice.
// I accept no liability for any errors in my coding but please
// let me know of any errors you find. My address is on my home page.

// Various date and time functions

// getFullYear returns a 4 digit year allowing for browser bugs
// now is a JavaScript date object which works from 1900
// assume that a date of 00 is actually a year 2000 bug NOT 1900

function getFullYear(now) {
  var year = now.getYear();
  if (year==0) {year=2000}
  if (year<1900) {year=year+1900}
  return year;
}

// Global variables

var month_length=new Array(31,28,31,30,31,30,31,31,30,31,30,31);

// Determines whether the year is a leapyear

function leapyear(year) {
  var leap=false;
  if (year % 4 == 0) leap = true;
  if (year % 100 ==0 ) leap = false;
  if (year % 400 == 0) leap = true;
  return leap;
}

// The Julian date at 0 hours UT at Greenwich

function jd0(year,month,day) {
  var y  = year;
  var m = month;
  if (m < 3) {m += 12; y -= 1};
  var a = Math.floor(y/100);
  var b = 2-a+Math.floor(a/4);
  var j = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524.5;
  return j;
}

// The calendar date from julian date
// Returns year, month, day, day of week, hours, minutes, seconds

function jdtocd(jd) {
  var Z=Math.floor(jd+0.5);
  var F=jd+0.5-Z;
  if (Z < 2299161) {
    var A=Z;
  } else {
    var alpha=Math.floor((Z-1867216.25)/36524.25);
    var A=Z+1+alpha-Math.floor(alpha/4);
  }
  var B=A+1524;
  var C=Math.floor((B-122.1)/365.25);
  var D=Math.floor(365.25*C);
  var E=Math.floor((B-D)/30.6001);
  var d=B-D-Math.floor(30.6001*E)+F;
  if (E < 14) {
    var month=E-1;
  } else {
    var month=E-13;
  }
  if ( month>2) {
    var year=C-4716;
  } else {
    var year=C-4715;
  }
  var day=Math.floor(d);
  var h=(d-day)*24;
  var hours=Math.floor(h);
  var m=(h-hours)*60;
  var minutes=Math.floor(m);
  var seconds=Math.round((m-minutes)*60);
  if (seconds >= 60) {
    minutes=minutes+1;
    seconds=seconds-60;
  }
  if (minutes >= 60) {
    hours=hours+1;
    minutes=0;
  }
  var dw=Math.floor(jd+1.5)-7*Math.floor((jd+1.5)/7);
  return new Array(year,month,day,dw,hours,minutes,seconds);  
}

function hmstring(t) {
  var hours = Math.abs(t);
  var minutes = Math.round(60.0*(hours-Math.floor(hours)));
  hours=Math.floor(hours);
  if (minutes >= 60) { hours+=1; minutes-=60; }
  if (hours >= 24) { hours-=24; }
  var hmstr=(t < 0) ? "-" : "";
  var twhours = hourclock(hours);
  hmstr+=twhours[0];
  hmstr+=((minutes < 10) ? ":0" : ":" )+minutes;
  hmstr+=" "+twhours[1];
return hmstr;
}

function hmsstring(t) {
// takes hours and returns hh:mm:ss
  var hours = Math.abs(t);
  var minutes = 60.0*(hours-Math.floor(hours));
  hours=Math.floor(hours);
  var seconds = Math.round(60.0*(minutes-Math.floor(minutes)));
  minutes=Math.floor(minutes);
  if (seconds >= 60) { minutes+=1; seconds-=60; }
  if (minutes >= 60) { hours+=1; minutes-=60; }
  if (hours >= 24) { hours-=24; }
  var hmsstr=(t < 0) ? "-" : "";
  hmsstr+=((hours < 10) ? "0" : "" )+hours;
  hmsstr+=((minutes < 10) ? ":0" : ":" )+minutes;
  hmsstr+=((seconds < 10) ? ":0" : ":" )+seconds;
  return hmsstr;
}

function hourclock(nhours) {
  if (nhours>=12) {
      AorP="PM";
  } else {
      AorP="AM";
  }
  if (nhours>=13) {
	  nhours-=12;
  }
  if (nhours==0) {
	  nhours=12;
  }
  return new Array(nhours,AorP);
}

function dstime(year,month,date) {
  var gmt = new Date;
  var lsm = new Date;
  var lso = new Date;
  lsm.setMonth(2); // March
  lsm.setDate(31);
  lsm.setYear(year);
  var day = lsm.getDay();// day of week of 31st
  lsm.setDate(31-day); // last Sunday
  lso.setMonth(9); // October
  lso.setDate(31);
  lso.setYear(year);
  day = lso.getDay();
  lso.setDate(31-day);
  gmt.setMonth(month-1); gmt.setYear(year); gmt.setDate(date);
  if ((gmt<lsm) || (gmt>=lso)) {
	  dst=0;
  } else {
	  dst=1;
  }
  return (dst);
}

function daytag(day) {
  if ((day==1) || (day==11) || (day==21) || (day==31)) {
	  var tag="st";
  } else if ((day==2) || (day==12) || (day==22)) {
	  var tag="nd";
  } else if ((day==3) || (day==13) || (day==23)) { 
	  var tag="rd";
  } else {
	  var tag="th";
  }
  return tag;
}
