var otherHolidays = 0;
var jewishHolidays = 1;
var civilHolidays = 1;

var now = new Date();
var m = now.getMonth();
var y = now.getYear();
if (y < 1000) y+=1900;

function updateForm() {
	document.calendar_control.month.selectedIndex = m;
	
	var after = m+2;
	if (after > 12) after = 1;
	
	var before = m;
	if (before < 1) before = 12;
	
	after_string = "<img class=\"right\" src=\"/images/buttons/btn_arrow_right_sm.gif\" alt=\"&gt;\" /> " + civMonth[after];
	before_string = "<img class=\"left\" src=\"/images/buttons/btn_arrow_left_sm.gif\" alt=\"&lt;\" /> " + civMonth[before];;
	
	var after  = document.getElementById("btn_after");
	var before = document.getElementById("btn_before");
	
	if (after != null)
		after.innerHTML = after_string;
	if (before != null)
		before.innerHTML = before_string;
}

function selectLoad() {
	//document.calendar_control.month.selectedIndex = m;
	doCal(m, y);
	updateForm();
}

function toggleOther(form) {
	otherHolidays = (otherHolidays == 1) ? 0 : 1;
	doCal(m, y);
}

function selectNext() {
	if (m < 11) m++;
	else { m = 0; y++;}
	doCal(m, y);
	updateForm();
}

function selectPrev() {
	if (m > 0) m -= 1;
	else {m = 11; y -= 1;}

	doCal(m, y);
	updateForm();
}

function selectForm(form) {
	m = form.month.selectedIndex;
	updateForm();
	doCal(m, y);
}

function selectHolidays(form) {
	jewishHolidays = 1;
	civilHolidays = 1;

	doCal(m, y);
}

function doCal(month, year) {
	var ret = calendar(month, year);
	var result = BuildLuachHTML(ret);
	var divbox = document.getElementById("calendar");
	divbox.innerHTML = result;
}

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 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 = '';

	result += '<table id="calTable" class="intCal">';
	result += '   <tr>';
	result += '    <th class="month_name" colspan="7"><span class="bkgdMnth">' + monthName + ' ' + cYear + '</span><span class="mainMnth">' + monthName + ' ' + cYear + '</span><br />' + hebSpan + '</th>';
	result += '   </tr>';

	// variables to hold constant settings
	var openCol = '<td class="day">';
	var closeCol = '</td>';
	var dowOpenCol = '<td class="dow">';
	
	// create first row of table to set column width and specify week day
	result += '<tr class="week">';
	for (var dayNum = 1; dayNum < 8; ++dayNum) {
	        result += dowOpenCol + weekDay[dayNum] + closeCol;
	}
	result += '</tr>';
 
    var cell = 1
    var cDay = 1
    var row;
    for (row = 1; row <= 6; row++) {
        result+='<tr class="week">'
        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 class="reg_day"></td>';
	        else {

				var moed = "";
				if(jewishHolidays)
					moed = moadim(cDay, cMonth, cYear, hebDay, hMonth, col);
				var holiday = "";
				if(civilHolidays)
					holiday = holidays(cDay, cMonth, cYear);

				var day_class;
		        if((cDay == tday) && (parms[3] == (tmonth+1)) && (parms[4] == tyear))
					// highight the current day
					day_class = 'current_day';
				else if (moed != "" || col == 7)
					// highlight Heb holiday
					day_class = 'heb_holiday';
				else if (holiday != "") {
					// highlight civil holiday
					day_class = 'civ_holiday';
				}
				else
					// no highlight
					day_class = 'reg_day';

				
					
				// assemble the contents of our day cell
				result += '<td>';

				result += '<div class="holiday_name">';
				if (moed != "")
					result += '<span class="heb_name">' + moed + "</span>";
				if (moed != "" && holiday != "")
					result += '</div>';
				if (holiday != "")
					result += '<span class="civ_name">' + holiday + "</span>";
				result += '</div>';
				result +=     '<div class="date '+day_class+'">' + cDay + '</div>';


		        cDay++
            }

            if (cDay <= lastDate)
                cell++
            else
                break;
        }

        result += '</tr>'
        if(cDay > parms[2])
                break;
    }

    result += '</table>'
    return result;
}

window.onload = selectLoad;
