// Date0 is the number of millisecond between 1-JAN-1970 00:00:00 UTC and the first millisecond(UTC) in the current year
var Date0=1293840000000; // at 2011-01-01 08:00:00 HKT

// 18 cycles for the Moon phase
var NewPhase=new Array(291780,2860260,5431560,8001120,10565460,13122180,15670440,18211200,20747040,23281740,25818960,28361400,30909960,33464340,36023700,38587020,41152680,43717620);
var FirstPhase=new Array(991860,3568680,6133500,8683500,11219580,13745460,16266540,18788880,21317940,23858100,26411880,28979520,31558500,34143000,36724860,39296460,41853420,44396160);
var FullPhase=new Array(1632060,4178160,6718200,9254640,11790540,14328840,16872000,19421820,21979620,24545160,27116160,29687760,32254200,34811640,37359540,39899940,42435300,44968320);
var LastPhase=new Array(2206620,4749960,7301220,9859620,12423120,14989680,17557320,20123640,22685940,25241400,27788940,30329280,32864880,35399040,37934700,40474200,43019220,45571260);
 
var d1 = new Date();
var Now=(d1.getTime()-Date0)/1000;
 
i=0;
 
while (i<18)
{ if ((NewPhase[i]<=Now)&&(Now<NewPhase[i+1]))
  { MoonAge=(Now-NewPhase[i])/3600/24;
    if (LastPhase[i]<Now)
    { ph=3;
      pt=parseInt((Now-LastPhase[i])/((NewPhase[i+1]-LastPhase[i])/8)+0.5);
    }
    else if (FullPhase[i]<Now)
    { ph=2;
      pt=parseInt((Now-FullPhase[i])/((LastPhase[i]-FullPhase[i])/8)+0.5);
    }
    else if (FirstPhase[i]<Now)
    { ph=1;
      pt=parseInt((Now-FirstPhase[i])/((FullPhase[i]-FirstPhase[i])/8)+0.5);
    }
    else
    { ph=0;
      pt=parseInt((Now-NewPhase[i])/((FirstPhase[i]-NewPhase[i])/8)+0.5);
    }
    if (pt==8)
    { ph=(ph+1)%4;
      pt=0;
    }
    num=i;
    i=17;
  }
  i++;
}
 
AgeDay=parseInt(MoonAge);
AgeHour=(MoonAge-AgeDay)*24;
AltE="Moon Age: " + AgeDay + " day(s) " + AgeHour.toFixed(1) + " hour(s)";
 
document.write("<div name=\"image\" id=\"image\" class=\"image\"><IMG SRC=\"icon30/MOON" + ph + "0" + pt + ".jpg\" ALT=\"" + AltE + "\" TITLE=\"" + AltE + "\"></div>");

