<!--

// toInt() - our own casting routine that always truncates the number passed
//               in regardless of sign.
//
function toInt(number)
{
        var negative = false;
        if (number < 0)
                negative = true;
        number = Math.floor(Math.abs(number));
        if (negative)
                number *= -1;
        return number;
}

// julianDay() - takes in year, month and date and returns the julian day number
//                 will return -1 on an illegal incoming date.  
//              
// based on JULDAY BASIC subroutine in "Astronomy with your Personal Computer"
// by Peter Duffett-Smith, Cambridge University Press, 1990
//
function julianDay(year, month, date)
{
        var a;  //  components of julian day formula
        var b = 0;
        var c;
        var d;
        var nDate;
        var nMonth;
        var nYear;
        var nJulDay;

        // first get everything into the proper format to avoid weirdo casting rules
        // inherent in JavaScript

        nYear = parseInt(year);
        nMonth = parseInt(month);
        nDate = parseInt(date);

        // do some basic error handling for illegal dates you can't have year 0 in Julian 

        if (nYear == 0)
                return -1;

        // do basic adjustments on month & year

        if (nYear < 1)
        {nYear  += 1;}

        if (nMonth < 3)
        {nMonth += 12;
        nYear-= 1;}

        // now deal with the gregorian calendar changes 
        // first check for illegal dates from Oct. 5-14 , 1582  

        if (nYear == 1582 && nMonth == 10  && (nDate >= 5 || nDate <= 14)) 
                return -1;

        if (nYear > 1582 || (nYear == 1582 && (nMonth > 10 || (nMonth == 10 && nDate > 15))))
        {
                // follow rules to generate component b

                a = Math.floor(nYear / 100);
                b = 2 - a + Math.floor(a / 4);
        }

        // follow rules to generate components c & d

        if (year > 0)
                c = Math.floor(365.25 * nYear) - 694025;
        else
                c = toInt((365.25 * nYear) - 0.75) - 694025;
        d = Math.floor(30.6001 * (nMonth + 1));
        julday = b + c + d + nDate + 2415020.0 - 0.5;
        return julday;
}

// calcMoonAge() - calculate the age of the moon in days from new
//
// Adapted from MOON EFFECTS BASIC application by Bradley E. Schaefer
// from Sky & Telescope magazine, April 1994.
//
function calcMoonAge(year, month, date)
{
        var jDate;
        var tempVal;

        // get julian date and fudge it a bit, this is a hueristic algorithm 
        // and isn't exact science.

        jDate = julianDay(year, month, date)+ 0.5;
        tempVal = (jDate - 2451550.1) / 29.530588853;
        tempVal -= Math.floor(tempVal);
        if (tempVal < 0) tempVal += 1;
        age = (tempVal * 29.530588853);
        return age;
}


// doMoonPhase() - test the calcMoonAge function from the form and
//                       output the phase in an alert
function doMoonPhase()
{ 
        var form;
        var mdate;
        var year;
        var month;
        var date;

        form = document.calform;
        mdate = new Date(form.datebox.value);
        year = mdate.getFullYear();
        month = mdate.getMonth() + 1;
        date = mdate.getDate();
        age = calcMoonAge(year, month, date);

        // do simple phase output 

        if (age >= 29.5 || age <= 0.5)
                alert("Age: " + parseInt(age) + " days. The Moon is New");

        if (age > 0.5 && age < 7.0)
                alert("Age: " + parseInt(age) + " days. The moon is a waxing crescent");

        if (age > 7.0 && age < 8.0)
                alert("Age: " + parseInt(age) + " days. The moon is at first quarter");

        if (age > 8.0 && age < 14.0)
                alert("Age: " + parseInt(age) + " days. The gibbous moon is waxing");

        if (age > 14.0 && age < 15.0)
                alert("Age: " + parseInt(age) + " days. The moon is full");

        if (age > 15.0 && age < 21.5)
                alert("Age: " + parseInt(age) + " days. The gibbous moon is wanning");

        if (age > 21.5 && age < 22.7)
                alert("Age: " + parseInt(age) + " days. The moon is at last quarter");

        if (age > 22.7 && age < 29.5)
                alert("Age: " + parseInt(age) + " days. The moon is a wanning crescent");
}

// moonPhasePic() - test the calcMoonAge function from the form 
//                  output the phase inline
function moonPhasePic(mdate)
{ 
		year = mdate.getFullYear();
		month = mdate.getMonth() + 1;
		date = mdate.getDate();
        var age = calcMoonAge(year, month, date);
        document.write("<img src=images/moons/moon"+ parseInt(age) +".gif alt=");
        moonPhaseShort(mdate);
        document.write(">");
}

// moonPhaseTxt() - test the calcMoonAge function from the form 
//                  output the phase inline
function moonPhaseTxt(mdate)
{ 
		year = mdate.getFullYear();
		month = mdate.getMonth() + 1;
		date = mdate.getDate();
        var age = calcMoonAge(year, month, date);

        if (age >= 29.5 || age <= 0.5)
                phase = "New Moon";

        if (age > 0.5 && age < 7.0)
                phase = "Waxing Crescent";

        if (age > 7.0 && age < 8.0)
                phase = "First Quarter";

        if (age > 8.0 && age < 14.0)
                phase = "Waxing Gibbous";

        if (age > 14.0 && age < 15.0)
                phase = "Full Moon";

        if (age > 15.0 && age < 21.5)
                phase = "Wanning Gibbous";

        if (age > 21.5 && age < 22.7)
                phase = "Last Quarter";

        if (age > 22.7 && age < 29.5)
                phase = "Wanning Crescent";

        document.write(phase);
}


// moonPhaseShort() - Short Moon Phase
function moonPhaseShort(mdate)
{ 
		year = mdate.getFullYear();
		month = mdate.getMonth() + 1;
		date = mdate.getDate();
        var age = calcMoonAge(year, month, date);

        if (age >= 29.5 || age <= 0.5)
                phase = "NewMoon";

        if (age > 0.5 && age < 7.0)
                phase = "WaxingCrescent";

        if (age > 7.0 && age < 8.0)
                phase = "FirstQuarter";

        if (age > 8.0 && age < 14.0)
                phase = "WaxingGibbous";

        if (age > 14.0 && age < 15.0)
                phase = "FullMoon";

        if (age > 15.0 && age < 21.5)
                phase = "WanningGibbous";

        if (age > 21.5 && age < 22.7)
                phase = "LastQuarter";

        if (age > 22.7 && age < 29.5)
                phase = "WanningCrescent";

        document.write(phase);

}


// moonPhaseAge() - Moon Phase Age
function moonPhaseAge(mdate)
{ 
		year = mdate.getFullYear();
		month = mdate.getMonth() + 1;
		date = mdate.getDate();
        var age = calcMoonAge(year, month, date);
        phase = parseInt(age)
        document.write(phase);

}

