function KaluachOut(){
	if(isOut(this))this.className='out'
};
function KaluachOver(){if(this.className!='over')this.className='over'};


var actionButton=null;
function calendarUp(){this.className='up'};
function calendarDown(){this.className='down'};
function calculatorUp(){this.className='up'};
function calculatorClick(){
	if(actionButton){
		var civil=actionButton.parentElement.parentElement.parentElement.all.Civil;
		if(actionButton.id=="incr")civil.innerHTML=(++year);
		else if(actionButton.id=="decr")civil.innerHTML=(--year);
		setNewYear(year)
	}
};
function calculatorDown(){
	this.className='down';
};


function buttonOver(){
	if(this.className!='over'){
		this.className='over';
		actionButton=this
	}
	return true
};
function buttonOut(){
	if(isOut(this)){
		this.className='out';
		actionButton=null
	}
	return true
};


var command=null;
function fixed(o){
	if(!o)o=this;
	if(o.id=='unfix'){
		if(command)command.id='unfix';
		command=o;
		o.id='fix';
		selectCel(o,o.parentElement.sectionRowIndex,o.cellIndex,o.date)
	}
};

function isOut(o){
	p=event.toElement;
	while(p&&o!=p)p=p.parentElement;
	return !p||o!=p;
}

document.documentElement.addBehavior('#default#userData');
function remove(id){document.documentElement.removeAttribute(id)}
function LoadData(id){
	var html=document.documentElement;
	html.load("Calendar");
	return html.getAttribute(id)
}
function SaveData(id,value){
	var html=document.documentElement;
	html.setAttribute(id,value);
	html.save("Calendar")
}
var monCount="1,32,60,91,121,152,182,213,244,274,305,335,366".split(",");
function doy(d,m,y){return parseInt(monCount[m-1])+d+(m>2&&((y%400==0)||(y%100!=0&&y%4==0)))}

var nDaysOfMon=new Array();
for(var m=1;m<monCount.length;m++)nDaysOfMon[nDaysOfMon.length]=parseInt(monCount[m])-parseInt(monCount[m-1]);

function suntime(
	dy, mn, yr,
	sundeg, sunmin,
	londeg, lonmin, ew,
	latdeg, latmin, ns,
	timezone){

	var invalid = 0;	// start out as OK

	longitude = (londeg + lonmin/60.0) * ((ew == 0) ? -1 : 1);
	latitude  = (latdeg + latmin/60.0) * ((ns == 0) ? 1 : -1);

	var yday = doy(dy, mn, yr);

	var A = 1.5708; 
	var B = 3.14159; 
	var C = 4.71239; 
	var D = 6.28319;      
	var E = 0.0174533 * latitude; 
	var F = 0.0174533 * longitude; 
	var G = 0.261799 * timezone; 

	var R = Math.cos(0.01745 * (sundeg + sunmin/60.0));

	var J;

	for (i = 0; i < 2; i++) { 

		if(!i)J =  A;
		else J = C;

		var K = yday + ((J - F) / D); 
		var L = (K * .017202) - .0574039;              // Solar Mean Anomoly 
		var M = L + .0334405 * Math.sin(L);            // Solar True Longitude 
		M += 4.93289 + (3.49066E-04) * Math.sin(2 * L); 
		
		// Quadrant Determination 
		if (D == 0) {
			alert("Trying to normalize with zero offset...");
			exit;
		} 

		while(M < 0)M = (M + D);
		while(M >= D)M = (M - D);

		if ((M / A) - Math.floor(M / A) == 0)M += 4.84814E-06;

		var P = Math.sin(M) / Math.cos(M);                   // Solar Right Ascension 
		P = Math.atan2(.91746 * P, 1); 

		// Quadrant Adjustment 
		if (M > C)P += D;
		else{if(M > A)P += B;} 

		var Q = .39782 * Math.sin(M);      // Solar Declination 
		Q = Q / Math.sqrt(-Q * Q + 1);     // This is how the original author wrote it! 
		Q = Math.atan2(Q, 1); 

		var S = R - (Math.sin(Q) * Math.sin(E)); 
		S = S / (Math.cos(Q) * Math.cos(E)); 

		if(Math.abs(S) > 1)invalid = 1;	// uh oh! no sunrise/sunset

		S = S / Math.sqrt(-S * S + 1); 
		S = A - Math.atan2(S, 1); 

		if(!i)
			S = D - S;	// sunrise

		var T = S + P - 0.0172028 * K - 1.73364;  // Local apparent time 
		var U = T - F;                            // Universal timer 
		var V = U + G;                            // Wall clock time 
		
		// Quadrant Determination 
		if(D == 0) {
			alert("Trying to normalize with zero offset...");
			exit;
		} 
		
		while(V < 0)V = V + D;
		while(V >= D)V = V - D;
		V = V * 3.81972; 

		if(!i)sr = V;	// sunrise
		else ss = V;	// sunset
	} 

	return [invalid,sr,ss];
}


function timeadj(t, ampm) {
	var hour;
	var min;

	var time = t;

	var hour = Math.floor(time);
	var min  = Math.floor((time - hour) * 60.0 + 0.5);

	if(min >= 60) {
		hour += 1;
		min  -= 60;
	}

	if(hour < 0)hour += 24;

	if(ampm) {
		ampm_str = (hour > 11) ? ' PM' : ' AM';
		hour %= 12;
		hour = (hour < 1) ? 12 : hour;
	}
	else ampm_str = '';

	var str = hour + ':' + ((min < 10) ? '0' : '') + min + ampm_str;
	return str;

}

function civ2heb(day,month,year){
	var d = parseInt(day);
	var m = parseInt(month);
	var y = parseInt(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)hy++;		// then bump up year
	if(m == 11)m += adarType;	// adjust for Adars

	return [d,m,hy];
}
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 moadim(cday, cmonth, cyear, hday, hmonth, dow) {
//status=cday+","+cmonth+","+cyear+","+hday+","+hmonth+","+dow
	if(hmonth == 6) {
		if(hday == 1 || hday == 2)return 0
		else if(hday == 3 && dow != 7)return 1;
		else if(hday == 4 && dow == 1)return 15;
		else if(hday == 10)return 2;
		else if(hday >= 15 && hday <= 22)return 3;
		else if(hday == 23)return 16;
	}
	else if(hmonth == 8) {
		if(hday >= 25)return 4;
	}
	else if(hmonth == 9) {
		if(hday <= 2)return 4;
		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 = hdate[0];
			if(hd == 29)return 4;
		}
		else if(hday == 10)
			return 5
	}
	else if(hmonth == 10) {
		if(hday==15)return 6;
	}
	else if(hmonth == 11 || hmonth == 13) {
		if(hday == 11 && dow == 5)return 7;
		else if(hday == 13 && dow != 7)return 17;
		else if(hday == 14)return 8;
		else if(hday == 15)return 18;
	}
	else if(hmonth == 0) {

		if(hday == 12 && dow == 5)return 19;
		else if(hday == 14 && dow != 7)return 19;
		else if(hday >= 15 && hday <= 21)return 9;
		else if(hday == 22)return 20;
	}
	else if(hmonth == 1) {
		if(hday == 3 && dow == 5)return 21;
		else if(hday == 4 && dow == 5)return 21;
		else if(hday == 5 && dow != 6 && dow != 7)return 21;
		if(hday == 14)return 22
		else if(hday == 18)return 10;
		if(hday == 28)return 23
	}
	else if(hmonth == 2) {
		if(hday == 6)return 11;
		else if(hday == 7)return 24
	}
	else if(hmonth == 3) {
		if(hday == 17 && dow != 7)return 12;
		if(hday == 18 && dow == 1)return 25
		if(hday == 3)return 13;
	}
	else if(hmonth == 4) {
		if(hday == 9 && dow != 7)return 14;
		if(hday == 10 && dow == 1)return 26
	}

	return "";
}

