// Clocks: written by Marc Fluks. Version 03/01/2006
// The results of the calculations are displayed at,
// http://www.me-net.combidom.com/javascript/time.html#clock
// http://www.me-net.combidom.com/javascript/time.html#surfnettime
// The results of the calculations are incorporated at,
// http://www.me-net.combidom.com/melist/day.htm
// http://www.me-net.combidom.com/melist/year.htm
//
// Contents
// --------
// 1. Static clocks.
//      1.1 Start static clocks.
//      1.2 SurfNet.
// 2. Dynamic clocks.
//      2.1 Start dynamic clocks.
//      2.2 Standard clocks (local, world, Internet).
//      2.3 SurfNet (identical to 1.2).
//      2.4 End dynamic clocks.
//    All dynamic clocks must be included, not just a specific clock.
//    Set the width of invisible clocks to zero.
//    Dynamic clocks are present on these pages:
//      http://www.me-net.combidom.com/home.html
//      http://www.me-net.combidom.com/javascript/index.html
//      http://www.me-net.combidom.com/javascript/time.html
//      http://www.me-net.combidom.com/melist/index.html
//      http://www.me-net.combidom.com/melist/search.html
// ------------------------------------------------------------------------
// 1. Static clocks.
// 1.1 Start static clocks.
   var now        = new Date()
// Set a time.
// now.setFullYear(2000,0,1)    // set local date 1 january 2000
// now.setHours(13,14,15)       // set local time 13:14:15
// now.setUTCFullYear(2000,0,1) // set world date 1 january 2000
// now.setUTCHours(13,14,15)    // set world time 13:14:15
// now.setTime(0)               // set unix time in milliseconds
// ------------------------------------------------------------------------
// 1.2 SurfNet.
// Find day of the week for March 25 and October 25.
   var julianmar  = Math.round((30.6*4)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+25+1720981.5
   var julianoct  = Math.round((30.6*11)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+25+1720981.5
   var daymar     = (julianmar+1.5)/7
   var dayoct     = (julianoct+1.5)/7
   var daymar     = Math.round(7*(daymar-Math.round(daymar-.5)))
   var dayoct     = Math.round(7*(dayoct-Math.round(dayoct-.5)))
// Find the last Sunday in March and October.
   var array      = [25,31,30,29,28,27,26,25]
   var daymar     = array[daymar]
   var dayoct     = array[dayoct]
// Find Julian Day for last Sunday in March and October at 1:00 UTC.
   var julianmar  = Math.round((30.6*4)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+daymar+1720981.5
   var julianmar  = julianmar+1.00/24
   var julianoct  = Math.round((30.6*11)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+dayoct+1720981.5
   var julianoct  = julianoct+1.00/24
// Find SurfNetTimezone (values -2 for DST and -1 for standard time).
   var julian     = Math.round(30.6*((now.getUTCMonth()+1)+(((now.getUTCMonth()+1)<3)?13:1))-0.5)+Math.round(365.25*(now.getUTCFullYear()+(((now.getUTCMonth()+1)<3)?-1:0))-0.5)+now.getUTCDate()+1720981.5
   var julian     = julian+(now.getUTCHours()+now.getUTCMinutes()/60+now.getUTCSeconds()/3600)/24
   var SurfNetTimezone = (julian-julianmar)*(julian-julianoct)
   var SurfNetTimezone = -1.5+.5*(SurfNetTimezone/Math.sqrt(SurfNetTimezone*SurfNetTimezone))
// Find SurfNetDay.
   var SurfNetDay = (now.getUTCHours()+now.getUTCMinutes()/60+now.getUTCSeconds()/3600-SurfNetTimezone)/24
   var SurfNetDay = Math.round(SurfNetDay-0.5)
   var SurfNetDay = 7+now.getUTCDay()+SurfNetDay
// Redefine Days from 0 to 6.
   var array      = [0,1,2,3,4,5,6,0,1,2,3,4,5,6,0]
   var SurfNetDay = array[SurfNetDay]
// Find shiftday: is localday later/earlier than SurfNetDay ?
   var shiftday   = 7+(now.getDay()-SurfNetDay)
   var array      = [0,1,0,0,0,0,-1,0,1,0,0,0,0,-1]
   var shiftday   = array[shiftday]
// How many days are present in the local month ?
   var array      = [1,0,0,0]
   var shiftyear  = array[Math.round(4*((now.getFullYear()/4)-Math.round((now.getFullYear()/4)-.5)))]
   var array      = [31,28,31,30,31,30,31,31,30,31,30,31]
   var shiftyear  = shiftyear*((30-array[now.getMonth()])*(31-array[now.getMonth()])/6)
   var daysinmonth = array[now.getMonth()]+shiftyear
// Find shiftweek: is localweek later/earlier than SurfNetWeek ?
   var month28    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0]
   var month29    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0]
   var month30    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0]
   var month31    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,1]
   var shiftweek28 = (month28[now.getDate()])*((29-daysinmonth)*(30-daysinmonth)*(31-daysinmonth)/6)
   var shiftweek29 = (month29[now.getDate()])*((daysinmonth-28)*(30-daysinmonth)*(31-daysinmonth)/2)
   var shiftweek30 = (month30[now.getDate()])*((daysinmonth-28)*(daysinmonth-29)*(31-daysinmonth)/2)
   var shiftweek31 = (month31[now.getDate()])*((daysinmonth-28)*(daysinmonth-29)*(daysinmonth-30)/6)
   var shiftweek  = shiftday*(shiftweek28+shiftweek29+shiftweek30+shiftweek31)
// Find SurfNetWeek and SurfNetYear.
   var array      = [0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5]
   var SurfNetWeek = (array[now.getDate()]+5*(now.getMonth()))-shiftweek
   var array      = [60,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,1]
   var shiftyear  = (array[SurfNetWeek]-SurfNetWeek)/60
   var SurfNetWeek = array[SurfNetWeek]
   var firstdate   = SurfNetWeek
   var SurfNetWeek = ((SurfNetWeek<10)?"0":"")+SurfNetWeek
   var SurfNetYear = now.getFullYear()-shiftyear
// Find SurfNetMonth (0=january, 11=december).
   var SurfNetMonth = Math.round(((SurfNetWeek-1)/5)-.5)
   var array      = ['January','February','March','April','May','June','July','August','September','October','November','December']
   var months     = array[SurfNetMonth]
// Find SurfNetLeapYear: is SurfNetYear a leap year ? (0=no, 1=yes)
   var array      = [1,0,0,0]
   var SurfNetLeapYear = array[Math.round(4*((now.getFullYear()/4)-Math.round((now.getFullYear()/4)-.5)))]
// Find first date of SurfNet week.
   var array      = [0,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29]
   var firstdate  = array[firstdate]
// Find first day of SurfNet week.
   var firstday   = Math.round(30.6*((SurfNetMonth+1)+(((SurfNetMonth+1)<3)?13:1))-0.5)+Math.round(365.25*(SurfNetYear+(((SurfNetMonth+1)<3)?-1:0))-0.5)+firstdate+1720981.5
   var firstday   = (firstday+1.5)/7
   var firstday   = Math.round(7*(firstday-Math.round(firstday-.5)))
// Find shiftdate: how many days is SurfNetDay later than first day of SurfNet week ?
   var shiftdate  = 7+SurfNetDay-firstday
   var array      = [0,1,2,3,4,5,6,0,1,2,3,4,5,6]    
   var shiftdate  = array[shiftdate]
// Find SurfNet date.
   var SurfNetDate = firstdate+shiftdate
   var SurfNetDate = ((SurfNetDate<10)?"0":"")+SurfNetDate
// Find SurfNet time.
   var array      = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2]
   var SurfNetHours   = 24+now.getUTCHours()-SurfNetTimezone
   var SurfNetHours   = array[SurfNetHours]
   var SurfNetMinutes = now.getUTCMinutes()
   var SurfNetSeconds = now.getUTCSeconds()
   var SurfNetTime    = ((SurfNetHours<10)?"0":"")+SurfNetHours+((SurfNetMinutes<10)?":0":":")+SurfNetMinutes+((SurfNetSeconds<10)?":0":":")+SurfNetSeconds
// ------------------------------------------------------------------------
// 2. Dynamic clocks.
// 2.1 Start dynamic clocks.
   var timerID = null
   var timerRunning = false
   function stopclock(){if(timerRunning)clearTimeout(timerID);timerRunning = false;}function showtime(){
   var now        = new Date()
// Set a time.
// now.setFullYear(2000,0,1)    // set local date 1 january 2000
// now.setHours(13,14,15)       // set local time 13:14:15
// now.setUTCFullYear(2000,0,1) // set world date 1 january 2000
// now.setUTCHours(13,14,15)    // set world time 13:14:15
// now.setTime(0)               // set unix time in milliseconds
// ------------------------------------------------------------------------
// 2.2 Standard clocks (local, world, Internet).
// Create standard clocks.
   var array      = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
// Local.
   var local      = " Local: "+array[now.getDay()]+", "+((now.getMonth()<9)?"0":"")+(now.getMonth()+1)+((now.getDate()<10)?"/0":"/")+now.getDate()+"/"+now.getFullYear()+", "+((now.getHours()<10)?"0":"")+now.getHours()+((now.getMinutes()<10)?":0":":")+now.getMinutes()+((now.getSeconds()<10)?":0":":")+now.getSeconds()+"    "
// World.
   var world      = " World: "+array[now.getUTCDay()]+", "+((now.getUTCMonth()<9)?"0":"")+(now.getUTCMonth()+1)+((now.getUTCDate()<10)?"/0":"/")+now.getUTCDate()+"/"+now.getUTCFullYear()+", "+((now.getUTCHours()<10)?"0":"")+now.getUTCHours()+((now.getUTCMinutes()<10)?":0":":")+now.getUTCMinutes()+((now.getUTCSeconds()<10)?":0":":")+now.getUTCSeconds()+"    "
// Internet Time.
   var itime      = (1+now.getUTCHours()+now.getUTCMinutes()/60+now.getUTCSeconds()/3600)/24
   var itime      = 1000*(itime-Math.round(itime-0.5))
   var itime      = Math.round(itime-0.5)
   var itime      = "Internet Time: @"+((itime<10)?"00":(itime<100)?"0":"")+itime
// Write clock.
   document.local.entry.value = local
   document.world.entry.value = world
   document.itime.entry.value = itime
// ------------------------------------------------------------------------
// 2.3 SurfNet (identical to 1.2).
// Find day of the week for March 25 and October 25.
   var julianmar  = Math.round((30.6*4)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+25+1720981.5
   var julianoct  = Math.round((30.6*11)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+25+1720981.5
   var daymar     = (julianmar+1.5)/7
   var dayoct     = (julianoct+1.5)/7
   var daymar     = Math.round(7*(daymar-Math.round(daymar-.5)))
   var dayoct     = Math.round(7*(dayoct-Math.round(dayoct-.5)))
// Find the last Sunday in March and October.
   var array      = [25,31,30,29,28,27,26,25]
   var daymar     = array[daymar]
   var dayoct     = array[dayoct]
// Find Julian Day for last Sunday in March and October at 1:00 UTC.
   var julianmar  = Math.round((30.6*4)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+daymar+1720981.5
   var julianmar  = julianmar+1.00/24
   var julianoct  = Math.round((30.6*11)-0.5)+Math.round((365.25*now.getUTCFullYear())-0.5)+dayoct+1720981.5
   var julianoct  = julianoct+1.00/24
// Find SurfNetTimezone (values -2 for DST and -1 for standard time).
   var julian     = Math.round(30.6*((now.getUTCMonth()+1)+(((now.getUTCMonth()+1)<3)?13:1))-0.5)+Math.round(365.25*(now.getUTCFullYear()+(((now.getUTCMonth()+1)<3)?-1:0))-0.5)+now.getUTCDate()+1720981.5
   var julian     = julian+(now.getUTCHours()+now.getUTCMinutes()/60+now.getUTCSeconds()/3600)/24
   var SurfNetTimezone = (julian-julianmar)*(julian-julianoct)
   var SurfNetTimezone = -1.5+.5*(SurfNetTimezone/Math.sqrt(SurfNetTimezone*SurfNetTimezone))
// Find SurfNetDay.
   var SurfNetDay = (now.getUTCHours()+now.getUTCMinutes()/60+now.getUTCSeconds()/3600-SurfNetTimezone)/24
   var SurfNetDay = Math.round(SurfNetDay-0.5)
   var SurfNetDay = 7+now.getUTCDay()+SurfNetDay
// Redefine Days from 0 to 6.
   var array      = [0,1,2,3,4,5,6,0,1,2,3,4,5,6,0]
   var SurfNetDay = array[SurfNetDay]
// Find shiftday: is localday later/earlier than SurfNetDay ?
   var shiftday   = 7+(now.getDay()-SurfNetDay)
   var array      = [0,1,0,0,0,0,-1,0,1,0,0,0,0,-1]
   var shiftday   = array[shiftday]
// How many days are present in the local month ?
   var array      = [1,0,0,0]
   var shiftyear  = array[Math.round(4*((now.getFullYear()/4)-Math.round((now.getFullYear()/4)-.5)))]
   var array      = [31,28,31,30,31,30,31,31,30,31,30,31]
   var shiftyear  = shiftyear*((30-array[now.getMonth()])*(31-array[now.getMonth()])/6)
   var daysinmonth = array[now.getMonth()]+shiftyear
// Find shiftweek: is localweek later/earlier than SurfNetWeek ?
   var month28    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0]
   var month29    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0]
   var month30    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0]
   var month31    = [0,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,1]
   var shiftweek28 = (month28[now.getDate()])*((29-daysinmonth)*(30-daysinmonth)*(31-daysinmonth)/6)
   var shiftweek29 = (month29[now.getDate()])*((daysinmonth-28)*(30-daysinmonth)*(31-daysinmonth)/2)
   var shiftweek30 = (month30[now.getDate()])*((daysinmonth-28)*(daysinmonth-29)*(31-daysinmonth)/2)
   var shiftweek31 = (month31[now.getDate()])*((daysinmonth-28)*(daysinmonth-29)*(daysinmonth-30)/6)
   var shiftweek  = shiftday*(shiftweek28+shiftweek29+shiftweek30+shiftweek31)
// Find SurfNetWeek and SurfNetYear.
   var array      = [0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5]
   var SurfNetWeek = (array[now.getDate()]+5*(now.getMonth()))-shiftweek
   var array      = [60,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,1]
   var shiftyear  = (array[SurfNetWeek]-SurfNetWeek)/60
   var SurfNetWeek = array[SurfNetWeek]
   var firstdate   = SurfNetWeek
   var SurfNetWeek = ((SurfNetWeek<10)?"0":"")+SurfNetWeek
   var SurfNetYear = now.getFullYear()-shiftyear
// Find SurfNetMonth (0=january, 11=december).
   var SurfNetMonth = Math.round(((SurfNetWeek-1)/5)-.5)
   var array      = ['January','February','March','April','May','June','July','August','September','October','November','December']
   var months     = array[SurfNetMonth]
// Find SurfNetLeapYear: is SurfNetYear a leap year ? (0=no, 1=yes)
   var array      = [1,0,0,0]
   var SurfNetLeapYear = array[Math.round(4*((now.getFullYear()/4)-Math.round((now.getFullYear()/4)-.5)))]
// Find first date of SurfNet week.
   var array      = [0,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29,1,8,15,22,29]
   var firstdate  = array[firstdate]
// Find first day of SurfNet week.
   var firstday   = Math.round(30.6*((SurfNetMonth+1)+(((SurfNetMonth+1)<3)?13:1))-0.5)+Math.round(365.25*(SurfNetYear+(((SurfNetMonth+1)<3)?-1:0))-0.5)+firstdate+1720981.5
   var firstday   = (firstday+1.5)/7
   var firstday   = Math.round(7*(firstday-Math.round(firstday-.5)))
// Find shiftdate: how many days is SurfNetDay later than first day of SurfNet week ?
   var shiftdate  = 7+SurfNetDay-firstday
   var array      = [0,1,2,3,4,5,6,0,1,2,3,4,5,6]    
   var shiftdate  = array[shiftdate]
// Find SurfNet date.
   var SurfNetDate = firstdate+shiftdate
   var SurfNetDate = ((SurfNetDate<10)?"0":"")+SurfNetDate
// Find SurfNet time.
   var array      = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2]
   var SurfNetHours   = 24+now.getUTCHours()-SurfNetTimezone
   var SurfNetHours   = array[SurfNetHours]
   var SurfNetMinutes = now.getUTCMinutes()
   var SurfNetSeconds = now.getUTCSeconds()
   var SurfNetTime    = ((SurfNetHours<10)?"0":"")+SurfNetHours+((SurfNetMinutes<10)?":0":":")+SurfNetMinutes+((SurfNetSeconds<10)?":0":":")+SurfNetSeconds
// Write clock.
   var array      = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
   var SurfNetClock = "SurfNet: "+array[SurfNetDay]+", "
   var array      = ['01','02','03','04','05','06','07','08','09','10','11','12']
   var SurfNetClock = SurfNetClock+array[SurfNetMonth]+"/"+SurfNetDate+"/"+SurfNetYear+", "+SurfNetTime
   document.time.surfnet.value = SurfNetClock
// ------------------------------------------------------------------------
// 2.4 End dynamic clocks.
   timerID = setTimeout("showtime()",1000);timerRunning=true}
   function startclock(){stopclock();showtime()}
