var n0 = parseInt( "0" );
var f0 = parseFloat( "0.0" );
var AG = f0;   
var DI = f0;   
var LA = f0;   
var LO = f0;   
var Phase = " ";
var Zodiac = " ";

function initialize()
{
    var d = new Date();
	calculate();
}

function calculate()
{	var d = new Date();

    var year  = parseInt( d.getFullYear());    
    var month = parseInt( d.getMonth()+1);    
    var day   = parseInt( d.getDate());

    if( !isdayofmonth( year, month, day ) )
    {
        alert( "Invalid date" );
        return;
    }

    moon_posit( year, month, day );
    var Pf = Phase;
}

var n28 = parseInt( "28" );
var n30 = parseInt( "30" );
var n31 = parseInt( "31" );
var dim = new Array( n31, n28, n31, n30, n31, n30, n31, n31, n30, n31, n30, n31 );

function isdayofmonth( y, m, d )
{
    if( m != 2 )
    {
        if( 1 <= d && d <= dim[m-1] )
            return true;
        else
            return false;
    }

    var feb = dim[1];
 
    if( isleapyear( y ) )
        feb += 1;                                  
    if( 1 <= d && d <= feb )
        return true;

    return false;       
}

function isleapyear( y )
{
    var x = Math.floor( y - 4*Math.floor( y/4 ) );
    var w = Math.floor( y - 100*Math.floor( y/100 ) );
    var z = Math.floor( y - 400*Math.floor( y/400 ) );

    if( x == 0 )                         
    {
        if( w == 0 && z != 0 )
            return false;                  
        else
            return true;                   
    }

    return false;
}

function moon_posit( Y, M, D )
{
    var YY = n0;
    var MM = n0;
    var K1 = n0; 
    var K2 = n0; 
    var K3 = n0;
    var JD = n0;
    var IP = f0;
    var DP = f0;
    var NP = f0;
    var RP = f0;
    
    
    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 + 0.5 );
    K3 = Math.floor( Math.floor( ( YY / 100 ) + 49 ) * 0.75 ) - 38;
    
    JD = K1 + K2 + D + 59;                  
    if( JD > 2299160 ) JD = JD - K3;       
        
    
    IP = normalize( ( JD - 2451550.1 ) / 29.530588853 );
    AG = IP*29.53;
    
    if(      AG <  1.84566 ) Phase = "new_moon.gif";
    else if( AG <  5.53699 ) Phase = "wax_crs.gif";
    else if( AG <  9.22831 ) Phase = "one_qw.gif";
    else if( AG < 12.91963 ) Phase = "wax_gib.gif";
    else if( AG < 16.61096 ) Phase = "full_moon.gif";
    else if( AG < 20.30228 ) Phase = "wan_gib.gif";
    else if( AG < 23.99361 ) Phase = "last_qw.gif";
    else if( AG < 27.68493 ) Phase = "wan_crs.gif";
    else                     Phase = "new_moon.gif";
	if(document.getElementById('moon_phase'))document.getElementById('moon_phase').src='/images/wether_img/'+Phase;

    IP = IP*2*Math.PI;                     
    DP = 2*Math.PI*normalize( ( JD - 2451562.2 ) / 27.55454988 );
    DI = 60.4 - 3.3*Math.cos( DP ) - 0.6*Math.cos( 2*IP - DP ) - 0.5*Math.cos( 2*IP );
    NP = 2*Math.PI*normalize( ( JD - 2451565.2 ) / 27.212220817 );
    LA = 5.1*Math.sin( NP );

    RP = normalize( ( JD - 2451555.8 ) / 27.321582241 );
    LO = 360*RP + 6.3*Math.sin( DP ) + 1.3*Math.sin( 2*IP - DP ) + 0.7*Math.sin( 2*IP );

    if(      LO <  33.18 ) Zodiac = "Pisces";
    else if( LO <  51.16 ) Zodiac = "Aries";
    else if( LO <  93.44 ) Zodiac = "Taurus";
    else if( LO < 119.48 ) Zodiac = "Gemini";
    else if( LO < 135.30 ) Zodiac = "Cancer";
    else if( LO < 173.34 ) Zodiac = "Leo";
    else if( LO < 224.17 ) Zodiac = "Virgo";
    else if( LO < 242.57 ) Zodiac = "Libra";
    else if( LO < 271.26 ) Zodiac = "Scorpio";
    else if( LO < 302.49 ) Zodiac = "Sagittarius";
    else if( LO < 311.72 ) Zodiac = "Capricorn";
    else if( LO < 348.58 ) Zodiac = "Aquarius";
    else                   Zodiac = "Pisces";

    if ( LO > 360 ) LO = LO - 360;
}

function round2( x )
{
    return ( Math.round( 100*x )/100.0 );
}
 
function normalize( v )
{
    v = v - Math.floor( v  ); 
    if( v < 0 )
        v = v + 1;
        
    return v;
}

function allclear()
{
    document.calc.year.value='0';
    document.calc.month.value='0';
    document.calc.day.value='0';
}


var P1,P2,DR,F,J

function init2() {
 today= new Date()
 ID=today.getDate()
 IM=today.getMonth()+1
 IY=today.getYear()+1900
 B5=55.76
 L5=37.61
 H=-3
 P1=3.14159265
 P2=2.*P1
 DR=P1/180.
 
 sunrise();
}

function myint(x){
 if (x>=0)
 x=Math.floor(x)
 
 else
 x=Math.ceil(x)
 return x 
}

function sunrise() {
 with (Math){

 H =parseInt('-3')
 SR=""
 SS=""
 L5=L5/360.
 Z0=H/24.

 G=1
 J3=0
 if (IY<1583){
 G=0
 }
 with(Math){
 D1=myint(ID)
 F=ID-D1-.5
 J=-myint(7*(myint((IM+9)/12)+IY)/4)
 if (G != 0) {
 S=((IM-9)<0?-1:1)
 A=abs(IM-9)
 J3=myint(IY+S*myint(A/7))
 J3=-myint((myint(J3/100)+1)*3/4)
 }

 J=J+myint(275*IM/9)+D1+G*J3
 J=J+1721027+2*G+367*IY
 if (F<0) {
 F=F+1
 J=J-1
 }
 }

 T=(J-2451545.)+F
 TT=T/36525.+1.
 T0=T/36525.
 S=24110.5+8640184.813*T0
 S=S+86636.6*Z0+86400.*L5
 S=S/86400.
 SM=myint(S)
 S=S-myint(S)
 T0=S*360.*DR
 T=T+Z0

 L=.779072+.00273790931*T
 G=.993126+.0027377785*T
 L=L-myint(L)
 G=G-myint(G)
 L=L*P2
 G=G*P2
 V=.39785*sin(L)
 V=V-.01000*sin(L-G)
 V=V+.00333*sin(L+G)
 V=V-.00021*TT*sin(L)
 U=1-.03349*cos(G)
 U=U-.00014*cos(2*L)
 U=U+.00008*cos(L)
 W=-.00010-.04129*sin(2*L)
 W=W+.03211*sin(G)
 W=W+.00104*sin(2*L-G)
 W=W-.00035*sin(2*L+G)
 W=W-.00008*TT*sin(G)
 S=W/sqrt(U-V*V)
 AA1=L+atan(S/sqrt(1-S*S))
 S=V/sqrt(U)
 DA1=atan(S/sqrt(1-S*S))
 R5=1.00021*sqrt(U)
 T=T+1


 L=.779072+.00273790931*T
 G=.993126+.0027377785*T
 L=L-myint(L)
 G=G-myint(G)
 L=L*P2
 G=G*P2
 V=.39785*sin(L)
 V=V-.01000*sin(L-G)
 V=V+.00333*sin(L+G)
 V=V-.00021*TT*sin(L)
 U=1-.03349*cos(G)
 U=U-.00014*cos(2*L)
 U=U+.00008*cos(L)
 W=-.00010-.04129*sin(2*L)
 W=W+.03211*sin(G)
 W=W+.00104*sin(2*L-G)
 W=W-.00035*sin(2*L+G)
 W=W-.00008*TT*sin(G)
 S=W/sqrt(U-V*V)
 AA2=L+atan(S/sqrt(1-S*S))
 S=V/sqrt(U)
 DA2=atan(S/sqrt(1-S*S))
 R5=1.00021*sqrt(U)
 if (AA2 < AA1) {
 AA2=AA2+P2
 }

 Z1=DR*(90.833)

 S=sin(B5*DR)
 C=cos(B5*DR)
 Z=cos(Z1)
 M8=0.
 W8=0.
 A0=AA1
 D0=DA1
 DA=AA2-AA1
 DD=DA2-DA1
 for(C0=0;C0<=23;C0++){
 P=(C0+1.)/24.
 A2=AA1+P*DA
 D2=DA1+P*DD
 K1=15.*DR*1.0027379
 L0=T0+C0*K1
 L2=L0+K1
 H0=L0-A0
 H2=L2-A2
 H1=(H2+H0)/2.
 D1=(D2+D0)/2.
 if (C0<=0.) {
 V0=S*sin(D0)+C*cos(D0)*cos(H0)-Z
 }
 V2=S*sin(D2)+C*cos(D2)*cos(H2)-Z
 
 if ((V0<0?-1:1)==(V2<0?-1:1)) {

 A0=A2
 D0=D2
 V0=V2

 continue
 }
 V1=S*sin(D1)+C*cos(D1)*cos(H1)-Z
 A=2.*V2-4.*V1+2.*V0
 B=4.*V1-3.*V0-V2
 D=B*B-4.*A*V0
 if (D<0) {

 A0=A2
 D0=D2
 V0=V2
 continue
 }
 D=sqrt(D)
 EE=(-B+D)/(2.*A)
 if (EE>1 || EE<0.) {
 EE=(-B-D)/(2.*A)
 }
 T3=C0+EE+1./120.
 H3=myint(T3)
 M3=myint((T3-H3)*60.)
 if (V0<0. && V2>0.) {
 SR=H3+":"+(M3<10?"0":"")+M3
 M8=1.
 }
 if (V0>0. && V2<0.) {
 SS=H3+":"+(M3<10?"0":"")+M3
 W8=1.
 }
 H7=H0+EE*(H2-H0)
 N7=-cos(D1)*sin(H7)
 D7=C*sin(D1)-S*cos(D1)*cos(H7)
 AZ=atan(N7/D7)/DR
 if (D7<0.) {
 AZ=AZ+180.
 }
 if (AZ<0.) {
 AZ=AZ+360.
 }
 if (AZ>360) {
 AZ=AZ-360.
 }

 A0=A2
 D0=D2
 V0=V2

 }


 }
 if(document.getElementById('sunrise_sr'))document.getElementById('sunrise_sr').innerHTML=SR;
  if(document.getElementById('sunrise_ss'))document.getElementById('sunrise_ss').innerHTML=SS;
}





