//These routines use formula to approximate the New Moon close to 

//the current date. 



monthname=new Array(

"January","February","March","April","May","June",

 "July","August","September","October","November", "December");


//initialize date
daty=new Date();

tz= daty.getTimezoneOffset();

yr=daty.getFullYear();

mo=daty.getMonth()+1;

dy=daty.getDate();

hr=daty.getHours();

mns=daty.getMinutes();



dr=Math.PI/180;


function doit() {

julian(yr,mo,dy); 

jdcurrent=jd+ hr/24+(mns+tz)/1440;

T=(jd-2415020)/36525;

k = inty((T*1236.85)+.5);

k = k+1;

Jnew(k);// next New Moon

jdcal(JdNew);

document.writeln("<br>Next New Moon:<br> ");

writer(y,m,d,f);

}





//this function displays a date and time

function writer(y,m,d,f){

//document.write (inty(24*f)+" hours ");

//document.write (inty(60*(24*f-inty(24*f)))+" minutes U.T. ");

document.write (inty(d)+" ");
document.write(monthname [m-1]+" ");
document.write(y+"   ");

}



//this function uses k to calculate JdNew, julian day of new moon

function Jnew(k){



Jd1=2415020.75933+29.53058868*k+.0001178*T*T-.000000155*T*T*T;

Jd1=Jd1+.00033*Math.sin((166.56+132.87*T-.009173*T*T)*dr)

T=k/1236.85;

M=359.2242+29.10535608*k-.0000333*T*T-.00000347*T*T*T;

Mpr=306.0253+385.81691806*k+.0107306*T*T+.00001236*T*T*T;

F=21.2964+390.67050646*k-.0016528*T*T-.00000239*T*T*T;

C1=(0.1734-.000393*T)*Math.sin(M*dr)+.0021*Math.sin(2*dr*M);

C1=C1-0.4068*Math.sin(Mpr*dr)+0.0161*Math.sin(dr*2*Mpr);

C1=C1-.0004*Math.sin(dr*3*Mpr);

C1=C1+.0104*Math.sin(dr*2*F)-.0051*Math.sin(dr*(M+Mpr));

C1=C1-.0074*Math.sin(dr*(M-Mpr))+.0004*Math.sin(dr*(2*F+M));

C1=C1-.0004*Math.sin(dr*(2*F-M))-.0006*Math.sin(dr*(2*F+Mpr));

C1=C1+.0010*Math.sin(dr*(2*F-Mpr))+.0005*Math.sin(dr*(2*Mpr+M));

// converts ephemeris time to Universal Time 

if (T<-11){deltat=0.001+0.000839*T+0.0002261*T*T-0.00000845*T*T*T-0.000000081*T*T*T*T;}

else {deltat=-0.000278+0.000265*T+0.000262*T*T;};

JdNew=Jd1+C1-deltat;



}



//this function takes the Julian day and converts it to a calendar date

function jdcal(jd){

Z=inty(jd+.5);

f=jd+.5-Z;

if (Z<2299161) {A=Z}

else

{alpha =inty((Z-1867216.25)/36524.25);A=Z+1+alpha-inty(alpha/4)};

B=A+1524;

C=inty((B-122.1)/365.25);

D=inty(365.25*C);

E=inty((B-D)/30.6001);

d=B-D - inty(30.6001*E); 

if (E<13.5) {m=E-1}

else {m=E-13};

if (m>2.5) {y=C-4716}

else {y=C-4715};

}



//this function takes in the year, month and day

//and calculates the Julian Day, jd

function julian(yr,mo,dy) {

jy= yr + mo/12;

bb=1;

if (jy < 1582+10/12) {bb=0;} 

else {bb=1;}

bg=0;

if (yr<0){bg=1};

if (mo<=2){mo=mo+12;yr=yr-1;}

a =inty(yr/100);

b = (2 - a + inty(a/4))*bb;

jd=inty(365.25*yr-bg*0.75) +inty(30.601*(mo+1))+dy+1720994.5+b;

}



//this function gets the integer value of x

function inty (x){

if (x>0) {intg=Math.floor(x)}else {intg=Math.ceil(x)};

return intg

}

