function makeArray() {
	this[0] = makeArray.arguments.length;
	for (i = 0; i < makeArray.arguments.length; i = i + 1)
		this[i+1] = makeArray.arguments[i];
}


var hebMonth = new makeArray(
	'Nissan', 'Iyar', 'Sivane', 'Tamouz', 'Av', 'Eloul',
	'Tichri', 'Hechvan', 'Kislev', 'Tévet', 'Chevat',
	'Adar', 'Adar I', 'Adar II');


var civMonth = new makeArray(
	'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
	'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');


var weekDay = new makeArray(
	'Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam');


function Gauss(year) {
	var a,b,c;
	var m;
	var	Mar;	// "day in March" on which Pesach falls (return value)


	a = Math.floor((12 * year + 17) % 19);
	b = Math.floor(year % 4);

	m = 32.044093161144 + 1.5542417966212 * a +  b / 4.0 - 0.0031777940220923 * year;
	if (m < 0)
		m -= 1;
	Mar = Math.floor(m);
	if (m < 0)
		m++;
	m -= Mar;


	c = Math.floor((Mar + 3 * year + 5 * b + 5) % 7);
	if(c == 0 && a > 11 && m >= 0.89772376543210 )
		Mar++;
	else if(c == 1 && a > 6 && m >= 0.63287037037037)
		Mar += 2;
	else if(c == 2 || c == 4 || c == 6)
		Mar++;


	Mar += Math.floor((year - 3760) / 100) - Math.floor((year - 3760) / 400) - 2;
	return Mar;
}


function leap(y) {

	return ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0));
}


function civMonthLength(month, year) {
	if(month == 2)
		return 28 + leap(year);
	else if(month == 4 || month == 6 || month == 9 || month == 11)
	   return 30;
	else
		return 31;
}


function civ2heb(day, month, year) {
	var d = day;
	var	m = month;
	var y = year;
	var hy;
	var pesach;
	var anchor;
	var adarType;


	m -= 2;
	if (m <= 0) { // Jan or Feb
		m += 12;
		y -= 1;
	}


	d += Math.floor(7 * m / 12 + 30 * (m - 1)); // day in March
	hy = y + 3760;	// get Hebrew year
	pesach = Gauss(hy);
	if (d <= pesach - 15) { // before 1 Nisan
		anchor = pesach;
		d += 365;
		if(leap(y))
			d++;
		y -= 1;
		hy -= 1;
		pesach = Gauss(hy);
	}
	else
		anchor = Gauss(hy + 1);


	d -= pesach - 15;
	anchor -= pesach - 12;
	y++;
	if(leap(y))
		anchor++;


	for(m = 0; m < 11; m++) {
		var days;
		if(m == 7 && anchor % 30 == 2)
			days = 30; // Cheshvan
		else if(m == 8 && anchor % 30 == 0)
			days = 29; // Kislev
		else
			days = 30 - m % 2;
		if(d <= days)
			break;
		d -= days;
	}


	adarType = 0;			// plain old Adar
	if (m == 11 && anchor >= 30) {
		if (d > 30) {
			adarType = 2;	// Adar 2
			d -= 30;
		}
		else
			adarType = 1;	// Adar 1
	}


	if(m >= 6)		// Tishrei or after?
		hy++;		// then bump up year


	if(m == 11)			// Adar?
		m += adarType;	// adjust for Adars


	return (d + ' ' + m + ' ' + hy);
}




function Easter(Y) {
	// based on the algorithm of Oudin
    var C = Math.floor(Y / 100);
    var N = Y - 19 * Math.floor(Y / 19);
    var K = Math.floor((C - 17) / 25);
    var I = C - Math.floor(C / 4) - Math.floor((C - K) / 3) + 19 * N + 15;
    I = I - 30*Math.floor((I / 30));
    I = I - Math.floor(I / 28) * (1 - Math.floor(I / 28) * Math.floor(29 / (I + 1)) * Math.floor((21 - N) / 11));
    var J = Y + Math.floor(Y / 4) + I + 2 - C + Math.floor(C / 4);
    J = J - 7 * Math.floor(J / 7);
    var L = I - J;
    var M = 3 + Math.floor((L + 40) / 44);
    var D = L + 28 - 31 * Math.floor(M / 4);


	var ret = new Object();
	ret[1] = M;
	ret[2] = D;
	return ret;
}


function DOW(day,month,year) {
	var a = Math.floor((14 - month)/12);
	var y = year - a;
	var m = month + 12*a - 2;
	var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
			Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
	return d + 1;
}


function NthDOW(nth,weekday,month,year) {
	if (nth > 0)
		return (nth - 1) * 7 + 1 + (7 + weekday - DOW((nth - 1) * 7 + 1, month, year)) % 7;
	var days = civMonthLength(month, year);
	return days - (DOW(days, month, year) - weekday + 7) % 7;
}


function holidays(cday, cmonth, cyear) {
	// American civil holidays and some major religious holiday
	if (cmonth == 1 && cday == 1)
		return "New Year's Day";
	else if (cmonth == 2 && cday == 12)
		return "Lincoln's Birthday";
	else if (cmonth == 2 && cday == 14)
		return "Valentine's Day";
	else if (cmonth == 2 && cday == NthDOW(3, 2, 2, cyear))
		return "President's Day";
	else if (cmonth == 3 && cday == 17)
		return "St. Patrick's Day";
	else if (cmonth == 3 || cmonth == 4) {
		var e = Easter(cyear);
	    if (cmonth == e[1] && cday == e[2])
			return "Easter";
	}
	else if (cmonth == 5 && cday == NthDOW(2, 1, 5, cyear))
		return "Mother's Day";
	else if (cmonth == 5 && cday == NthDOW(3, 7, 5, cyear))
		return "Armed Forces Day";
	else if (cmonth == 5 && cday == NthDOW(0, 2, 5, cyear))
		return "Memorial Day";
	else if (cmonth == 6 && cday == 14)
		return "Flag Day";
	else if (cmonth == 6 && cday == NthDOW(3, 1, 6, cyear))
		return "Father's Day";
	else if (cmonth == 7 && cday == 4)
		return "Independence Day";
	else if (cmonth == 9 && cday == NthDOW(1, 2, 9, cyear))
		return "Labor Day";
	else if (cmonth == 10 && cday == NthDOW(2, 2, 10, cyear))
		return "Columbus Day";
	else if (cmonth == 10 && cday == 31)
		return "Halloween";
	else if (cmonth == 11 && cday == 11)
		return "Veterans' Day";
	else if (cmonth == 11 && cday == NthDOW(4, 5, 11, cyear))
		return "Thanksgiving";
	else if (cmonth == 12 && cday == 25)
		return "Christmas";


	return "";
}


function moadim(cday, cmonth, cyear, hday, hmonth, dow) {
	if(hmonth == 6) {
		if(hday == 1 || hday == 2)
			return "Roch Hashana"
		else if(hday == 3 && dow != 7)
			return "Jeûne de Guédalia";
		else if(hday == 4 && dow == 1)
			return "Jeûne de Guédalia";
		else if(hday == 10)
			return "Yom Kippour"
		else if(hday >= 15 && hday <= 22)
			return "Soucote"
		else if(hday == 23)
			return "Soucote (d)"
	}
	else if(hmonth == 8) {

		if(hday >= 25)
			return "Hanoucah"
	}
	else if(hmonth == 9) {
		if(hday <= 2) {
			return "Hanoucah"
		}
		else if(hday == 3) {
			// Kislev can be malei or chaser
			if(cday == 1) {
				cday = 29;
				cmonth = 11;
			}
			else if(cday == 2) {
				cday = 30;
				cmonth = 11;
			}
			else
				cday -= 3;
			var hdate = civ2heb(cday, cmonth, cyear);
			hd = eval(hdate.substring(0, hdate.indexOf(' ')));
			if(hd == 29)
				return "Hanoucah"
		}
		else if(hday == 10)
			return "Jeûne de Tevet"
	}
	else if(hmonth == 10) {
		if(hday==15)
			return "Tou bichvate"
	}
	else if(hmonth == 11 || hmonth == 13) {
		if(hday == 11 && dow == 5)
			return "Jeûne d'Esther"
		else if(hday == 13 && dow != 7)
			return "Jeûne d'Esther"
		else if(hday == 14)
			return "Pourrim"
		else if(hday == 15)
			return "Chouchane Pourim"
	}
	else if(hmonth == 0) {


		if(hday == 12 && dow == 5)
			return "Taanit Bechorot"
		else if(hday == 14 && dow != 7)
			return "Taanit Bechorot"
		else if(hday >= 15 && hday <= 21)
			return "Péssa'h"
		else if(hday == 22)
			return "Péssa'h (d)"
	}
	else if(hmonth == 1) {
		if(hday == 3 && dow == 5)
			return "Yom Atzmaout"
		else if(hday == 4 && dow == 5)
			return "Yom Atzmaout"
		else if(hday == 5 && dow != 6 && dow != 7)
			return "Yom Atzmaout"
		if(hday == 14)
			return "Péssa'h chéni"
		else if(hday == 18)
			return "Lag Ba'omer"
		if(hday == 28)
			return "Jour de la libération de Jérusalem"
	}
	else if(hmonth == 2) {
		if(hday == 6)
			return "Chavouôte"

		else if(hday == 7)
			return "Chavouôte (d)"
	}
	else if(hmonth == 3) {
		if(hday == 17 && dow != 7)
			return "Jeûne du 17 Tamouz"
		if(hday == 18 && dow == 1)
			return "Jeûne du 17 Tamouz"
	}
	else if(hmonth == 4) {
		if(hday == 9 && dow != 7)
			return "Ticha Be'Av"
		if(hday == 10 && dow == 1)
			return "Ticha Bé'Av"
		if(hday == 15)
			return "Tou Bé'Av"
	}


	return "";
}


var otherHolidays = 0;
var jewishHolidays = 1;
var civilHolidays = 0;


function selectLoad() {
	var now = new Date();
	var m = now.getMonth();
	var y = now.getYear();
	if(y < 1000)
		y += 1900;


	document.calendar_control.month.selectedIndex = m;
	document.calendar_control.yr.value = y;
	doCal(m, y);
}


function selectCurrent() {
	var now = new Date();
	var y = now.getYear();
	var m = now.getMonth();
	if(y < 1000)
		y += 1900;


	document.calendar_control.month.selectedIndex = m;
	document.calendar_control.yr.value = y;
	doCal(m, y);
}


function toggleOther(form) {
	otherHolidays = (otherHolidays == 1) ? 0 : 1;
	var y = parseInt(document.calendar_control.yr.value);
	var m = document.calendar_control.month.selectedIndex;
	doCal(m, y);
}


function selectNext() {
	var y = parseInt(document.calendar_control.yr.value);
	var m = document.calendar_control.month.selectedIndex;


	if(m < 11)
		m++;
	else {
		m = 0;
		y++;
	}


	document.calendar_control.month.selectedIndex = m;
	document.calendar_control.yr.value = y;
	doCal(m, y);
}


function selectPrev() {
	var y = parseInt(document.calendar_control.yr.value);
	var m = document.calendar_control.month.selectedIndex;


	if(m > 0)
		m -= 1;
	else {
		m = 11;
		y -= 1;
	}


	document.calendar_control.month.selectedIndex = m;
	document.calendar_control.yr.value = y;
	doCal(m, y);

}


function selectNextYear(num) {
	var y = parseInt(document.calendar_control.yr.value);
	var m = document.calendar_control.month.selectedIndex;
	y += num;
	document.calendar_control.yr.value = y;
	doCal(m, y);
}


function selectPrevYear(num) {

	var y = parseInt(document.calendar_control.yr.value);
	var m = document.calendar_control.month.selectedIndex;
	y -= num;
	document.calendar_control.yr.value = y;
	doCal(m, y);
}


function selectForm(form) {
	var y = parseInt(form.yr.value);
	var m = form.month.selectedIndex;
	doCal(m, y);
}


function selectHolidays(form) {
	var y = parseInt(form.yr.value);
	var m = form.month.selectedIndex;
	var h = form.holidays.selectedIndex;
	if(h == 0) {
		jewishHolidays = 1;
		civilHolidays = 0;
	}
	else if(h == 1) {
		jewishHolidays = 0;
		civilHolidays = 0;
	}


	doCal(m, y);
}


function doCal(month, year) {
	var ret = calendar(month, year);
	var doc = parent.frames.result.document;
	doc.close();
	doc.open("text/html");
	var result = BuildLuachHTML(ret);
	doc.write(result);
	doc.close();
}


function calendar(selM, selY) {
	var m = selM + 1;
	var y = selY;
	var d = civMonthLength(m, y);
	var firstOfMonth = new Date (y, selM, 1);
	var startPos = firstOfMonth.getDay() + 1;
	var retVal = new Object();
	retVal[1] = startPos;
	retVal[2] = d;
	retVal[3] = m;
	retVal[4] = y;
	return (retVal);

}


function BuildLuachHTML(parms)  {
	var hdrSize = "+2";				// size of tables header font
	var border = 0;					// 3D height of table's border
	var cellspacing = 1;			// width of table's border
	var hdrHeight = 50;				// height of the table's header cell
	var dayCellHeight = 25;			// height of cells containing days of the week
	var dayColor = "darkred";		// color of font representing week days
	var hebColor = "darkblue";		// color of font representing Heb date
	var civColor = "black";		// color of font representing Civ date
	var cellWidth  = 66;			// width of columns in table
	var cellHeight = 50;			// height of day cells calendar
	var hebDate;
	var hebDay;
	var now = new Date();
	var tday = now.getDate();
	var tmonth = now.getMonth();
	var tyear = now.getYear();
	if(tyear < 1000)
		tyear += 1900;
	var cMonth = parms[3];
	var cYear = parms[4];
	var monthName = civMonth[cMonth];
	var lastDate = civMonthLength(cMonth, cYear);
	var hm;
	var hMonth;
	var hYear;


	// get starting Heb month in civil month
	hebDate = civ2heb(1, cMonth, cYear);
	hmS = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
	hMonth = eval(hmS.substring(0, hmS.indexOf(' ')));
	hYear = hmS.substring(hmS.indexOf(' ')+1, hmS.length);
	var start = hebMonth[hMonth+1] + ' ' + hYear;


	// get ending Heb month in civil month
	hebDate = civ2heb(lastDate, cMonth, cYear);
	hmE = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
	hMonth = eval(hmE.substring(0, hmE.indexOf(' ')));
	hYear = hmE.substring(hmE.indexOf(' ')+1, hmE.length);
	var end = hebMonth[hMonth+1] + ' ' + hYear;


	var hebSpan;
	// check if start and end Heb months are the same
	if(hmS == hmE)
		hebSpan = start;
	else
		hebSpan = start + ' / ' + end


    var result = '<html>\n';
	result += '<HEAD><TITLE>Calendrier Civil/Hebraïque</TITLE>\n<LINK REL="Stylesheet" TYPE="text/css" HREF="style/style2.css">\n</HEAD>\n';
	result += '<BODY topmargin="0">\n';
    result += '<center>\n';


	// set up our table structure
	result += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + '>';		// table settings
	result +=	 '<TH COLSPAN=7 HEIGHT=' + hdrHeight + ' align="center">';		// create table header cell
	result +=		'<FONT class="civtxt">';	// set font for table header
	result +=			monthName + ' ' + cYear;
	result +=		'</FONT>';		// close table header's font settings
	result +=		'<FONT class="hebtxt">'; // set font for table header
	result +=			'&nbsp&nbsp ' + hebSpan;
	result +=		'</FONT>'; // close table header's font settings
	result +=	 '</TH>'; // close header cell


	// variables to hold constant settings
	var openCol = '<TD WIDTH=' + cellWidth + ' HEIGHT=' + dayCellHeight + ' class="titleCal">';
//	openCol += '<FONT face="Arial" COLOR="' + dayColor + '">';
	var closeCol = '</TD>';


	// create first row of table to set column width and specify week day
	result += '<TR ALIGN="center" VALIGN="center">';
	for (var dayNum = 1; dayNum < 8; ++dayNum) {
	        result += openCol + weekDay[dayNum] + closeCol;
	}
	result += '</TR>';
 
    var cell = 1
    var cDay = 1
    var row;
    for (row = 1; row <= 6; row++) {
        result+='<TR VALIGN="top">'
        for (col = 1; col <= 7; col++)  {


			// convert civil date to hebrew
			hebDate = civ2heb(cDay, cMonth, cYear);
			hebDay = eval(hebDate.substring(0, hebDate.indexOf(' ')));


			var hm = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
			var hMonth = eval(hm.substring(0, hm.indexOf(' ')));


	        if (cell < parms[1])
				result += '<TD></TD>';
	        else {


				var moed = "";
				if(jewishHolidays)
					moed = moadim(cDay, cMonth, cYear, hebDay, hMonth, col);
				var holiday = "";
				if(civilHolidays)
					holiday = holidays(cDay, cMonth, cYear);


				var bg;
		        if((cDay == tday) && (parms[3] == (tmonth+1)) && (parms[4] == tyear))
					// highight the current day
					bg = 'bgColor=#e0f0ff'
				else if (moed != "")
					// highlight Heb holiday
					bg = 'bgColor=#c0d0f0'
				else if (holiday != "") {
					// highlight civil holiday
					bg = 'bgColor=#d0c0c0'
//					moed = holiday;
				}
				else
					// no highlight
					bg = '';


				// assemble the contents of our day cell
//				if(col == 7)
					result += '<TD class="cel" HEIGHT=' + cellHeight + ' align="center" ' + bg + ' style="CURSOR:Hand" onMouseOver=\'this.style.backgroundColor="#dddddd"\' onMouseOut=\'self.status="";this.style.backgroundColor = "";return;\' >';
	//			else
		//			result += '<TD HEIGHT=' + cellHeight + ' align="center" ' + bg + '>';
				result +=   '<table BORDER=0 COLS=2 WIDTH=' + cellWidth + 'HEIGHT=' + cellHeight + ' >';
				result +=     '<tr>';

				result +=       '<td>';
				result +=         '<FONT class="civtxt">';
				result +=           cDay;
				result +=         '</font>';
				result +=       '</td>';
				result +=       '<td>';
				result +=         '<FONT class="hebtxt">';
				result +=           '<div align=right>' + hebDay + '</div>';
				result +=         '</FONT>';
				result +=       '</td>';
				result +=     '</tr>';
				result +=     '<tr>';
				result +=       '<td colspan="2">';
//				result +=   '<FONT face="Arial" size=-2>'

				if (moed != "")
					result += '<FONT class="hebholiday">' + moed + '</FONT>';
				if (moed != "" && holiday != "")
					result += '<br>';
				if (holiday != "")
					result += '<FONT class="civholiday">' + holiday + '</FONT>';
//				result +=   '</FONT>'
				result +=       '</td>';
				result +=     '</tr>';
				result +=   '</table>';
				result += '</TD>';


		        cDay++
            }


            if (cDay <= lastDate)
                cell++
            else
                break;
        }


        result += '</TR>'
        if(cDay > parms[2])
                break;
    }


    result += '</table>'
    result += '</center></body></html>'
    return result;
}



