/* KaluachJS - Kaluach Javascript Hebrew/civil calendar
 *   Version 2.00 (beta 001)
 * Copyright (C) 5760 - 5766 (2000 - 2006 CE), by Abu Mami.
 *   All Rights Reserved.
 *   All copyright notices in this script must be left intact.
 * Requires kdate.js - Kaluach Javascript Hebrew date routines
 * Acknowledgment given to scripts by:
 *	 - Tomer and Yehuda Shiran (docjs.com)
 *   - Gordon McComb
 *   - John Hinshaw (Smart Business Calendar)
 *   - irt.org
 *   - javascripter.net
 * Terms of use:
 *   - Permission will be granted to use this script on personal
 *     or non-profit web pages. All that's required is that you
 *     please ask. (Of course if you want to send a few dollars,
 *     that's OK too :-)
 *   - Use on commercial web sites requires a $50 payment.
 * website: http://www.kaluach.org
 */

window.onerror = null

//***************************************
// COLORS

// setup the user variables
var showPrevNext = true	// option to show prev/next buttons

// font colors
var hebHolidayColor = 'black'
var civHolidayColor = 'red'
var hebColor = 'blue';		// color of font representing Heb date
var civColor = 'green';		// color of font representing Civ date

// background colors
var civBG = '#f0e0e0'
var	hebBG = '#A9C7EF'
var selBG = '#FFCC99'		// background color for selected day's cell
var dayBG = 'beige'
var zmnBG = '#e0f0d0'
var bgcolor = "#FFFFFF"		//set this color to match the 'body' background color above
var hdColor = "darkgreen"	// month and days of week color

var selColor = hdColor;	// border color for selected day's cell

//********************************************************************************************
// DATE VARIABLES

var now
var tDay
var year
var month
var date
var nMonth
var today
var selected

var nxtMonth
var nxtYear
var nxtMonthBtn
var nxtYearBtn
var nxtMonthStat
var nxtYearStat

var prvMonth
var prvYear
var prvMonthBtn
var prvYearBtn
var prvMonthStat
var prvYearStat

var holidaysSelection
var displayZmanim


//********************************************************************************************
// INITIALIZE

displayZmanim = true
holidaysSelection = 0;	// jewish (0-jewish, 1-civil, 2-all, 3-none)
now = new Date()
tDay = now.getDate()
year = now.getFullYear()
month = now.getMonth()
nMonth = civMonth[month+1]
today = new Date(nMonth  + " " + tDay + ", " + year)
selected = today

function selectHolidays(form) {
	onError=null
	holidaysSelection = form.holidaysSelect.selectedIndex;
	defineLuach()
}

function jumpToday() {
	tDay = now.getDate()
	year = now.getFullYear()
	month = now.getMonth()
	nMonth = civMonth[month+1]
	today = new Date(nMonth  + " " + tDay + ", " + year)
	selected = today
	defineLuach()
}

function showHebrewDay(d, m, y, cd, cm, cy) {
	self.status = y + " " + hebMonth[m+1] + " " + d + ". (" + cy + " " + civMonth[cm] + " " + cd + ".)";
}

function nextMonth() {
	onError=null
	month = month + 1
	if (month == 12) {
		year = year + 1
		month = 0
	}
	nMonth = civMonth[month]
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days
	chgSelected(tDay, month+1, year);
}
	
function prevMonth() {
	onError=null
	month = month - 1 
	if (month == -1) {
		year = year - 1
		month = 11
	}
	nMonth = civMonth[month]
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days
	chgSelected(tDay, month+1, year);
}
	
function nextYear() {
	onError=null
	year = year + 1
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days
	chgSelected(tDay, month+1, year);
}
	
function prevYear() {
	onError=null
	year = year - 1
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days
	chgSelected(tDay, month+1, year);
}


function define() {
	// set up next buttons
	nxtYearStat = "Következő év - " + (year + 1) + "  " + civMonth[month+1]
	nxtYear = year
	nxtMonth = month + 1
	if (nxtMonth == 12) {
		nxtYear = nxtYear + 1
		nxtMonth = 0
	}
	nxtMonthStat = "Következő hónap - " + nxtYear + "  " + civMonth[nxtMonth+1]
	nxtMonthBtn  = "<a href='javascript: nextMonth()' onMouseOver='self.status=nxtMonthStat;return true' onMouseOut='self.status=\"\";return true'><img src='single-right.gif' width='19' height='25' align='middle' alt='Következő hónap'></a>"
	nxtYearBtn   = "<a href='javascript: nextYear()' onMouseOver='self.status=nxtYearStat;return true' onMouseOut='self.status=\"\";return true'><img src='double-right.gif' width='19' height='25' align='middle' alt='Következő év'></a>"

	// set up previous buttons
	prvYearStat = "Előző év - " + (year - 1) + "  " + civMonth[month+1]
	prvYear = year
	prvMonth = month - 1
	if (prvMonth == -1) {
		prvYear = prvYear - 1
		prvMonth = 11
	}
	prvMonthStat = "Előző hónap - " + prvYear + "  " + civMonth[prvMonth+1]
	prvMonthBtn  = "<a href='javascript: prevMonth()' onMouseOver='self.status=prvMonthStat;return true' onMouseOut='self.status=\"\";return true'><img src='single-left.gif' width='19' height='25' align='middle' alt='Előző hónap'></a>"
	prvYearBtn   = "<a href='javascript: prevYear()' onMouseOver='self.status=prvYearStat;return true' onMouseOut='self.status=\"\";return true'><img src='double-left.gif' width='19' height='25' align='middle' alt='Előző év'></a>"

}

	
function defineLuach() {
	define()

	// find out what day of the week is the first day of the month
	var fDayMonth = new Date(year, month, 1)
	var dayFirst  = fDayMonth.getDay()
	fDayMonth = null

	// number of days in current month
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days

	// draw the calendar
	buildLuach(dayFirst + 1, days, tDay, month, year)
}

function chgSelected(d, m, y) {
	month = m - 1;
	year = y
	tDay = d
	nMonth = civMonth[month+1]
	selected = new Date(nMonth  + " " + tDay + ", " + year)

	define()

	// find out what day of the week is the first day of the month
	var fDayMonth = new Date(year, month, 1)
	var dayFirst  = fDayMonth.getDay()
	fDayMonth = null

	// number of days in current month
	var days = civMonthLength(month+1, year)
	if(tDay > days)
		tDay = days

	// draw the calendar
	buildLuach(dayFirst + 1, days, tDay, month, year)
}

function restart() {
	defineLuach()
    mywindow.close();
}

function locationWindow() {
    mywindow=open('loc.htm','locwin','resizable=no,width=600,height=200,screenX=200,screenY=200,top=200,left=200');
	mywindow.location.href = 'loc.htm';
    if (mywindow.opener == null)
		mywindow.opener = self;
}

function buildLuach(dayFirst, dayLast, date, month, year) {
	var hebDate
	var hMonth;
	var hYear;

	var selDay		= date;
	var selMonth	= month;
	var	selYear		= year;

	month++;

	// get starting Heb month in civil month
	hebDate = civ2heb(1, month, year);
	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 = hYear + ' ' + hebMonth[hMonth+1];

	// get ending Heb month in civil month
	hebDate = civ2heb(dayLast, month, year);
	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 = hYear + ' ' + hebMonth[hMonth+1];

	var hebSpan;
	// check if start and end Heb months are the same
	if(hmS == hmE)
		hebSpan = start;
	else
		hebSpan = start + ' / ' + end

	var nMonth = civMonth[month]

	if (ver4 && showPrevNext)
		showPrevNext = true
	else
		showPrevNext = false
	
	var text = "" 
	
	
	
    text += "<a href='javascript: jumpToday()' onMouseOver='self.status=\"Jelenlegi hónap... (" + today.getYear() + " " + civMonth[today.getMonth()+1] + " " + today.getDate() + ".)\";return true' onMouseOut='self.status=\"\";return true'><img src='today.gif' width='33' height='32' align='middle' alt='Jelenlegi hónap'></a>&nbsp"
	
	
	text += '<center><form name="luachform">'
	text += '<table width="98%" cellspacing=4>' // table settings

	text += '<tr>'
	
	text += '<td colspan="1" align="center">'
	if (showPrevNext == true)
		text +=  prvYearBtn
	text += ' '
	if (showPrevNext == true)
		text +=  prvMonthBtn

	text += '</td>'


	text += '<td colspan="5" align="center">'


	// display the civil header
	text += '<b><font size="3" font color=' + civColor + ' face="sans-serif, verdana, helvetica, arial">' // set font for table header
	text +=  year + ' ' + nMonth 
	text += '</font><b>'

	// display the Hebrew header (spanning months)
	text += '<b><font size="3" font color=' + hebColor + ' face="sans-serif, verdana, helvetica, arial">' // set font for table header
	text +=	'&nbsp&nbsp ' + hebSpan;
	text += '</font><b>'

	text += '</td>'

	text += '<td colspan="1" align="center">'
	if (showPrevNext == true)
		text += nxtMonthBtn
	text += ' '
	if (showPrevNext == true)
		text += nxtYearBtn
	text += '</td>'
	text += '</tr>'



	// end of prev and next buttons

	// setup the column headers
	text += '<tr align="center" valign="center"><font face="sans-serif, verdana, helvetica, arial" color=' + dayBG + '>'
	for (var dayNum = 0; dayNum < 7; ++dayNum) {
		text += '<td width="14%" height="2%" align="center" valign="middle">'
		text += '<font size="2" face="sans-serif, verdana, helvetica, arial" color=' + hdColor + '><b>'
		text += weekDay[dayNum+1]
		text += '</b></font></td>'
	}
	text += '</font></tr></table>'
	text += '<table cols=7 width="98%" height="20%" border="3" bordercolor=' + bgcolor + ' cellspacing=4>'
	var dayNumber = 1
	var thisDOW = 1
	
	for (var row = 1; row <= 6; ++row) {
		var bg;
		bg = dayBG		//'beige'
		text += '<tr border=3 align="left" valign="top">'

		for (var col = 1; col <= 7; ++col) {

			// convert civil date to hebrew
			hebDate = civ2heb(dayNumber, month, year);
			var hebDay = eval(hebDate.substring(0, hebDate.indexOf(' ')));
			var hm = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
			var hMonth = eval(hm.substring(0, hm.indexOf(' ')));
				hYear  = hm.substring(hm.indexOf(' ')+1, hm.length);

			if (dayNumber > dayLast) {
				if(row == 6 && col > 2)
					break
				text += '<td width="14%" height="14%"></td>'
				continue;
			}
			if (thisDOW < dayFirst) {
				text += '<td></td>';
				thisDOW++
			}
			else {

				var moed = "";
				if(holidaysSelection == 0 || holidaysSelection == 2)
					moed = moadim(dayNumber, month, year, hebDay, hMonth, col);
				var holiday = "";
				if(holidaysSelection == 1 || holidaysSelection == 2)
					holiday = holidays(dayNumber, month, year);

				var currDate = new Date(nMonth + " " + dayNumber + ", " + year)

				if (moed != "")
					// highlight Heb holiday
					bg = hebBG
				else if (holiday != "")
					// highlight civil holiday
					bg = civBG
				else
					bg = dayBG

				var frameColor;
				if (currDate.toString() == selected.toString())
					frameColor = selColor;
				else
					frameColor = hdColor;

				if(NS4)
					text += '<td width="14%" height="14%" bgcolor=' + bg + ' bordercolor=' + frameColor + '>'
				else
					text += '<td width="14%" height="14%" bgcolor=' + bg + ' bordercolor=' + frameColor + ' style="CURSOR:Hand" onClick="chgSelected(' + dayNumber + ',' + month + ',' + year + ') " onMouseOver="showHebrewDay(' + hebDay + ',' + hMonth + ',' + hYear + ',' + dayNumber + ',' + month + ',' + year + '); return true"' + "onMouseOut='self.status=\"\";return true'>"

				text   +=   '<table border=0 cols=2 width="100%" height="20%">';
				text   +=     '<tr>';
				text   +=       '<td>';
				text   +=         '<font size=1 face="sans-serif, verdana, helvetica, arial">'
				if (currDate.toString() == selected.toString())
					text += '<font size="-2" color=' + civColor + '>'
				else
					text += '<font size="-2" color=' + civColor + '>'
				if(NS4)
					text   += '<A HREF="#" style="CURSOR:Hand" onClick="chgSelected(' + dayNumber + ',' + month + ',' + year + ') " onMouseOver="showHebrewDay(' + hebDay + ',' + hMonth + ',' + hYear + ',' + dayNumber + ',' + month + ',' + year + '); return true"' + "onMouseOut='self.status=\"\";return true'>"
				text   +=           dayNumber;
				if(NS4)
					text   += '</a>'
				text   +=         '</font></font>';
				text   +=       '</td>';
				text   +=       '<td>';
				text   +=         '<font size=1 face="sans-serif, verdana, helvetica, arial">'
				if (currDate.toString() == selected.toString())
					text += '<font size="4" color=' + hebColor + '>'
				else
					text += '<font size="4" color=' + hebColor + '>'
				if(NS4)
					text   += '<A HREF="#" style="CURSOR:Hand" onClick="chgSelected(' + dayNumber + ',' + month + ',' + year + ') " onMouseOver="showHebrewDay(' + hebDay + ',' + hMonth + ',' + hYear + ',' + dayNumber + ',' + month + ',' + year + '); return true"' + "onMouseOut='self.status=\"\";return true'>"
				text   +=           '<div align=right>' + hebDay + '</div>';
				if(NS4)
					text   += '</a>'
				text   +=         '</font></font>';
				text   +=       '</td>';
				text   +=     '</tr>';
				text   +=   '</table>';

				text += '<font size=1 face="ms sans serif, sans-serif, verdana, helvetica, arial">'

				text   += '<center>'
				if (moed != "") {
					text += '<font color=' + hebHolidayColor + '>'
					text += moed;
					text += '</font>'
				}
				if (moed != "" && holiday != "")
					text += '<br>';
				if (holiday != "") {
					text += '<font color=' + civHolidayColor + '>'
					text += holiday;
					text += '</font>'
				}
				text   += '</center>'

				text += '</td>'
				dayNumber++
			}
		}


		text += '</tr>'
	}	

	text += '</table>'






	var whichEl
	if (NS4) {
		whichEl = document.layers.luach.document;
		whichEl.open();
		whichEl.write(text);
		whichEl.close();
	}
	else if (IE4) {
		whichEl = document.all.luach
		whichEl.innerHTML = text;
	}
	else
		document.write(text)
}

