  var AtlasData = new Array(620)
    var sMonth = new Array(13)

    var HourRise, HourSet ;
    var AlphaSun, DeltaSun ;

    var T, T2, T3 ;
    var ThetaSun, Rsun, LonE

    var Pi=Math.PI ;
    var Twopi=2.*Pi ;
    var Drc = Math.PI/180. ;

    function Initialize() {
// -- Month names --
      sMonth[1] = "January  "
      sMonth[2] = "February "
      sMonth[3] = "March    "
      sMonth[4] = "April    "
      sMonth[5] = "May      "
      sMonth[6] = "June     "
      sMonth[7] = "July     "
      sMonth[8] = "August   "
      sMonth[9] = "September"
      sMonth[10] = "October  "
      sMonth[11] = "November "
      sMonth[12] = "December "

      document.shabatForm.CandleTime.value=""
      CityData()
      SetAtlasDST()
      getRiseSet()
    }

    function SetAtlasDST() {
      s=GetCookie()
      if (s.length > 6) {
        s=s.substr(6, s.length-6) ;
        document.shabatForm.atlas.selectedIndex=parseFloat(s)

        flag=s.charAt(s.length-1)
//        document.shabatForm.dst[1].checked=true
        if (s.charCodeAt(s.length-1) != 49) document.shabatForm.dst[1].checked=true
        if (s.charCodeAt(s.length-1) == 49) document.shabatForm.dst[0].checked=true
      }
      else {
        document.shabatForm.dst[1].checked=true
      }
    }


    function getDate(JD) {
      ZZ = JD + 0.5
      Z = Math.floor(ZZ)
      F = ZZ - Z

      if (Z < 2299161) {
        a = Z
      }
      else {
        Alpha = Math.floor((Z - 1867216.25) / 36524.25)
        a = Z + 1 + Alpha - Math.floor(Alpha / 4)
      }

      B = a + 1524
      C = Math.floor((B - 122.1) / 365.25)
      D = Math.floor(365.25 * C)
      e = Math.floor((B - D) / 30.6001)
      Daye = B - D - Math.floor(30.6001 * e) + F

      if (e < 14)
        Mnth = e - 1
      else
        Mnth = e - 13


      if (Mnth > 2.5)
        Yr = C - 4716
      else
        Yr = C - 4715

      //alert("JD: " + JD + "   " + Mnth + "/" + Daye + "/" + Yr)
//      s=NumForm(Mnth, -2, " ") + "/" + NumForm(Daye, -2, "0") + "/" + NumForm(Yr, -4, " ")
      dd=Math.floor(Daye)
      if (Daye < 10) dd="0" + dd

      yy=Yr
      if (Yr <   10) yy = "   " + y
      if (Yr <  100) yy = "  "  + y
      if (Yr < 1000) yy = " "   + y

      s=sMonth[Mnth].substring(0, 3) + " " + dd + " " + yy
      //alert(s)
      return s
    }


    function RiseSet(JD, UT, DeltaGMT, Geolat, Geolon) {
      var ASUN = new Array(4) ;
      var DSUN = new Array(4) ;
      var SmallH0, JD0, T, T2, T3 ;

      JD0 = JD - UT / 24 ;
//      SmallH0=-.56667*Drc ;
      SmallH0=-.83333*Drc ;
//      SmallH0=-6.*Drc ;
//      SmallH0 = 0 ;

      for(k = -1; k < 2; k++) {
        JD = JD0 + k ;
        T = (JD - 2415020.) / 36525. ;
        T2 = T * T ;
        T3 = T2 * T ;
        Sun(T, T2, T3) ;
        SunAD(T, T2, T3) ;
        ASUN[k + 2] = AlphaSun ;
        DSUN[k + 2] = DeltaSun ;

        CosH0 = (Math.sin(SmallH0) - Math.sin(Drc * Geolat) * Math.sin(DSUN[2])) / (Math.cos(Drc * Geolat) * Math.cos(DSUN[2])) ;
      }
//      alert("Alpha: " + Alpha + "  Delta: " + Delta)

      if (Math.abs(CosH0) > 1) {
        HourRise = 999 ;
        HourSet = 999 ;
      }

      var SinH0 = Math.sqrt(1. - CosH0*CosH0) ;
      var Xterm = CosH0 ;
      var Yterm = SinH0 ;
      var Answer=Math.atan2(Yterm, Xterm) ;
      var H0 = Answer / Drc ;

// Calculate Theta0
      T = (JD0 - 2415020.) / 36525. ;
      T2 = T * T ;
      T3 = T2 * T ;
      var T0 = T;  // Used for sunrise and sunset
      var Theta0 = 0.276919398 + 100.0021359 * T + 0.000001075 * T2 ;
      Theta0 = 360. * (Theta0 - Math.floor(Theta0)) ;

      var M0 = (ASUN[2] / Drc + Geolon - Theta0) / 360. ;
      var M1 = M0 - H0 / 360. ;
      var M2 = M0 + H0 / 360. ;
//      alert(ASUN[2]/Drc + " " + Geolon + " " + Theta0 + "   " + M0 + " " + M1)
//      alert("Asun[2]: " + ASUN[2] + "  Geolon: " + Geolon)

      if (M0 < 0) M0++ ;
      if (M1 < 0) M1++ ;
      if (M2 < 0) M2++ ;
      M0 = M0 - Math.floor(M0) ;
      M1 = M1 - Math.floor(M1) ;
      M2 = M2 - Math.floor(M2) ;

//      alert("Loop 2: Alpha: " + Alpha + "  Delta: " + Delta)
      for (k=0; k<3; k++) {
        Theta = Theta0 + 360.985647 * M1 ;
        T = T0 + M1 / 36525. ;
        T2 = T * T ;
        T3 = T2 * T ;
        //alert("In Loop:   M1: " + M1 + "  Theta0: " + Theta0 + " T0: " + T0 + "  T: " + T)

        Sun(T, T2, T3) ;
        SunAD(T, T2, T3) ;
        //alert("In Loop 2: Alpha: " + Alpha + "  Delta: " + Delta)
        H = Theta - Geolon - AlphaSun / Drc ;
        Arg = Math.sin(Drc * Geolat) * Math.sin(DeltaSun) + Math.cos(Drc * Geolat) * Math.cos(Delta) * Math.cos(Drc * H) ;
        Answer=Math.asin(Arg) ;
        SmallH = Answer / Drc ;
        DeltaM = (SmallH - SmallH0 / Drc) / (360. * Math.cos(DeltaSun) * Math.cos(Drc * Geolat) * Math.sin(Drc * H)) ;
        M1 = M1 + DeltaM ;
      }

      HourRise = 24. * M1 - DeltaGMT ;
      if (HourRise > 24.) HourRise = HourRise - 24. ;
      if (HourRise < 0.) HourRise = HourRise + 24. ;
//      alert("HourRise: " + HourRise)


      for (k=0; k<3; k++) {
        Theta = Theta0 + 360.985647 * M2 ;
        T = T0 + M2 / 36525. ;
        T2 = T * T ;
        T3 = T2 * T ;
        Sun(T, T2, T3) ;
        SunAD(T, T2, T3) ;
        H = Theta - Geolon - AlphaSun / Drc ;
        Arg = Math.sin(Drc * Geolat) * Math.sin(DeltaSun) + Math.cos(Drc * Geolat) * Math.cos(Delta) * Math.cos(Drc * H) ;
        Answer=Math.asin(Arg) ;
        SmallH = Answer / Drc ;
        DeltaM = (SmallH - SmallH0 / Drc) / (360. * Math.cos(DeltaSun) * Math.cos(Drc * Geolat) * Math.sin(Drc * H)) ;
        M2 = M2 + DeltaM ;
      }
//      alert("24*M2: " + 24*M2 + "  DeltaGMT: " + DeltaGMT)
//      HourSet = 24. * M2 - DeltaGMT ;
//      HourSet -= .3

      HourSet=24.*M2
      if (HourSet < 0.) HourSet += 24. ;
//      alert("HourSet: " + HourSet)
    }




    function getRiseSet(Month, Day, Year, Geolat, Geolon, DeltaGMT) {
      var Month=0, Day=0, Year ;
      var UT ;
      Today = new Date() ;

      Month=Today.getMonth() + 1
      Day=Today.getDate()
      Year=Today.getFullYear()

      Hr=Today.getHours()
      Mn=Today.getMinutes()
      Sc=Today.getSeconds()

      Index=document.shabatForm.atlas.selectedIndex
      DeltaGMT=parseFloat(AtlasData[Index].substr(30,3))
      if (AtlasData[Index].substr(33,2) != "  ") DeltaGMT += parseFloat(AtlasData[Index].substr(33,2))/60.

      LatRef=parseFloat(AtlasData[Index].substr(0,2)) + parseFloat(AtlasData[Index].substr(3,2))/60. +  parseFloat(AtlasData[Index].substr(6,2))/3600.
      LonRef=parseFloat(AtlasData[Index].substr(12,3)) + parseFloat(AtlasData[Index].substr(16,2))/60. +  parseFloat(AtlasData[Index].substr(19,2))/3600.

      sLatRefDir=AtlasData[Index].charAt(9)
      sLonRefDir=AtlasData[Index].charAt(22)

//      alert(Index + ":   " + AtlasData[Index] + "         --" + AtlasData[Index].substr(0,2) + ":" + parseFloat(AtlasData[Index].substr(3,2)) + ":" + parseFloat(AtlasData[Index].substr(6,2)))
//      alert(parseFloat(AtlasData[Index].substr(30,3)) + "  " + parseFloat(AtlasData[Index].substr(33,2)))
//      return

      if (sLonRefDir == "E") LMT = UT + LonRef / 15.
      if (sLonRefDir == "W") LMT = UT - LonRef / 15.

      Geolat=LatRef
      if (sLatRefDir == "S") Geolat=-Geolat

      Geolon=LonRef
      if (sLonRefDir == "W") Geolon=-Geolon

      DeltaLonGMT=Geolon/15.

//      alert("Lat: " + Geolat + " " + sLatRefDir + "  Lon: " + Geolon + " " + sLonRefDir + "  DeltaGMT: " + DeltaGMT)

/*  Test case comparison to printouts
      Month=6
      Day=23
*/


/* Test case comparison to Nautical Almanac
      Month=2
      Day=10
      Year=1999
*/

/*
      Geolat=40.
      Geolon=0.
      DeltaGMT=0.
*/

      CT = parseFloat(Hr) + parseFloat(Mn) / 60. + parseFloat(Sc)/3600.
//      alert("Hr: " + Hr + "  Mn: " + Mn + "  Sc: " + Sc)
      UT = CT - DeltaGMT

//      alert("Geolon: " + Geolon + "  DeltaGMT: " + DeltaGMT)
//      alert("CT: " + CT + "  DeltaGMT: " + DeltaGMT + "  UT: " + UT)

 //     alert(Month + "/" + Day + "/" + Year + "  UT: " + UT)

      UT=15. ;
      Geolon=0.
      DeltaGMT=0.

      var JDS=JDCalc(Month, Day, Year, UT) ;
      DOW=GetDayOfWeek(JDS, UT)
//      alert("DOW: " + DOW)
      if (DOW < 6) JDS += 6 - DOW
      if (DOW == 7) JDS += 6

      RiseSet(JDS, UT, DeltaGMT, Geolat, Geolon) ;

      hh=Math.floor(HourRise)
      mm=Math.round(60*(HourRise - hh))
      if (mm < 10) mm = "0" + mm
//      document.shabatForm.sunrise.value = hh + ":" + mm ;

      DeltaGMT=parseFloat(AtlasData[Index].substr(30,3))
      if (AtlasData[Index].substr(33,2) != "  ") DeltaGMT += parseFloat(AtlasData[Index].substr(33,2))/60.
//      alert("DeltaLonGMT: " + DeltaLonGMT + "  DeltaGMT: " + DeltaGMT + "  Delta: " + DeltaLonGMT + DeltaGMT)

      HourSet += -DeltaLonGMT + DeltaGMT  // Conversion to local time
      HourSet += -.3  // Subtract 18 minutes for candle lighting time
      if (Index == 246) HourSet -= 11/30.
      if (document.shabatForm.dst[0].checked) HourSet++

      hh=Math.floor(HourSet)
      mm=Math.floor(60*(HourSet - hh))
      if (mm == 60) {
        mm=0
        hh += 1
      }
      if (mm < 10) mm = "0" + mm
      s="    The next Shabbat starts on " + getDate(JDS) + " at " + hh + ":" + mm ;
      if (hh > 12) s += "  (" + Math.floor(hh-12) + ":" + mm  + " PM)"
      document.shabatForm.CandleTime.value = s
    }


    function GetDayOfWeek(JD, UT) {
      DOW = (JD - UT / 24. + 1.5) / 7.
      DOW = Math.floor(7 * (DOW - Math.floor(DOW)) + 1.00001)

      return(DOW)
    }


// ****** JDCalc ******
    function JDCalc (Mnth, Dy, Yr, UT) {
      var M, Y ;

      if (Mnth > 2) {
        Y = Math.floor(Yr) ;
        M = Math.floor(Mnth) ;
      }
      else {
        Y = Math.floor(Yr - 1) ;
        M = Math.floor(Mnth + 12) ;
      }

      A = Math.floor(Y / 100) ;
      B = Math.floor(2 - A) + Math.floor(A / 4) ;
      JD1 = Math.floor(365.25 * Y) ;
      JD2 = Math.floor(30.6001 * (M + 1)) ;
      JD = JD1 + JD2 + Dy + 1720994.5 + B ;
      JD = JD + UT / 24 ;
      T = (JD - 2415020.) / 36525 ;
      T2 = T * T ;
      T3 = T2 * T ;

      return JD ;
   }

    function LoseFocus() {
      document.shabatForm.CandleTime.blur()
    }

    function UpdateTime() {
      SetCookie()
      getRiseSet()
    }

    function SetCookie() {
      var d = new Date() ;
      ExpDate=d.getTime() + (36500*24*3600*1000) ;
      d.setTime(ExpDate) ;

      Index=document.shabatForm.atlas.selectedIndex
      s = "index=" + escape(Index)
      s += " dst="
      if (document.shabatForm.dst[0].checked) s+=escape(1)
      else s += escape(0)
      s += "; expires=" + d.toGMTString()  ;
      document.cookie = s ;
    }

    function GetCookie() {
      document.cookie ;
      return(unescape(document.cookie))
    }
