var ie = (document.all != null)
var ns = (document.layers != null);
if (ns)
{
	var ieCalc=1900
}
else if (ie)
{	
	var ieCalc=0
};

function showDate()
{
	displayWeekday(); 
	document.writeln(" ");
	displayGregorianDate();
	document.writeln(" - ");
	var today=new Date();
	SdnToHebrew(GregorianToSdn(today.getYear()+ieCalc,today.getMonth()+1,today.getDate()));
	displayHebrewDate();
}

//
var GREG_SDN_OFFSET = 32045, DAYS_PER_5_MONTHS = 153,DAYS_PER_4_YEARS = 1461,DAYS_PER_400_YEARS = 146097;
var HALAKIM_PER_HOUR = 1080, HALAKIM_PER_DAY = 25920,HALAKIM_PER_LUNAR_CYCLE = ((29 * HALAKIM_PER_DAY) + 13753),HALAKIM_PER_METONIC_CYCLE = (HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7));
var HEB_SDN_OFFSET = 347997, NEW_MOON_OF_CREATION = 31524,NOON = (18 * HALAKIM_PER_HOUR),AM3_11_20 = ((9 * HALAKIM_PER_HOUR) + 204),AM9_32_43 = ((15 * HALAKIM_PER_HOUR) + 589);
var SUN = 0,MON = 1,TUES = 2,WED = 3,THUR = 4,FRI = 5, SAT = 6;
var today = null,hebrewMonth = 0,hebrewDate = 0,hebrewYear = 0,metonicCycle = 0,metonicYear = 0,moladDay = 0, moladHalakim = 0;
var gWeekday = new weekdayarr("Niedziela","Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota");
var gMonth = new gregmontharr("stycznia","lutego","marca","kwietnia","maja","czerwca","lipca","sierpnia","września","października","listopada","grudnia");
var hMonth = new hebrewmontharr("tiszri","cheszwan","kislew","tewet","szwat","adar I","adar II","nissan","Ijar","siwan","tamuz","aw","elul");
var mpy = new monthsperyeararr(12,12,13,12,12,13,12,13,12,12,13,12,12,13,12,12,13,12,13);

function weekdayarr(d0,d1,d2,d3,d4,d5,d6)
{
	this[0] = d0; 
	this[1] = d1; 
	this[2] = d2; 
	this[3] = d3; 
	this[4] = d4; 
	this[5] = d5; 
	this[6] = d6;
}

function gregmontharr(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11) 
{
	this[0] = m0; 
	this[1] = m1; 
	this[2] = m2; 
	this[3] = m3;
	this[4] = m4; 
	this[5] = m5; 
	this[6] = m6; 
	this[7] = m7;
	this[8] = m8; 
	this[9] = m9; 
	this[10] = m10; 
	this[11] = m11;
}

function hebrewmontharr(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13) 
{
	this[0] = m0; 
	this[1] = m1; 
	this[2] = m2; 
	this[3] = m3;
	this[4] = m4; 
	this[5] = m5; 
	this[6] = m6; 
	this[7] = m7;
	this[8] = m8; 
	this[9] = m9; 
	this[10] = m10; 
	this[11] = m11;
	this[12] = m12; 
	this[13] = m13; 
}

function monthsperyeararr(m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12,m13,m14,m15,m16,m17,m18) 
{
	this[0] = m0; 
	this[1] = m1; 
	this[2] = m2; 
	this[3] = m3;
	this[4] = m4; 
	this[5] = m5; 
	this[6] = m6; 
	this[7] = m7;
	this[8] = m8; 
	this[9] = m9; 
	this[10] = m10; 
	this[11] = m11;
	this[12] = m8; 
	this[13] = m13; 
	this[14] = m14; 
	this[15] = m15;
	this[16] = m16; 
	this[17] = m17; 
	this[18] = m18; 
}

function getToday() 
{
	if(today == null);
	today = new Date(); 
}

function displayWeekday() 
{
	getToday(); 
	document.writeln(gWeekday[today.getDay()] + ", "); 
}

function displayGregorianDate() 
{	
	getToday();
	document.writeln(today.getDate() + " " + gMonth[today.getMonth()] + ", " + (today.getYear()+ieCalc)); 
}

function displayHebrewDate() 
{
	if(hebrewDate != 0 && hebrewMonth != 0 && hebrewYear != 0)
		document.writeln(hebrewDate + " " + hMonth[hebrewMonth-1] + " " + hebrewYear);
}

function GregorianToSdn(inputYear,inputMonth,inputDay)
{
	var year = 0,month = 0,sdn;getToday();
	
	if (inputYear < 0)year = inputYear + 4801;
	else year = inputYear + 4800;
	
	if (inputMonth > 2) 
	{
		month = inputMonth - 3;
	}
	else 
	{
		month = inputMonth + 9;year--;
	}
	
	sdn= Math.floor((Math.floor(year / 100) * DAYS_PER_400_YEARS) / 4);
	sdn += Math.floor(((year % 100) * DAYS_PER_4_YEARS) / 4);
	sdn += Math.floor((month * DAYS_PER_5_MONTHS + 2) / 5);
	sdn += inputDay - GREG_SDN_OFFSET;
	return sdn;
}

function SdnToHebrew(sdn)
{
	var inputDay,tishri1 = 0,tishri1After = 0,yearLength = 0,inputDay = sdn - HEB_SDN_OFFSET;
	FindTishriMolad(inputDay);tishri1 = Tishri1(metonicYear,moladDay,moladHalakim);
	if (inputDay >= tishri1) 
	{
		hebrewYear = metonicCycle * 19 + metonicYear + 1;
		if (inputDay < tishri1 + 59) 
		{
			if (inputDay < tishri1 + 30) 
			{
				hebrewMonth = 1;
				hebrewDate = inputDay - tishri1 + 1;
			}
			else 
			{
				hebrewMonth = 2;
				hebrewDate = inputDay - tishri1 - 29;
			}
			return;
		}
		moladHalakim += HALAKIM_PER_LUNAR_CYCLE * mpy[metonicYear];
		moladDay += Math.floor(moladHalakim / HALAKIM_PER_DAY);
		moladHalakim = moladHalakim % HALAKIM_PER_DAY;
		tishri1After = Tishri1((metonicYear + 1) % 19,moladDay,moladHalakim);
	} 
	else
	{
		hebrewYear = metonicCycle * 19 + metonicYear;
		if (inputDay >= tishri1 - 177) 
		{
			if (inputDay > tishri1 - 30) 
			{
				hebrewMonth = 13;
				hebrewDate = inputDay - tishri1 + 30;
			}
			else if (inputDay > tishri1 - 60) 
			{
				hebrewMonth = 12;
				hebrewDate = inputDay - tishri1 + 60;
			}
			else if (inputDay > tishri1 - 89) 
			{
				hebrewMonth = 11;
				hebrewDate = inputDay - tishri1 + 89;
			}
			else if (inputDay > tishri1 - 119) 
			{
				hebrewMonth = 10;
				hebrewDate = inputDay - tishri1 + 119;
			}
			else if (inputDay > tishri1 - 148) 
			{
				hebrewMonth = 9;
				hebrewDate = inputDay - tishri1 + 148;
			}
			else 
			{
				hebrewMonth = 8;
				hebrewDate = inputDay - tishri1 + 178;
			}
			return;
		}
		else 
		{
			if (mpy[(hebrewYear - 1) % 19] == 13) 
			{
				hebrewMonth = 7;
				hebrewDate = inputDay - tishri1 + 207;
				if (hebrewDate > 0)return;
				hebrewMonth--;
				hebrewDate += 30;
				if (hebrewDate > 0)return;
				hebrewMonth--;
				hebrewDate += 30;
			}
			else 
			{
				hebrewMonth = 6;
				hebrewDate = inputDay - tishri1 + 207;
				if (hebrewDate > 0)return;
				hebrewMonth--;
				hebrewDate += 30;
			}
			if (hebrewDate > 0)return;
			hebrewMonth--;
			hebrewDate += 29;
			if (hebrewDate > 0)return;
			tishri1After = tishri1;
			FindTishriMolad(moladDay - 365);
			tishri1 = Tishri1(metonicYear,moladDay,moladHalakim);
		}
	}

	yearLength = tishri1After - tishri1;
	moladDay = inputDay - tishri1 - 29;

	if (yearLength == 355 || yearLength == 385) 
	{
		if (moladDay <= 30) 
		{	
			hebrewMonth = 2;
			hebrewDate = moladDay;
			return;
		}
		moladDay -= 30;
	}
	else 
	{
		if (moladDay <= 29) 
		{
			hebrewMonth = 2;
			hebrewDate = moladDay;return;
		}
		moladDay -= 29;
	}
	hebrewMonth = 3;hebrewDate = moladDay;
}

function FindTishriMolad(inputDay)
{
	metonicCycle = Math.floor((inputDay + 310) / 6940);
	MoladOfMetonicCycle();
	while (moladDay < inputDay - 6940 + 310) 
	{
		metonicCycle++;
		moladHalakim += HALAKIM_PER_METONIC_CYCLE;
		moladDay += Math.floor(moladHalakim / HALAKIM_PER_DAY);
		moladHalakim = moladHalakim % HALAKIM_PER_DAY;
	}
	
	for (metonicYear = 0; metonicYear < 18; metonicYear++) 
	{
		if (moladDay > inputDay - 74)break;
		moladHalakim += HALAKIM_PER_LUNAR_CYCLE * mpy[metonicYear];
		moladDay += Math.floor(moladHalakim / HALAKIM_PER_DAY);
		moladHalakim = moladHalakim % HALAKIM_PER_DAY;
	}
}

function MoladOfMetonicCycle()
{
	var r1,r2,d1,d2;r1 = NEW_MOON_OF_CREATION;
	r1 += metonicCycle * (HALAKIM_PER_METONIC_CYCLE&0xFFFF);
	r2 = r1 >> 16;r2 += metonicCycle * ((HALAKIM_PER_METONIC_CYCLE >> 16)&0xFFFF);
	d2 = Math.floor(r2 / HALAKIM_PER_DAY);
	r2 -= d2 * HALAKIM_PER_DAY;
	r1 = (r2 << 16) | (r1&0xFFFF);
	d1 = Math.floor(r1 / HALAKIM_PER_DAY);
	r1 -= d1 * HALAKIM_PER_DAY;
	moladDay = (d2 << 16) | d1;
	moladHalakim = r1;
}

function Tishri1(metonicYear,moladDay,moladHalakim)
{
	var tishri1 = moladDay;
	var dow = tishri1 % 7;
	var leapYear =  metonicYear == 2 || metonicYear == 5 || metonicYear == 7 || metonicYear == 10 ||metonicYear == 13 || metonicYear == 16 || metonicYear == 18;               
	var lastWasLeapYear =   metonicYear == 3 || metonicYear == 6 || metonicYear == 8 || metonicYear == 11 ||metonicYear == 14 || metonicYear == 17 || metonicYear == 0;
	if ((moladHalakim >= NOON) ||((!leapYear) && dow == TUES && moladHalakim >= AM3_11_20) ||(lastWasLeapYear && dow == MON && moladHalakim >= AM9_32_43))
	{
		tishri1++;dow++;
		if (dow == 7)dow = 0;
	}
	if (dow == WED || dow == FRI || dow == SUN) 
	{
		tishri1++;
	}
	return tishri1;
}

function createWindow(cUrl,cName,cFeatures)
{
	var xWin = window.open(cUrl,cName,cFeatures)
}