<!--

////////////////////////////////////////////////////////
// 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/
//
// The Bahá'í calendar is a solar calendar organised as a
// hierarchy of cycles, each of length 19, commemorating the 19
// year period between the 1844 proclamation of the Báb in
// Shiraz and the revelation by Bahá'u'lláh in 1863.  Days are named in a
// cycle of 19 names.  Nineteen of these cycles of 19 days,
// usually called "months" even though they have nothing
// whatsoever to do with the Moon, make up a year, with a
// period between the 18th and 19th months referred to as
// Ayyám-i-Há not considered part of any month; this
// period is four days in normal years and five days in leap
// years.  The rule for leap years is identical to that of the
// Gregorian calendar, so the Bahá'í calendar shares its
// accuracy and remains synchronised.  The same cycle of 19
// names is used for days and months.
//
// The year begins at the equinox, March 21, the Feast of
// Naw-Rúz; days begin at sunset.  Years have their own
// cycle of 19 names, called the Váhid.  Successive cycles of
// 19 years are numbered, with cycle 1 commencing on March 21, 1844,
// the year in which the Báb announced his prophecy.
// Cycles, in turn, are assembled into Kull-I-Shay
// super-cycles of 361 (19˛) years.  The first Kull-I-Shay
// will not end until Gregorian calendar year 2205.  A week of seven
// days is superimposed on the calendar, with the week considered to
// begin on Saturday.  Confusingly, three of the names of weekdays
// are identical to names in the 19 name cycles for days and months.


var BAHAI_EPOCH = 2394646 + 0.5;

function BahaiDate()
{
  this.weekdayStr  = "";
  this.dayStr      = "";
  this.monthStr    = "";
  this.yearStr     = "";
  this.day         = "";
  this.month       = "";
  this.year        = "";
  this.vahid       = "";
  this.kull_i_shay = "";
}

function BahaiToJDN(major, cycle, year, month, day)
{
    var gy = (361 * (major - 1)) + (19 * (cycle - 1)) + (year - 1) + JDNToGregorian(BAHAI_EPOCH).year;
    var jdn = GregorianToJDN(gy, 3, 20) + (19 * (month - 1)) + ((month != 20) ? 0 : (isGregorianLeapYear(gy + 1) ? -14 : -15))  + day;

    jdn += 0.5; // calculate Chronological Julian Day
   
    return jdn;
}

function JDNToBahai(jdn)
{
    var BAHAI_YEARS = new Array("Alif", "Bá'", "Ab", "Dál", "Báb", "Váv", "Abad", "Jád", "Bahá", "Hubb", "Bahháj", "Javáb", "Ahad", "Vahháb", "Vidád", "Badí'", "Bahí", "Abhá", "Vahíd");
    var BAHAI_MONTHS = new Array("Bahá", "Jalál", "Jamál", "`Azamat", "Núr", "Rahmat", "Kalimát", "Kamál", "Asmá'", "`Izzat", "Mashíyyat", "`Ilm", "Qudrat", "Qawl", "Masáil", "Sharaf", "Sultán", "Mulk", "Ayyám-i-Há", "`Alá'");
    var BAHAI_DAYS = new Array("Bahá", "Jalál", "Jamál", "`Azamat", "Núr", "Rahmat", "Kalimát", "Kamál", "Asmá'", "`Izzat", "Mashíyyat", "`Ilm", "Qudrat", "Qawl", "Masáil", "Sharaf", "Sultán", "Mulk", "`Alá'");
    var BAHAI_WEEKDAYS = new Array("Jamál", "Kamál", "Fidál", "Idál", "Istijlál", "Istiqlál", "Jalál");
    
    var date = new BahaiDate();
    
    var major, cycle, year, month, day, gy, bstarty, bys, days, bld, weekday;

    weekday = dow(jdn);
    jdn += 1.5; // calculate Chronological Julian Day
    
    jdn = Math.floor(jdn) + 0.5;
    gy = JDNToGregorian(jdn).year;
    bstarty = JDNToGregorian(BAHAI_EPOCH).year;
    bys = gy - (bstarty + (((GregorianToJDN(gy, 1, 1) <= jdn) && (jdn <= GregorianToJDN(gy, 3, 20))) ? 1 : 0));
    major = Math.floor(bys / 361) + 1;
    cycle = Math.floor(mod(bys,361) / 19) + 1;
    year = mod(bys,19) + 1;
    days = jdn - BahaiToJDN(major, cycle, year, 1, 1);
    bld = BahaiToJDN(major, cycle, year, 20, 1);
    month = (jdn >= bld) ? 20 : (Math.floor(days / 19) + 1);
    day = (jdn + 1) - BahaiToJDN(major, cycle, year, month, 1);

    date.kull_i_shay = major;
    date.vahid       = cycle;
    date.year        = year;
    date.month       = month;
    date.day         = day;
    date.yearStr     = BAHAI_YEARS[year-1];
    date.monthStr    = BAHAI_MONTHS[month-1];
    date.dayStr      = BAHAI_DAYS[day-1];
    date.weekdayStr  = BAHAI_WEEKDAYS[weekday];
    
    return date;
}

//-->