HeDate.endDeyHours=24;

function HeDate(year,month,date,day){
	
	if (typeof(year) == "number"){
        this.year=year;
        this.month=month;
        this.date=date;
        this.day=day;}
    else if ((typeof(year) == "object") && (year.constructor == HeDate)){ // copy constructor
        this.hours=year.hours;
        this.year=year.getYear(0);
        this.month=year.getMonth(0);
        this.date=year.getDate(0);
        this.day=year.getDay(0);}
    else if ((typeof(year) == "object") && (year.constructor == Date)){ // Convert constructor 
        this.hours=year.getHours();
        if (this.hours>=HeDate.endDeyHours)
            year.setDate(year.getDate()+1);
        this.day=year.getDay()+1;
        d=year.getDate();
        m=year.getMonth()+1;
        y=year.getYear();
	    if (y<2000) 
	        y+=1900; //FireFox Fix
        if (y==2009){
            if ((m==7 && d>=22)||(m==8 && d<=20)) {if (m==7) {this.date= d-21;}else {this.date= d+10;} this.month= 11; this.year=5769;}
            else if ((m==8 && d>=21)||(m==9 && d<=18)) {if (m==8) {this.date= d-20;}else {this.date= d+11;} this.month= 12; this.year=5769;}
            else if ((m==9 && d>=19)||(m==10 && d<=18)) {if (m==9) {this.date= d-18;}else {this.date= d+12;} this.month= 1; this.year=5770;}
            else if ((m==10 && d>=19)||(m==11 && d<=17)) {if (m==10) {this.date= d-18;}else {this.date= d+13;} this.month= 2; this.year=5770;}
            else if ((m==11 && d>=18)||(m==12 && d<=17)) {if (m==11) {this.date= d-17;}else {this.date= d+13;} this.month= 3; this.year=5770;}
            else if (m==12 && d>=18) {this.date= d-17;  this.month= 4; this.year=5770;}
        }
        else if (y==2010){
            if (m==1 && d<=15) {this.date= d+14; this.month= 4; this.year=5770;}
            else if ((m==1 && d>=16)||(m==2 && d<=14)) {if (m==1) {this.date= d-15;}else {this.date= d+16;} this.month= 5; this.year=5770;}
            else if ((m==2 && d>=15)||(m==3 && d<=15)) {if (m==2) {this.date= d-14;}else {this.date= d+14;} this.month= 6; this.year=5770;}
            else if ((m==3 && d>=16)||(m==4 && d<=14)) {if (m==3) {this.date= d-15;}else {this.date= d+16;} this.month= 7; this.year=5770;}
            else if ((m==4 && d>=15)||(m==5 && d<=13)) {if (m==4) {this.date= d-14;}else {this.date= d+16;} this.month= 8; this.year=5770;}
            else if ((m==5 && d>=14)||(m==6 && d<=12)) {if (m==5) {this.date= d-13;}else {this.date= d+18;} this.month= 9; this.year=5770;}
            else if ((m==6 && d>=13)||(m==7 && d<=11)) {if (m==6) {this.date= d-12;}else {this.date= d+18;} this.month= 10; this.year=5770;}
            else if ((m==7 && d>=12)||(m==8 && d<=10)) {if (m==7) {this.date= d-11;}else {this.date= d+20;} this.month= 11; this.year=5770;}
            else if ((m==8 && d>=11)||(m==9 && d<=8)) {if (m==8) {this.date= d-10;}else {this.date= d+21;} this.month= 12; this.year=5770;}
            else if ((m==9 && d>=9)||(m==10 && d<=8)) {if (m==9) {this.date= d-8;}else {this.date= d+22;} this.month= 1; this.year=5771;}
            else if ((m==10 && d>=9)||(m==11 && d<=7)) {if (m==10) {this.date= d-8;}else {this.date= d+23;} this.month= 2; this.year=5771;}
            else if ((m==11 && d>=8)||(m==12 && d<=7)) {if (m==11) {this.date= d-7;}else {this.date= d+23;} this.month= 3; this.year=5771;}
            else if (m==12 && d>=8) {this.date= d-7;  this.month= 4; this.year=5771;}
        }
        else if (y==2011){
            if (m==1 && d<=5) {this.date= d+24; this.month= 4; this.year=5771;}
            else if ((m==1 && d>=6)||(m==2 && d<=4)) {if (m==1) {this.date= d-5;}else {this.date= d+26;} this.month= 5; this.year=5771;}
            else if ((m==2 && d>=5)||(m==3 && d<=6)) {if (m==2) {this.date= d-4;}else {this.date= d+24;} this.date=(-1)*this.date; this.month= 6; this.year=5771;}
            else if ((m==3 && d>=7)||(m==4 && d<=4)) {if (m==3) {this.date= d-6;}else {this.date= d+25;} this.month= 6; this.year=5771;}
            else if ((m==4 && d>=5)||(m==5 && d<=4)) {if (m==4) {this.date= d-4;}else {this.date= d+26;} this.month= 7; this.year=5771;}
            else if ((m==5 && d>=5)||(m==6 && d<=2)) {if (m==5) {this.date= d-4;}else {this.date= d+27;} this.month= 8; this.year=5771;}
            else if ((m==6 && d>=3)||(m==7 && d<=2)) {if (m==6) {this.date= d-2;}else {this.date= d+28;} this.month= 9; this.year=5771;}
            else if (m==7 && d>=3) {this.date= d-2; this.month= 10; this.year=5771;}
            else if (m==8 && d<=30) {this.date= d; this.month= 11; this.year=5771;}
            else if ((m==8 && d>=31)||(m==9 && d<=28)) {if (m==8) {this.date= d-30;}else {this.date= d+1;} this.month= 12; this.year=5771;}
            else if ((m==9 && d>=29)||(m==10 && d<=28)) {if (m==9) {this.date= d-28;}else {this.date= d+2;} this.month= 1; this.year=5772;}
            else if ((m==10 && d>=29)||(m==11 && d<=26)) {if (m==10) {this.date= d-28;}else {this.date= d+3;} this.month= 2; this.year=5772;}
            else if ((m==11 && d>=27)||(m==12 && d<=26)) {if (m==11) {this.date= d-26;}else {this.date= d+4;} this.month= 3; this.year=5772;}
            else if (m==12 && d>=27) {this.date= d-26;  this.month= 4; this.year=5772;}
        }
        else if (y==2012){
            if (m==1 && d<=24) {this.date= d+5; this.month= 4; this.year=5772;}
            else if ((m==1 && d>=25)||(m==2 && d<=23)) {if (m==1) {this.date= d-24;}else {this.date= d+7;} this.month= 5; this.year=5772;}
            else if ((m==2 && d>=24)||(m==3 && d<=23)) {if (m==2) {this.date= d-23;}else {this.date= d+6;} this.month= 6; this.year=5772;}
            else if ((m==3 && d>=24)||(m==4 && d<=22)) {if (m==3) {this.date= d-23;}else {this.date= d+8;} this.month= 7; this.year=5772;}
            else if ((m==4 && d>=23)||(m==5 && d<=21)) {if (m==4) {this.date= d-22;}else {this.date= d+8;} this.month= 8; this.year=5772;}
            else if ((m==5 && d>=22)||(m==6 && d<=20)) {if (m==5) {this.date= d-21;}else {this.date= d+10;} this.month= 9; this.year=5772;}
            else if ((m==6 && d>=21)||(m==7 && d<=19)) {if (m==6) {this.date= d-20;}else {this.date= d+10;} this.month= 10; this.year=5772;}
            else if ((m==7 && d>=20)||(m==8 && d<=18)) {if (m==7) {this.date= d-19;}else {this.date= d+12;} this.month= 11; this.year=5772;}
            else if ((m==8 && d>=19)||(m==9 && d<=16)) {if (m==8) {this.date= d-18;}else {this.date= d+13;} this.month= 12; this.year=5772;}
            else if ((m==9 && d>=17)||(m==10 && d<=16)) {if (m==9) {this.date= d-16;}else {this.date= d+14;} this.month= 1; this.year=5773;}
            else if ((m==10 && d>=17)||(m==11 && d<=14)) {if (m==10) {this.date= d-16;}else {this.date= d+15;} this.month= 2; this.year=5773;}
            else if ((m==11 && d>=15)||(m==12 && d<=13)) {if (m==11) {this.date= d-14;}else {this.date= d+16;} this.month= 3; this.year=5773;}
            else if (m==12 && d>=14) {this.date= d-13;  this.month= 4; this.year=5773;}
        }
        else if (y==2013){
            if (m==1 && d<=11) {this.date= d+18; this.month= 4; this.year=5773;}
            else if ((m==1 && d>=12)||(m==2 && d<=10)) {if (m==1) {this.date= d-11;}else {this.date= d+20;} this.month= 5; this.year=5773;}
            else if ((m==2 && d>=11)||(m==3 && d<=11)) {if (m==2) {this.date= d-10;}else {this.date= d+18;} this.month= 6; this.year=5773;}
            else if ((m==3 && d>=12)||(m==4 && d<=10)) {if (m==3) {this.date= d-11;}else {this.date= d+20;} this.month= 7; this.year=5773;}
            else if ((m==4 && d>=11)||(m==5 && d<=9)) {if (m==4) {this.date= d-10;}else {this.date= d+20;} this.month= 8; this.year=5773;}
            else if ((m==5 && d>=10)||(m==6 && d<=8)) {if (m==5) {this.date= d-9;}else {this.date= d+22;} this.month= 9; this.year=5773;}
            else if ((m==6 && d>=9)||(m==7 && d<=7)) {if (m==6) {this.date= d-8;}else {this.date= d+22;} this.month= 10; this.year=5773;}
            else if ((m==7 && d>=8)||(m==8 && d<=6)) {if (m==7) {this.date= d-7;}else {this.date= d+24;} this.month= 11; this.year=5773;}
            else if ((m==8 && d>=7)||(m==9 && d<=4)) {if (m==8) {this.date= d-6;}else {this.date= d+25;} this.month= 12; this.year=5773;}
        }
    }
}
HeDate.prototype.getYear = function (display){
    if (display==0) return this.year;
    if (display==1) return HeDate.gimatria(this.year);
    return HeDate.gimatria(this.year);
}

HeDate.prototype.getMonth = function (display){
    if (display==0) return this.month;
    if (HeDate.LeapYear(this.year)==true && this.month==6){
        if (this.date>0)
            return HeDate.month(this.month)+ " ב'";
        else
            return HeDate.month(this.month)+ " א'";
        }
    else
        return HeDate.month(this.month);
   
}

HeDate.prototype.getDate = function (display){
    if (display==0) return this.date;
    if (display==1) return HeDate.gimatria(Math.abs(this.date));
    return HeDate.gimatria(Math.abs(this.date));
}

HeDate.prototype.getDay = function (display){
    if (display==0) return this.day;
    if (display==1) return HeDate.day(this.day);
    return HeDate.day(this.day);
}

HeDate.prototype.toString = function (){
    var ans=""
    if (this.getDay(1)!="")
        ans=this.getDay(1) + ", ";
    ans = ans + this.getDate(1) + " ";
    if (this.getMonth(0)>0){
        if(this.getDate(0)>0)
            ans = ans + "ב";
        ans= ans + this.getMonth(1)+ " ";
        }
    ans = ans + this.getYear(1)
	//return (ans + ThisDayName + " ב" + ThisMonthName + "<br><font color=#C6BB63>" + holiday +"</font>");
    return (ans);
}

HeDate.gimatria = function (num){
	var ans="";
	var temp=num%1000;
	var abc=" אבגדהוזחטיכלמנסעפצקרשת"
	
	if (temp>=100 && temp<=999) 	//מאות
		{var hundreds = (temp-temp%100)/100;
		temp=temp%100;
		while (hundreds >= 4)
			{ans = ans + abc.charAt(22);
			hundreds = hundreds - 4;}
		if (hundreds >> 0)
			ans = ans + abc.charAt(18 + hundreds);}

	if (temp>=10 && temp<=99) 	//עשרות
		{var tens=(temp-temp%10)/10;
		temp=temp%10;
		if ((tens==1) && ((temp==5)||(temp==6))) //ט"ו  ט"ז
			{tens = tens-1;
			temp = temp + 1;}
		ans = ans + abc.charAt(9 + tens);
		}

	if (temp>=1 && temp<=9) 	// יחידות
		ans = ans + abc.charAt(temp);

	if (ans.length>=2)		//הוספת גרש או גרשיים
		ans = ans.substring(0,(ans.length-1)) + '"' + ans.substring(ans.length-1);
	else if (ans.length==1)
		ans =ans + "'";

	if (num>=1000 && num<=9999) //אלפים
		ans = abc.charAt((num-num%1000)/1000) + "'" + ans;

	return ans
}

HeDate.regimatria = function (str){
	var ans=0;
	var temp=str;
	var abc=" אבגדהוזחטיכלמנסעפצקרשת";
	var index;
	while (temp.length>>0){
		index=abc.indexOf(temp.charAt(0));
		if (index>0){
			if (index<=9)
				ans=ans+index;
			else if (index<=18)
				ans=ans+(index-9)*10;
			else
				ans=ans+(index-18)*100;
		}
		temp=temp.substring(1,temp.length);
	}
	index=abc.indexOf(str.charAt(0));
	if (ans>index & index>0 & index<=9 & ans!=15 & ans!=16)
		ans=ans + index*1000 - index;
	return ans;
}

HeDate.LeapYear = function (year){
	year=year%19;
	if (year==3 || year==6 || year==8 || year==11 || year==14 || year==17 || year==0)
		return true;
	else
		return false;
}

HeDate.month = function (intMonth){
	var months = ['','תשרי','חשון','כסלו','טבת','שבט','אדר','ניסן','אייר','סיון','תמוז','אב','אלול']
	return months[intMonth];
}

HeDate.day = function (intDay){
    var day = ['','יום ראשון','יום שני','יום שלישי','יום רביעי','יום חמישי','יום שישי','שבת']
    if (intDay>0 && intDay<=7)
        return day[intDay];
    else
        return "";
}

HeDate.omer = function (heDate,display,endDeyH){
    var days=-1;
    if (heDate.month==7 && heDate.date>15)
        days=heDate.date - 15;
    else if (heDate.month==8)
        days=heDate.date + 15;
    else if (heDate.month==9 && heDate.date<6)
        days=heDate.date + 44;
    if (HeDate.endDeyHours == 24 & endDeyH>=15 & endDeyH<=heDate.hours)
        days=days+1;
    if (display==0)
        return days;
    else 
        if (days>=1 && days<=49){
            var show= new Array(50);
            show[1]="היום יום אחד לעומר"; show[2]="היום שני ימים לעומר"; show[3]="היום שלושה ימים לעומר";
            show[4]="היום ארבעה ימים לעומר"; show[5]="היום חמישה ימים לעומר"; show[6]="היום שישה ימים לעומר";
            show[7]="היום שיבעה ימים לעומר, שהם שבוע אחד"; show[8]="היום שמונה ימים לעומר, שהם שבוע אחד ויום אחד"; show[9]="היום תישעה ימים לעומר, שהם שבוע אחד ושני ימים";
            show[10]="היום עשרה ימים לעומר, שהם שבוע אחד ושלושה ימים"; show[11]="היום אחד עשר יום לעומר, שהם שבוע אחד וארבעה ימים"; show[12]="היום שנים עשר יום לעומר, שהם שבוע אחד וחמישה ימים";
            show[13]="היום שלושה עשר יום לעומר, שהם שבוע אחד ושישה ימים"; show[14]="היום ארבעה עשר יום לעומר, שהם שני שבועות"; show[15]="היום חמישה עשר יום לעומר, שהם שני שבועות ויום אחד";
            show[16]="היום שישה עשר יום לעומר, שהם שני שבועות ושני ימים"; show[17]="היום שיבעה עשר יום לעומר, שהם שני שבועות ושלושה ימים"; show[18]="היום שמונה עשר יום לעומר, שהם שני שבועות וארבעה ימים";
            show[19]="היום תישעה עשר יום לעומר, שהם שני שבועות וחמישה ימים"; show[20]="היום עשרים יום לעומר, שהם שני שבועות ושישה ימים"; show[21]="היום אחד ועשרים יום לעומר, שהם שלושה שבועות";
            show[22]="היום שנים ועשרים יום לעומר, שהם שלושה שבועות ויום אחד"; show[23]="היום שלושה ועשרים יום לעומר, שהם שלושה שבועות ושני ימים"; show[24]="היום ארבעה ועשרים יום לעומר, שהם שלושה שבועות ושלושה ימים";
            show[25]="היום חמישה ועשרים יום לעומר, שהם שלושה שבועות וארבעה ימים"; show[26]="היום שישה ועשרים יום לעומר, שהם שלושה שבועות וחמישה ימים"; show[27]="היום שיבעה ועשרים יום לעומר, שהם שלושה שבועות ושישה ימים";
            show[28]="היום שמונה ועשרים יום לעומר, שהם ארבעה שבועות"; show[29]="היום תישעה ועשרים יום לעומר, שהם ארבעה שבועות ויום אחד"; show[30]="היום שלושים יום לעומר, שהם ארבעה שבועות ושני ימים";
            show[31]="היום אחד ושלושים יום לעומר, שהם ארבעה שבועות ושלושה ימים"; show[32]="היום שנים ושלושים יום לעומר, שהם ארבעה שבועות וארבעה ימים"; show[33]="היום שלושה ושלושים יום לעומר, שהם ארבעה שבועות וחמישה ימים";
            show[34]="היום ארבעה ושלושים יום לעומר, שהם ארבעה שבועות ושישה ימים"; show[35]="היום חמישה ושלושים יום לעומר, שהם חמישה שבועות"; show[36]="היום שישה ושלושים יום לעומר, שהם חמישה שבועות ויום אחד";
            show[37]="היום שיבעה ושלושים יום לעומר, שהם חמישה שבועות ושני ימים"; show[38]="היום שמונה ושלושים יום לעומר, שהם חמישה שבועות ושלושה ימים"; show[39]="היום תישעה ושלושים יום לעומר, שהם חמישה שבועות וארבעה ימים";
            show[40]="היום ארבעים יום לעומר, שהם חמישה שבועות וחמישה ימים"; show[41]="היום אחד וארבעים יום לעומר, שהם חמישה שבועות ושישה ימים"; show[42]="היום שנים וארבעים יום לעומר, שהם שישה שבועות";
            show[43]="היום שלושה וארבעים יום לעומר, שהם שישה שבועות ויום אחד"; show[44]="היום ארבעה וארבעים יום לעומר, שהם שישה שבועות ושני ימים"; show[45]="היום חמישה וארבעים יום לעומר, שהם שישה שבועות ושלושה ימים";
            show[46]="היום שישה וארבעים יום לעומר, שהם שישה שבועות וארבעה ימים"; show[47]="היום שיבעה וארבעים יום לעומר, שהם שישה שבועות וחמישה ימים"; show[48]="היום שמונה וארבעים יום לעומר, שהם שישה שבועות ושישה ימים";
            show[49]="היום תישעה וארבעים יום לעומר, שהם שיבעה שבועות";
            return show[days];
        }
        else
            return "";
}

function toString(){
    var ans=""
	var num=""
    var ThisMonthName=this.getMonth();
	var ThisDayName=this.getDate()
	var holiday=""

  if (ThisMonthName == "תשרי") {
    if (ThisDayName == "א'") {holiday = "א' דראש השנה";}
    if (ThisDayName ==  "ב'") {holiday = "ב' דראש השנה"; }
    if (ThisDayName ==  "ג'") {holiday = "צום גדליה"; }
    if (ThisDayName == "ט'") {holiday = "ערב יום הכיפורים"; }
    if (ThisDayName == "י'") {holiday = "יום הכיפורים"; }
    if (ThisDayName == 'ט"ו') {holiday = "סוכות"; }
    if (ThisDayName == 'ט"ז') {holiday = "א' דחול המועד סוכות"; }
    if (ThisDayName == 'י"ז') {holiday = "ב' דחול המועד סוכות"; }
    if (ThisDayName == 'י"ח') {holiday = "ג' דחול המועד סוכות"; }
    if (ThisDayName == 'י"ט') {holiday = "ד' דחול המועד סוכות"; }
    if (ThisDayName == "כ'") {holiday = "ה' דחול המועד סוכות"; }
    if (ThisDayName == 'כ"א') {holiday = "ו' דחול המועד סוכות - הושענא רבא"; }
    if (ThisDayName == 'כ"ב') {holiday = "שמחת תורה"; }
  }

  if (ThisMonthName == "כסלו") { 
    if (ThisDayName == 'כ"ה') {holiday = "נר ראשון של חנוכה"; }
    if (ThisDayName == 'כ"ו') {holiday = "נר שני של חנוכה"; }
    if (ThisDayName == 'כ"ז') {holiday = "נר שלישי של חנוכה"; }
    if (ThisDayName == 'כ"ח') {holiday = "נר רביעי של חנוכה"; }
    if (ThisDayName == 'כ"ט') {holiday = "נר חמישי של חנוכה"; }
  }

  if (ThisMonthName == "טבת") { 
    if (ThisDayName == "א'") {holiday = "נר שישי של חנוכה"; }
    if (ThisDayName == "ב'") {holiday = "נר שביעי של חנוכה"; }
    if (ThisDayName == "ג'") {holiday = "נר שמיני של חנוכה"; }
    if (ThisDayName == "י'") {holiday = "צום עשרה בטבת"; }
  }

  if (ThisMonthName == "שבט") {
    if (ThisDayName == 'ו"ט') {holiday = "חג האילנות"; }
  }

  // ימים מיוחדים בחודש אדר בהנחה שאין חודש אדר נוסף (שנה מעוברת)
  if (ThisMonthName == "אדר") {
    if (ThisDayName == 'י"ג') {holiday = "תענית אסתר"; }
    if (ThisDayName == 'י"ד') {holiday = "פורים"; }
    if (ThisDayName == 'ו"ט') {holiday = "שושן פורים"; }
  } 

  // ימים מיוחדים בחודש אדר ב' במידה והוא קיים
  if (ThisMonthName == "אדר ב'") {
    if (ThisDayName == 'י"ג') {holiday = "תענית אסתר"; }
    if (ThisDayName == 'י"ד') {holiday = "פורים"; }
    if (ThisDayName == 'י"ה') {holiday = "שושן פורים"; }
  } 

  if (ThisMonthName == "ניסן") {
    if (ThisDayName == 'י"ד') {holiday = "ותענית בכורות וערב הפסח"; }
    if (ThisDayName == 'ו"ט') {holiday = "חג ראשון של פסח"; }
    if (ThisDayName == 'ז"ט') {holiday = "א' חול המועד פסח"; }
    if (ThisDayName == 'י"ז') {holiday = "ב' חול המועד פסח"; }
    if (ThisDayName == 'י"ח') {holiday = "ג' חול המועד פסח"; }
    if (ThisDayName == 'י"ט') {holiday = "ד' חול המועד פסח"; }
    if (ThisDayName == "כ'") {holiday = "ה' חול המועד פסח"; }
    if (ThisDayName == 'כ"א') {holiday = "חג שני של פסח"; }
    if (ThisDayName == 'כ"ז') {holiday = "יום הזיכרון לשואה ולגבורה"; }
  }   

  if (ThisMonthName == "אייר") {
   if (ThisDayName == "ג'") {holiday = "יום הזיכרון לחללי מערכות ישראל"; }
    if (ThisDayName == "ד'") {holiday = "יום העצמאות ה - ";
	                     holiday += (this.year-5708);
   						 holiday += " למדינת ישראל"; }
   if (ThisDayName == 'י"ח') {holiday = "ל\"ג בעומר - הדלקת מדורות"; }
   if (ThisDayName == 'כ"ח') {holiday = "יום ירושלים"; }
  }

  if (ThisMonthName == "סיוון") {
    if (ThisDayName == "ה'") {holiday = "ערב שבועות"; }
    if (ThisDayName == "ו'") {holiday = "שבועות"; }
    if (ThisDayName == "ז'") {holiday = "שבועות"; }
  }

  if (ThisMonthName == "תמוז") {
    if (ThisDayName == 'י"ז') {holiday = "צום י\"ז בתמוז"; }
  }

  if (ThisMonthName == "אלול") {
    if (ThisDayName == 'כ"ט') {holiday = "ערב ראש השנה";}
  }

	
	return (ans + ThisDayName + " ב" + ThisMonthName + "<br><font color=#C6BB63>" + holiday +"</font>");
}
HeDate.prototype.toString = toString;


//%>-->
