<!--

						/*
						 * Today's Hebrew Date
						 * (c) Copyright 1996: Right to Left Software
						 *
						 * Reference: Understanding The Jewish Calendar / Rabbi Nathan Bushwick
						 */

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] = "Tishrei";
	this[1] = "Cheshvan";
	this[2] = "Kislev";
	this[3] = "Tevet";
	this[4] = "Shvat";
	this[5] = "Adar";

	var incr = 0;
	if (isleap) {
		this[5] = "Adar1";
		this[6] = "Adar2";
		incr = 1;
	}

	this[6+incr] = "Nisan";
	this[7+incr] = "Iyyar";
	this[8+incr] = "Sivan";
	this[9+incr] = "Tamuz";
	this[10+incr] = "Av";
	this[11+incr] = "Elul";
}

var HebrewMonthsNames = new InitHebrewMonthsNames(false);
var HebrewMonthsNamesLeap = new InitHebrewMonthsNames(true);

function InitGlobalTable()
{
this.length = 2001;

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";

}

var GlobalTable = new InitGlobalTable();

function JulianYearToHebrew(/* int */ year)
{
	return year + 3760;
}

function HebrewToJulianYear(/* int */ year)
{
	return year - 3760;
}

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 jyear = HebrewToJulianYear(hebyear)-1;
	var ytype = GlobalTable[jyear].charAt(3);

	if (ytype == 3)
		return 30;
	else
		return 29;
}

function DaysInKislev(hebyear)
{
	var jyear = HebrewToJulianYear(hebyear)-1;
	var ytype = GlobalTable[jyear].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

function AddDays(
	/* date */ date,
	/* int */  days)
{
	var time = date.getTime();
	time += days*MsecPerDay;
	date.setTime(time);

	return date;
}


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);
}

function IsHebLastMonth()
{
	if (this.month == 11 && !IsHebrewLeapYear(this.year))
		return true;

	if (this.month == 12 && IsHebrewLeapYear(this.year))
		return true;

	return false;
}



function HebNextMonth()
{
	if (this.IsLastMonth()) {
		this.year++;
		this.month = 0;
	} else
		this.month++;


	this.DaysInThisMonth = DaysInHebrewMonth(this.month, this.year);
}


function HebNextDay()
{
	this.day++;
	if (this.day > this.DaysInThisMonth) {
		this.day = 1;
		this.NextMonth();
	}
}

function HebAddDays(days)
{
	while (days > 0) {
		if (this.day == 1 && days >= this.DaysInThisMonth) {
			days -= this.DaysInThisMonth;
			this.NextMonth();
		} else {
			days--;
			this.NextDay();
		}
	}
}

function HebPrint()
{
	var ThisMonthName = (IsHebrewLeapYear(this.year)) ?
		HebrewMonthsNamesLeap[this.month] :
		HebrewMonthsNames[this.month];

	document.write("" + this.day + " " + ThisMonthName + " " + this.year);
}

function HebPrintHebrew()
{
	var HebYear = new IntegerToHebrewString(this.year - 5000);
	HebYear.Print();
	document.write('<img src="space.gif">');

	var ThisMonthName = (IsHebrewLeapYear(this.year)) ?
										HebrewMonthsNamesLeap[this.month] :
										HebrewMonthsNames[this.month];
	ThisMonthName = ThisMonthName.toLowerCase();


	document.write('<img src="b_' + ThisMonthName + '.gif">');
	document.write('<img src="space.gif">');

	var HebDay = new IntegerToHebrewString(this.day);
	HebDay.Print();
}

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;
	this.PrintHebrew = HebPrintHebrew;
}

function ShowHebrewDate(date)
{
	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
	HebrewDate.AddDays(DiffDays);
	HebrewDate.Print();
}	

var date = new Date();
var mon = date.getMonth() + 1;

ShowHebrewDate(date);

document.write(" / " + date.getDate() + "-" + mon + "-" + date.getFullYear() + "");

// -->

