/* ***************************************
   Supporting code and necessary functions
*****************************************/
// constants
var synodic = 29.53058867;     // the synodic period
var msPerDay = 24 * 60 * 60 * 1000;   // milliseconds per day

function rnd(val, prec) 
  {
  // general function for rounding to specified precision
  val = val * Math.pow(10,prec);
  val = Math.round(val);
  val = val / Math.pow(10,prec);
  return val;
  }

function moonPhPer(anyDate)
  {
  // calculate the moon phase as a percent
  // a convenient New Moon date was Dec 18, 1998 at 21:36 UT (16:36 Eastern)
  // Updated on Aug 26, 2003 for more current full moon date, July 29, at 6:53 UT
  var baseDate = new Date();
  baseDate.setUTCFullYear(2003);
  baseDate.setUTCMonth(6);
  baseDate.setUTCDate(29);
  baseDate.setUTCHours(6);
  baseDate.setUTCMinutes(53);
  baseDate.setUTCSeconds(0);

  var diff = anyDate - baseDate + msPerDay;   // the difference in milliseconds
  var phase = Math.abs(diff) / (synodic * msPerDay); // phase percent
  phase *=100;        // make it a ##.##% figure
  while(phase>100)
    {
    phase -= 100;
    }
  return phase;
  }

function moonFile(phase)
  // select one of moon images based on moon phase value
{
  var pVal = phase * 3.6;
  var mFile = '';
  pVal = Math.round(pVal);
  var pRem = pVal % 2;
  if (pRem > 0) pVal += 1;
  pVal += '';
  if (pVal < 10)
    mFile = 'm00'+pVal+'.gif';
  else if (pVal < 100)
    mFile = 'm0'+pVal+'.gif';
  else
    mFile = 'm'+pVal+'.gif';
  return mFile;
}


var today = new Date();
var phasePercent = moonPhPer(today);
var mfName = moonFile(phasePercent);


document.write("<IMG SRC=\"/Image/moon/");
document.write(mfName);
document.write("\" width=\"100\" height=\"100\" alt=\"Current lunar phase\"><BR>");
