<!--

////////////////////////////////////////////////////////
// Copyright 2003, Timothy James Forsythe, all rights reserved.
// Permission granted to use, copy, modify, and distribute so long as
// the above copyright and this permission statement are retained in all
// copies.  THERE IS NO WARRANTY - USE AT YOUR OWN RISK.
//
// based on the algorithms of John Walker
// http://www.fourmilab.com/documents/calendar/
//
// A bewildering variety of calendars have been and continue to be
// used in the Indian subcontinent.  In 1957 the Indian
// government's Calendar Reform Committee adopted the National
// Calendar of India for civil purposes and, in addition,
// defined guidelines to standardise computation of the
// religious calendar, which is based on astronomical
// observations.  The civil calendar is used throughout India
// today for administrative purposes, but a variety of
// religious calendars remain in use.  
//
// The National Calendar of India is composed of 12 months.
// The first month, Caitra, is 30 days in normal
// and 31 days in leap years.  This is followed by five
// consecutive 31 day months, then six 30 day months.  Leap
// years in the Indian calendar occur in the same years as
// as in the Gregorian calendar; the two calendars thus
// have identical accuracy and remain synchronised.
//
// Years in the Indian calendar are counted from the start of
// the Saka Era, the equinox of March 22nd of year 79 in the
// Gregorian calendar, designated day 1 of month
// Caitra of year 1 in the Saka Era.  The calendar was
// officially adopted on 1 Caitra, 1879 Saka Era, or
// March 22nd, 1957 Gregorian.  Since year 1 of the Indian
// calendar differs from year 1 of the Gregorian, to
// determine whether a year in the Indian calendar is a leap
// year, add 78 to the year of the Saka era then
// apply the Gregorian calendar rule to the sum.

function IndianCivilDate()
{
  this.weekdayStr = "";
  this.monthStr   = "";
  this.day        = "";
  this.month      = "";
  this.year       = "";
}

function IndianCivilToJDN(year, month, day)
{
    var Caitra, gyear, leap, start, jdn, m;

    gyear = year + 78;
    leap = isGregorianLeapYear(gyear);     
    start = GregorianToJDN(gyear, 3, leap ? 21 : 22);
    Caitra = leap ? 31 : 30;

    if (month == 1) 
    {
        jdn = start + (day - 1);
    } 
    else 
    {
        jdn = start + Caitra;
        m = month - 2;
        m = Math.min(m, 5);
        jdn += m * 31;
        if (month >= 8) 
        {
            m = month - 7;
            jdn += m * 30;
        }
        jdn += day - 1;
    }

    jdn += 0.5; // calculate Chronological Julian Day

    return jdn;
}

function JDNToIndianCivil(jdn)
{
    var INDIAN_CIVIL_WEEKDAYS = new Array("Ravivara", "Somavara", "Mangalavara", "Budhavara", "Brahaspativara", "Sukravara", "Sanivara");
    var INDIAN_CIVIL_MONTHS = new Array("Caitra", "Vaisakha", "Jyaistha", "Asadha", "Sravana", "Bhadra", "Asvina", "Kartika", "Agrahayana", "Pausa", "Magha", "Phalguna");

    var date = new IndianCivilDate();

    var Caitra, Saka, greg, greg0, leap, start, year, yday, mday, weekday, day, month;

    Saka = 79 - 1;                    // Offset in years from Saka era to Gregorian epoch
    start = 80;                       // Day offset between Saka and Gregorian
    
    weekday = dow(jdn);
    jdn += 0.5; // calculate Chronological Julian Day
    
    jdn = Math.floor(jdn) + 0.5;
    greg = JDNToGregorian(jdn);       // Gregorian date for Julian day
    leap = isGregorianLeapYear(greg.year);   // Is this a leap year?
    year = greg.year - Saka;            // Tentative year in Saka era
    greg0 = GregorianToJDN(greg.year, 1, 1); // jdn at start of Gregorian year
    yday = jdn - greg0;                // Day number (0 based) in Gregorian year
    Caitra = leap ? 31 : 30;          // Days in Caitra this year
	
    if (yday < start) 
    {
        //  Day is at the end of the preceding Saka year
        year--;
        yday += Caitra + (31 * 5) + (30 * 3) + 10 + start;
    }

    yday -= start;
    if (yday < Caitra) 
    {
        month = 1;
        day = yday + 1;
    } 
    else 
    {
        mday = yday - Caitra;
        if (mday < (31 * 5)) 
        {
            month = Math.floor(mday / 31) + 2;
            day = (mday % 31) + 1;
        } 
        else 
        {
            mday -= 31 * 5;
            month = Math.floor(mday / 30) + 7;
            day = (mday % 30) + 1;
        }
    }

    date.year        = year;
    date.month       = month;
    date.day         = day;
    date.monthStr    = INDIAN_CIVIL_MONTHS[month-1];
    date.weekdayStr  = INDIAN_CIVIL_WEEKDAYS[weekday];

    return date;
}

//-->