// JavaScript Document
 // Returns integer portion of x
         function trunc(x)
         {
            if (x < 0)
               return Math.ceil(x);
            else
               return Math.floor(x);
         }

         // Returns fractional portion of x
         function frac(x)
         {
            return Math.abs(x - trunc(x));
         }

         // Converts x (degrees) into radians
         function radians(x)
         {
            return (Math.PI/180 * x);
         }

         // Converts x (radians) into degrees
         function degrees(x)
         {
            return (180/Math.PI * x);
         }

         // Normalizes x (degrees) between 0 and 360
         function norm(x)
         {
            x = x % 360;
            if (x < 0)
               x += 360;
            return x;
         }

         // Provides an unambiguous version of atan(y/x)
         function invtan(y,x)
         {
            ap = degrees(Math.atan(y/x));
            if (y > 0)
            {
               if (x > 0)
               {
                  for (j=-360;j<=360;j+=180)
                  {
                     a = ap + j;
                     if ((a >= 0) && (a < 90))
                        return a;
                  }
               }
               else
               {
                  for (j=-360;j<=360;j+=180)
                  {
                     a = ap + j;
                     if ((a >= 90) && (a < 180))
                        return a;
                  }
               }
            }
            else
            {
               if (x < 0)
               {
                  for (j=-360;j<=360;j+=180)
                  {
                     a = ap + j;
                     if ((a >= 180) && (a < 270))
                        return a;
                  }
               }
               else
               {
                  for (j=-360;j<=360;j+=180)
                  {
                     a = ap + j;
                     if ((a >= 270) && (a < 360))
                        return a;
                  }
               }
            }
            return ap;
         }

         // Validates form entry fields
         function validate()
         {
            if ((parseInt(document.form.TH.value) < 1) || (parseInt(document.form.TH.value) > 12))
            {
               alert("Hour must be between 1 and 12");
               return false;
            }
            if ((parseInt(document.form.TM.value) < 0) || (parseInt(document.form.TM.value) > 59))
            {
               alert("Minute must be between 0 and 59");
               return false;
            }
            if ((parseInt(document.form.TS.value) < 0) || (parseInt(document.form.TS.value) > 59))
            {
               alert("Second must be between 0 and 59");
               return false;
            }
            return true;
         }

         // Returns entered time in Universal Time format
         function getUT()
         {
            hr = parseFloat(document.form.TH.value) + 12 * parseFloat(document.form.TAMPM.options[document.form.TAMPM.selectedIndex].value);
            if (hr % 12 == 0)
               hr -= 12;
            min = parseFloat(document.form.TM.value);
            sec = parseFloat(document.form.TS.value);
            hr = hr + min/60 + sec/3600;
            if (parseFloat(document.form.TCS.options[document.form.TCS.selectedIndex].value) == 0)
               hr = hr - parseFloat(document.form.TC.value);
            else
               hr = hr + parseFloat(document.form.TC.value);
            return hr;
         }

         // Returns entered date in Julian Day format
         // Argument is for fraction of day
         function getJD(time)
         {
            year = parseFloat(document.form.DY.value);
            month = parseFloat(document.form.DM.options[document.form.DM.selectedIndex].value);
            date = parseFloat(document.form.DD.value);
            date = date + time/24;
            correction = false;

            if ((year > 1582) ||
                ((year == 1582) && (month > 10)) ||
                ((year == 1582) && (month == 10) && (day >= 15)))
            {
               correction = true;
            }
            if (month <= 2)
            {
               year--;
               month += 12;
            }
            if (correction)
            {
               A = trunc(year/100);
               B = 2 - A + trunc(A/4);
            }
            else
               B = 0;
            if (year < 0)
               C = trunc((365.25 * year) - 0.75);
            else
               C = trunc(365.25 * year);
            D = trunc(30.6001 * (month + 1));
            return B + C + D + date + 1720994.5;
         }

         // Performs calculation for Moon's position
         function calculate()
         {
            if (validate())
            {
               //
               // NOTE: Epoch used is 1990 January 0.0
               //

               // Time calculations
               UT = getUT();                                         // Universal Time
               document.form.UT.value = UT;
               JD = getJD(UT);                                       // Julian Day
               document.form.JD.value = JD;
               D = JD - 2447891.5;                                   // Days since epoch
               document.form.D.value = D;
               T = (JD - 2415020)/36525;                             // Julian Century

               // Sun calculations
               eg = 279.403303;                                      // Mean ecliptic longitude of Sun at epoch
               wg = 282.768422;                                      // Ecliptic longitude of Sun at perigee at epoch
               e = 0.016713;                                         // Eccentricity of Earth's orbit at epoch
               N = norm(360/365.242191 * D);
               Ms = norm(N + eg - wg);                               // Mean anomaly of Sun
               document.form.MS.value = Ms;
               Ec = 360/Math.PI * e * Math.sin(radians(Ms));
               ls = norm(N + Ec + eg);                               // Longitude of Sun
               document.form.LS.value = ls;

               // Moon calculations
               l0 = 318.351648;                                      // Mean longitude of Moon at epoch
               P0 = 36.340410;                                       // Mean longitude of perigee at epoch
               N0 = 318.510107;                                      // Mean longitude of node at epoch
               i = 5.145396;                                         // Inclination of Moon's orbit
               l = norm(13.1763966*D + l0);                          // Mean longitude of Moon
               document.form.L.value = l;
               Mm = norm(l - 0.1114141*D - P0);                      // Mean anomaly of Moon
               document.form.MM.value = Mm;
               N = norm(N0 - 0.0529539*D);                           // Mean longitude of ascending node
               C = l - ls;
               Ev = 1.2739 * Math.sin(radians(2*C-Mm));              // Correction for evection
               Ae = 0.1858 * Math.sin(radians(Ms));                  // Correction for annual equation
               A3 = 0.37 * Math.sin(radians(Ms));                    // Third correction
               Mmp = norm(Mm + Ev - Ae - A3);                        // Corrected anomaly of Moon
               document.form.MMP.value = Mmp;
               Ec = 6.2886 * Math.sin(radians(Mmp));                 // Correction for equation of the centre
               A4 = 0.214 * Math.sin(radians(2*Mmp));                // Fourth correction
               lp = norm(l + Ev + Ec - Ae + A4);                     // Corrected longitude of Moon
               document.form.LP.value = lp;
               V = 0.6583 * Math.sin(radians(2*(lp-ls)));            // Variation
               lpp = norm(lp + V);                                   // True orbital longitude of Moon
               document.form.LPP.value = lpp;
               Np = norm(N - 0.16*Math.sin(radians(Ms)));            // Corrected longitude of node
               lm = invtan(Math.sin(radians(lpp-Np))*Math.cos(radians(i)),Math.cos(radians(lpp-Np))) + Np;
                                                                     // Ecliptic longitude of Moon
               document.form.LM.value = lm;
               bm = norm(degrees(Math.asin(Math.sin(radians(lpp-Np)) * Math.sin(radians(i)))));
                                                                     // Ecliptic latitude of Moon
               document.form.BM.value = bm;
               e = (23+26/60+21.45/3600) - (46.815/3600)*T - (0.0006/3600)*T*T + (0.00181/3600)*T*T*T;
                                                                     // Obliquity of the ecliptic
               ra = invtan(Math.sin(radians(lm))*Math.cos(radians(e)) - Math.tan(radians(bm))*Math.sin(radians(e)),Math.cos(radians(lm)));
                                                                     // R.A. of Moon
               ra /= 15;                                             // Convert from degrees to hours
               rah = trunc(ra);
               ram = trunc(frac(ra) * 60);
               ras = trunc(frac(frac(ra) * 60) * 60);
               document.form.RAH.value = rah;
               document.form.RAM.value = ram;
               document.form.RAS.value = ras;
               dec = norm(degrees(Math.asin(Math.sin(radians(bm))*Math.cos(radians(e)) + Math.cos(radians(bm))*Math.sin(radians(e))*Math.sin(radians(lm)))));
                                                                     // Dec. of Moon
               if (dec > 90)
                  dec -= 360;
               decd = trunc(dec);
               decm = trunc(frac(dec) * 60);
               decs = trunc(frac(frac(dec) * 60) * 60);
               document.form.DECD.value = decd;
               document.form.DECM.value = decm;
               document.form.DECS.value = decs;
               nak(ra,dec,e);
            }
         }

         // Perform Nakshatram calculation based on Moon's position
         function nak(ra,dec,e)
         {
            AngleBetEqAndEcliptic = e;
            FPAShift = 22.3488;
           StarsMalayalam = ["Ashwini (Aswathi)","Bharani","Krithika (Karthikai)","Rohini","Mrigasira (Magayiriyam)",
                          "Thiruvathira (Athira)","Punarvasu (Punartham)","Pushya (Pooyam)","Aslesha (Ayilyam)","Magha (Makam)",
                          "Purva Phalguni (Pooram)","Uttara Phalguni (Uthram)","Hasta (Atham)","Chitra (Chithira)","Swati (Chothy)",
                          "Visakha (Visakam)","Anuradha (Anizham)","Jyestha (Triketta)","Moola (Moolam)","PurvaAshadha (Pooradam)",
                          "UttaraAshadha (Uthradam)","Sravana (Thiruvonam)","Dhanishta (Avittam)","Satabhisha (Chathayam)",
                          "PurvaBhadra (Pururuttathy)","UttaraBhadra (Uthrattathy)","Revathi"];
		   
		   
		   
		   

                        StarsEnglish = ["Sheratan (Beta Arietis)",
                            "41 Arietis",
                            "Pleiades (Nu Tauri)",
                            "Aldebaran (Alpha Tauri)",
                            "Heka",
                            "Betelgeuse (Alpha Orionis)",
                            "Pollux (Beta Geminorum)",
                            "Asellus Australis (Delta Cancri)",
                            "Acubens (Alpha Cancri)",
                            "Regulus (Alpha Leonis)",
                            "Zosma (Delta Leonis)",
                            "Denebola (Beta Leonis)",
                            "Algorab (Delta Corvi)",
                            "Spica (Alpha Virginis)",
                            "Arcturus (Alpha Bootis)",
                            "Iota Librae",
                            "Delta Scorpii",
                            "Antares (Alpha Scorpii)",
                            "Shaula (Lambda Scorpii)",
                            "Kaus Media (Delta Sagittarii)",
                            "Nunki (Sigma Sagittarii)",
                            "Altair (Alpha Aquilae)",
                            "Alpha Delphini",
                            "Lambda Aquarii",
                            "Markab (Alpha Pegasi)",
                            "Alpheratz (Alpha Andromedae)",
                            "Zeta Piscium"];

            RA = radians(ra * 360/24);

            D = radians(dec);

            E = radians(AngleBetEqAndEcliptic);
            A = radians(360/27);
            R = Math.sin(D) * Math.sin(E) + Math.cos(D) * Math.cos(E) * Math.sin(RA);
            R = R / (Math.cos(D) * Math.cos(RA));

            if (Math.cos(RA) * Math.cos(D) < 0)
               S = Math.PI;
            else
               S = 0;

            if (Math.abs(Math.cos(D) * Math.cos(RA)) > 0.00000001)
               L = Math.atan(R) + S;
            else
               L = Math.PI/2 + S;

            L = L - radians(FPAShift) + 10 * Math.PI;
            N = 270 + Math.floor(L/A);
            N = N % 27;

            document.form.NAL.value = StarsMalayalam[N];

            document.form.NALE.value = StarsEnglish[N];
         }