	var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	function dayToDays(inTime)
	{
		return (Math.floor(inTime.getTime() / (1000 * 60 * 60 * 24)));
	}

	function daysTill(dd, mm, yyyy)
	{
		now = new Date;
		then = new Date(yyyy,mm-1,dd);
		return dayToDays(then) - dayToDays(now);
	}

	function returnSuffix(dd)
	{
		d = dd % 10;
		if (d < 4 && d > 0 & (dd < 10 || dd > 20)) {
			if (d == 1 ) { return "st"; }
			if (d == 2 ) { return "nd"; }
			if (d == 3 ) { return "rd"; }
		}
		return "th";

	}

	var HebrewLeapYears = "0010010100100100101";

	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] = "Shevat";
		this[5] = "Adar";
		var incr = 0;
		if (isleap) {
			this[5] = "Adar I";
			this[6] = "Adar II";
			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 = 2020;
		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();

	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[2] = 384;
			this[3] = 385;
		} else {

			this[0] = 353;
			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 Nisan = 6;
	var Iyyar = 7;
	var Sivan = 8;

	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

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

	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(doIcon, dow)
	{
		var ThisMonthName = (IsHebrewLeapYear(this.year)) ?
		HebrewMonthsNamesLeap[this.month] :
	        HebrewMonthsNames[this.month];
		// add returnSuffix(this.day) to get 'th' on the date
		document.write(this.day + " " + ThisMonthName + ", " + this.year);
		// do icon stuff after the date
		if (0 && doIcon) {
			document.write("&nbsp;&nbsp;&nbsp;");
			if (this.day == 1 || this.day == 30)
				document.write("<img src=/images/METIcon-NewMoon.jpg hspace=6 valign=bottom>");
			if (this.day == 15 &&
			    ((IsHebrewLeapYear(this.year) &&
			      this.month == Adar2) ||
			     (!IsHebrewLeapYear(this.year) &&
                              this.month == Adar1)))
				document.write("<img src=/images/METIcon-Purim.jpg hspace=6 valign=bottom>");
			if ((this.day == 14 || this.day == 15) &&
			    ((IsHebrewLeapYear(this.year) &&
			      this.month == (Nisan + 1)) ||
			     (!IsHebrewLeapYear(this.year) &&
			      this.month == Nisan)))
				document.write("<img src=/images/METIcon-Seder.jpg hspace=6 valign=bottom>");
			if ((this.day >= 15 && this.day <= 21) &&
			    ((IsHebrewLeapYear(this.year) &&
			      this.month == (Nisan + 1)) ||
			     (!IsHebrewLeapYear(this.year) &&
			      this.month == Nisan)))
				document.write("<img src=/images/METIcon-Matzah.jpg hspace=6 valign=bottom>");
			if ((this.day >= 16 &&
			     (IsHebrewLeapYear(this.year) &&
			      this.month == (Nisan + 1)) ||
			     (!IsHebrewLeapYear(this.year) &&
			      this.month == Nisan)) ||
			    (IsHebrewLeapYear(this.year) &&
			     this.month == (Iyyar + 1)) ||
			    (!IsHebrewLeapYear(this.year) &&
			     this.month == (Iyyar)) ||
                            (this.day <= 8 &&
                             (IsHebrewLeapYear(this.year) &&
                              this.month == (Sivan + 1)) ||
                             (!IsHebrewLeapYear(this.year) &&
                              this.month == Sivan)))
				document.write("<img src=/images/METIcon-Omer.jpg hspace=6 valign=bottom>");
                        if (this.day == 9 &&
                            (IsHebrewLeapYear(this.year) &&
                             this.month == (Sivan + 1)) ||
                            (!IsHebrewLeapYear(this.year) &&
                             this.month == Sivan))
				document.write("<img src=/images/METShavuot-Omer.jpg hspace=6 valign=bottom>");
			if (this.month == Tishrei)
			{  if (this.day == 1 || this.day == 2)
			      document.write("<img src=/images/METIcon-Shofar.jpg hspace=6 valign=bottom>");
			   if (this.day >= 1 && this.day <= 10)
			      document.write("<img src=/images/METIcon-Scales.jpg hspace=6 valign=bottom>");
			   if (this.day == 10)
			      document.write("<img src=/images/METIcon-Goat.jpg hspace=6 valign=bottom>");
			   if (this.day >= 15 && this.day <= 22)
			      document.write("<img src=/images/METIcon-Lulavetrog.jpg hspace=6 valign=bottom>");
			}
			if (dow == 7)
				document.write("<img src=/images/METIcon-Shabbt.jpg hspace=6 valign=bottom>");
		}
	}

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

	var Today = new Date();

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

		// add returnSuffix(Today.getDate()) to get the 'th' in a date
		document.write(dayNames[Today.getDay()] + ", " + monthNames[Today.getMonth()] + " " + Today.getDate() + ", " + date.getFullYear() + "&nbsp;&nbsp;&nbsp;&nbsp;");
		HebrewDate.Print(1, Today.getDay());
	}
