/***************************************
 *        anniversaires.js             *
 ***************************************
 * Birthday / Yahrzeit computation     *
 * in Hebrew calendar                  *
 ***************************************
 *  October 4, 2002 -  Tishri 28, 5763 *
 *  v2.00                              *
 *                                     *
 * (c) Gabriel Zerbib,                 *
 *   gabriel@bumpt.net                 *
 *   http://www.bumpt.net              *
 *                                     *
 * It is strictly forbidden to use or  *
 * reproduce all or parts of this      *
 * program without author's explicit   *
 * permission.                         *
 * Commercial use of this program is   *
 * subject to purchase. Please contact *
 * the author.                         *
 ***************************************/

function Yahrzeit(birthHDate, HY, bAshke)
{
	HY = parseInt(HY);
	var yzHDate = new HDate(1, HDate.NISSAN, HY);

	if( ((birthHDate.getMonth() == 8) || (birthHDate.getMonth() == 9)) && (birthHDate.getDay() == 30) )
	{
		yzHDate.setMonth(birthHDate.getMonth());
		if(yzHDate.getMonthLength() == 30)
			yzHDate.setDay(30);
		else
		{
			var firstyearHDate = new HDate(1, birthHDate.getMonth(), birthHDate.getYear() + 1);

			if(firstyearHDate.getMonthLength() == 30)
			{
				yzHDate.setMonth(yzHDate.getMonth() + 1);
				yzHDate.setDay(1);
			}
			else
				yzHDate.setDay(29);
		}
	}

	else if( ( ! birthHDate.isLeap()) && (birthHDate.getMonth() == 12) && (yzHDate.isLeap()) )
	{
		yzHDate.setDay(birthHDate.getDay());
		yzHDate.setMonth(bAshke ? 12 : 13);
	}

	else if( (birthHDate.isLeap()) && ( ! yzHDate.isLeap()) && (birthHDate.getMonth() == 12) && (birthHDate.getDay() == 30) )
	{
		yzHDate.setDay(30);
		yzHDate.setMonth(11);
	}

	else if( (birthHDate.isLeap()) && ( ! yzHDate.isLeap()) && (birthHDate.getMonth() == 13) )
	{
		yzHDate.setDay(birthHDate.getDay());
		yzHDate.setMonth(12);
	}

	else
	{
		yzHDate.setMonth(birthHDate.getMonth());
		yzHDate.setDay(birthHDate.getDay());
	}

	return yzHDate;
}

function Birthday(birthHDate, HY)
{
	HY = parseInt(HY);
	var birthdayHDate = new HDate(1, HDate.NISSAN, HY);

	// 30 Heshvan & 30 Kislev
	if(((birthHDate.getMonth() == HDate.CHESHVAN) || (birthHDate.getMonth() == HDate.KISLEV)) && (birthHDate.getDay() == 30))
	{
		birthdayHDate.setMonth(birthHDate.getMonth());
		if(birthdayHDate.getMonthLength() < 30)
		{
			birthdayHDate.setDay(1);
			birthdayHDate.setMonth(birthdayHDate.getMonth() + 1);
		}
		else
			birthdayHDate.setDay(30);
	}

	// Adar common year
	else if( (! birthHDate.isLeap()) && (birthHDate.getMonth() == HDate.ADAR) )
	{
		birthdayHDate.setDay(birthHDate.getDay());
		birthdayHDate.setMonth(birthHDate.getMonth());

		if(birthdayHDate.isLeap())
			birthdayHDate.setMonth(birthdayHDate.getMonth() + 1);
	}

	//30 Adar-I Leap year
	else if( (birthHDate.isLeap()) && ( ! birthdayHDate.isLeap()) && (birthHDate.getMonth() == HDate.ADAR) && (birthHDate.getDay() == 30))
	{
		birthdayHDate.setDay(1);
		birthdayHDate.setMonth(1);
	}

	// Adar-II
	else if((birthHDate.isLeap()) && ( ! birthdayHDate.isLeap()) && (birthHDate.getMonth() == HDate.ADAR2))
	{
		birthdayHDate.setDay(birthHDate.getDay());
		birthdayHDate.setMonth(HDate.ADAR2);
	}

	// All other cases
	else
	{
		birthdayHDate.setDay(birthHDate.getDay());
		birthdayHDate.setMonth(birthHDate.getMonth());
	}

	return birthdayHDate;
}
