/* kzman.js - Kaluach halachic times Javascript routines
 *   Version 2.01 (handle invalid sunrise/set, different knissat shabbat times)
 * Copyright (C) 5760,5761 (2000 CE), by Abu Mami and Yisrael Hersch.
 *   All Rights Reserved.
 *   All copyright notices in this script must be left intact.
 *	Permission will be granted to use this script on your web page
 *   if you wish. All that's required is that you please ask.
 *   (Of course if you want to send a few dollars, that's OK too :-)
 * website: http://www.kaluach.net
 * email: abumami@kaluach.org
* שינויים, שיפורים ותרגום חלקי לעברית - יהונתן סלע
 */
var month = 0, day = 0, year = 0;
var lat = 0, lng = 0;	// sun's location
var latd = -1, latm = 0;// lat on earth
var lngd = -1, lngm = 0;// long on earth
var ns = 'N', ew = 'E';	// hemisphere
var dst = 0;			// daylight saving time = שעון קיץ
var ampm = 0;			// am/pm or 24 hour display

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(
	'בניסן', 'באייר', 'בסיון', 'בתמוז', 'באב', 'באלול',
	'בתשרי', 'בחשוון', 'בכסלו', 'בטבת', 'בשבט',
	'באדר', 'באדר א', 'באדר ב');

var civMonth = new makeArray(
	'January', 'February', 'March', 'April', 'May', 'June',
	'July', 'August', 'September', 'October', 'November', 'December');

var weekDay = new makeArray(
	'Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Shabbat');

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 moadim(cday, cmonth, cyear, hday, hmonth, dow) {
	if(hmonth == 6) {
		if(hday == 1 || hday == 2)
			return "ראש השנה"
		else if(hday == 3 && dow != 7)
			return "צום גדליה";
		else if(hday == 4 && dow == 1)
			return "צום גדליה";
		else if(hday == 10)
			return "יום כיפור"
		else if(hday >= 15 && hday <= 22)
			return "סוכות"
		//else if(hday == 23)
		//	return "Sukkot (d)"
	}
	else if(hmonth == 8) {
		if(hday >= 25)
			return "חנוכה"
	}
	else if(hmonth == 9) {
		if(hday <= 2) {
			return "חנוכה"
		}
		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 "חנוכה"
		}
		else if(hday == 10)
			return "עשרה בטבת"
	}
	else if(hmonth == 10) {
		if(hday==15)
			return "ט''ו בשבט"
	}
	else if(hmonth == 11 || hmonth == 13) {
		if(hday == 11 && dow == 5)
			return "תענית אסתר"
		else if(hday == 13 && dow != 7)
			return "תענית אסתר"
		else if(hday == 14)
			return "פורים"
		else if(hday == 15)
			return "שושן פורים"
	}
	else if(hmonth == 0) {

		if(hday == 12 && dow == 5)
			return "תענית בכורות"
		else if(hday == 14 && dow != 7)
			return "תענית בכורות"
		else if(hday >= 15 && hday <= 21)
			return "פסח"
		//else if(hday == 22)
		//	return "Pesach (d)"
	}
	else if(hmonth == 1) {
		if(hday == 3 && dow == 5)
			return "יום העצמאות"
		else if(hday == 4 && dow == 5)
			return "יום העצמאות"
		else if(hday == 5 && dow != 6 && dow != 7)
			return "יום העצמאות"
		if(hday == 14)
			return "פסח שני"
		else if(hday == 18)
			return "ל''ג בעומר"
		if(hday == 28)
			return "יום ירושלים"
	}
	else if(hmonth == 2) {
		if(hday == 6)
			return "שבועות"
		//else if(hday == 7)
		//	return "Shavuot (d)"
	}
	else if(hmonth == 3) {
		if(hday == 17 && dow != 7)
			return "י''ז בתמוז"
		if(hday == 18 && dow == 1)
			return "צום הרביעי" //שם נרדף ליז בתמוז
	}
	else if(hmonth == 4) {
		if(hday == 9 && dow != 7)
			return "תשעה באב"
		if(hday == 10 && dow == 1)
			return "תשעה באב"
		if(hday == 15)
			return "ט''ו באב"
	}

	return "";
}

function ToGim (num) //makes a GIMATRIA of the year
{
	var HebOnes = new makeArray(
		'', 'א', 'ב', 'ג', 'ד', 'ה', 'ו',
		'ז', 'ח', 'ט');
	var HebTens = new makeArray(
		'', 'י', 'כ', 'ל', 'מ', 'נ', 'ס',
		'ע', 'פ', 'צ');
	var HebHunds = new makeArray(
		'', 'ק', 'ר', 'ש', 'ת', 'תק', 'תר',
		'תש', 'תת', 'תתק');
	var thousands = Math.floor (num/1000);
	var hundreds = Math.floor ((num - (1000 * thousands))/100);
	var tens = Math.floor ((num - (1000 * thousands) - (100 * hundreds))/10);
	var ones = num - (1000 * thousands) - (100 * hundreds) - (10 * tens);
	if ((tens == 1) && ((ones == 5) || (ones == 6)))
	{
		if (thousands > 0)
			return (HebOnes[thousands+1] + "'" + HebHunds[hundreds+1] + "ט" + HebOnes [ones+2]);
		else
			return (HebHunds[hundreds+1] + "ט" + HebOnes [ones+2])
	}
	else	
		if (thousands > 0)
			return (HebOnes[thousands+1] + "'" + HebHunds[hundreds+1] + HebTens [tens+1] + HebOnes [ones+1]);
		else
			return (HebHunds[hundreds+1] + HebTens [tens+1] + HebOnes [ones+1])
	
}
function dow_Name(dow)
{
	var days = new makeArray(
		'יום ראשון', 'יום שני', 'יום שלישי', 'יום רביעי', 'יום חמישי', 'יום שישי',
		'שבת קודש');
	return (days[dow])
}
function hebrew_date(d,m,y)
{	var now = new Date;
	var tday = d + 1;
	var tmonth = m + 1;
	var tyear = y;
	var hebDate = civ2heb(d, m, y);
	var hmS = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
	var hDay = eval(hebDate.substring(0, hebDate.indexOf(' ')));
	var hMonth = eval(hmS.substring(0, hmS.indexOf(' ')));
	var hYear = hmS.substring(hmS.indexOf(' ')+1, hmS.length);
	var hYearHeb = ToGim (hYear);
	var hDayHeb = ToGim (hDay);
	var yom = new Date (y, m-1, d);
	var dow = yom.getDay() + 1;
	return (dow_Name(dow) + ' ,' + hDayHeb + ' ' + hebMonth[hMonth+1] + ' ' + hYearHeb + '.    ' + moadim (d,m,y,hDay,hMonth,dow))
}

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 set_ampm(val) {
	ampm = val;
	doit("");
}

function set_dst() {
	dst = document.data.dst.checked;
	doit("");
}

function change_year(num) {
	var y = parseInt(document.data.year.value);
	y += num;
	document.data.year.value = y;
	year = y;
	date_vars_doit();
}

function list_pos(w) {
	var str, place, desc
	var i;

	i = w.options.selectedIndex;
	with(document.data) { // reset all prior selections
		israel_city.options[0].selected = 1;
		//diaspora_city.options[0].selected = 1;
	}
	w.options[i].selected = 1; // restore current selection
	with (w) {
		desc = options[0].text;
		str = options[options.selectedIndex].value;
		place = options[options.selectedIndex].text;
		if(i == 0)
			document.data.placename.value = '';
	}

	i = str.indexOf(",");
	ns = str.substring(0, i);
	str = str.substring(i+1, str.length);

	i = str.indexOf(",");
	latd = eval(str.substring(0, i));
	str = str.substring(i+1, str.length);

	i = str.indexOf(",");
	latm = eval(str.substring(0, i));
	str = str.substring(i+1, str.length);

	i = str.indexOf(",");
	ew = str.substring(0, i);
	str = str.substring(i+1, str.length);

	i = str.indexOf(",");
	lngd = eval(str.substring(0, i));
	str = str.substring(i+1, str.length);

	i = str.indexOf(",");
	lngm = eval(str.substring(0, i));

	var tz = eval(str.substring(i+1, str.length));

	if((latd != -1) && (lngd != -1)) {
		document.data.tz.options[12+tz].selected = 1;
		doit(place);
	}

}

function man_pos() {

	latd = Math.abs(eval(document.data.latd.value));
	latm = Math.abs(eval(document.data.latm.value));
	ns = (document.data.lats[1].checked) ? 'S' : 'N';

	lngd = Math.abs(eval(document.data.lngd.value));
	lngm = Math.abs(eval(document.data.lngm.value));
	ew = (document.data.lngs[1].checked) ? 'E' : 'W';

	var tz = - (12 - document.data.tz.options.selectedIndex);
	document.data.tz.options[12+tz].selected = 1;
	doit("(manual entry)");
	return 1;
}

function doit(title) {

	var d, m, y;
	var nsi, ewi;
	var i;
 
	if(title != "")
		document.data.placename.value = title;
 
	document.data.latd.value = latd;
	document.data.latm.value = latm;
	i = ns.indexOf("N");
	nsi = (i != -1) ? 0 : 1;
	document.data.lats[nsi].checked = 1;
 
	document.data.lngd.value = lngd;
	document.data.lngm.value = lngm;
	i = ew.indexOf("W");
	ewi = (i != -1) ? 0 : 1;
	document.data.lngs[ewi].checked = 1;
 
	d = day + 1;
	m = month + 1;
	y = year;
 
	var adj = - (12 - document.data.tz.options.selectedIndex);
	adj += dst;

	var time;
	var sunrise, sunset;
	var shaa_zmanit;

	time = suntime(d, m, y, 90, 50, lngd, lngm, ewi, latd, latm, nsi, adj);
	if(time[1] == 0) {
		sunrise = time[2];
		sunset  = time[3];
		if(document.data.placename.value == "גוש עציון")
			document.data.hanetz.value = timeadj(sunrise - 1.0/60.0, ampm);
		else
			document.data.hanetz.value = timeadj(sunrise, ampm);
		if(document.data.placename.value == "גוש עציון")
			document.data.shkia.value = timeadj(sunset + 2.0/60.0, ampm);
		else
			document.data.shkia.value = timeadj(sunset, ampm);
		shaa_zmanit = (sunset - sunrise) / 12;
	}
	else {
		document.data.hanetz.value = "";
		document.data.shkia.value = "";
	}
	//time = suntime(d, m, y, 106, 6, lngd, lngm, ewi, latd, latm, nsi, adj);
	time = suntime(d, m, y, 109, 23, lngd, lngm, ewi, latd, latm, nsi, adj); //אני הקדמתי את זה כדי להקדים את עלות השחר לזמן הכתוב בלוחות
	if(time[1] == 0)
		if(document.data.placename.value == "גוש עציון")
			document.data.alot.value = timeadj(time[2] - 1.0/60.0, ampm);
		else
			document.data.alot.value = timeadj(time[2], ampm);
	else
		document.data.alot.value = "";

	time = suntime(d, m, y, 101, 0, lngd, lngm, ewi, latd, latm, nsi, adj);
	if(time[1] == 0)
		if(document.data.placename.value == "גוש עציון")
			document.data.misheyakir.value = timeadj(time[2] - 1.0/60.0, ampm);
		else
			document.data.misheyakir.value = timeadj(time[2], ampm);
	else
		document.data.misheyakir.value = "";

	time = suntime(d, m, y, 96, 0, lngd, lngm, ewi, latd, latm, nsi, adj);
	if(time[1] == 0)
		document.data.tzeit.value = timeadj(time[3], ampm);
	else	
		document.data.tzeit.value = "";

	document.data.shema.value    = timeadj(sunrise + shaa_zmanit * 3, ampm);
	document.data.tefillah.value = timeadj(sunrise + shaa_zmanit * 4, ampm);
	document.data.chatzot.value  = timeadj(sunrise + shaa_zmanit * 6, ampm);
	document.data.minchag.value  = timeadj(sunrise + shaa_zmanit * 6.5, ampm);
	document.data.minchak.value  = timeadj(sunrise + shaa_zmanit * 9.5, ampm);
	document.data.plag.value     = timeadj(sunrise + shaa_zmanit * 10.75, ampm);
		
	var yom = new Date (y, m-1, d);
	if(yom.getDay() == 6) {

		// motzei shabbat (3 small stars)
		time = suntime(d, m, y, 98, 30, lngd, lngm, ewi, latd, latm, nsi, adj);
		if(time[1] == 0)
			if(document.data.placename.value == "גוש עציון")
				document.data.motzeiShabbat.value = timeadj(time[3] + 2.0/60.0, ampm);
			else
				document.data.motzeiShabbat.value = timeadj(time[3], ampm);
		else
			document.data.motzeiShabbat.value = "";

		// knissat shabbat (sunset from day before)
		var day_before = new Date(yom.getTime() - 86400000);
		db = day_before.getDate();
		mb = day_before.getMonth() + 1;
		yb = day_before.getYear();
		if(yb < 1900)
			yb += 1900;
		time = suntime(db, mb, yb, 90, 50, lngd, lngm, ewi, latd, latm, nsi, adj);
		if(document.data.placename.value == "ירושלים")
			document.data.knissatShabbat.value = timeadj(time[3] - 40.0/60.0, ampm);
		else if(document.data.placename.value == "חיפה")
			document.data.knissatShabbat.value = timeadj(time[3] - 30.0/60.0, ampm);
		else if(document.data.placename.value == "באר שבע")
			document.data.knissatShabbat.value = timeadj(time[3] - 30.0/60.0, ampm);
		else if(document.data.placename.value == "קרני שומרון")
			document.data.knissatShabbat.value = timeadj(time[3] - 22.0/60.0, ampm);
		else if(document.data.placename.value == "תל אביב")
			document.data.knissatShabbat.value = timeadj(time[3] - 22.0/60.0, ampm);
		else
			document.data.knissatShabbat.value = timeadj(time[3] - 18.0/60.0, ampm);
	}
	else {
		document.data.motzeiShabbat.value = '';
		document.data.knissatShabbat.value = '';
	}

document.data.hebrdate.value     = hebrew_date(parseInt(d), parseInt(m), parseInt(y));
}

function set_date_vars() {
	month = document.data.month.selectedIndex;
	day   = document.data.day.selectedIndex;
	year  = document.data.year.value;

	var len = civMonthLength(month+1, year);
	if(day >= len) {
		day = len - 1;
		document.data.day.selectedIndex = day;
	}
}

function date_vars_doit() {
	set_date_vars();
	doit("");
	
}

function set_default_date() {
	var now = new Date();
	var d = now.getDate();
	var m = now.getMonth();
	year = now.getYear();
	if(year < 1900)
		year += 1900;
	document.data.month.selectedIndex = m;
	document.data.day.selectedIndex = d - 1;
	document.data.year.value = year;
	set_date_vars("");
}
