// JavaScript modification T. Dacre 1999
// based on a BASIC program MOON EFFECTS by Bradley E. Schaefer
//
//  taken from http://www.dacre.net/toby.html
//
//   This program helps anyone who needs to know the Moon's
//   phase (age) on
//   any date within several thousand years in the past or future.
//   To illustrate its application, Bradley Schaefer applied it
//   to a number of famous events influenced by the Moon in
//   World War II.  His article appeared in Sky & Telescope for
//   April 1994, page 86.

// --------------------------------------------------
function MoonPhase(day)
    {
	var day2=day;
	Date.UTC(day);
	var Y=day2.getFullYear();
	var M=day2.getMonth()+1;
	var D=day2.getDate();
	var YY,MM,K1,K2,K3,J,V,IP,AG;

	YY=Y-Math.floor((12-M)/10);
	MM=M+9;
	if (MM>=12) {MM=MM-12};
	K1=Math.floor(365.25*(YY+4712));
	K2=Math.floor(30.6*MM+.5);
	K3=Math.floor(Math.floor((YY/100)+49)*.75)-38;
	J=K1+K2+D+59;
	if (J>2299160) {J=J-K3};
	V=(J-2451550.1)/29.530588853;
	V=V-Math.floor(V);
	if (V<0) {V=V+1};
	IP = V * 29;
	//AG=IP*29.53;
	return IP
    }
// --------------------------------------------------
function calceclipse(calcmonth,calcyear){
//
EC1=0;
EC2=0;
EM1="";
EM2="";
R1=3.14159265/180;
U=0;
Y=calcyear;
calcmonth++;  // javascript years are 0 to 11
G=1;
K0=Math.floor((Y-1900)*12.3685);
T=(Y-1899.5)/100;
T2=T*T;
T3=T*T*T;
J0=2415020+29*K0;
F0=0.0001178*T2-0.000000155*T3;
F0=F0+0.75933+0.53058868*K0;
F0=F0-0.000837*T-0.000335*T2;
J0=J0+Math.floor(F0);
F0=F0-Math.floor(F0);
M0=K0*0.08084821133;
M0=360*(M0-Math.floor(M0))+359.2242;
M0=M0-0.0000333*T2;
M0=M0-0.00000347*T3;
M1=K0*0.07171366128;
M1=360*(M1-Math.floor(M1))+306.0253;
M1=M1+0.0107306*T2;
M1=M1+0.00001236*T3;
B1=K0*0.08519585128;
B1=360*(B1-Math.floor(B1))+21.2964;
B1=B1-0.0016528*T2;
B1=B1-0.00000239*T3;
for (K9=1;K9<=27;K9+=2)
   {
	J=J0+14*K9;
	F=F0+0.765294*K9;
	K=K9/2;
	M5=(M0+K*29.10535608)*R1;
	M6=(M1+K*385.81691806)*R1;
	B6=(B1+K*390.67050646)*R1;
	F=F-0.4068*Math.sin(M6);
	F=F+(0.1734-0.000393*T)*Math.sin(M5);
	F=F+0.0161*Math.sin(2*M6);
	F=F-0.0104*Math.sin(2*B6);
	F=F-0.0074*Math.sin(M5-M6);
	F=F-0.0051*Math.sin(M5+M6);
	F=F+0.0021*Math.sin(2*M5);
	F=F+0.5/1440;
	J=J+Math.floor(F);
    F=F-Math.floor(F);
   }
}
// ---------------------------------------
function calc( calcday,calcmonth,calcyear)
   {

calceclipse(calcmonth,calcyear);

var Y,JJ,K1,K2,K3,D,M,MM,FM1,FM2,NM1,NM2;
FM1=0;
FM2=0;
NM1=0;
NM2=0;
Tt1="";
Tt2="";
Tt3="";
Tt4="";

Y=calcyear;  // year
M=calcmonth;
M++;
D=1; //-1
YY=Y-Math.floor((12-M)/10);
MM=M+9;
if(MM>=12){MM=MM-12}
K1=Math.floor(365.25*(YY+4712));
K2=Math.floor(30.6*MM+.5);
K3=Math.floor(Math.floor((YY/100)+49)*.75)-38;
JJ=K1+K2+D+59;
if (JJ>2299160){JJ=JJ-K3}

var A,B,C,Dq,E,G,D1,H1,M9,Mq,H,Yq,A1,Fq,Jq;
var R1,U,K0,T,T2,T3,J0,F0,J,M0,M1,B1,M5,M6,K9,F;
R1=3.14159265/180;
U=0;
K0=Math.floor((Y-1900)*12.3685);
T=(Y-1899.5)/100;
T2=T*T;
T3=T*T*T;
J0=2415020+29*K0;
F0=0.0001178*T2-0.000000155*T3;
F0=F0+0.75933+0.53058868*K0;
F0=F0-0.000837*T-0.000335*T2;
J=J+Math.floor(F);
F=F-Math.floor(F);
M0=K0*0.08084821133;
M0=360*(M0-Math.floor(M0))+359.2242;
M0=M0-0.0000333*T2;
M0=M0-0.00000347*T3;
M1=K0*0.07171366128;
M1=360*(M1-Math.floor(M1))+306.0253;
M1=M1+0.0107306*T2;
M1=M1+0.00001236*T3;
B1=K0*0.08519585128;
B1=360*(B1-Math.floor(B1))+21.2964;
B1=B1-0.0016528*T2;
B1=B1-0.00000239*T3;

for(K9=0;K9<29;K9++)
  {
	J=J0+14*K9;
	F=F0+0.765294*K9;
	K=K9/2;
	M5=(M0+K*29.10535608)*R1;
	M6=(M1+K*385.81691806)*R1;
	B6=(B1+K*390.67050646)*R1;
	F=F-0.4068*Math.sin(M6);
	F=F+(0.1734-0.000393*T)*Math.sin(M5);
	F=F+0.0161*Math.sin(2*M6);
	F=F+0.0104*Math.sin(2*B6);
	F=F-0.0074*Math.sin(M5-M6);
	F=F-0.0051*Math.sin(M5+M6);
	F=F+0.0021*Math.sin(2*M5);
	F=F+0.0010*Math.sin(2*B6-M6);
	J=J+Math.floor(F);
	F=F-Math.floor(F);

	Jq=J;
	Fq=F;
	G=1;
	if ( Y<1583){G=0}

	Fq=Fq+0.5;
	if (!(Fq<1))
	   {
	   Fq=Fq-1;
	   Jq=Jq+1;
	   }
	if (G==1)
	  {
	  A1=Math.floor((Jq/36524.25)-51.12264);
	  A=Jq+1+A1-Math.floor(A1/4)
	  }
	else
	  {
	  A=Jq
	  };

	B=A+1524;
	C=Math.floor((B/365.25)-0.3343);
	Dq=Math.floor(365.25*C);
	E=Math.floor((B-Dq)/30.61);
	Dq=B-Dq-Math.floor(30.61*E)+Fq;
	Mq=E-1;
	Yq=C-4716;
	if (E>13.5){Mq=Mq-12};
	if (Mq<2.5){Yq=Yq+1};
	D1=Math.floor(Dq);
	H=24*(Dq-D1);
	H1=Math.floor(H);
	M9=Math.floor(60*(H-H1));
	if (M9<10){M9="0"+M9};

	if ((U==0) && (Mq==M) && (Yq==Y)){if (NM1==0){NM1=D1;Tt1=H1+":"+M9} else {if(NM2==0){NM2=D1;Tt2=H1+":"+M9}}};
	if ((U==1) && (Mq==M) && (Yq==Y)){if (FM1==0){FM1=D1;Tt3=H1+":"+M9} else {if(FM2==0){FM2=D1;Tt4=H1+":"+M9}}};
	U=U+1;

	if (U==2){U=0};
}

var temp,dayoffset,days,i,j,monthnames,todayday, isToday, dow;
today=new Date(Date.UTC(calcyear,calcmonth,10,1,1));

month=new Date(Date.UTC(calcyear,calcmonth,10,1,1));

todayday=today.getDate();
month.setDate(1);
dayoffset=month.getDay()-1;
if (dayoffset==-1){dayoffset=6};
today.setDate(1);
today.setMonth(today.getMonth()+1);
days=(today-month)/1000/60/60/24;
dayNumber = new Date();

month.setHours(11);
monthnames=["January","February","March","April","May","June","July","August","September","October","November","December"];
dow=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
MD1="New Moon "+Tt1;
MD2="New Moon "+Tt2;
MD3="Full Moon "+Tt3;
MD4="Full Moon "+Tt4;
if (FM1==EC1){MD3=MD3+String.fromCharCode(10)+EM1}
if (FM2==EC1){MD4=MD4+String.fromCharCode(10)+EM1}
if (FM2==EC2){MD4=MD4+String.fromCharCode(10)+EM2}


if( calcday == 0 ) // full month
  {
  document.write('<H1>Phases of the Moon ', monthnames[month.getMonth()]," ",month.getFullYear(),"</H1>");

  document.write('<table class="month"><tr class="rowCol">')
  for( var i1=0; i1 < 7; i1++)
     {
     document.write('<td><div class="dayDisp">' + dow[i1] + '</div></td>');
     }
  document.write('</tr>');

  for (i=0;i<(days+dayoffset)/7;i++)
     {
     document.write('<tr>');

     for (j=1;j<8;j++)
       {
       isToday = (i*7)+j-dayoffset
       if( isToday !=  dayNumber.getDate() )
         document.write('<td><div class="dateDisp">');
       else
         document.write('<td><div class="today">');

       if (isToday<=0 || isToday>days)
          {
          document.write("&nbsp");
          }
       else
         {
         document.write(isToday);
         displayImage(isToday, FM1, FM2, NM1, NM2, EC1, EC2, MD1, MD2, MD3, MD4)
         }
       document.write("</div></td>");
       }
       document.write("</tr>");
      }
   document.write('</table>');
   }
 else
   {
  // just put out one image for todays state

   document.write('<div class="dateDisp">');
   displayImage( calcday, FM1, FM2, NM1, NM2, EC1, EC2, MD1, MD2, MD3, MD4 )
   document.write('</div>')
   }


   // document.write('</body></html>');
document.close();
}
// ------------------------------------------
function displayImage( calcday, FM1, FM2, NM1, NM2, EC1, EC2, MD1, MD2, MD3, MD4 )
    {
     var isToday = calcday;

     if( isToday==FM2 )
       {
       if ( FM1> 0 && FM2 > 0)
         document.write(' <span class="blue">Blue</span>')
       else
         document.write(' <span class="desc">Full</span>')
       }

	 if ((isToday==FM1)){document.write(' <span class="desc">Full</span>')}
	 if ((isToday==NM1) || (isToday==NM2) ){document.write(' <span class="desc">New</span>')}

	 if ( isToday==EC1 || isToday == EC2)
		  {
		  if (EM1.indexOf("otal")!=-1 || EM2.indexOf("otal")!=-1 )
		    document.write(' *T')
		  else
	  	    document.write(' *P')
	  	  }

		document.write('<br>');
		month.setDate(isToday);

		document.write('<img src="img/moon/moon',Math.floor(MoonPhase(month))+1,'.gif" width="50" height="50" ');
		if (isToday==FM1){document.write(' title="' + MD3 +'"')}
		if (isToday==NM1){document.write(' title="' + MD1 +'"')}
		if (isToday==FM2){document.write(' title="' + MD4 +'"')}
		if (isToday==NM2){document.write(' title="' + MD2 +'"')}

	document.write('>');
    }
// --------------------------------------------------
function doMonthCalc()
    {
    var i,month,year;

    month=document.forms[0].a.options[document.forms[0].a.selectedIndex].value;
    year=document.forms[0].b.options[document.forms[0].b.selectedIndex].value;

    calc(-1,month,year);
    }
// --------------------------------------------------
function doTodayCalc()
    {
    var i,day, month,year;
    var dToday = new Date();

    day = dToday.getDate();
    month= dToday.getMonth();
    year= dToday.getFullYear();

    calc(day,month,year);
    }
// --------------------------------------------------
function doThisMonthCalc()
    {
    var i,month,year;
    var dt = new Date();

    month= dt.getMonth()  ;
    year=dt.getFullYear();

    calc(0,month,year);
    }
// --------------------------------------------------    
function doCalcJulianDate(  calcDay, calcMonth, calcYear )
    {
	var d, y,yy, jj,k1,k2,k3,d,m,mm;

	y=calcYear;  // year
	m=calcMonth;
	d=calcDay;
	yy=y-Math.floor((12-m)/10);
	mm=m+9;
	if(mm>=12){mm=mm-12}
	k1=Math.floor(365.25*(yy+4712));
	k2=Math.floor(30.6*mm+.5);
	k3=Math.floor(Math.floor((yy/100)+49)*.75)-38;
	jj=k1+k2+d+59;
	if (jj>2299160){jj=jj-k3}
	return( jj )    
    }
    
// --------------------------------------------------    
function doCalcSideralTime( )
    {
    var hh, mm, ss;
    var dte = new Date();
    var ut = dte.getHours() + dte.getMinutes() / 100 + dte.getSeconds() / 10000
    return ( ut * 1.00273790935 );
    }
// --------------------------------------------------    
function doCalcSideralTimeFmt( )
    {
    var hh, mm, ss;
    var sd = doCalcSideralTime( );
    hh = Math.floor( sd )
    mm = Math.floor( (sd - hh) * 60 )
    if( mm < 10 )
      mm = "0" + mm;

    return ( hh + ":" + mm);
    }    
//---------------------------------------------------

function calcEc(){
monthA=new Array(12);
monthA=["January","February","March","April","May","June","July","August","September","October","November","December"];
R1=3.14159265/180;
U=0;
 
YEARS=5; // next 5 years

    var dt0 = new Date();
    
    Y = dt0.getFullYear();

//Y=2006; 

G=1;
K0=Math.floor((Y-1900)*12.3685);
T=(Y-1899.5)/100;
T2=T*T;
T3=T*T*T;
J0=2415020+29*K0;
F0=(0.0001178*T2-0.000000155*T3) + (0.75933+0.53058868*K0) - (0.000837*T-0.000335*T2);
//F0=F0+(0.75933+0.53058868*K0);
//F0=F0 -0.000837*T-0.000335*T2;

J0=J0+Math.floor(F0);
F0=F0-Math.floor(F0);

M0=(K0*0.08084821133);
M0=(360*(M0-Math.floor(M0))+359.2242)  - (-0.0000333*T2) - (0.00000347*T3);
//M0=M0-0.0000333*T2;
//M0=M0-0.00000347*T3;
M1=K0*0.07171366128;
M1=(360*(M1-Math.floor(M1))+306.0253)  + (0.0107306*T2) + (0.00001236*T3);
//M1=M1+0.0107306*T2;
//M1=M1+0.00001236*T3;

B1=K0*0.08519585128;
B1=360*(B1-Math.floor(B1))+21.2964;
B1=B1-0.0016528*T2;
B1=B1-0.00000239*T3;

for (K9=1;K9<=27*YEARS;K9=K9+2)
    {
    J=J0+14*K9;
    F=F0+0.765294*K9;
    K=K9/2;
    M5=(M0+K*29.10535608)*R1;
    M6=(M1+K*385.81691806)*R1;
    B6=(B1+K*390.67050646)*R1;
    F=F-0.4068*Math.sin(M6);
    F=F+(0.1734-0.000393*T)*Math.sin(M5);
    F=F+0.0161*Math.sin(2*M6);
    F=F-0.0104*Math.sin(2*B6);
    F=F-0.0074*Math.sin(M5-M6);
    F=F-0.0051*Math.sin(M5+M6);
    F=F+0.0021*Math.sin(2*M5);
    F=F+0.5/1440;
    J=J+Math.floor(F);
    F=F-Math.floor(F);

    eclipser();
    if ((estr != "") && (Yq-Y<YEARS) && (Yq>=Y))
       {document.write(estr,"<br><br>")}
    }

// -------------------------------------------------------------
//100 REM  LUNAR ECLIPSE SUBROUTINE
function eclipser()
    {
    var todayDay, todayMonth, todayYear;
    var dt = new Date();
    var currentDate = "";
    var thisDate = ""
    
    todayDay   = dt.getDate();
    todayMonth = dt.getMonth()+1;
    currentDate  = dt.getFullYear();
	
    if( todayMonth < 10 )
      currentDate = currentDate + "0" + todayMonth
    else
      currentDate = currentDate + todayMonth
      
    if( todayDay < 10 )
      currentDate = currentDate + "0" + todayDay
    else
      currentDate = currentDate + todayDay
    
    estr="";
    D7=0;
    if (Math.abs(Math.sin(B6))>0.36){return 0} 

    S=(5.19595-0.0048*Math.cos(M5) ) + (0.0020*Math.cos(2*M5)) - (0.3283*Math.cos(M6)) -(0.0060*Math.cos(M5+M6)) + (0.0041*Math.cos(M5-M6));
    C1=(0.2070*Math.sin(M5) ) + (0.0024*Math.sin(2*M5)) - (0.0390*Math.sin(M6)) + (0.0115*Math.sin(2*M6)) - (0.0073*Math.sin(M5+M6)) - (0.0067*Math.sin(M5-M6) ) + (0.0117*Math.sin(2*B6) );
    D9=Math.abs(S*Math.sin(B6)+C1*Math.cos(B6));
    U=(0.0059+0.0046*Math.cos(M5)) - (0.0182*Math.cos(M6)) + (0.0004*Math.cos(2*M6)) -( 0.0005*Math.cos(M5+M6));
    RP=1.2847+U;
    RU=0.7404-U;
    MP=(1.5572+U-D9)/0.545;
    if (MP<0){return 0}
    MU=(1.0129-U-D9)/0.545;
    D5=1.5572+U;
    D6=1.0129-U;
    D7=0.4679-U;
    N=(0.5458+0.04*Math.cos(M6))/60;
    D5=((D5*D5-D9*D9)*(D5*D5-D9*D9))/N;
    if (!(MU<=0)){ D6=((D6*D6-D9*D9)*(D6*D6-D9*D9))/N};
    if (!(MU<=1)){  D7=((D7*D7-D9*D9)*(D7*D7-D9*D9))/N};


	Jq=J;
	Fq=F;
	G=1;
	if ( Y<1583){G=0}

	Fq=Fq+0.5;

	if (!(Fq<1))
	  {
	  Fq=Fq-1;
	  Jq=Jq+1;
	  }

	if (G==1)
	  {
	  A1=Math.floor((Jq/36524.25)-51.12264);
	  A=Jq+1+A1-Math.floor(A1/4)
	  }
	else 
	  {A=Jq};

	B=A+1524;
	C=Math.floor((B/365.25)-0.3343);
	Dq=Math.floor(365.25*C);
	E=Math.floor((B-Dq)/30.61);
	Dq=B-Dq-Math.floor(30.61*E)+Fq;
	Mq=E-1;
	Yq=C-4716;
	if (E>13.5){Mq=Mq-12};
	if (Mq<2.5) {Yq=Yq+1};
	D1=Math.floor(Dq);
	H=24*(Dq-D1);
	H1=Math.floor(H);
	M9=Math.floor(60*(H-H1));
	if (M9<10){M9="0"+M9};
   
        thisDate = Yq;
	
        if( Mq < 10 )
          thisDate = thisDate + "0" + Mq
        else
          thisDate = thisDate + Mq
      
       if( D1 < 10 )
         thisDate = thisDate + "0" + D1
       else
         thisDate = thisDate + D1

       if( parseInt(thisDate,10) >= parseInt(currentDate,10) )
	  {
	  estr="<div class='eclipse'><fieldset><legend>"+D1+' '+monthA[Mq-1]+" "+Yq;
	  if (MU<0)
	    estr=estr+" Partial Eclipse"
	  else 
	    estr=estr+" Total Eclipse"

	  estr=estr + "</legend>"

	  estr=estr+"<p> Maximum phase ";
	  estr=estr+H1+":"+M9+" UT"+"<br>";
	  MP=Math.floor(1000*MP+0.5)/1000;
	  estr=estr+" Penumbral <acronym title='magnitude'>mag.</acronym> "+MP+"</p>";

	  if (!(MU<=0))
	     { 
	     MU=Math.floor(1000*MU+0.5)/1000;
	     estr=estr+" Umbral <acronym title='magnitude'>mag.</acronym> "+MU;
	     }

	  estr=estr+"<p> SEMIDURATIONS --<ul>";

	  D5=Math.floor(D5+0.5);
	  // REM ROUND OFF;
	  if (D5<60)
	    {
	    estr=estr+"<li>Penumbra "+D5+"m</li>"
	    }
	  else 
	    {
	    estr=estr+"<li>Penumbra "+Math.floor(D5/60)+"h "+(D5-(Math.floor(D5/60)*60))+"m</li>"
	    }

	  if (MU<0 )
	   {
	   estr=estr+"</div>";
	   return 0
	   }

	  D6=Math.floor(D6+0.5);
	  D7=Math.floor((D7+0.5)*10)/10;
	  if (D6<60)
	    {
	    estr=estr+"<li>Umbra    "+D6+"m</li>"
	    }
	  else 
	    {
	    estr=estr+"<li>Umbra    "+Math.floor(D6/60)+"h "+(D6-(Math.floor(D6/60)*60))+"m</li>"
	    }
	  estr=estr+"<li>Totality "+D7+"m</li></ul>";
	  estr=estr+"</div>";   
	  }
 }
}
// -------------------------------------------------------------
