<!---
// Copyright 2005 by Jordan Lee Wagner.  All rights reserved.

// Define constants for readability:

var SUN = 0, MON = 1, TUE = 2, TUES = 2, WED = 3, THU = 4, THUR = 4, FRI = 5, SAT = 6;
		   
var JAN=0, FEB=1, MAR=2, APR=3, MAY=4, JUN=5, JUL=6, AUG=7, SEP=8, OCT=9, NOV=10, DEC=11;


//==========================================================================

// Jewish time is measured in halakim (or "parts"); 
// abbpreviated "p" to distinguish from "h" (for "hours").
// There are 18 halakim per minute, so 1p = 3+1/3 sec.

var HALAKIM_PER_HOUR = 1080;  	// = 18 p/m * 60 m/h
var HALAKIM_PER_DAY = 25920;  	// = 1080 p/h * 24 h/d
var HALAKIM_PER_WEEK = 181440;   	// = 25920 p/d * 7 d/w
/*
The absolute time of any event can be expressed without reference to the calendar, 
because time can be expressed in halakim since Creation. 
(This is how astronomical calculations are done.) 

The Jewish calendar features lunar months (i.e., months that start and end with each new moon);  
so you can approximate the date just by looking up at night.  The time of the "new moon" is when 
the moon is at its closest point to the line from the sun to the earth, called the lunar conjunction.  
This monthly astronomical event is the basis for starting the Hebrew months and years.  

The actual time between new moons varies with the seasons, but the average time between them is constant. 
Jewish calendrical calculations use the average length of the lunar cycle to calculate the start time 
of each lunar cycle.  The "molad" of a month is its calculated time of lunar conjunction based on average lunar cycles.   
This is the basis for starting the Hebrew months and years.  (However, the months and years don't always 
start on the exact day of the molad, due to a variety of "postponement rules", described later.)   

(There can be as much as 15 hours difference between a month's true lunar conjunction 
and its mean conjunction.)

(The minimum interval between a true conjunction and a visible crescent moon is 18 hours.)  
*/ 
// Jewish astronomical calculations are based on the following approximation of the average lunar cycle:  
var HALAKIM_PER_LUNAR_CYCLE = ((29 * HALAKIM_PER_DAY) + 13753);  // = 29 days + 12 hrs + 793 halakim. 
/*
Since this is about 29 1/2 days, Jewish months are either 29 or 30 days long.  
A normal Jewish year consists of 12 months that alternate between 29 and 30 days.

This falls short of the solar year.  Synchronization with the solar cycle, 
so that a given month always occurs in the same season, is achieved by adding 
an extra month in leap years.  Adding 7 months every 19 years harmonizes the
lunar and solar cycles.  The 19 year period is called a metonic cycle.  
*/
var HEBREW_MONTHS_PER_METONIC_CYCLE = 235 ;   
// 12 lunar months per regular year * 19 years per cycle + 7 leap months per 19 year cycle.  19*12+7=235. 
 
// Jewish regular years have 12 months, leap years have 13 months.  
// The pattern of regular and leap years repeats every 19 years, 
// called a metonic cycle.  There are 7 leap years in each metonic cycle.
// (So there are 235 hebrew months in each metonic cycle.)

var HALAKIM_PER_METONIC_CYCLE = (HALAKIM_PER_LUNAR_CYCLE * HEBREW_MONTHS_PER_METONIC_CYCLE); 
	     
// The leap years are years 3, 6, 8, 11, 14, 17, and 19 of each 19 year cycle. 

function isLeapYear(hyear)
{
 	var temp = hyear % 19 ;
 	var isLeap =  (temp == 3 || temp == 6 || temp == 8 || temp == 11 || temp == 14 || temp == 17 || temp == 0 );
	return isLeap;
	// Alternate method:  temp = (235*hyear-234)%19 ;  isLeap = (temp>11) ;  return isLeap;
}

var monthsPerYear = new metonicArray (12,12,13,12,12,13,12,13,12,12,13,12,12,13,12,12,13,12,13); // not used in this file

              function metonicArray(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16,m17,m18)
              {
                      this[0] = m0; this[1] = m1; this[2] = m2; this[3] = m3;
                      this[4] = m4; this[5] = m5; this[6] = m6; this[7] = m7;
                      this[8] = m8; this[9] = m9; this[10] = m10; this[11] = m11;
                      this[12] = m8; this[13] = m13; this[14] = m14; this[15] = m15;
                      this[16] = m16; this[17] = m17; this[18] = m18;
              }
	
// The names of the thirteen Hebrew months are:
 
var TISHRI=0; var HESHVAN=1; var KISLEV=2; var TEVET=3; var SHEVAT=4; 
var ADAR_I=5; 
var ADAR_II=6; var NISAN=7;  var IYAR=8;  var SIVAN=9; var TAMMUZ=10; var AV=11; var ELUL=12; 

// Adar I ("First Adar") is the leap month.  (It has 30 days.)
var DaysPerMonthInNormalNonLeapYear = new hMonthArray( 30, 29, 30, 29,  30, 0, 29, 30, 29, 30, 29, 30, 29) ;  // not used in this file
/*
In some years Kislev is shortened to 29 days (in which case the year is called "deficient"), 
and in other years Cheshvan is lengthened to 30 days (in which case the year is called "abundant").  
These adjustments are based on lunar astronomical considerations that are not fixed with respect 
to the 19 year cycle.  

Thus a Hebrew calendar year can have 6 different lengths (in days).   
(Kislev plus Cheshvan = 58, 59, or 60; and leap days = 0 or 30.)

Although the pattern of months-per-year repeats every 19 Hebrew years (and so all 19 year periods have the 
same number of months), the varying lengths of the years (in days) cause the 19 year cycles to have 
varying lengths (in days).  

All periods of 19 Hebrew years are either 6938, 6939, 6940, 6941, or 6942 days long.  
None of the 5 possible lengths are divisible by 7, so the cycles begin on different days of the week.
*/
var LONGEST_POSSIBLE_19_YEARS = 6942 ;   // Not used in this file.  
/*
Jewish days start and end at sundown, rather than midnight; so they start and end at 
different times in different locations, and they even differ from one day to the next 
in the same location.  But Jewish calendrical calculations identify the time of day 
(in hours, minutes, and halakim) based on the convention that all Hebrew dates start at 
6pm the evening before the corresponding Gregorian date.  So 0 hours = 6pm, and 18 hours = 12 noon.  

Thus, Jewish weeks start and end on Saturday evening at 6pm.  The second day of the week 
(Monday) starts Sunday evening and ends Monday evening; and so on.  The sixth day of the 
week (Friday) starts Thursday evening and ends Friday evening, which is when the seventh 
day (the Sabbath) begins.  

Jewish calendrical calculations postulate that the molad of the first lunar cycle was on Yom Sheini 
(the second day of the week) at 5 hours, and 204 halakim.  (That's 20 seconds after 11:11 pm on Sunday evening.)
This is the molad of 1 Tishrei of Year 1, which is called Molad Tohu.   
*/
var MOLAD_OF_FIRST_LUNAR_CYCLE = 31524;    // 31524 halakim = 1d + 5 hours + 204 halakim (after Saturday evening at 6pm).  
/*
Performing calendrical calculations does *not* require knowing the reason for this.  However, the reason is as follows:  
Rosh HaShanah (the Jewish New Year) is the annual anniversary of the creation of human beings.  
Adam was created at the second hour of the daytime (i.e., the 14th hour of the day) on the Friday before the first Sabbath.  
So that day is 1 Tishrei, the beginning of a year.  But Creation started several days before this, on 25 Elul.  
So this is the Rosh Hashannah of year 2, not year 1.  Creation began 25 Elul of Year 1.  
The moon was set in its course on the fourth day of Creation, which was Elul 28 of Year 1.  
1 Tishrei of Year 2 was the sixth day of Creation, when human beings were created, and 2 Tishrei 2 was the first Sabbath.  

By extrapolating 12 average lunar cycles before 1 Tishrei of year 2, we get to the molad of the start of Year 1 
and of the first metonic cycle.  By assigning to this moment the number of halakim since the Saturday night preceding it, 
all the calendrical calculations are made very simple. 

All calendars need a zero point from which to count years and days.  This is called the begining of the epoch.  
In the Jewish calendar, that point of absolute zero halakim occurs well before Creation!  Absolute zero, for purposes of 
calendrical calculation, begins at 6pm on the Saturday evening before the begining of the year that ended with the 5th day 
of Creation (i.e., the day before Adams was created).  The molad of Year 1 is called Molad Tohu (The Molad of the Chaos).

For any event, we'll call the elapsed time since absolute zero the "total halakim" of the event.  
*/

function getDayOfWeekAndTimeOfDay(totalHalakim)    				// Not used in the file.  
{
// For any moment in history (expressed in total halakim), this function returns a 2-item array 
// containing the day of the week (as 0 to 6), and the time of the day (in halakim since 6pm).
	var temp = totalHalakim % HALAKIM_PER_WEEK ;		// Halakim since this week began.   
	var returnArray = new Array(2) ; 
	returnArray[0] = Math.floor(temp / HALAKIM_PER_DAY);  	// Day of week.
	returnArray[1] = temp % HALAKIM_PER_DAY ; 			// Time of day.
	return returnArray ;
}	

// gets the starting molad of Tishrei for any Hebrew year (which is *not* necessarily that year's 1 Tshrei, i.e. Rosh Hashanah). 
function getMoladOfYear(Hyr) 
{
	temp = (235*Hyr - 234) /19 ;
 	monthsSinceFirstLunarCycleBegan = Math.floor(temp);  
	// Based on the Gauss Pesach Formula, published 1802.  
	// C.f., "The Calculated Confusion of Calendars", by Wolfgang Alexander Shocken, p.35.
	// C.f., "Calendric Calculations", by Nachum Dershowitz & Edward Reingold, p.90-92.
	
	// Alternate method: 	
	// halakim = HALAKIM_PER_METONIC_CYCLE * Math.floor(Hyr/19) ; 
	// months = 0 ;  
	// for ( var i=1; i<=(Hyr % 19); i++ ) { 
	//		months += monthsPerYear[i] ; 
	// }
	 //moladOfYear = halakim + months*HALAKIM_PER_LUNAR_CYCLE + MOLAD_OF_FIRST_LUNAR_CYCLE ;  
	// return moladOfYear;   // in total halakim
	  
	halakimSinceFirstLunarCycle = monthsSinceFirstLunarCycleBegan * HALAKIM_PER_LUNAR_CYCLE ;  	 
	moladOfYear = halakimSinceFirstLunarCycle + MOLAD_OF_FIRST_LUNAR_CYCLE ;  
	// = molad of Tishrei for this Hyr
	return moladOfYear ;  // in total halakim
}	


// Calendars must be constructed out of complete days.  If we simply started every Hebrew year 
// on the weekday of the molad of Tishrei, Hebrew non-leap-years would have 354 or 355 days, 
// Hebrew leap-years would have 383 or 384 days, and any year could start on any day of the week.
// This would produce 28 possible types of years (one for each combination of length and start day).  
// However, Tishrei 1 is often postponed one or two days, based on four postponement rules.  
// These reduce the number of types of years to 14.  The postponement rules are called the "Dechiyyot".

function getRoshHashanah(theoreticalStartTimeOfYear, Hyr)
{
// Accepts the time of the astronomical molad of Tishrei 1 (in total halakim), 
// applies the postponement rules for Tishrei 1, and returns a 2-item array
// containing: the Day of the Week of Tishrei 1; and the total halakim of the 
// beginning (i.e., at 6 pm) of the postponed Rosh Hashanah (Jewish New Year; Tishrei 1). 
	var Hyear = Hyr ;
		
	var temp = theoreticalStartTimeOfYear % HALAKIM_PER_WEEK ;  	// halakim in the partial week	
	DayOfWeek = Math.floor(temp / HALAKIM_PER_DAY);
	TimeOfDay = temp % HALAKIM_PER_DAY ; 

	// First Postponement Rule: Dechiyyah Molad Zaqen (the "old molad" rule.)
	// Rule:  Postpone Tishrei 1 if its molad is after midday. 
	// (Ensures that no month of the coming year will begin before the calculated molad of that month.)    
      var NOON = (18 * HALAKIM_PER_HOUR);   // Jewish calendrical days start at 6 PM.  
	
 	// Second Postponement Rule: Dechiyyah Lo ADU Rosh
	// Prevents Yom Kippur from adjoining Shabbat (which would be a hardship and would delay burials for too long), 
	// and prevents Hoshanah Rabbah (when willow branches must be beaten) from being on Shabbat,
	// and reduces 28 potential year-types to 16.  (which are further reduced to 14 by other rules.) 
	// Rule: Rosh HaShannah cannot be SUN, WED, or FRI.  Delay it a day if necessary.  (Note that if the second rule 
	// applies as a result of the first rule having been applied, then 1 Tishrei can end up postponed by two days.)    
	
	// Third Postponement Rule:  Dechiyyah GaTaRaD
	// Eliminates all of the 356-day years created by Dechiyyah Lo ADU Rosh.  
	// (If the molad is on Tuesday, and is at or later than 9hrs and 204 halakim (which is 3:11 AM plus 20 seconds), 
	// and this is not a leap year, then the following Tishrei would be postponed from Saturday to Monday, 
	// making this year 356-days long.)  Rule:  In this case, move Rosh Hashanah from Tuesday to Thursday.   
      var AM_3_11_20 = ((9 * HALAKIM_PER_HOUR) + 204);
	
	// Fourth Postponement Rule:  Dechiyyah BeTU'TeKaPoT  
	// Eliminates all the 382-day years resulting from Dechiyyah Lo ADU Rosh. 
	// (If the molad of a year is on Monday, and is at or after 15 hours and 589 parts (which is 9:32 AM and 43 seconds),
	// and the previous year was a leap year, then 1 Tishrei of that leap year had been postponed from Tuesday to Thursday, 
	// so that leap year will be only 382 days unless we delay the current year.)  Rule: In this case, postpone from Monday to Tuesday.  
      var AM_9_32_43 = ((15 * HALAKIM_PER_HOUR) + 589);

	var RoshHashanah = theoreticalStartTimeOfYear ;  	  
	if (	(TimeOfDay > NOON) ||
		(  (DayOfWeek==TUE)&&(TimeOfDay>=AM_3_11_20)&&( !isLeapYear(Hyear)  )  )  ||
		(  (DayOfWeek==MON)&&(TimeOfDay>=AM_9_32_43)&&( isLeapYear(Hyear-1) )  )   )		
	{
		DayOfWeek = (DayOfWeek+1)%7; 		
		RoshHashanah+=HALAKIM_PER_DAY ;		
	}
	// Apply Dechiyyah Lo ADU Rosh to see if there's another delay: 
	if ( (DayOfWeek==SUN)||(DayOfWeek==WED)||(DayOfWeek==FRI) ) {
		DayOfWeek = (DayOfWeek+1);
		RoshHashanah+=HALAKIM_PER_DAY ;
	}
	var tempArray = new Array(2) ; 
	tempArray[0]=DayOfWeek ; tempArray[1]=RoshHashanah-TimeOfDay ;
	return tempArray;
}	





// returns a 2-item array: 	[0] = the halakim (since Creation) of the 6pm that begins the input Hebrew date
//					[1] = the number of days (of this Hebrew year) so far, including the input date. 
HALAKIM = 0 ; DAYNUMBER = 1 ; // indices into the Array

function getHalakimAndDayNumberOfHdate(Hyr,Hmon,HdateOfMon)
{
	outputArray = new Array(2) ; 
	
	molad = getMoladOfYear(Hyr); //document.write("got molad<BR>"); 
	thisRH = new Array(2) ;  thisRH = getRoshHashanah(molad,Hyr) ;  //document.write("got thisRH<BR>"); 	
	outputArray[HALAKIM] = thisRH[1] ; 	// the total halakim of the 6pm that starts Tishrei 1 of this year
		
	// Must find how many days were in the completed months of this year:  	
	nextMolad = getMoladOfYear(Hyr+1) //; document.write("got nextMolad<BR>"); 
	nextRH = new Array(2) ;  nextRH = getRoshHashanah(nextMolad,Hyr+1) ; //document.write("got nextRH<BR>"); 
	var lengthOfYear = (nextRH[1] - thisRH[1]) / HALAKIM_PER_DAY ;  // in days
//document.write("got lengthOfYear<BR>"); 	
	cheshvanLength=29; kislevLength=30; leapAdarLength=0; // in normal years
	if ( (lengthOfYear==353)||(lengthOfYear==383) ) { kislevLength=29; }  		// in "deficient" years
	if ( (lengthOfYear==355)||(lengthOfYear==385) ) { cheshvanLength=30; }  	// in "abundant" years
	if (lengthOfYear>375) { leapAdarLength=30 } 						// in leap years
	// var DaysPerMonth = new hMonthArray( 30, cheshvanLength, kislevLength, 29,  30, leapAdarLength, 29, 30, 29, 30, 29, 30, 29) ;

	var CK = cheshvanLength+kislevLength ; // can be 58, 59, or 60.   //document.write("got CK<BR>"); 
	var cumulativeDaysBeforeHebrewMonth = new hMonthArray(0, 30, 30+cheshvanLength, 30+CK, 30+CK+29, 30+CK+29+30, 30+CK+29+30+leapAdarLength, 30+CK+29+30+leapAdarLength+29,  30+CK+29+30+leapAdarLength+29+30, 30+CK+29+30+leapAdarLength+29+30+29,  30+CK+29+30+leapAdarLength+29+30+29+30, 30+CK+29+30+leapAdarLength+29+30+29+30+29, 30+CK+29+30+leapAdarLength+29+30+29+30+29+30 ) ;

	outputArray[DAYNUMBER] = cumulativeDaysBeforeHebrewMonth[Hmon] + HdateOfMon  			// includes the input date
	outputArray[HALAKIM]  = outputArray[HALAKIM] + ( HALAKIM_PER_DAY * (outputArray[1]-1) ) ;   	// excludes the input date

//document.write("output=" + output); 	
	return outputArray; 
}




function getDayOfWeekOfHdate(Hyr,Hmon,Hdate) 
{
 	halakim =  getHalakimAndDayNumberOfHdate(Hyr,Hmon,Hdate)[HALAKIM] ;
	dow = Math.floor(halakim/HALAKIM_PER_DAY) % 7 ;  
	return dow;
}



/*
NOTE:  getHdateOfHalakim() returns the true Hebrew Date 
(evening to evening rather than midnight to midnight) of the input event.  
The returned hDdate & hTimeOfDay reflect that all Hebrew calendar dates start and end at 6pm.  
If you want the Hebrew date normally associated with a Gregorian date 
(i.e., sharing the common period of daylight), make sure the input event is before 6pm.  
*/ 
function getHdateOfHalakim(halakimOfAnyEventSinceCreation) 
{
	var inputTime = halakimOfAnyEventSinceCreation ; 		// the time of the event in halakim

	// This function returns: 
	var hTimeOfDay = inputTime % HALAKIM_PER_DAY ; 		// in halakim since 6pm the previpus evening, which is the start of this Hebrew date
	var cycleIndex = 0 ; 		// where in the 19-year cycle this event's Hebrew year falls (as 0 to 18); for indexing arrays
	var cyclesElapsed = 0 ; 	// the number of completed metonic cycles before this event
	var hYear = 0 ; 	var hMonth = 0 ;  
	var lengthOfYear = 0 ; 	var moladOfThisYear = 0 ; 		// Molad of Tishrei
	//  also returns the Weekday of Tishrei 1, and the date within the Hebrew month.
	

	cyclesElapsed = Math.floor( (inputTime - MOLAD_OF_FIRST_LUNAR_CYCLE) /HALAKIM_PER_METONIC_CYCLE ) ; 	
	// This is just a lower bound.  It will be adjusted, if needed, by the loop below. 
	hYear = (cyclesElapsed * 19) + 1  ;  // This too is only a lower bound.  [+1 is because year 20 is the first year of a cycle (index=0)] 
	
  	moladOfThisYear = cyclesElapsed*HALAKIM_PER_METONIC_CYCLE + MOLAD_OF_FIRST_LUNAR_CYCLE ; // molad of the cycle
	// Alternate method:    moladOfThisYear = getMoladOfYear(hYear) ; 

	var moladOfNextYear = getMoladOfYear(hYear+1) ; 
	while (inputTime >= moladOfNextYear) {
		hYear ++ ;
		moladOfThisYear = moladOfNextYear ;  moladOfNextYear = getMoladOfYear(hYear+1) ;  
		if (cycleIndex==18) {
			cyclesElapsed++ ;  cycleIndex = 0;
		} else {
			cycleIndex ++ ;  
		}
		
	}     
/* We have adjusted Hyear so that we are now certain that inputTime is between the molad of Hyear and the molad of Hyear+1 */
	// document.write(hYear) // diagnostic 
	
	
	var rh = new Array(2); rh = getRoshHashanah(moladOfThisYear, hYear) ; 
	// rh[0] is the weekday of Tishrei 1.  rh[1] = the starting (i.e. at 6pm) halakim (since Creation) of Rosh Hashanah.
//document.write("DayOfWeek returned  by getRH() for this year = " + weekdayName[rh[0]] + "<BR><BR>" ) ;	
  
	if (inputTime<rh[1]) {	
		// The inputTime was after the astronomical Molad of the year, but Rosh Hashanah was
		// postponed beyond the inputTime.  So it must be Elul 28 or 29 of the previous year. 
		hYear-- ;		
		moladOfNextYear = moladOfThisYear; 	moladOfThisYear = getMoladOfYear(hYear);
		if (cycleIndex==0) {
			cyclesElapsed-- ;  cycleIndex = 18;
		} else {
			cycleIndex -- ;  
		}
		rh = getRoshHashanah(moladOfThisYear, hYear) ; 
	}
	var nextRoshHashanah = new Array(2); nextRoshHashanah = getRoshHashanah(moladOfNextYear, hYear+1) ; 
//document.write("DayOfWeek returned  by getRH() for next year = " + weekdayName[nextRoshHashanah[0]] + "<BR><BR>") ;	
	//var lengthOfYear = Math.floor(nextRoshHashanah[1]/HALAKIM_PER_DAY) - Math.floor(rh[1]/HALAKIM_PER_DAY) ;  // in days
	// Since TimeOfDay was subtracted in the getRoshHashanah() function, rh[1] is always a multiple of HALAKIM_PER_DAY.  Math.floor() is not needed.
	var lengthOfYear = (nextRoshHashanah[1] - rh[1]) / HALAKIM_PER_DAY ;  // in days

	cheshvanLength=29; kislevLength=30; leapAdarLength=0; // in normal years
	if ( (lengthOfYear==353)||(lengthOfYear==383) ) { kislevLength=29; }  		// in "deficient" years
	if ( (lengthOfYear==355)||(lengthOfYear==385) ) { cheshvanLength=30; }  	// in "abundant" years
	if (lengthOfYear>375) { leapAdarLength=30 } 						// in leap years
	var DaysPerMonth = new hMonthArray( 30, cheshvanLength, kislevLength, 29,  30, leapAdarLength, 29, 30, 29, 30, 29, 30, 29) ;
	
	DaysIntoYear = (inputTime-rh[1])/HALAKIM_PER_DAY ;  	// rh[1] is the START (6pm) of Tishrei 1 (with no extra parts).  
	// DaysIntoYear is how many 24-hour periods (including a fractional part) lie beyond the 6pm that begins Tishrei 1.  
	// hMonth is initially 0  
	while (DaysIntoYear>=DaysPerMonth[hMonth]) {
		DaysIntoYear = DaysIntoYear - DaysPerMonth[hMonth] ; 
		hMonth++ ;
	}
	
	/*  Added by JLW on August 31, 2008. We must fix the following bug: 
	If the input event is the early morning hours of a day like September 30, 2008 -- 
	when the input date is very early am of Rosh Hashanah itself -- 
	so that the halakim of the preceding evening's 6pm is the halakim of Rosh Hashanah of the new year (Hyear+1) --
	even though the molad of the new year (Hyear+1) is after the halakim of the input event ---  
	then the result of the above code is to produces a nonsensical Hebrew date for this input event (Rosh Hashanah) equal to: 
	the 1st day of hMonth=13 (while Elul is month 12!!!), and no advance of Hyear.
	Therefore: to fix this bug:  */
	if (hMonth == 13) { hMonth=0; hYear++; }

		
	// The remaining DaysIntoYear is how many 24-hour periods (including a fractional part) 
	// lie beyond the 6pm that begins the first of the month.  
	DaysIntoYear = Math.ceil(DaysIntoYear) ; // This is hebrewDateOfMonth.  (dates are 1 to 30, not 0 to 29.) 
	
	var hDate = new hDateArray(hYear, hMonth, DaysIntoYear, hTimeOfDay, cyclesElapsed, cycleIndex, moladOfThisYear, lengthOfYear, rh[0] ) ;	
	return hDate ;  
}

             function hDateArray(m0,m1,m2,m3,m4,m5,m6,m7,m8)
              {
                      this[0] = m0; this[1] = m1; this[2] = m2; this[3] = m3;
                      this[4] = m4; this[5] = m5; this[6] = m6; this[7] = m7;
                      this[8] = m8; 
              }

//------------------------------------------------------------------------------


var WEEKDAY = 0 , LENGTH = 1 ;  // values of the index into the typeOfYear array.
  
function getTypeOfYear(Hyr)
{
/* 
This function returns an array whose two items are:  the Weekday of Tishrei 1 (Rosh Hashanah), 
and the length of the year in days.  This is sufficient to define which of the 14 types of year it is.
The entire Jewish calendar of holidays and Torah readings can be set up from just this.    
This function is not used elsewhere in this file, but the scripts in LuachData.js use it extensively.
*/
	var thisRH = new Array(2);  var nextRH= new Array(2) ;
	thisRH = getRoshHashanah(getMoladOfYear(Hyr),Hyr) ; // [day of week, theoretical start time plus postponements]
	nextRH = getRoshHashanah(getMoladOfYear(Hyr+1), Hyr+1) ;
	lengthOfYear = (nextRH[1]/HALAKIM_PER_DAY) - (thisRH[1]/HALAKIM_PER_DAY) ;  
	thisRH[1]=lengthOfYear ; 
	// thisRH[0] still equals the day of week.  
	return thisRH;
}


//===============================================================
/*
Every Gregorian Calendar year divisible by 4 is leap, 
except if it is divisible by 100 but not divisible by 400.
So there are *usually* 1461 days in 4 Gregorian years; 1461 = 365 * 4 + 1.
But there are *always* the same number of days in 400 years: 
*/
var DAYS_PER_400_GREG_YEARS = 146097 ; 	// = 1461 * 100 - 3 
// -3 because we skip the leapdays in three out of four years ending in 00.
/* 
We want to relate Gregorian dates and Hebrew dates.  We know how to count time in each, 
so all we need is to know the dates in both calendars at some common point in time:  

1 Tishrei 1 corresponds to the Gregorian date Monday, September 7, -3760.
  
For convenience, a Gregorian year 0 is presumed to have existed between 
Gregorian years -1 and +1.  It spans the Hebrew years 3760 and 3761.
(Without a gregorian year zero, the corresponding year would be -3761.) 

1 Tishrei 1 started at 6:00 pm the evening before, so this molad falls on 
Sunday evening September 6, -3760; at 11:00 pm plus 204 halakim = 680 seconds;
which is:  11:11:20 pm, or 23:11:20.  

We want to pick this reference time that will relate the two calendars, 
but javascript Date() objects sometimes have problems with negative years, 
so we can't pick 1 Tishrei 1 (the Molad of the Chaos) as our reference time.  
*/

var DAYS_PER_6000_GREG_YEARS = 146097 * 15  ;  // = DAYS_PER_400_GREG_YEARS * 15
 
// Let's use 6000 gregorian years *after* the molad of 1 Tishrei 1 as our reference time.
// -3760 years + 6000 years = 2240 years
 

function getHalakimOfGdate(gregorianDateObject) 
	{
		inputDate = new Date() ; inputDate.setTime( gregorianDateObject.getTime() ) ;
		var referenceDate = new Date(2240,8,6,23,11,20);  //  6000 gregorian years after the Molad of the Chaos
		
		diff = (referenceDate.getTime()-inputDate.getTime()) ; // Milliseconds until reference event
		
		// convert milliseconds to halakim: 
		diff = 18 * diff/60000;  			// Halakim until reference event.  (= 18p/m * diff in minutes)
				
		halakimOfGdate = MOLAD_OF_FIRST_LUNAR_CYCLE + (DAYS_PER_6000_GREG_YEARS * HALAKIM_PER_DAY) - diff ;	 
		return halakimOfGdate;
	}
	
function getGdateOfHalakim(halakimSinceCreation)
	{
		var inputEvent = halakimSinceCreation ; 
		var referenceEvent = MOLAD_OF_FIRST_LUNAR_CYCLE + (DAYS_PER_4400_GREG_YEARS * HALAKIM_PER_DAY) ;
		diff = referenceEvent-inputEvent ;  // Halakim until the reference event
		// convert to milliseconds:
		diff = 60000 * diff/18 ;  // Milliseconds until reference event 
		var outputDate = new Date(2160,8,6,23,11,20); // the reference date
		outputDate.setTime(outputDate.getTime()-diff) ;
		return outputDate ;
	}

//===============================================================
// DISPLAY FUNCTIONS: 

var hMonthName = new hMonthArray("Tishrei","Heshvan","Kislev","Tevet","Shevat","Adar I","Adar II","Nisan","Iyar","Sivan","Tammuz","Av","Elul");

             function hMonthArray(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12)
              {
                      this[0] = m0; this[1] = m1; this[2] = m2; this[3] = m3;
                      this[4] = m4; this[5] = m5; this[6] = m6; this[7] = m7;
                      this[8] = m8; this[9] = m9; this[10] = m10; this[11] = m11;
                      this[12] = m12; 
              }

function displayHdate(gregorianDateObject) 
	{
		displayDate = gregorianDateObject ; 
		halakimOfGdate = getHalakimOfGdate(displayDate) ;
		
		Hdate = new Array(9) ;  
		Hdate = getHdateOfHalakim(halakimOfGdate) ;  
		var output = new String ;
		output = Hdate[2] + " " + hMonthName[Hdate[1]] + " " + Hdate[0] ;

		document.write(output) ; 	
		return;
	}

function displayYearlessHdate(gregorianDateObject) 
	{
		displayDate = gregorianDateObject ; 
		halakimOfGdate = getHalakimOfGdate(displayDate) ;
		
		Hdate = new Array(9) ;  
		Hdate = getHdateOfHalakim(halakimOfGdate) ;  
		var output = new String ;
		output = Hdate[2] + " " + hMonthName[Hdate[1]] ; //+ " " + Hdate[0] ;

		document.write(output) ; 	
		return;
	}		
//var hebrewWeekdayStrings = new Array["Yom Rishon","Yom Sheini","Yom Shlishi","Yom Rvi'i","Yom Hamishi","Yom ShiShi","Shabbat"] ;
// Note: The constructor function weekdayArray() is in the standard page template for shul's web page.


//Next To Do: 
//Test for JavaScript turned on, and for Javascript version; break the webpage gracefully.

//	-->
