<!----------------------------------------------------------------------
/**********************************************************************\
*       The JavaScript code in this section was written by:            *
*                          Rahul Mittal                                *
*       Sophomore, Dept. of Astronomy, Villanova University            *
* Feel free to use the code below provided this header remains intact! *
*       Drop me e-mail at rasteroid@hotmail.com if you want, and       *
*             that way I can check out your homepage... :)             *
*      Corrected Y2K bug October 3, 2003 (JD 2452915) by Adric Riedel
\**********************************************************************/

function calculateJD(calendarDate) {
   cdDate = new Date(calendarDate)
   year = cdDate.getYear()          //added + 1900 10/03/2003
   if (year < 1000 ) {year+=1900}  // modified 10/03/2003
   //year = 1900+cdDate.getYear()
   month = cdDate.getMonth() + 1 //getMonth() returns 0-11
   day = cdDate.getDate()
   hour = cdDate.getHours()
   min = cdDate.getMinutes()
   sec = cdDate.getSeconds()
   univTime = hour+(min/60)+(sec/3600)
   if ((100*year+month-190002.5) >= 0) {sign = 1}
      else {sign = -1}
   with (Math) {
      part1 = 367 * year
      part2 = floor((7*(year+floor((month+9)/12)))/4)
      part3 = day+floor((275*month)/9)
      part4 = 1721013.5+(univTime/24)
      part5 = 0.5*sign
      jd = part1-part2+part3+part4-part5+0.5
   }
   return jd
}

function calculateCD(julianDate) {
   with (Math) {
      X = parseFloat(julianDate)+0.5
      Z = floor(X)
      F = X - Z
      Y = floor((Z-1867216.25)/36524.25)
      A = Z+1+Y-floor(Y/4)
      B = A+1524
      C = floor((B-122.1)/365.25)
      D = floor(365.25*C)
      G = floor((B-D)/30.6001)
      month = (G<13.5) ? (G-1) : (G-13)
      year = (month<2.5) ? (C-4715) : (C-4716)
      month -= 1 // month in JavaScript is from 0 to 11
      UT = B-D-floor(30.6001*G)+F
      day = floor(UT)
      UT -= floor(UT)
      UT *= 24;
      hour = floor(UT)
      UT -= floor(UT)
      UT *= 60
      minute = floor(UT)
      UT -= floor(UT)
      UT *= 60
      second = round(UT)
   }
   cdDate = new Date(Date.UTC(year, month, day, hour, minute, second))
   return cdDate.toGMTString()
}

function calculatePhase(tom, period, today) {
   var phase
   with (Math) {
      phase = (today-tom)/period
      phase = phase-floor(phase)
   }
   return phase
}

function calculateTimesOfMinima(form) {
   var tom, phase, period, startJD, endJD, fStr, today
   startJD = calculateJD(form.startDate.value)
   endJD = calculateJD(form.endDate.value)
   if(endJD <= startJD) {
      alert("Ending Julian Date is less than or equal to the starting Julian Date!")
   }
   else {
      period = parseFloat(form.period.value)
      tom = parseFloat(form.tom.value)
      phase = calculatePhase(tom, period, startJD)
      with (Math) {
         numPeriods = ceil((startJD-tom)/period)
         today = numPeriods*period + tom
      }
      fStr = "<HTML>"
      fStr += "<HEAD><TITLE>Times of Minima Chart</TITLE><CENTER><H2>Times of Minima Chart</H2></CENTER></HEAD>"
      fStr += "<BODY><CENTER>"
      fStr += "<TT><P><B>"
      fStr += ("Starting Date = " + form.startDate.value + "<BR>")
      fStr += ("Ending Date = " + form.endDate.value + "<BR>")
      fStr += ("Time of Minimum = " + tom + "<BR>")
      fStr += ("Period = " + period + "<BR>")
      //fStr += ("Current Phase = " + phase + "<BR>")
      fStr += "</B></P>"
      fStr += "<TABLE BORDER=1 SIZE=100%>"
      fStr += "<TR><TD><CENTER><B>Julian Date</B></CENTER></TD><TD><CENTER><B>Calendar Date</B></CENTER></TD></TR>"
      while(today < endJD) {
         calDate = this.calculateCD(today)
         fStr += ("<TR><TD> " + today + " </TD><TD> " + calDate + " </TD></TR>")
         numPeriods++
         today = numPeriods*period + tom
      }
      fStr += "</TABLE></TT>"
      fStr += "<P><I>"
      fStr += "JavaScript code by Rahul Mittal, Villanova University, Villanova PA 19085.<BR>"
      fStr += "E-mail: <A HREF='mailto:rmitta01@merlin.engr.vill.edu'>rmitta01@merlin.engr.vill.edu</A>"
      fStr += "</I></P></CENTER></BODY></HTML>"
      newwindow = window.open("","Results","toolbar=yes,width=550,height=400,directories=no,status=no,scrollbars=yes,resize=no,menubar=yes")
      newwindow.document.writeln(fStr)
   }
}
