<!--
////////////////////////////////////////////////////////
// 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 Consant Rite Smith
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Algorithm copyright 15.18.1.5, Consant Rite Smith, all rights reserved.
//
// Alpo Balognia's Calendar A
//
// Each year of Calendar A is divided into four seasons: Spring, Summer, Autumn, and Winter.  The year ends is a Feast period which can last anywhere from 1 to 5 days.  Each year begins with 1 Spring. Each season is made up of 91 days divided into 13 weeks of 7 days each.  The names for the days in Alpobalognian are remarkably, once translated, the same as those used in Gregorian and Julian calendars, so for convienience, The English version of the names are used.  All seasons start on Monday.  The Feast days at the end of the year are used to periodically realign the calendar with the seasons.  There will be at least one Feast day every year.  The number of Feast days are based on the average length of the time between Spring Equinoxs which the Alpobalognians calculated to be roughly 365.2423 and 1/30000 days.  This calculation matches most modern calculations almost exactly when allowing for slowing of the tropical year over time.  There will be totals of 2 Feast days every 5 years, 3 Feast days every 25 years, 4 Feast days every 450 years, and 5 Feast days every 9000 years.  After this time the cycle renews.  The Alpobalognia did not name the current cycle, so in deference to their great empire, I have given the first, or current, cycle is name of The Alpobalognian Cycle.  The five Feast days are named as follows:  The Feast of the Dead (Ar), The Feast of the Mother (Om), The Feast of the Earth (Po), The Feast of the Father (Og), and The Feast of the Living (Ri).
// Calendar A is made up of the following periods of time: cycles, collapses, generations, stages, years, and days.  Seasons, and weekdays are also used to divide up the year with catchy names.  The cycle is the period of time in which the calendar comes full circle and is completely realigned.  Each cycle is made up of exactly 3287181 days, 9000 years, 1800 stages, 360 generations, or 20 collapses.  A collapse is exactly 164359 days, 450 years, 90 stages, or 18 generations.  A generation is exactly 9131 days, 25 years, or 5 stages.  A stage is exactly 1826 days, or 5 years.  Each year consists of 365, 366, 367, 368, or 369 days, depending on how many Feast days the year includes.  Each season is exactly 91 days or 13 weeks.  Each week is exactly 7 weekdays.
//
// There are 3 standard notations for writing dates.
// Long Cycle:
// FORMAT: cycleNumber.collapseNumber.generationNumber.stageNumber.yearNumber[.dayNumber]
// EXAMPLES 0.20.18.5.5.369, or 1.20.18.5.5
//
// Short Cycle: (The short cycle method assumes the Alpobalognian cycle)
// FORMAT: weekNumber Season (or Feast) collapseNumber.generationNumber.stageNumber.yearNumber
// EXAMPLES 1 Spring 1.1.1.1, The Feast of the Dead 1.1.1.1, or The Feast of Ar 1.1.1.1
//
// Informal: (The informal method assumes the Alpobalognian cycle and the current collapse.  It should never be used for long term record keeping as it repeats every 450 years.  It is ideal for informal dating however.)
// FORMAT: weekNumber Season (or Feast) generationNumber.stageNumber.yearNumber
// EXAMPLES: 1 Spring 1.1.1, or The Feast of the Dead 1.1.1, or just plain Ar 1.1.1
//
// The last day of the previous cycle (0.20.18.5.5.369) is equivalent to:
// Gregorian Calendar: 21 March 4727 B.C.E or Julian Day Number: -4996
//
// The first day of the Alpobalognian cycle (1 Spring 1.1.1.1) is equivalent to:
// Gregorian Calendar: 22 March 4727 B.C.E or Julian Day Number: -4995
//
// The last day of the Alpobalognian cycle (1.20.18.5.5.369) is equivalent to:
// Gregorian Calendar: 20 March 4274 or Julian Day Number: 3282185
//
// Calendar A
//
//  M  T  W  T  F  S  S
//  1  2  3  4  5  6  7
//  8  9 10 11 12 13 14
// 15 16 17 18 19 20 21
// 22 23 24 25 26 27 28
// 29 30 31 32 33 34 35
// 36 37 38 39 40 41 42
// 43 44 45 46 47 48 49
// 50 51 52 53 54 55 56
// 57 58 59 60 61 62 63
// 64 65 66 67 68 69 70
// 71 72 73 74 75 76 77
// 78 79 80 81 82 83 84
// 85 86 87 88 89 90 91
//       Ar Om Po Og Ri
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var CA_JDN_CORRELATION  = -4995;

var CA_DAYS_PER_WEEK            =  7;
var CA_WEEKS_PER_SEASON         = 13;
var CA_SEASONS_PER_YEAR         =  4;
var CA_YEARS_PER_STAGE          =  5;
var CA_STAGES_PER_GENERATION    =  5;
var CA_GENERATIONS_PER_COLLAPSE = 18;
var CA_COLLAPSES_PER_CYCLE      = 20;

var CA_FEAST_DAY               =  1;

var CA_FEAST_SEASON            = CA_SEASONS_PER_YEAR + 1; 

var CA_DAYS_PER_SEASON     = (CA_DAYS_PER_WEEK       * CA_WEEKS_PER_SEASON);
var CA_DAYS_PER_YEAR       = (CA_DAYS_PER_SEASON     * CA_SEASONS_PER_YEAR)        + CA_FEAST_DAY; // 365
var CA_DAYS_PER_STAGE      = (CA_DAYS_PER_YEAR       * CA_YEARS_PER_STAGE)         + CA_FEAST_DAY; // 1826
var CA_DAYS_PER_GENERATION = (CA_DAYS_PER_STAGE      * CA_STAGES_PER_GENERATION)   + CA_FEAST_DAY; // 9131
var CA_DAYS_PER_COLLAPSE   = (CA_DAYS_PER_GENERATION * CA_GENERATIONS_PER_COLLAPSE) + CA_FEAST_DAY; // 164359
var CA_DAYS_PER_CYCLE      = (CA_DAYS_PER_COLLAPSE   * CA_COLLAPSES_PER_CYCLE)      + CA_FEAST_DAY; // 3287181

function CADate(cycle,collapse,generation,stage,year,doy,season,day,lcstr,scstr,dstr,sstr,istr)
{
  this.cycle      = cycle;	
  this.collapse   = collapse;	
  this.generation = generation;
  this.stage      = stage;
  this.year       = year;
  this.doy        = doy;
  
  this.season = season;
  this.day = day;
  
  this.lcstr = lcstr;
  this.scstr = scstr;
  this.sstr  = sstr;
  this.dstr  = dstr;
}


function CalendarAToJDN(day,s,y,d,g,e,c)
{
  var jdn = CA_JDN_CORRELATION;
  
  jdn += (day-1);
  jdn += (s-1) * CA_DAYS_PER_SEASON;
  jdn += (y-1) * CA_DAYS_PER_YEAR;
  jdn += (d-1) * CA_DAYS_PER_STAGE;
  jdn += (g-1) * CA_DAYS_PER_GENERATION;
  jdn += (e-1) * CA_DAYS_PER_COLLAPSE;
  jdn += (c-1) * CA_DAYS_PER_CYCLE;

  return jdn;
}


function JDNToCalendarA(jdn)
{
  var DAYS     = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Feast of the Dead", "Feast of the Mother", "Feast of the Earth", "Feast of the Father", "Feast of the Living");
  var SEASONS  = new Array("Spring", "Summer", "Autumn", "Winter", "Feast");

  var date = new CADate(1,1,1,1,1,1,1,1,0,0,0,0,0,0);
  var temp;
 
  jdn = trunc(jdn);
  
  jdn -= CA_JDN_CORRELATION;

  while (jdn < 0)
  {
    jdn = jdn + CA_DAYS_PER_CYCLE;
    date.cycle = date.cycle - 1; 
  }

  while (jdn >= CA_DAYS_PER_CYCLE)
  {
    jdn = jdn - CA_DAYS_PER_CYCLE;
    date.cycle = date.cycle + 1; 
  }

  temp = Math.floor(jdn/CA_DAYS_PER_COLLAPSE);
  date.collapse = date.collapse + temp;
  jdn -= (temp * CA_DAYS_PER_COLLAPSE);
  
  if (date.collapse > CA_COLLAPSES_PER_CYCLE)
  {
    date.collapse = date.collapse - 1;
    jdn += CA_DAYS_PER_COLLAPSE;
  }
  
  temp = Math.floor(jdn/CA_DAYS_PER_GENERATION);
  date.generation = date.generation + temp;
  jdn -= (temp * CA_DAYS_PER_GENERATION);
  
  if (date.generation > CA_GENERATIONS_PER_COLLAPSE)
  {
    date.generation = date.generation - 1;
    jdn += CA_DAYS_PER_GENERATION;
  }
  
  temp = Math.floor(jdn/CA_DAYS_PER_STAGE);
  date.stage = date.stage + temp;
  jdn -= (temp * CA_DAYS_PER_STAGE);
  
  if (date.stage > CA_STAGES_PER_GENERATION)
  {
    date.stage = date.stage - 1;
    jdn += CA_DAYS_PER_STAGE;
  }

  temp = Math.floor(jdn/CA_DAYS_PER_YEAR);
  date.year = date.year + temp;
  jdn -= (temp * CA_DAYS_PER_YEAR);

  if (date.year > CA_YEARS_PER_STAGE)
  {
    date.year = date.year - 1;
    jdn += CA_DAYS_PER_YEAR;
  }

  date.doy = date.doy + jdn;
  
  temp = Math.floor(jdn/CA_DAYS_PER_SEASON);
  date.season = date.season + temp;
  jdn -= (temp * CA_DAYS_PER_SEASON);
  
  date.day = date.day + jdn;

  date.dstr = DAYS[((date.day-1) % CA_DAYS_PER_WEEK) + (CA_DAYS_PER_WEEK * Math.floor(date.season/CA_FEAST_SEASON))];
  date.sstr = SEASONS[date.season-1];

  if (date.doy >= CA_DAYS_PER_YEAR)
  {
    date.day    = -1;
    date.season = -1;
  }

  date.lcstr = date.cycle + ".";
  date.lcstr = date.lcstr + date.collapse + ".";
  date.lcstr = date.lcstr + date.generation + ".";
  date.lcstr = date.lcstr + date.stage + ".";
  date.lcstr = date.lcstr + date.year + ".";
  date.lcstr = date.lcstr + date.doy;
    
  date.scstr = date.dstr;
  date.istr = date.dstr;
  
  if (date.doy < CA_DAYS_PER_YEAR)
  {
    date.scstr = date.scstr + ", " + date.day + " " + date.sstr + " ";
    date.istr = date.istr + ", " + date.day + " " + date.sstr + " ";
  }  

  date.scstr = date.scstr + date.collapse + ".";
  date.scstr = date.scstr + date.generation + ".";
  date.scstr = date.scstr + date.stage + ".";
  date.scstr = date.scstr + date.year;

  date.istr  = date.istr + date.generation + ".";
  date.istr  = date.istr + date.stage + ".";
  date.istr  = date.istr + date.year;

  return date;
}

-->

