// Convert date to Julian Day Number.  From http://www.basicguru.com/zijlema/javascript.html
function JulianDayNumber(yr, mt, dy)
{
  if (mt < 3) {            // January & February to be considered as  
    mt = mt + 12;           // 13th & 14th month
    yr = yr - 1;            // of previous year
   } 
   
  var years = Math.floor((4712 + yr) * 365.25);              // days of full years since -4712  
  var leaps = Math.floor(yr / 100) - Math.floor(yr / 400);   // calculate invalid leap days
  var mnths = Math.floor(((mt - 1) * 30.6) + 0.2);           // days of full months

  return (years - leaps + mnths + dy);                        
}

// end hide



//Converting a JDN to a Year, Month, and Day inside an array.  A comment from the web page
// this was taken from:
// "It does'nt make much sense to me, but it works! So take it for granted." [sic]
//TOOGAM added a return statement
var YMD = new Array(2)                             // 3 elements: 0, 1 and 2
function JulToYMD(jul)
{	
  jul = jul + 68569
  var temp = Math.floor((4 * jul) / 146097)
  jul = jul - Math.floor((146097 * temp + 3) / 4)
  var tmpYear = Math.floor((4000 * (jul + 1)) / 1461001)
  jul = jul - Math.floor((1461 * tmpYear) / 4) + 31
  var tmpMonth = Math.floor((80 * jul) / 2447)  
  YMD[0] = 100 * (temp - 49) + tmpYear + Math.floor(tmpMonth / 11)  // year = element 0
  YMD[1] = tmpMonth + 2 - (12 * Math.floor(tmpMonth / 11))          // month = element 1
  YMD[2] = jul - Math.floor((2447 * tmpMonth) / 80)                 // day = element 2
  return YMD;
}
// end hide -->





//Easter
//Description from web page this came from:
//The function to calculate Easter (Easter Sunday) for a given year deserves the same comment as the
//JulToYMD function: frankly I don't know how it works, but it does its job flawlessly. The only
//thing I'm responsible for, is to return the result as a Julian Day Number, making it very easy
//to calculate Easter-related holidays as well. Good Friday, two days before Easter, can now be
//obtained as Easter minus 2, Easter Monday as Easter plus 1. And so forth.
//Here goes the code: 
function Easter(yr)
{
  var temp1 = Math.floor((8 * Math.floor(yr / 100) + 13) / 25)
  var temp2 = Math.floor(yr / 100) - Math.floor(yr / 400) + 4
  var days  = (19 * (yr % 19) + (11 + temp2 - temp1) % 30) % 30
  var refdays = days
  if (refdays == 29) {
    days = 28
    }
  if (refdays == 28 && (yr % 19) > 10) {
    days = 27
    }
  var factor = ( 2 * (yr % 4) + 4 * (yr % 7) + 6 * days + temp2 % 7 ) % 7 
  days = days + factor + 21
  
  return Julian(yr, 3, 1) + days;   // March One + number of found days
}









//To convert from JDN to current date

function MonthName(month)
{
  var mthname=new Array(13);  // 0 thru 12 (record 0 = empty)
    mthname[1]="January";
    mthname[2]="February";
    mthname[3]="March";
    mthname[4]="April";
    mthname[5]="May";
    mthname[6]="June";
    mthname[7]="July";
    mthname[8]="August";
    mthname[9]="September";
    mthname[10]="October";
    mthname[11]="November";
    mthname[12]="December";
  return mthname[month];
}  

//Now we are ready for the LongDate function itself (including
//error trapping for calendar dates before November 25, 4714 b.C. and
//later than December 31, 9999): 
//TOOGAM modified: Made it call the renamed JulianDayNumber() (was named Julian())

function LongDate(yr, mt, dy)
{ 
  if (JulianDayNumber(yr, mt, dy) < 1) {
    alert("Date before 11/25/4714 b.C. not allowed")
    yr = -4713                              // display lbound date instead
    mt = 11
    dy = 25
   } 

  if (yr > 9999) {
    alert("Date after 12/31/9999 not allowed")
    yr = 9999                               // display ubound date instead
    mt = 12
    dy = 31
   }

  if (yr < 1) {
    var annm = yr - 1                       // make computational year a true BC-year
    var year = Math.abs(annm) + " b.C."     // skip minus-sign and add "b.C."
   }
  else {
    var year = yr    
   }  

  return MonthName(mt) + " " + dy + ", " + year;  
}
// end hide -->


//Created by TOOGAM based on above
//similar to x=LongDate(yr,mt,dy);x=x.replace(/ /g,"&"+"nbsp;")
// except that the .replace of strings appears to be JavaScript 1.3 only
function LongDate_use_nbsp(yr, mt, dy)
{ 
  if (JulianDayNumber(yr, mt, dy) < 1) {
    alert("Date before 11/25/4714 b.C. not allowed")
    yr = -4713                              // display lbound date instead
    mt = 11
    dy = 25
   } 

  if (yr > 9999) {
    alert("Date after 12/31/9999 not allowed")
    yr = 9999                               // display ubound date instead
    mt = 12
    dy = 31
   }

  if (yr < 1) {
    var annm = yr - 1                       // make computational year a true BC-year
    var year = Math.abs(annm) + "&nbsp;b.C."     // skip minus-sign and add "b.C."
   }
  else {
    var year = yr    
   }  

  return MonthName(mt) + "&nbsp;" + dy + ",&nbsp;" + year;  
}






//This returns a value in the range 0 - 6, where 0 = Sunday ....... 6 = Saturday. Simply assign a dayname to these numbers and there you are! But the limitation of this coding is evident: it only retrieves the day of the week for 'today', i.e. the system date of the visitor's computer. However, using the Julian function as explained above the day of the week (or dayname) for any date can be fetched. This is the way it works: 
// Get the JDN for the given date
// Use the MOD-operator (percent-sign) to find what's left after a repetetive division by 7
// Add 1 to change the order into: 1 = Monday ....... 7 = Sunday 
//Here goes the code for both functions, DayOfWeek(yr, mt, dy) and DayName(dow): 
//TOOGAM modified: Made it call the renamed JulianDayNumber() (was called Julian())
function DayOfWeek(yr, mt, dy)

   // NOTE: I add 400 to 'yr' in order to avoid negative modulo operations in BC-years
   // this is no problem, because every 400 years a calendar pattern repeats itself
{
  return JulianDayNumber(yr + 400, mt, dy) % 7 + 1;
}

function DayName(dow)
{
  var dayname=new Array(8);   // 0 thru 7  (record 0 = empty)
    dayname[1]="Monday";
    dayname[2]="Tuesday";
    dayname[3]="Wednesday";
    dayname[4]="Thursday";
    dayname[5]="Friday";
    dayname[6]="Saturday";
    dayname[7]="Sunday";  
  return dayname[dow];
}
// end hide -->


