
function setClock()
 {
  baselvl = new Date()
  basesec = baselvl.getSeconds()
  getDayLightStart(reftime.year)
  getDayLightEnd()
  lastSatFeb = getlastsat_feb(reftime.year);
  lastSunFeb = getlastsun_feb(reftime.year);
  SunGeMar9  = getsunge_mar9();
  firstSunOct = getfirstsun_oct();
  firstSunGeOct9 = getsunge_oct9();
  firstSunGeApr6 = getsunge_apr6();
  firstSunGeSep8 = getsunge_sep8(); 
  makeMonthArray()
  makeDayArray()
  makePlaceArrays()
  return true
 }

 
function getStandardTime(stime)   // 45
 {
   var syear   = stime.year
   var smonth  = stime.month
   var sdate   = stime.date
   var sday    = stime.day
   var shour   = stime.hour
   var smin    = stime.min
   var ssec    = stime.sec 
   var year4   = syear  
   var Mlength = months[smonth].length
   if (smonth == 2)
     { Mlength = getFebMonthDays(year4)}
   if (ssec > 59)
     {
       ssec -= 60
       smin += 1
     } 
   if (smin  > 59)
     {                                // 64
      smin -= 60
      shour += 1 
     }
   if (shour > 23)
    {
    shour = shour - 24
    sdate = sdate + 1
    sday  = sday + 1
    }
   if (sday > 6)
    { sday = 0 }

   if (sdate > Mlength)
    {
     sdate  = 1
     smonth += 1
   
    if (smonth > 12) 
    {
     smonth = 1
     syear += 1
    }
   }
  // when correction is -ve

  if (smin < 0)
   {
    smin += 60
    shour -= 1
   }
  if (shour < 0)
   {
    shour += 24
    sday  -= 1
    sdate -= 1
   }
  if (sday < 0 )
   { sday = 6}
  if (sdate <= 0)
    {
    smonth -= 1                 
    if (smonth == 0) 
     { 
     smonth = 12
     syear -= 1
     }                          // 110
    Mlength = months[smonth].length
    if (smonth == 2)
     { Mlength = getFebMonthDays(year4)} 
    sdate = Mlength                 
    }                              // 110
    stime.year  = syear
    stime.month = smonth
    stime.date  = sdate
    stime.day   = sday
    stime.hour  = shour
    stime.min   = smin
    stime.sec   = ssec 

    return stime;    
 }

function showTime()                // 125
{
 var ss
 var cs
 var cd  
 if (revstep == 0) 
 { reftime.sec += 1 }
 else
  { revstep += 1 }    
 reftime=getStandardTime(reftime)    
 computeTime();
 execnum +=1 
 if (execnum == 10)
  { 
   execnum= 0
   cd = new Date()
   cs = cd.getSeconds()
   ss = cs - basesec
   if (ss < 0)
    { ss +=60 }
   syncClock(ss)
   basesec = cs
  }
 setTimeout("showTime()", timeintvl);
}                                         // 145
function syncClock(ss)
 {
   if (ss > 10 )
    { reftime.sec += ss - 10 }
    else
      { revstep = ss - 10 }
   if (timeintvl > 100)
    {

   if (ss > 0)  
    {
      timeintvl *= 10
      timeintvl /= ss
    }
    }
 
 } 
function getDayLightStart(anyyear)
 {
    
   anyyear -= 2000
  
   var x = anyyear % 28
    
   if (x == 0)
       dayStart = 2
   else if (x == 1)               // 154
       dayStart = 1
   else if (x == 2)
       dayStart = 7
   else if (x == 3)
       dayStart = 6
   else if (x == 4)
       dayStart = 4
   else if (x == 5)
       dayStart = 3
   else if (x == 6)
       dayStart = 2
   else if (x == 7)
       dayStart = 1
   else if (x == 8)
       dayStart = 6
   else if (x == 9)
       dayStart = 5
   else if (x == 10)
       dayStart = 4
   else if (x == 11)
       dayStart = 3
   else if (x == 12)
       dayStart = 1
   else if (x == 13)
       dayStart = 7
   else if (x == 14)
       dayStart = 6
   else if (x == 15)
       dayStart = 5
   else if (x == 16)                 
       dayStart = 3
   else if (x == 17)
       dayStart = 2
   else if (x == 18)
       dayStart = 1
   else if (x == 19)
       dayStart = 7
   else if (x == 20)
       dayStart = 5
   else if (x == 21)
       dayStart = 4
   else if (x == 22)
       dayStart = 3
   else if (x == 23)
       dayStart = 2
   else if (x == 24)               // 200
       dayStart = 7
   else if (x == 25)
       dayStart = 6
   else if (x == 26)
       dayStart = 5
   else if (x == 27)
       dayStart = 4
  return true
 }
function getDayLightEnd()
{
 if (dayStart < 5 )
     dayEnd = dayStart + 27
 else if (dayStart > 4)            
     dayEnd = dayStart + 20       // 215 
}
function getlastsat_feb(x)
 {                               // 100
  if (dayStart < 6)
   return (dayStart + 23 + leapyear(x))
  else return (dayStart + 16 + leapyear(x))
 }

function getlastsun_feb(x)
 {
  if (dayStart < 5)
   return (dayStart + 24 + leapyear(x))
  else return (dayStart + 17 + leapyear(x))  
 } 
function getsunge_mar9()
 {
  if (dayStart > 5)
     return (dayStart + 3)
  else return (dayStart + 10)
 } 
function getsunge_apr6()
 {
 if (dayStart > 5)
     return dayStart
  else return (dayStart + 7)
 } 
function getfirstsun_oct()
{
 if (dayStart == 1)
     return 7
  else return (dayStart - 1)
 }
function getsunge_sep8()
{
  if (dayStart == 7)
  return 8
  else
  return (dayStart + 8)
 } 
function getsunge_oct9()
{
  if (dayStart < 3)
     return (dayStart + 13)
  else return (dayStart + 6)
 } 
function leapyear(pyear)
 {
 if ((pyear % 4 == 0 && pyear % 100 !=0) ||(pyear % 400 ==0))
  return 1
else
  return 0
}
function computeTime()
{
 var i = 1;
 
 for (i=1; i <= 16; i++)
  {
    saPlaces[i] = placeTime(saPlaces[i]);     
    document.saForm.saTime[i-1].value= saPlaces[i].curtime;
  }
}
function placeTime(splace)
 {
  var  refx= new xtime()
  refx.hour = reftime.hour + splace.hrcorr
  refx.sec  = reftime.sec 
  refx.min  = reftime.min + splace.mincorr 
  refx.day  = reftime.day
  refx.date = reftime.date
  refx.month= reftime.month
  refx.year = reftime.year              
  var apind = "AM"
  refx=getStandardTime(refx);
  // apply day light correction if applicable
  if (splace.dual == 1)           // 230
   {
    refx.hour += correctDLH1(refx); 
    refx=getStandardTime(refx);  
   } 
  else if (splace.dual == 2)
    {
    refx.hour += 2 * correctDLH2(refx); 
    refx=getStandardTime(refx);  
    }  
  else if (splace.dual == 3)
    {
    refx.hour += correctDLH3(refx); 
    refx=getStandardTime(refx);  
    } 
  else if (splace.dual == 4)
    {
    refx.hour += correctDLH4(refx); 
    refx=getStandardTime(refx);  
    }                               
  if (refx.hour == 12)
   {
     
    apind = "NN"
   } 
    
  
  if (refx.hour > 12)
   {
    refx.hour -= 12
    apind = "PM"
   } 
    
  splace.curtime = dayText(refx.day) + " "
  splace.curtime += monthText(refx.month) + " "
  splace.curtime += refx.date
  splace.curtime += ", "
  splace.curtime += refx.hour
  splace.curtime += ((refx.min < 10) ? ":0" : ":") + refx.min;
  splace.curtime += ((refx.sec < 10) ? ":0" : ":") + refx.sec;
  splace.curtime += apind
  return (splace)
 }
function correctDLH(refx)
 {
  if ((refx.month < 4 || (refx.month == 4 && refx.date  < dayStart)  ||
     ( refx.month == 4 && refx.date  == dayStart && refx.hour < 2)) ||
     ( refx.month > 10 || (refx.month == 10 && refx.date > dayEnd)  ||
     ( refx.month == 10 && refx.date == dayEnd && refx.hour > 2) ||
     (refx.month == 10 && refx.date == dayEnd && refx.hour == 2 && refx.min > 0) ||
     (refx.month == 10 && refx.date == dayEnd && refx.hour == 2 && refx.min == 0 && refx.sec > 0)))  
     {      return 0  }
  else     return -1
 }
function correctDLH1(refx)
 {
  if ((refx.month < 2 || (refx.month == 2 && refx.date  < lastSunFeb))  ||
     ( refx.month > 10 || (refx.month == 10 && refx.date >= firstSunOct)))  
     {      return 0  }
  else     return -1
 }
function correctDLH2(refx)
 {
  if ((refx.month < 3 || (refx.month == 3 && refx.date  <  SunGeMar9))  ||
     ( refx.month > 10 || (refx.month == 10 && refx.date >= firstSunGeOct9)))  
     {      return 0  }
  else     return -1
 }
function correctDLH3(refx)
 {
  if ((refx.month < 2 || (refx.month == 2 && refx.date  <  lastSatFeb))  ||
     ( refx.month > 10 || (refx.month == 10 && refx.date >= firstSunOct)))  
     {      return 0  }
  else     return -1
 }
function correctDLH4(refx)
 {
  if ((refx.month < 4 || (refx.month == 4 && refx.date  <  firstSunGeApr6))  ||
     ( refx.month > 9 || (refx.month == 9 && refx.date >= firstSunGeSep8)))  
     {      return 0  }
  else     return -1
 }
function dayText(ii)
  {
   return days[ii +1]
  }
  
function monthText(ii)
  {
   return months[ii].name
  }
function makeDayArray()  
 {
  days = new makeArray(7)
  days[1]  = "Sun"
  days[2]  = "Mon"
  days[3]  = "Tue"                // 251
  days[4]  = "Wed"
  days[5]  = "Thu"
  days[6]  = "Fri"
  days[7]  = "Sat"
 }

function makeMonthArray()  
 {
  months = new makeArray(12)
  months[1]  = new month("Jan",31,0)
  months[2]  = new month("Feb",28,1)
  months[3]  = new month("Mar",31,2)
  months[4]  = new month("Apr",30,3)
  months[5]  = new month("May",31,4)
  months[6]  = new month("Jun",30,5)
  months[7]  = new month("Jul",31,6)
  months[8]  = new month("Aug",31,7)
  months[9]  = new month("Sep",30,8)
  months[10] = new month("Oct",31,9)
  months[11] = new month("Nov",30,10)
  months[12] = new month("Dec",31,11) 
 }
  
function makePlaceArrays()  
 {
   saPlaces = new makeArray(16) 
 saPlaces[1]  = new place("Argentina",0,-3,0,0,"") 
 saPlaces[2]  = new place("Bolivia",0,-4,0,1,"") 
 saPlaces[3]  = new place("Brazil Andes",1,-5,0,2,"") 
 saPlaces[4]  = new place("Brazil Western",1,-4,0,3,"") 
 saPlaces[5]  = new place("Brazil Eastern",1,-3,0,4,"") 
 saPlaces[6]  = new place("Chile",2,-3,0,5,"") 
 saPlaces[7]  = new place("Colombia",0,-5,0,6,"") 
 saPlaces[8]  = new place("Ecuador",1,-5,0,7,"") 
 saPlaces[9]  = new place("Falkland Islands",4,-3,0,8,"") 
 saPlaces[10]  = new place("Guyana",0,-3,0,9,"") 
 saPlaces[11]  = new place("Paraguay",3,-3,0,10,"") 
 saPlaces[12]  = new place("Peru",0,-5,0,11,"") 
 saPlaces[13]  = new place("South Georgia",0,-3,0,12,"") 
 saPlaces[14]  = new place("Surinam",0,-3,0,13,"") 
 saPlaces[15]  = new place("Uruguay",0,-3,0,14,"") 
 saPlaces[16]  = new place("Venezuela",0,-4,0,15,"") 
}
 
function makeArray(arrayLength)  // 325  
 {
  this.length = arrayLength
  for (i=1; i <= arrayLength; i++)
   this[i] = 0
  return this
 }
 
function month(name, length, index)  
 {
 this.name   = name    // property
 this.length = length  // property
 this.index  = index   // property
 }                                  // 338
function place(name, dual, hrcorr, mincorr, index, curtime)  
 {                                         // 340
 this.name     = name      // property
 this.dual     = dual      // property
 this.hrcorr   = hrcorr    // property
 this.mincorr  = mincorr   // property
 this.index    = index     // property
 this.curtime  = curtime  // property  
 } 
 
function getFebMonthDays(pyear) //  
{
 if ((pyear % 4 == 0 && pyear % 100 !=0) ||(pyear % 400 ==0))
  return 29
else
  return 28
} 