// 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=1262304000000; // at 2010-01-01 08:00:00 HKT

// 18 cycles for the Moon phase
var NewPhase=new Array(1235460,3811860,6382860,8944140,11495040,14037300,16573200,19105680,21637800,24173040,26715120,29266560,31827780,34396260,36967560,39537120,42101460,44658180);
var FirstPhase=new Array(1939980,4495320,7038000,9570000,12094980,14617740,17143860,19678440,22225800,24787620,27362340,29944740,32527860,35104680,37669500,40219500,42755580,45281460);
var FullPhase=new Array(2528280,5071080,7611900,10153080,12697620,15247800,17804220,20365500,22929420,25493760,28056420,30615180,33168060,35714160,38254200,40790640,43326540,45864840);
var LastPhase=new Array(3109680,5672520,8242620,10815300,13385580,15950100,18507540,21057720,23601120,26138760,28672560,31205880,33742620,36285960,38837220,41395620,43959120,46525680);
 
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>");
