
/* ------------------------------------------------------------------------------
 * This Script was found on another website and modified by Yehoshua ben Avraham
 * on May 7, 2003 • 5 Iyar 5763
 * Several unnecessary functions have been deleted and others added and modified
 * to display the Secular and Hebrew dates as well as the Hebrew day of the week
 * (with pronunciation) according to Hebrew custom. This is a "self-contained"
 * script that needs only to be "called" in order to display these dates. The
 * minimal html code necessary to display these dates is shown below and can be  
 * placed almost anywhere in a document.
 *
 *    <script language="javascript" src="Home_Page_Hebrew-Secular_Date.js"
 *    type="text/javascript"></script>
 *
 * ------------------------------------------------------------------------------
 */


// ======== Beginning of code for displaying secular date and Hebrew day of the week =========

ShortSecularDay = new Array( "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" );
LongSecularDay = new Array( "Sunday", "Monday", "Tuesday",
   "Wednesday", "Thursday", "Friday", "Saturday" );
//day = new Array( "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Rest" );
//hebday = new Array( "Yom Reeshon", "Yom Shay´nee", "Yom Shlee´shee",
//   "Yom Revee´ee", "Yom Kha´mee´shee", "Yom Ha´shee´shee", "SHABBAT" );
month = new Array( "January ", "February ", "March ", "April ", "May ", "June ",
   "July ", "August ", "September ", "October ", "November ", "December " );

today = new Date();
date = today.getDate();
year = today.getYear();

if (year <= 2000 )
  year += 1900;

DayNumber = today.getDay();


// Create short and long secular date formats
shortSecularDate = ShortSecularDay[DayNumber] + ", " + month[today.getMonth()] + date + ", " + year + "<br>";
longSecularDate = LongSecularDay[DayNumber] + ", " + month[today.getMonth()] + date + ", " + year + "<br>";

// If longSecularDate format is too long, switch to shortSecularDate format.
if ( longSecularDate.length > 30 )
  document.write( shortSecularDate )  // Secular date with short weekday format
else
  document.write( longSecularDate )   // Secular date with long weekday format


// Create a dividing line (makhitzah) between secular and Hebrew dates
//Makhitzah = "";
//for ( i = 1; i <= 7; i++ ) {
//  if ( i == DayNumber + 1 )
//    Makhitzah += "<font color='red'>" + "—</font>"  // Color the dash red.
//  else
//    Makhitzah += "—"    // Leave the dash the selected color.
//
//  if ( i != 7 )    
//    Makhitzah += " ";
//}
//Makhitzah += "<br>";
//document.write( Makhitzah );


if (year < 2000) { year = year + 1900; }   // Y2K Fix, Isaac Powell  http://onyx.idbsu.edu/~ipowell

//if ( DayNumber == 6 )  {
//   dayOfWeek = "<font color='red'><b>" + day[DayNumber] + "</b></font><br>"
//}
//else { dayOfWeek = "<font color='red'><b>" + day[DayNumber] + " day" + "</font>" + " of the week" + "</b><br>" }
//
//hebDayOfWeek = "<font color='red'><b>" + hebday[DayNumber] + "</b></font></br>"
//
//document.write( dayOfWeek );
//document.write( hebDayOfWeek );

// ========= End of code for displaying secular date and Hebrew day of the week ==========



function makeArray() {
   this[0] = makeArray.arguments.length;
   for (i = 0; i < makeArray.arguments.length; i = i + 1) {
      this[i+1] = makeArray.arguments[i];
   }
}

var hebMonth = new makeArray(
   'Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul', 'Tishrei', 'Kheshvan',
   'Kislev', 'Tevet', 'Shevat', 'Adar', 'Adar I', 'Adar II');

var civMonth = new makeArray(
   'January', 'February', 'March', 'April', 'May', 'June',
   'July', 'August', 'September', 'October', 'November', 'December');

// var weekDay = new makeArray(
//    'Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Shabbat');


function Gauss(year) {
   var a,b,c;
   var m;
   var Mar;   // "day in March" on which Pesach falls (return value)

   a = Math.floor((12 * year + 17) % 19);
   b = Math.floor(year % 4);
   m = 32.044093161144 + 1.5542417966212 * a +  b / 4.0 - 0.0031777940220923 * year;
   if (m < 0)
      m -= 1;
   Mar = Math.floor(m);
   if (m < 0)
      m++;
   m -= Mar;

   c = Math.floor((Mar + 3 * year + 5 * b + 5) % 7);
   if(c == 0 && a > 11 && m >= 0.89772376543210 )
      Mar++;
   else if(c == 1 && a > 6 && m >= 0.63287037037037)
      Mar += 2;
   else if(c == 2 || c == 4 || c == 6)
      Mar++;

   Mar += Math.floor((year - 3760) / 100) - Math.floor((year - 3760) / 400) - 2;
   return Mar;
}


function leap(y) {
   return ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0));
}


function civMonthLength(month, year) {
   if(month == 2)
      return 28 + leap(year);
   else if(month == 4 || month == 6 || month == 9 || month == 11)
      return 30;
   else
      return 31;
}


function civ2heb(day, month, year) {
   var d = day;
   var m = month;
   var y = year;
   var hy;
   var pesach;
   var anchor;
   var adarType;

   m -= 2;
   if (m <= 0) { // Jan or Feb
      m += 12;
      y -= 1;
   }

   d += Math.floor(7 * m / 12 + 30 * (m - 1));    // day in March
   hy = y + 3760;     // get Hebrew year
   pesach = Gauss(hy);
   if (d <= pesach - 15) {    // before 1 Nisan
      anchor = pesach;
      d += 365;
      if(leap(y))
      d++;
      y -= 1;
      hy -= 1;
      pesach = Gauss(hy);
   }
   else
      anchor = Gauss(hy + 1);

   d -= pesach - 15;
   anchor -= pesach - 12;
   y++;
   if(leap(y))
      anchor++;

   for(m = 0; m < 11; m++) {
      var days;
      if(m == 7 && anchor % 30 == 2)
         days = 30; // Kheshvan
      else if(m == 8 && anchor % 30 == 0)
         days = 29; // Kislev
      else
         days = 30 - m % 2;
      if(d <= days)
         break;
      d -= days;
   }

   adarType = 0;         // plain old Adar
   if (m == 11 && anchor >= 30) {
      if (d > 30) {
         adarType = 2;   // Adar 2
         d -= 30;
      }
      else
         adarType = 1;   // Adar 1
   }

   if(m >= 6)            // Tishrei or after?
      hy++;              // then bump up year

   if(m == 11)           // Adar?
      m += adarType;     // adjust for Adars

   return (d + ' ' + m + ' ' + hy);
}


function DOW(day,month,year) {
   var a = Math.floor((14 - month)/12);
   var y = year - a;
   var m = month + 12*a - 2;
   var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
   return d + 1;
}


function NthDOW(nth,weekday,month,year) {
   if (nth > 0)
      return (nth - 1) * 7 + 1 + (7 + weekday - DOW((nth - 1) * 7 + 1, month, year)) % 7;
   var days = civMonthLength(month, year);
   return days - (DOW(days, month, year) - weekday + 7) % 7;
}


display_date()

function display_date() {
   var day, month, year;
   var today = new Date;
   var yesterday = new Date(today.getTime() - 86400000);

   td = today.getDate();
   tm = today.getMonth() + 1;
   ty = today.getYear();
   if(ty < 1900)
      ty += 1900;    // if date from Netscape, then add 1900

   var hebDate = civ2heb(td, tm, ty);
   var hmS = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
   var hDay = eval(hebDate.substring(0, hebDate.indexOf(' ')));
   var hMonth = eval(hmS.substring(0, hmS.indexOf(' ')));
   var hYear = hmS.substring(hmS.indexOf(' ')+1, hmS.length);

   // Display Hebrew date
   document.write( hDay + ' ' + hebMonth[hMonth+1] + ' ' + hYear + '</font>' );
}


