

//====================================================================
//
//οvZTvvO  $Revision:   1.1  $
//Coded by H.Takano (C)1993,1994
//http://www.vector.co.jp/soft/dos/personal/se016093.html
//
//
//====================================================================

//
var k = Math.PI / 180;
var ndt = new Date() //
var tz = ndt.getTimezoneOffset() / 1440; //
var rm_sun0; //

// 
Date.prototype.getJD = Date_getJD;
Date.prototype.setJD = Date_setJD;
function Date_getJD() {
return 2440587 + this.getTime() / 864e5 - tz;
}
function Date_setJD(jd) {
this.setTime((jd + tz - 2440587) * 864e5);
}

//====================================================================

function kyureki(tm) {
var i,lap,state;
var chu = new Array(4);
var saku = new Array(5);
var m = new Array(5);
for(i = 0; i < 5; i++) m[i] = new Object;

//====================================================================
chu[0] = calc_chu(tm, 90);
//====================================================================
m[0].month = Math.floor(rm_sun0 / 30) + 2;

for(i = 1; i < 4; i++) {
chu[i] = calc_chu(chu[i - 1] + 32, 30);
}

saku[0] = calc_saku(chu[0]);

for(i = 1; i < 5; i++) {
saku[i] = calc_saku(saku[i - 1] + 30);

if(Math.abs(Math.floor(saku[i - 1]) - Math.floor(saku[i])) <= 26) {
saku[i] = calc_saku(saku[i - 1] + 35);
}
}

if( Math.floor(saku[1]) <= Math.floor(chu[0]) ) {
for(i = 0; i < 4; i++) {
saku[i] = saku[i + 1];
}
saku[4] = calc_saku(saku[3] + 35);
}

else if( Math.floor(saku[0]) > Math.floor(chu[0]) ) {
for(i = 4; i > 0; i--) saku[i] = saku[i - 1];
saku[0] = calc_saku(saku[0] - 27);
}

lap = ( Math.floor(saku[4]) <= Math.floor(chu[3]) );


m[0].uruu = false;
m[0].jd = Math.floor(saku[0]);
for(i = 1; i < 5; i++) {
if( lap && i > 1 ) {
if( chu[i - 1] <= Math.floor(saku[i - 1])
|| chu[i - 1] >= Math.floor(saku[i]) ) {
m[i-1].month = m[i-2].month;
m[i-1].uruu = true;
m[i-1].jd = Math.floor(saku[i - 1]);
lap = false;
}
}
m[i].month = m[i-1].month + 1;
if( m[i].month > 12 ) m[i].month -= 12;
m[i].jd = Math.floor(saku[i]);
m[i].uruu = false;
}

state = 0;
for(i = 0; i < 5; i++) {
if( Math.floor(tm) < Math.floor(m[i].jd) ) {
state = 1;
break;
}
else if( Math.floor(tm) == Math.floor(m[i].jd) ) {
state = 2;
break;
}
}
if( state == 0 || state == 1 ) i--;

this.uruu = m[i].uruu;
this.month = m[i].month;
this.day = Math.floor(tm) - Math.floor(m[i].jd) + 1;
//
if(mcont == 1) {
var tm12 = new Date(yy,mm-1,dd,12);	//
var tm12 = tm12.getJD();
this.moon = Math.round((tm12 - calc_saku(tm)) * 10.0)/10.0;//
} else {
this.moon = Math.round((tm - calc_saku(tm)) * 10.0)/10.0;//
}
if ( this.moon >= 29.9 ) { this.moon -= 29.8; }	//
if ( this.moon < 0.0 ) { this.moon = 29.3; }	//
this.moon = Math.round( this.moon * 10 ) / 10;
//

var a = new Date();
a.setJD(tm);
//	
this.year = a.getFullYear();
this.year = a.getYear(); 
if (this.year < 2000) this.year += 1900;
if( this.month > 9 && this.month > a.getMonth() + 1 ) this.year--;


//	

//
var rokuyo = new Array("ζ","Fψ","ζ","§Ε","εΐ","Τϋ");
this.rokuyo = rokuyo[(this.month + this.day - 2) % 6];
//for(i=0;i<5;i++)document.writeln(m[i].month,"\t",m[i].uruu,"\t",m[i].jd);
//for(i=0;i<4;i++)document.writeln(chu[i]);
}



function calc_chu(tm, longitude) {
var tm1,tm2,t,rm_sun,delta_rm;

tm1 = Math.floor(tm);
tm2 = tm - tm1 + tz; //

t = (tm2 + .5) / 36525 + (tm1 - 2451545) / 36525;
rm_sun = LONGITUDE_SUN(t);
rm_sun0 = longitude * Math.floor(rm_sun / longitude);


var delta_t1 = 0, delta_t2 = 1;
for( ; Math.abs(delta_t1 + delta_t2) > (1 / 86400); ) {

//	 
t = (tm + .5 - 2451545) / 36525;
t = (tm2 + .5) / 36525 + (tm1 - 2451545) / 36525;
rm_sun = LONGITUDE_SUN(t);

delta_rm = rm_sun - rm_sun0 ;

if( delta_rm > 180 ) {
delta_rm -= 360;
} else if( delta_rm < -180 ) {
delta_rm += 360;
}

delta_t1 = Math.floor(delta_rm * 365.2 / 360);
delta_t2 = delta_rm * 365.2 / 360 - delta_t1;

// tm -= delta_t;

tm1 = tm1 - delta_t1;
tm2 = tm2 - delta_t2;
if( tm2 < 0 ) {
tm1 -= 1;
tm2 += 1;
}
}

return tm2 + tm1 - tz;
}


function calc_saku(tm) {
var lc,t,tm1,tm2,rm_sun,rm_moon,delta_rm;

lc = 1;

tm1 = Math.floor(tm);
tm2 = tm - tm1 + tz;	

var delta_t1 = 0, delta_t2 = 1;
for( ; Math.abs( delta_t1 + delta_t2 ) > ( 1 / 86400 ) ; lc++) {
//	 
t = (tm + .5 - 2451545) / 36525;

t = (tm2 + .5) / 36525 + (tm1 - 2451545) / 36525;
rm_sun = LONGITUDE_SUN(t);
rm_moon = LONGITUDE_MOON(t);

// ’ΙΙmoon|Ιsun

delta_rm = rm_moon - rm_sun ;

if( lc==1 && delta_rm < 0 ) {
delta_rm = NORMALIZATION_ANGLE(delta_rm);
}

else if( rm_sun >= 0 && rm_sun <= 20 && rm_moon >= 300 ) {
delta_rm = NORMALIZATION_ANGLE(delta_rm);
delta_rm = 360 - delta_rm;
}

else if( Math.abs(delta_rm) > 40 ) {
delta_rm = NORMALIZATION_ANGLE(delta_rm);
}


// 
delta_t = delta_rm * 29.530589 / 360;

delta_t1 = Math.floor(delta_rm * 29.530589 / 360);
delta_t2 = delta_rm * 29.530589 / 360 - delta_t1;

// 
tm -= delta_t;

tm1 = tm1 - delta_t1;
tm2 = tm2 - delta_t2;
if( tm2 < 0 ) {
tm1 -= 1;
tm2 += 1;
}


if( lc == 15 && Math.abs(delta_t1 + delta_t2) > (1 / 86400) ) {
tm1 = Math.floor(tm - 26);
tm2 = 0;
}

else if( lc > 30 && Math.abs(delta_t1 + delta_t2) > (1 / 86400) ) {
tm1 = tm;
tm2 = 0;
break;
}
}

return tm2 + tm1 - tz;
}


function NORMALIZATION_ANGLE(angle) {
return angle - 360 * Math.floor(angle / 360);
}


function LONGITUDE_SUN(t) {
var ang,th;
with(Math) {

	th = .0004 * cos( k * NORMALIZATION_ANGLE( 31557 * t + 161 ) );
	th += .0004 * cos( k * NORMALIZATION_ANGLE( 29930 * t + 48 ) );
	th += .0005 * cos( k * NORMALIZATION_ANGLE( 2281 * t + 221 ) );
	th += .0005 * cos( k * NORMALIZATION_ANGLE( 155 * t + 118 ) );
	th += .0006 * cos( k * NORMALIZATION_ANGLE( 33718 * t + 316 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 9038 * t + 64 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 3035 * t + 110 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 65929 * t + 45 ) );
	th += .0013 * cos( k * NORMALIZATION_ANGLE( 22519 * t + 352 ) );
	th += .0015 * cos( k * NORMALIZATION_ANGLE( 45038 * t + 254 ) );
	th += .0018 * cos( k * NORMALIZATION_ANGLE( 445267 * t + 208 ) );
	th += .0018 * cos( k * NORMALIZATION_ANGLE( 19 * t + 159 ) );
	th += .0020 * cos( k * NORMALIZATION_ANGLE( 32964 * t + 158 ) );
	th += .0200 * cos( k * NORMALIZATION_ANGLE( 71998.1 * t + 265.1 ) );
	ang = NORMALIZATION_ANGLE( 35999.05 * t + 267.52 );
	th = th - .0048 * t * cos( k * ang ) ;
	th += 1.9147 * cos( k * ang ) ;
	
	ang = NORMALIZATION_ANGLE( 36000.7695 * t );
	ang = NORMALIZATION_ANGLE( ang + 280.4659 );
	th = NORMALIZATION_ANGLE( th + ang );
	}
	return th;
}


function LONGITUDE_MOON(t) {
var ang,th;
with(Math) {

	th = .0003 * cos( k * NORMALIZATION_ANGLE( 2322131 * t + 191 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 4067 * t + 70 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 549197 * t + 220 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 1808933 * t + 58 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 349472 * t + 337 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 381404 * t + 354 ) );
	th += .0003 * cos( k * NORMALIZATION_ANGLE( 958465 * t + 340 ) );
	th += .0004 * cos( k * NORMALIZATION_ANGLE( 12006 * t + 187 ) );
	th += .0004 * cos( k * NORMALIZATION_ANGLE( 39871 * t + 223 ) );
	th += .0005 * cos( k * NORMALIZATION_ANGLE( 509131 * t + 242 ) );
	th += .0005 * cos( k * NORMALIZATION_ANGLE( 1745069 * t + 24 ) );
	th += .0005 * cos( k * NORMALIZATION_ANGLE( 1908795 * t + 90 ) );
	th += .0006 * cos( k * NORMALIZATION_ANGLE( 2258267 * t + 156 ) );
	th += .0006 * cos( k * NORMALIZATION_ANGLE( 111869 * t + 38 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 27864 * t + 127 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 485333 * t + 186 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 405201 * t + 50 ) );
	th += .0007 * cos( k * NORMALIZATION_ANGLE( 790672 * t + 114 ) );
	th += .0008 * cos( k * NORMALIZATION_ANGLE( 1403732 * t + 98 ) );
	th += .0009 * cos( k * NORMALIZATION_ANGLE( 858602 * t + 129 ) );
	th += .0011 * cos( k * NORMALIZATION_ANGLE( 1920802 * t + 186 ) );
	th += .0012 * cos( k * NORMALIZATION_ANGLE( 1267871 * t + 249 ) );
	th += .0016 * cos( k * NORMALIZATION_ANGLE( 1856938 * t + 152 ) );
	th += .0018 * cos( k * NORMALIZATION_ANGLE( 401329 * t + 274 ) );
	th += .0021 * cos( k * NORMALIZATION_ANGLE( 341337 * t + 16 ) );
	th += .0021 * cos( k * NORMALIZATION_ANGLE( 71998 * t + 85 ) );
	th += .0021 * cos( k * NORMALIZATION_ANGLE( 990397 * t + 357 ) );
	th += .0022 * cos( k * NORMALIZATION_ANGLE( 818536 * t + 151 ) );
	th += .0023 * cos( k * NORMALIZATION_ANGLE( 922466 * t + 163 ) );
	th += .0024 * cos( k * NORMALIZATION_ANGLE( 99863 * t + 122 ) );
	th += .0026 * cos( k * NORMALIZATION_ANGLE( 1379739 * t + 17 ) );
	th += .0027 * cos( k * NORMALIZATION_ANGLE( 918399 * t + 182 ) );
	th += .0028 * cos( k * NORMALIZATION_ANGLE( 1934 * t + 145 ) );
	th += .0037 * cos( k * NORMALIZATION_ANGLE( 541062 * t + 259 ) );
	th += .0038 * cos( k * NORMALIZATION_ANGLE( 1781068 * t + 21 ) );
	th += .0040 * cos( k * NORMALIZATION_ANGLE( 133 * t + 29 ) );
	th += .0040 * cos( k * NORMALIZATION_ANGLE( 1844932 * t + 56 ) );
	th += .0040 * cos( k * NORMALIZATION_ANGLE( 1331734 * t + 283 ) );
	th += .0050 * cos( k * NORMALIZATION_ANGLE( 481266 * t + 205 ) );
	th += .0052 * cos( k * NORMALIZATION_ANGLE( 31932 * t + 107 ) );
	th += .0068 * cos( k * NORMALIZATION_ANGLE( 926533 * t + 323 ) );
	th += .0079 * cos( k * NORMALIZATION_ANGLE( 449334 * t + 188 ) );
	th += .0085 * cos( k * NORMALIZATION_ANGLE( 826671 * t + 111 ) );
	th += .0100 * cos( k * NORMALIZATION_ANGLE( 1431597 * t + 315 ) );
	th += .0107 * cos( k * NORMALIZATION_ANGLE( 1303870 * t + 246 ) );
	th += .0110 * cos( k * NORMALIZATION_ANGLE( 489205 * t + 142 ) );
	th += .0125 * cos( k * NORMALIZATION_ANGLE( 1443603 * t + 52 ) );
	th += .0154 * cos( k * NORMALIZATION_ANGLE( 75870 * t + 41 ) );
	th += .0304 * cos( k * NORMALIZATION_ANGLE( 513197.9 * t + 222.5 ) );
	th += .0347 * cos( k * NORMALIZATION_ANGLE( 445267.1 * t + 27.9 ) );
	th += .0409 * cos( k * NORMALIZATION_ANGLE( 441199.8 * t + 47.4 ) );
	th += .0458 * cos( k * NORMALIZATION_ANGLE( 854535.2 * t + 148.2 ) );
	th += .0533 * cos( k * NORMALIZATION_ANGLE( 1367733.1 * t + 280.7 ) );
	th += .0571 * cos( k * NORMALIZATION_ANGLE( 377336.3 * t + 13.2 ) );
	th += .0588 * cos( k * NORMALIZATION_ANGLE( 63863.5 * t + 124.2 ) );
	th += .1144 * cos( k * NORMALIZATION_ANGLE( 966404 * t + 276.5 ) );
	th += .1851 * cos( k * NORMALIZATION_ANGLE( 35999.05 * t + 87.53 ) );
	th += .2136 * cos( k * NORMALIZATION_ANGLE( 954397.74 * t + 179.93 ) );
	th += .6583 * cos( k * NORMALIZATION_ANGLE( 890534.22 * t + 145.7 ) );
	th += 1.2740 * cos( k * NORMALIZATION_ANGLE( 413335.35 * t + 10.74 ) );
	th += 6.2888 * cos( k * NORMALIZATION_ANGLE( 477198.868 * t + 44.963 ) );

	ang = NORMALIZATION_ANGLE( 481267.8809 * t );
	ang = NORMALIZATION_ANGLE( ang + 218.3162 );
	th = NORMALIZATION_ANGLE( th + ang );
	}
	return th
}

