// JavaScript Document
var HebrewLeapYears = "0010010100100100101";
function showdate(){ today = new Date();

year = today.getYear(); if (year<1000) year = year + 1900

iday  = today.getDay(); day = "SunMonTueWedThuFriSat".substr(iday*3,3)

imonth= today.getMonth(); month = "JanFebMarAprMayJunJulAugSepOctNovDec".substr(imonth*3,3)

todayStr = day + " " + today.getDate() + " " +  month + ", " + year

return todayStr

     }

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

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

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

}


// print Hebrew date
function HebPrint()
{
	var ThisMonthName = (IsHebrewLeapYear(this.year)) ?
	HebrewMonthsNamesLeap[this.month] :
	HebrewMonthsNames[this.month];
	document.write("" + this.day + " " + ThisMonthName + ", " + this.year);
}


// the following functions are for printing the date in Hebrew
function HebPrintHebrewString()
{
	if (this.length == 1)
		document.write('<img src="ssquote.gif">');
	for (var i = this.length-1 ; i >= 0 ; i--) {
		var s = ""; // ot sofit
		if (i == this.length-1 && i > 0 &&
			 (this[i] == 20 || this[i] == 40 ||
			  this[i] == 50 || this[i] == 80 || this[i] == 90)) {
			s = "s";
		}
		document.write('<img src="s' + this[i] + s + '.gif">');
		if (i > 0 && i == this.length - 1)
			document.write('<img src="sdquote.gif">');
	}
}



// constructor function for Hebrew string object
function IntegerToHebrewString(num)
{
	var len = 0;
	while (num > 0) {
		var incr = 100;
		var i;

		if (num == 15 || num == 16) {
			this[len++] = 9;
			this[len++] = num - 9;
			break;
		}

		for (i = 400 ; i > num ; i -= incr) { // tav to aleph
			if (i == incr)
				incr = incr / 10;
		}

		this[len++] = i;
		num -= i;
	}

	this.length = len;

	// print method
	this.Print = HebPrintHebrewString;
}


// print date in Hebrew
function HebPrintHebrew()
{
	var HebYear = new IntegerToHebrewString(this.year - 5000);
	HebYear.Print();
	document.write('<img src="sspace.gif">');

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


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

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

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

// end of HebrewDate object definition

var Today = new Date();

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