/*
 * Today's Hebrew Date
 * (c) Copyright 1996: Right to Left Software
 *
 * Reference: Understanding The Jewish Calendar / Rabbi Nathan Bushwick
 */

browserName = navigator.appName;
version = "";
     browserVer = parseInt(navigator.appVersion);
             if (browserName == "Netscape" && browserVer <= 4) version = "n3";

             if (version == "n3")
             {
document.write("דפדנך מיושן מדי. לא ניתן לצפות בתאריך העברי.");
             }
else
             {

var HebrewLeapYears = "0010010100100100101";
var Version = 1.00;
function IsHebrewLeapYear(year)
{
	var y = (year - 1) % 19;
	return parseInt(HebrewLeapYears.charAt(y));
}
function InitHebrewMonthsNames(isleap)
{
	this.length = (isleap) ? 13 : 12;
	this[0] = "תשרי";
	this[1] = "חשוון";
	this[2] = "כסלב";
	this[3] = "תבט";
	this[4] = "שבט";
	this[5] = "אדר";
	var incr = 0;
	if (isleap) {
		this[5] = "אדר א'";
		this[6] = "אדר ב'";
		incr = 1;
	}
	this[6+incr] = "ניסן";
	this[7+incr] = "אייר";
	this[8+incr] = "סיוון";
	this[9+incr] = "תמוז";
	this[10+incr] = "אב";
	this[11+incr] = "אלול";
}
var HebrewMonthsNames = new InitHebrewMonthsNames(false);
var HebrewMonthsNamesLeap = new InitHebrewMonthsNames(true);

function InitGlobalTable()
{
	this.length = 2002;
this[1980] = "11S0";
this[1981] = "29S2";
this[1982] = "18S3";
this[1983] = "08S3";
this[1984] = "27S2";
this[1985] = "16S0";
this[1986] = "04O3";
this[1987] = "24S2";
this[1988] = "12S0";
this[1989] = "30S3";
this[1990] = "20S2";
this[1991] = "09S3";
this[1992] = "28S0";
this[1993] = "16S3";
this[1994] = "06S2";
this[1995] = "25S3";
this[1996] = "14S0";
this[1997] = "02O2";
this[1998] = "21S3";
this[1999] = "11S3";
this[2000] = "30S0";
this[2001] = "18S2";
this[2002] = "07S3";
this[2003] = "27S3";
this[2004] = "16S0";
this[2005] = "04O2";
this[2006] = "23S3";
this[2007] = "13S0";
this[2008] = "30S2";
this[2009] = "19S3";
this[2010] = "09S3";
this[2011] = "29S2";
this[2012] = "17S0";
this[2013] = "05S3";
this[2014] = "25S2";
this[2015] = "14S3";
this[2016] = "03O0";
this[2017] = "21S2";
this[2018] = "10S3";
this[2019] = "30S3";
this[2020] = "19S0";
}

var GlobalTable = new InitGlobalTable();
/*
 * Convert Julian year to Hebrew year
 */
function JulianYearToHebrew(/* int */ year)
{
	return year + 3760;
}
function HebrewToJulianYear(/* int */ year)
{
	return year - 3760;
}
/*
 * Year length in days
 * indexed by the year type, given in GlobalTable
 */
function InitYearLength(isleap)
{
	this.length = 4;
	if (isleap) {
		this[0] = 383;
		//this[1] = ; error
		this[2] = 384;
		this[3] = 385;
	} else {
		this[0] = 353;
		//this[1] = ; error
		this[2] = 354;
		this[3] = 355;
	}
}
var HebrewYearLengthLeap = new InitYearLength(true);
var HebrewYearLength = new InitYearLength(false);
var Tishrei = 0;
var Cheshvan = 1;
var Kislev = 2;
var Adar1 = 5;
var Adar2 = 6;
var HebMonths30 =     "1010101010101";
var HebMonths30Leap = "10101011010101";
function DaysInHebrewFixedMonth(month, year)
{
	var incr;
	if (IsHebrewLeapYear(year))
		incr = parseInt(HebMonths30Leap.charAt(month));
	else
		incr = parseInt(HebMonths30.charAt(month));
	return 29 + incr; // 29 or 30
}


function DaysInCheshvan(hebyear)
{
	var ytype = GlobalTable[HebrewToJulianYear(hebyear)-1].charAt(3);
	if (ytype == 3)
		return 30;
	else
		return 29;
}

function DaysInKislev(hebyear)
{
	var ytype = GlobalTable[HebrewToJulianYear(hebyear)-1].charAt(3);
	if (ytype == 0)
		return 29;
	else
		return 30;
}

function DaysInHebrewMonth(month, year)
{
	if (month == Cheshvan)
		return DaysInCheshvan(year);
	else if (month == Kislev)
		return DaysInKislev(year);
	else
		return DaysInHebrewFixedMonth(month, year);
}





function DaysInHebrewYear(/* int */ year)
{
	var ytype = GlobalTable[year].charAt(3);
	if (IsHebrewLeapYear(year)) {
		return HebrewYearLengthLeap[ytype];
	} else {
		return HebrewYearLength[ytype];
	}
}

var MsecPerDay = 1000*3600*24;  // miliseconds per day

/*
 * Add days to a Julian date
 */
function	AddDays(
	/* date */ date,
	/* int */  days)
{
	var time = date.getTime();
	time += days*MsecPerDay;
	date.setTime(time);

	return date;
}


/*
 * given a Julian date of a holiday (e.g. rosh hashana),
 * returns the date that the same holiday occurred in the year 'toyear'
 */
function SameHolidayInJulianYear(
	/* date */ rosh_shana,
	/* int */  toyear)
{
	var fromyear = rosh_shana.getFullYear() + 1900;
	var incr = 1; // increment fromyear

	if (fromyear == toyear)
		return rosh_shana;
	else if (fromyear > toyear)
		incr = -1; // decrement from_year

	var days;
	for (days = 0 ; fromyear != toyear ; fromyear += incr) {
		days += DaysInHebrewYear(JulianYearToHebrew(fromyear));
	}
	AddDays(rosh_shana, incr*days);
	return rosh_shana;
}
// returns date when Rosh Hashana occurs on this year
function GetRoshHashana(/* int */ cvyear)
{
	var month;
	var day;
	var yearstr = GlobalTable[cvyear];
	var m = yearstr.charAt(2);
	month = 8; // usually September
	if (m == 'O')
		month = 9; // sometimes October
	day = parseInt(yearstr.charAt(0))*10 + parseInt(yearstr.charAt(1));
	return new Date(cvyear, month, day);
}

/*
 * The following functions are methods for
 * HebrewDate object
 */

// is last month of the year?
function IsHebLastMonth()
{
	if (this.month == 11 && !IsHebrewLeapYear(this.year))
		return true;

	if (this.month == 12 && IsHebrewLeapYear(this.year))
		return true;

	return false;
}

// go to next Hebrew month
function HebNextMonth()
{
	if (this.IsLastMonth()) {
		this.year++;
		this.month = 0;
	} else
		this.month++;

	this.DaysInThisMonth = DaysInHebrewMonth(this.month, this.year);
}

// next Hebrew day
function HebNextDay()
{
	this.day++;
	if (this.day > this.DaysInThisMonth) {
		this.day = 1;
		this.NextMonth();
	}
}

// add days to Hebrew date
function HebAddDays(days)
{
	while (days > 0) {
		if (this.day == 1 && days >= this.DaysInThisMonth) {
			days -= this.DaysInThisMonth;
			this.NextMonth();
		} else {
			days--;
			this.NextDay();
		}
	}
//document.write("Current date: " + this.day + ", " + this.month + ", " + this.year + "<br>");
}

// print Hebrew date
function HebPrint()
{
	var ThisMonthName = (IsHebrewLeapYear(this.year)) ?
										HebrewMonthsNamesLeap[this.month] :

											HebrewMonthsNames[this.month];
// לוח מועדים
  if (ThisMonthName == "תשרי") {
    if (this.day ==  1) { this.holiday = "א' דראש השנה"; }
    if (this.day ==  2) { this.holiday = "ב' דראש השנה"; }
    if (this.day ==  3) { this.holiday = "צום גדליה"; }
    if (this.day == 9) { this.holiday = "ערב יום הכיפורים"; }
    if (this.day == 10) { this.holiday = "יום הכיפורים"; }
    if (this.day == 15) { this.holiday = "סוכות"; }
    if (this.day == 16) { this.holiday = "א' דחול המועד סוכות"; }
    if (this.day == 17) { this.holiday = "ב' דחול המועד סוכות"; }
    if (this.day == 18) { this.holiday = "ג' דחול המועד סוכות"; }
    if (this.day == 19) { this.holiday = "ד' דחול המועד סוכות"; }
    if (this.day == 20) { this.holiday = "ה' דחול המועד סוכות"; }
    if (this.day == 21) { this.holiday = "ו' דחול המועד סוכות - הושענא רבא"; }
    if (this.day == 22) { this.holiday = "שמחת תורה"; }
  }

  if (ThisMonthName == "כסלו") { 
    if (this.day == 25) { this.holiday = "נר ראשון של חנוכה"; }
    if (this.day == 26) { this.holiday = "נר שני של חנוכה"; }
    if (this.day == 27) { this.holiday = "נר שלישי של חנוכה"; }
    if (this.day == 28) { this.holiday = "נר רביעי של חנוכה"; }
    if (this.day == 29) { this.holiday = "נר חמישי של חנוכה"; }
  }

  if (ThisMonthName == "טבת") { 
    if (this.day ==  1) { this.holiday = "נר שישי של חנוכה"; }
    if (this.day ==  2) { this.holiday = "נר שביעי של חנוכה"; }
    if (this.day ==  3) { this.holiday = "נר שמיני של חנוכה"; }
    if (this.day == 10) { this.holiday = "צום עשרה בטבת"; }
  }

  if (ThisMonthName == "שבט") {
    if (this.day == 15) { this.holiday = "חג האילנות"; }
  }

  // ימים מיוחדים בחודש אדר בהנחה שאין חודש אדר נוסף (שנה מעוברת)
  if (ThisMonthName == "אדר") {
    if (this.day == 13) { this.holiday = "תענית אסתר"; }
    if (this.day == 14) { this.holiday = "פורים"; }
    if (this.day == 15) { this.holiday = "שושן פורים"; }
  } 

  // ימים מיוחדים בחודש אדר ב' במידה והוא קיים
  if (ThisMonthName == "אדר ב'") {
    if (this.day == 13) { this.holiday = "תענית אסתר"; }
    if (this.day == 14) { this.holiday = "פורים"; }
    if (this.day == 15) { this.holiday = "שושן פורים"; }
  } 

  if (ThisMonthName == "ניסן") {
    if (this.day == 14) { this.holiday = "ותענית בכורות וערב הפסח"; }
    if (this.day == 15) { this.holiday = "חג ראשון של פסח"; }
    if (this.day == 16) { this.holiday = "א' חול המועד פסח"; }
    if (this.day == 17) { this.holiday = "ב' חול המועד פסח"; }
    if (this.day == 18) { this.holiday = "ג' חול המועד פסח"; }
    if (this.day == 19) { this.holiday = "ד' חול המועד פסח"; }
    if (this.day == 20) { this.holiday = "ה' חול המועד פסח"; }
    if (this.day == 21) { this.holiday = "חג שני של פסח"; }
    if (this.day == 27) { this.holiday = "יום הזיכרון לשואה ולגבורה"; }
  }   

  if (ThisMonthName == "אייר") {
   if (this.day == 4) { this.holiday = "יום הזיכרון לחללי מערכות ישראל"; }
    if (this.day ==  5) { this.holiday = "יום העצמאות ה - ";
	                      this.holiday += (this.year-5708);
   						  this.holiday += " למדינת ישראל"; }
   if (this.day == 18) { this.holiday = "ל\"ג בעומר - הדלקת מדורות"; }
   if (this.day == 28) { this.holiday = "יום ירושלים"; }
  }

  if (ThisMonthName == "סיוון") {
    if (this.day ==  5) { this.holiday = "ערב שבועות"; }
    if (this.day ==  6) { this.holiday = "שבועות"; }
    if (this.day ==  7) { this.holiday = "שבועות"; }
  }

  if (ThisMonthName == "תמוז") {
    if (this.day == 17) { this.holiday = "צום י\"ז בתמוז"; }
  }

  if (ThisMonthName == "אלול") {
    if (this.day ==  29) { this.holiday = "ערב ראש השנה";}
  }



// הפכת היום במספר לעברית
if (this.day == "1") { this.day = "א'"; }
if (this.day == "2") { this.day = "ב'"; }
if (this.day == "3") { this.day = "ג'"; }
if (this.day == "4") { this.day = "ד'"; }
if (this.day == "5") { this.day = "ה'"; }
if (this.day == "6") { this.day = "ו'"; }
if (this.day == "7") { this.day = "ז'"; }
if (this.day == "8") { this.day = "ח'"; }
if (this.day == "9") { this.day = "ט'"; }
if (this.day == "10") { this.day = "י'"; }
if (this.day == "11") { this.day = "י\"א"; }
if (this.day == "12") { this.day = "י\"ב"; }
if (this.day == "13") { this.day = "י\"ג"; }
if (this.day == "14") { this.day = "י\"ד"; }
if (this.day == "15") { this.day = "ט\"ו"; }
if (this.day == "16") { this.day = "ט\"ז"; }
if (this.day == "17") { this.day = "י\"ז"; }
if (this.day == "18") { this.day = "י\"ח"; }
if (this.day == "19") { this.day = "י\"ט"; }
if (this.day == "20") { this.day = "כ'"; }
if (this.day == "21") { this.day = "כ\"א"; }
if (this.day == "22") { this.day = "כ\"ב"; }
if (this.day == "23") { this.day = "כ\"ג"; }
if (this.day == "24") { this.day = "כ\"ד"; }
if (this.day == "25") { this.day = "כ\"ה"; }
if (this.day == "26") { this.day = "כ\"ו"; }
if (this.day == "27") { this.day = "כ\"ז"; }
if (this.day == "28") { this.day = "כ\"ח"; }
if (this.day == "29") { this.day = "כ\"ט"; }
if (this.day == "30") { this.day = "ל'"; }	

// הפיכת השנה ממספרים לעברית
if (this.year == "5761") { this.year = "התשס\"א"; }
if (this.year == "5762") { this.year = "התשס\"ב"; }
if (this.year == "5763") { this.year = "התשס\"ג"; }
if (this.year == "5764") { this.year = "התשס\"ד"; }
if (this.year == "5765") { this.year = "התשס\"ה"; }
if (this.year == "5766") { this.year = "התשס\"ו"; }
if (this.year == "5767") { this.year = "התשס\"ז"; }
if (this.year == "5768") { this.year = "התשס\"ח"; }
if (this.year == "5769") { this.year = "התשס\"ט"; }
if (this.year == "5770") { this.year = "התש\"ע"; }
if (this.year == "5771") { this.year = "התשע\"א"; }
if (this.year == "5772") { this.year = "התשע\"ב"; }
if (this.year == "5773") { this.year = "התשע\"ג"; }
if (this.year == "5774") { this.year = "התשע\"ד"; }
if (this.year == "5775") { this.year = "התשע\"ה"; }
if (this.year == "5776") { this.year = "התשע\"ו"; }
if (this.year == "5777") { this.year = "התשע\"ז"; }
if (this.year == "5778") { this.year = "התשע\"ח"; }
if (this.year == "5779") { this.year = "התשע\"ט"; }
if (this.year == "5780") { this.year = "התש\"פ"; }


if (this.holiday) {

document.write(""+this.holiday+ ", " + this.day +" ב" + ThisMonthName + " " + this.year);
}

else {
document.write("" + this.day + " ב" + ThisMonthName + " " + this.year);
}
}

// constructor for HebrewDate object
function InitHebrewDate(day, month, year)
{
	// fields
	this.day = day;
	this.month = month;
	this.year = year;
	this.DaysInThisMonth = DaysInHebrewMonth(month, year);
	// methods
	this.IsLastMonth = IsHebLastMonth;
	this.NextMonth = HebNextMonth;
	this.NextDay = HebNextDay;
	this.AddDays = HebAddDays;
	this.Print = HebPrint;
}
// end of HebrewDate object definition
var Today = new Date();

var isnMonths=new Array("ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר");
var isnDays= new Array("יום ראשון","יום שני","יום שלישי","יום רביעי","יום חמישי","יום שישי","יום שבת");
todayG=new Date();

function getFullYear(d) {
    var y = d.getYear();
    if (y < 1000) {y += 1900};
    return y;
}

function ShowHebrewDate(date)
{
	//var MyRoshHashana = new Date(99, 8, 11); // sep 11, 1999
	var PrevYear = date.getFullYear() - 1;
	var PrevRoshHashana = GetRoshHashana(PrevYear);
	var DiffDays = Math.floor((date.getTime() - PrevRoshHashana.getTime()) / MsecPerDay);
	var HebrewYear = JulianYearToHebrew(PrevYear);
	var HebrewDate = new InitHebrewDate(1, 0, HebrewYear+1); // 1, Tishrei, year
	var Year = getFullYear(todayG);
	HebrewDate.AddDays(DiffDays);
   // date in English
document.write("<span dir='rtl'>");
document.write("ב&quot;ה. "+isnDays[todayG.getDay()]+" ");
        HebrewDate.Print();
document.write(" ("+todayG.getDate()+" ב"+isnMonths[todayG.getMonth()]+" "+Year+")");
//document.write(" ("+todayG.getDate()+" ב"+isnMonths[todayG.getMonth()]+" "+Year+")");
document.write("<\/span>");
}



}

