
/* Yehuda Ribco les solicita que no hagan copias ilegales de este software
 * http://serjudio.com
 * 5761 
 */

function makeArray() {
	this[0] = makeArray.arguments.length;
	for (i = 0; i < makeArray.arguments.length; i = i + 1)
		this[i+1] = makeArray.arguments[i];
}

var MesHeb = new makeArray(
	'Nisan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul',
	'Tishrei', 'Jeshvan', 'Kislev', 'Tevet', 'Shevat',
	'Adar', 'Adar I', 'Adar II');

var MesCivil = new makeArray(
	'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo',  'Junio', 'Julio', 'Agosto', 'Setiembre', 'Octubre', 'Noviembre', 'Diciembre');


var DiaSemana = new makeArray(
	'Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Shabbat');

function Gauss(year) {
	var a,b,c;
	var m;
	var	Mar;	// dia marciano de Pesaj (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 MesCivilLength(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 pesaj;
	var anchor;
	var adarType;

	m -= 2;
	if (m <= 0) { // Ene o Feb
		m += 12;
		y -= 1;
	}

	d += Math.floor(7 * m / 12 + 30 * (m - 1)); // dia en marzo
	hy = y + 3760;	// año hebreo conseguido
	pesaj = Gauss(hy);
	if (d <= pesaj - 15) { // antes 1 Nisan
		anchor = pesaj;
		d += 365;
		if(leap(y))
			d++;
		y -= 1;
		hy -= 1;
		pesaj = Gauss(hy);
	}
	else
		anchor = Gauss(hy + 1);

	d -= pesaj - 15;
	anchor -= pesaj - 12;
	y++;
	if(leap(y))
		anchor++;

	for(m = 0; m < 11; m++) {
		var days;
		if(m == 7 && anchor % 30 == 2)
			days = 30; // Jeshvan
		else if(m == 8 && anchor % 30 == 0)
			days = 29; // Kislev
		else
			days = 30 - m % 2;
		if(d <= days)
			break;
		d -= days;
	}

	adarType = 0;			// viejo Adar
	if (m == 11 && anchor >= 30) {
		if (d > 30) {
			adarType = 2;	// Adar 2
			d -= 30;
		}
		else
			adarType = 1;	// Adar 1
	}

	if(m >= 6)		// Tishrei o despues?
		hy++;		// suma un año

	if(m == 11)			// Adar?
		m += adarType;	// ajusta los Adares


	return (d + ' ' + m + ' ' + hy);
}


function Pascua(Y) {
	// basado en el algoritmo de 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 CUA(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 NthCUA(nth,DiaSemana,month,year) {
	if (nth > 0)
		return (nth - 1) * 7 + 1 + (7 + DiaSemana - CUA((nth - 1) * 7 + 1, month, year)) % 7;
	var days = MesCivilLength(month, year);
	return days - (CUA(days, month, year) - DiaSemana + 7) % 7;
}

function fiestas(cday, cmonth, cyear) {
	// Feriados nacionales y algunas celebraciones religiosas
	if (cmonth == 1 && cday == 1)
		return "Año Nuevo";
	else if (cmonth == 6 && cday == 19)
		return "Natalicio de Artigas";
	else if (cmonth == 1 && cday == 6)
		return "Día de los niños";
	else if (cmonth == 4 && cday == 19)
		return "Desembarco de los 33";
	else if (cmonth == 3 || cmonth == 4) {
		var e = Pascua(cyear);
	    if (cmonth == e[1] && cday == e[2])
			return "Pascua";
	}
	else if (cmonth == 5 && cday == 18)
		return "Batalla de las Piedras";
	else if (cmonth == 8 && cday == 25)
		return "Día de la Independencia";
	else if (cmonth == 10 && cday == 12)
		return "Día de la raza";
	else if (cmonth == 7 && cday == 18)
		return "Jura de la Constitución";
	else if (cmonth == 12 && cday == 25)
		return "Día de las Familias";
	else if (cmonth == 5 && cday == 1)
		return "Día de los Trabajadores";
	else if (cmonth == 11 && cday == 2)
		return "Día de los difuntos";
	return "";
}

function moadim(cday, cmonth, cyear, hday, hmonth, CUA) {
	if(hmonth == 6) {
		if(hday == 1 || hday == 2)
			return "Rosh HaShana"
		else if(hday == 3 && CUA != 7)
			return "Ayuno de Guedalia";
		else if(hday == 4 && CUA == 1)
			return "Ayuno de Guedalia";
		else if(hday == 10)
			return "Iom Kippur"
		else if(hday >= 15 && hday <= 22)
			return "Sucot"
		else if(hday == 23)
			return "Sucot (g)"
	}
	else if(hmonth == 8) {
		if(hday >= 25)
			return "Janucá"
	}
	else if(hmonth == 9) {
		if(hday <= 2) {
			return "Janucá"
		}
		else if(hday == 3) {
			// Kislev puede ser male o jaser
			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 "Janucá"
		}
		else if(hday == 10)
			return "Ayuno de 10 de Tevet"
	}
	else if(hmonth == 10) {
		if(hday==15)
			return "Tu biShevat"
	}
	else if(hmonth == 11 || hmonth == 13) {
		if(hday == 11 && CUA == 5)
			return "Taanit Esther"
		else if(hday == 13 && CUA != 7)
			return "Taanit Esther"
		else if(hday == 14)
			return "Purim"
		else if(hday == 15)
			return "Shushan Purim"
	}
	else if(hmonth == 0) {

		if(hday == 12 && CUA == 5)
			return "Taanit Bejorot"
		else if(hday == 14 && CUA != 7)
			return "Taanit Bejorot"
		else if(hday >= 15 && hday <= 21)
			return "Pesaj"
		else if(hday == 22)
			return "Pesaj (g)"
	}
	else if(hmonth == 1) {
		if(hday == 3 && CUA == 5)
			return "Iom HaAtzmaut"
		else if(hday == 4 && CUA == 5)
			return "Iom HaAtzmaut"
		else if(hday == 5 && CUA != 6 && CUA != 7)
			return "Iom HaAtzmaut"
		if(hday == 14)
			return "Pesaj sheni"
		else if(hday == 18)
			return "Lag BaOmer"
		if(hday == 28)
			return "Iom Ierushalaim"
	}
	else if(hmonth == 2) {
		if(hday == 6)
			return "Shavuot"
		else if(hday == 7)
			return "Shavuot (g)"
	}
	else if(hmonth == 3) {
		if(hday == 17 && CUA != 7)
			return "Ayuno del 17 de Tammuz"
		if(hday == 18 && CUA == 1)
			return "Ayuno del 17 de Tammuz"
	}
	else if(hmonth == 4) {
		if(hday == 9 && CUA != 7)
			return "Tisha BeAv"
		if(hday == 10 && CUA == 1)
			return "Tisha BeAv"
		if(hday == 15)
			return "Tu BeAv"
	}

	return "";
}

