var hebrewMonth					= 0
var hebrewDate					= 0
var hebrewYear					= 0
var metonicCycle				= 0
var metonicYear					= 0
var moladDay					= 0
var moladHalakim				= 0    
var FinalDayNumber				= 0
var SUN							= 0
var MON							= 1
var TUES						= 2
var WED							= 3
var THUR						= 4
var FRI							= 5
var SAT							= 6
var HEB_SDN_OFFSET				= 347997
var NEW_MOON_OF_CREATION		= 31524
var GREG_SDN_OFFSET				= 32045
var DAYS_PER_5_MONTHS			= 153
var DAYS_PER_4_YEARS			= 1461
var DAYS_PER_400_YEARS			= 146097
var HALAKIM_PER_HOUR			= 1080
var HALAKIM_PER_DAY				= 25920
var HALAKIM_PER_LUNAR_CYCLE		= ((29 * HALAKIM_PER_DAY) + 13753)
var HALAKIM_PER_METONIC_CYCLE	= (HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7))
var NOON						= (18 * HALAKIM_PER_HOUR)
var AM3_11_20					= ((9 * HALAKIM_PER_HOUR) + 204)
var AM9_32_43					= ((15 * HALAKIM_PER_HOUR) + 589)
var today						= null
var FinalYearString				= ""
var FinalDayString				= ""
var FinalMonthString			= ""
var gMonth						= new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var gWeekday					= new Array("ראשון","שני","שלישי","רביעי","חמישי","שישי","שבת")
var hMonth						= new Array("תשרי","חשוון","כסלו","טבת","שבט","אדר א","אדר ב","ניסן","אייר","סיוון","תמוז","אב","אלול")
var HebABCString				= new Array("א","ב","ג","ד","ה","ו","ז","ח","ט","י","כ","ל","מ","נ","ס","ע","פ","צ","ק","ר","ש","ת")
var mpy							= new Array(12, 12, 13, 12, 12, 13, 12, 13, 12, 12, 13, 12, 12, 13, 12, 12, 13, 12, 13)
var HebABCNumber				= new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400)
        
        function getToday(){
				var SelDateArr;
				SelDateArr = SelDate.value.split("/");
				today = new Date(SelDateArr[2],SelDateArr[1]-1,SelDateArr[0])
        }
        
        function displayWeekday(){
			getToday()
			FinalDayString = "יום " + gWeekday[today.getDay()]
        }                
        
        function displayHebrewDate(){
            if(hebrewDate != 0 && hebrewMonth != 0 && hebrewYear != 0){
				FinalYearString  = GetHebStringDay(hebrewYear, true)
				FinalDayNumber   = GetHebStringDay(hebrewDate, false)
				FinalMonthString = hMonth[hebrewMonth - 1]          									
            }    
        }
        
        function GetHebStringDay(dayNumber, Flag){
        var Couner = HebABCString.length - 1
        var Value  = ""
        var Temp   = dayNumber
		if(Temp == 15)
			Value =	"טו"
		else if(Temp == 16)
			Value = "טז"
		else if(Temp == 1)
			Value = "א"
		else{
			if(Flag) Temp -= 5000
						
			for(var i=Couner;i>=1;i--){
				while(Temp - HebABCNumber[i] >= 0){
					Temp  -= HebABCNumber[i]
					Value += HebABCString[i]
				}
			}}
			
			if(Flag) 
				Value = Value.substr(0, Value.length - 1) + "\"" + Value.substr(Value.length - 1, 1)
			else
			{
				if(Value.length == 1)
					Value += "\'"
				else
					Value = Value.substr(0, Value.length - 1) + "\"" + Value.substr(Value.length - 1, 1)
			}
				
			return Value
        }
                
        function GregorianToSdn(inputYear, inputMonth, inputDay){
        var year  = 0
        var month = 0
        var sdn
        
			getToday()
			
            //Make year a positive number
            if(inputYear < 0)
				year = inputYear + 4801
            else
				year = inputYear + 4800
				
            //Adjust the start of the year
            if(inputMonth > 2)
				month = inputMonth - 3           
            else{
                month = inputMonth + 9
                year--
            }
            
            sdn  = Math.floor((Math.floor(year / 100) * DAYS_PER_400_YEARS) / 4)
            sdn += Math.floor(((year % 100) * DAYS_PER_4_YEARS) / 4)
            sdn += Math.floor((month * DAYS_PER_5_MONTHS + 2) / 5)
            sdn += inputDay - GREG_SDN_OFFSET
            return sdn
        }
        
        function SdnToHebrew(sdn){        
        var tishri1      = 0
        var tishri1After = 0
        var yearLength   = 0
        var inputDay     = sdn - HEB_SDN_OFFSET
        var inputDay
        
			FindTishriMolad(inputDay)
            tishri1 = Tishri1(metonicYear, moladDay, moladHalakim)
            if(inputDay >= tishri1){
				//It found Tishri 1 at the start of the year.
				hebrewYear = metonicCycle * 19 + metonicYear + 1
				if(inputDay < tishri1 + 59){
					if(inputDay < tishri1 + 30){
						hebrewMonth = 1
                        hebrewDate  = inputDay - tishri1 + 1
                    }
                    else{
                        hebrewMonth = 2
                        hebrewDate  = inputDay - tishri1 - 29
                    }
                    return
				}
                //We need the length of the year to figure this out,so find Tishri 1 of the next year.
                moladHalakim += HALAKIM_PER_LUNAR_CYCLE * mpy[metonicYear]
                moladDay     += Math.floor(moladHalakim / HALAKIM_PER_DAY)
                moladHalakim %= HALAKIM_PER_DAY
                tishri1After  = Tishri1((metonicYear + 1) % 19, moladDay, moladHalakim)
			}
            else{
				//It found Tishri 1 at the end of the year.
                hebrewYear = metonicCycle * 19 + metonicYear
                if(inputDay >= tishri1 - 177){
					//It is one of the last 6 months of the year.
					if(inputDay > tishri1 - 30){
						hebrewMonth = 13
                        hebrewDate  = inputDay - tishri1 + 30
                    }
                    else if(inputDay > tishri1 - 60){
						hebrewMonth = 12
                        hebrewDate  = inputDay - tishri1 + 60
                    }
                    else if(inputDay > tishri1 - 89){
						hebrewMonth = 11
                        hebrewDate  = inputDay - tishri1 + 89
                    }
                    else if(inputDay > tishri1 - 119){
						hebrewMonth = 10
                        hebrewDate  = inputDay - tishri1 + 119
                    }
					else if(inputDay > tishri1 - 148){
						hebrewMonth = 9
                        hebrewDate  = inputDay - tishri1 + 148
                    }
                    else{
						hebrewMonth = 8
                        hebrewDate  = inputDay - tishri1 + 178
                    }
				return
			}
            else{
				if(mpy[(hebrewYear - 1) % 19] == 13) {
					hebrewMonth = 7
					hebrewDate  = inputDay - tishri1 + 207
                    if(hebrewDate > 0) return
                    hebrewMonth--
                    hebrewDate += 30
                    if(hebrewDate > 0) return
                    hebrewMonth--
                    hebrewDate += 30
				}
				else{
					hebrewMonth = 6
                    hebrewDate  = inputDay - tishri1 + 207
                    if(hebrewDate > 0) return
                    hebrewMonth--
                    hebrewDate += 30
				}
				if(hebrewDate > 0) return
                hebrewMonth--
				hebrewDate += 29
				if(hebrewDate > 0) return
				
				//We need the length of the year to figure this out,so find Tishri 1 of this year.
				tishri1After = tishri1
                FindTishriMolad(moladDay - 365)
                tishri1 = Tishri1(metonicYear, moladDay, moladHalakim)
			}
		}
		yearLength = tishri1After - tishri1
        moladDay   = inputDay - tishri1 - 29
		if(yearLength == 355 || yearLength == 385){
			//Heshvan has 30 days
			if(moladDay <= 30){
				hebrewMonth = 2
				hebrewDate  = moladDay
                return
			}
			moladDay -= 30
		}
		else{
			//Heshvan has 29 days
			if(moladDay <= 29){
				hebrewMonth = 2
				hebrewDate  = moladDay
                return
			}
			moladDay -= 29
		}
		
        //It has to be Kislev.
		hebrewMonth = 3
        hebrewDate  = moladDay
	}
        
    function FindTishriMolad(inputDay){
		//Estimate the metonic cycle number.  Note that this may be an under
        //estimate because there are 6939.6896 days in a metonic cycle not
        //6940,but it will never be an over estimate.   The loop below will
        //correct for any error in this estimate.        
        metonicCycle = Math.floor((inputDay + 310) / 6940)
        
		//Calculate the time of the starting molad for this metonic cycle.
        MoladOfMetonicCycle()
        
        //If the above was an under estimate,increment the cycle number until
        //the correct one is found.  For modern dates this loop is about 98.6%
        //likely to not execute,even once,because the above estimate is
        //really quite close.
        while(moladDay < inputDay - 6940 + 310){
			metonicCycle++
			moladHalakim += HALAKIM_PER_METONIC_CYCLE
			moladDay     += Math.floor(moladHalakim / HALAKIM_PER_DAY)
			moladHalakim %= HALAKIM_PER_DAY
        }
        
        //Find the molad of Tishri closest to this date.
        for(metonicYear = 0; metonicYear < 18; metonicYear++){
            if(moladDay > inputDay - 74) break
            moladHalakim += HALAKIM_PER_LUNAR_CYCLE * mpy[metonicYear]
            moladDay     += Math.floor(moladHalakim / HALAKIM_PER_DAY)
            moladHalakim %= HALAKIM_PER_DAY
        }
	}
	
    function MoladOfMetonicCycle(){
	var r1, r2, d1, d2
	
		//Start with the time of the first molad after creation.
		r1 = NEW_MOON_OF_CREATION
		
		//Calculate gMetonicCycle * HALAKIM_PER_METONIC_CYCLE.  The upper 32
		//bits of the result will be in r2 and the lower 16 bits will be in r1.
		r1 += metonicCycle * (HALAKIM_PER_METONIC_CYCLE&0xFFFF)
		r2	= r1 >> 16
		r2 += metonicCycle * ((HALAKIM_PER_METONIC_CYCLE >> 16) & 0xFFFF)
		
		//Calculate r2r1 / HALAKIM_PER_DAY.  The remainder will be in r1,the
		//upper 16 bits of the quotient will be in d2 and the lower 16 bits
		//will be in d1.
		d2	= Math.floor(r2 / HALAKIM_PER_DAY)
		r2 -= d2 * HALAKIM_PER_DAY
		r1	= (r2 << 16) | (r1&0xFFFF)
		d1	= Math.floor(r1 / HALAKIM_PER_DAY)
		r1 -= d1 * HALAKIM_PER_DAY
		
		moladDay     = (d2 << 16) | d1
		moladHalakim = r1
	}
	
    function Tishri1(metonicYear, moladDay, moladHalakim){
	var tishri1         = moladDay
	var dow             = tishri1 % 7
	var leapYear        = metonicYear == 2 || metonicYear == 5 || metonicYear == 7 || metonicYear == 10 || metonicYear == 13 || metonicYear == 16 || metonicYear == 18
	var lastWasLeapYear = metonicYear == 3 || metonicYear == 6 || metonicYear == 8 || metonicYear == 11 || metonicYear == 14 || metonicYear == 17 || metonicYear == 0
	
		//Apply rules 2,3 and 4
		if((moladHalakim >= NOON) || ((!leapYear) && dow == TUES && moladHalakim >= AM3_11_20) || (lastWasLeapYear && dow == MON && moladHalakim >= AM9_32_43)){
			tishri1++
		    dow++
		    if(dow == 7) dow = 0
		}
		
		//Apply rule 1 after the others because it can cause an additional delay of one day.
		if(dow == WED || dow == FRI || dow == SUN) tishri1++
		return tishri1
	}
        
	function ShowHebrewDate(NameOfContainer){
		displayWeekday()
		SdnToHebrew(GregorianToSdn(today.getFullYear(), today.getMonth() + 1, today.getDate()))
		displayHebrewDate()
		document.all.item(NameOfContainer).innerHTML = FinalDayString + " " + FinalDayNumber + " " + FinalMonthString + " " + FinalYearString
	}
	
	
	function omer(hday, hmonth)
	{
		if(hmonth == 0)
		{
			if(hday >= 16 && hday <= 30)
				return (hday-15) + " Of The Omer";
		}
		if(hmonth == 1)
		{
			if(hday >= 1 && hday <= 30)
				return (hday+15) + " Of The Omer";
		}
		if(hmonth == 2)
		{
			if(hday >= 1 && hday <= 5)
				return (hday+44) + " Of The Omer";
		}
		return "";
	}
