var sJewishDay, sJewishMonth, sJewishFirstDayMonth, sJewishLastDayMonth;
var sJewishFirstDayYear, sJewishLastDayYear;

sJewishDay = sJewishMonth = sJewishFirstDayMonth = sJewishLastDayMonth =
sJewishFirstDayYear = sJewishLastDayYear = '';

var arrHebrewMonth = new Array();
arrHebrewMonth[0] = 'Nisan';
arrHebrewMonth[1] = 'Iyar';
arrHebrewMonth[2] = 'Sivan';
arrHebrewMonth[3] = 'Tamuz';
arrHebrewMonth[4] = 'Av';
arrHebrewMonth[5] = 'Elul';
arrHebrewMonth[6] = 'Tishrei';
arrHebrewMonth[7] = 'Cheshvan';
arrHebrewMonth[8] = 'Kislev';
arrHebrewMonth[9] = 'Tevet';
arrHebrewMonth[10] = 'Shvat';
arrHebrewMonth[11] = 'Adar';
arrHebrewMonth[12] = 'Vedar';

/*  You may notice that a variety of array variables logically local
    to functions are declared globally here.  In JavaScript, construction
    of an array variable from source code occurs as the code is
    interpreted.  Making these variables pseudo-globals permits us
    to avoid overhead constructing and disposing of them in each
    call on the function in which whey are used.  */

var J0000 = 1721424.5;                // Julian date of Gregorian epoch: 0000-01-01
var J1970 = 2440587.5;                // Julian date at Unix epoch: 1970-01-01
var JMJD  = 2400000.5;                // Epoch of Modified Julian Date system
var J1900 = 2415020.5;                // Epoch (day 1) of Excel 1900 date system (PC)
var J1904 = 2416480.5;                // Epoch (day 0) of Excel 1904 date system (Mac)

var NormLeap = new Array("Normal year", "Leap year");

//  calcGregorian  --  Perform calculation starting with a Gregorian date

function calcGregorian(day, month, year)
{
    updateFromGregorian(day, month, year);
//	return sJewishDayMonth;
}

/*  updateFromGregorian  --  Update all calendars from Gregorian.
                             "Why not Julian date?" you ask.  Because
                             starting from Gregorian guarantees we're
                             already snapped to an integral second, so
                             we don't get roundoff errors in other
                             calendars.  */

function updateFromGregorian(pDay, pMonth, pYear)
{
    var j, year, mon, mday, hour, min, sec,
        weekday, julcal, hebcal, islcal, hmindex, utime, isoweek,
        may_countcal, mayhaabcal, maytzolkincal, bahcal, frrcal,
        indcal, isoday, xgregcal;

    year = new Number(pYear);
    mon = new Number(pMonth);
    mday = new Number(pDay);
    hour = min = sec = 0;


//    hour = new Number(document.gregorian.hour.value);
//    min = new Number(document.gregorian.min.value);
//    sec = new Number(document.gregorian.sec.value);

    //  Update Julian day

    j = gregorian_to_jd(year, mon, mday) +
           ((sec + 60 * (min + 60 * hour)) / 86400.0);

//    document.julianday.day.value = j;
	var iModifiedJulianDay
    iModifiedJulianDay = j - JMJD;

    //  Update day of week in Gregorian box

    weekday = jwday(j);
    var iGregorianWeekDay
    iGregorianWeekDay = Weekdays[weekday];

    //  Update leap year status in Gregorian box

	var iGregorianLeepYear
    iGregorianLeepYear = NormLeap[leap_gregorian(year) ? 1 : 0];
    //  Update Hebrew Calendar

    hebcal = jd_to_hebrew(j);
    if (hebrew_leap(hebcal[0])) {    
        arrHebrewMonth.length = 13;
        arrHebrewMonth[11] = 'Adar I';
        arrHebrewMonth[12] = 'Adar II';
    } else {
        arrHebrewMonth.length = 12;
        arrHebrewMonth[11] = 'Adar';
    }	

//    document.hebrew.year.value = hebcal[0];
//    document.hebrew.month.selectedIndex = hebcal[1] - 1;
//    document.hebrew.day.value = hebcal[2];

	// ***** Mahesh Kulkarni - 25/Jul/2002
	// ***** Assigning converted date parts to page variables

//	sJewishDayMonth = hebcal[2] + ' ' +  arrHebrewMonth[hebcal[1] - 1];


	if (mday == 1)
		{
		sJewishFirstDayMonth = arrHebrewMonth[hebcal[1] - 1];
		sJewishFirstDayYear = hebcal[0];
		}
	else if (((mon == 1) || (mon == 3) || (mon == 5) || (mon == 7) ||
		(mon == 8) || (mon == 10) || (mon == 12)) && (mday == 31))
		{
		sJewishLastDayMonth = arrHebrewMonth[hebcal[1] - 1];
		sJewishLastDayYear = hebcal[0];
		}
	else if (((mon == 4) || (mon == 6) || (mon == 9) || (mon == 11))
			&& (mday == 30))
		{
		sJewishLastDayMonth = arrHebrewMonth[hebcal[1] - 1];
		sJewishLastDayYear = hebcal[0];
		}
	else if ((mon == 2) && (iGregorianLeepYear == 1) && (mday == 29))		
		{
		sJewishLastDayMonth = arrHebrewMonth[hebcal[1] - 1];
		sJewishLastDayYear = hebcal[0];
		}
	else if ((mon == 2) && (mday == 28)) 		
		{
		sJewishLastDayMonth = arrHebrewMonth[hebcal[1] - 1];
		sJewishLastDayYear = hebcal[0];
		}

		sJewishDay = hebcal[2];
		sJewishMonth = arrHebrewMonth[hebcal[1] - 1];		

    hmindex = hebcal[1];
    if (hmindex == 12 && !hebrew_leap(hebcal[0])) {
        hmindex = 14;
    }

}

//  LEAP_GREGORIAN  --  Is a given year in the Gregorian calendar a leap year ?

function leap_gregorian(year)
{
    return ((year % 4) == 0) &&
            (!(((year % 100) == 0) && ((year % 400) != 0)));
}


//  GREGORIAN_TO_JD  --  Determine Julian day number from Gregorian calendar date

var GREGORIAN_EPOCH = 1721425.5;

function gregorian_to_jd(year, month, day)
{
    return (GREGORIAN_EPOCH - 1) +
           (365 * (year - 1)) +
           Math.floor((year - 1) / 4) +
           (-Math.floor((year - 1) / 100)) +
           Math.floor((year - 1) / 400) +
           Math.floor((((367 * month) - 362) / 12) +
           ((month <= 2) ? 0 :
                               (leap_gregorian(year) ? -1 : -2)
           ) +
           day);
}

/*  JD_TO_HEBREW  --  Convert Julian date to Hebrew date
                      This works by making multiple calls to
                      the inverse function, and is this very
                      slow.  */

function jd_to_hebrew(jd)
{
    var year, month, day, i, count, first;

    jd = Math.floor(jd) + 0.5;
    count = Math.floor(((jd - HEBREW_EPOCH) * 98496.0) / 35975351.0);
    year = count - 1;
    for (i = count; jd >= hebrew_to_jd(i, 7, 1); i++) {
        year++;
    }
    first = (jd < hebrew_to_jd(year, 1, 1)) ? 7 : 1;
    month = first;
    for (i = first; jd > hebrew_to_jd(year, i, hebrew_month_days(year, i)); i++) {
        month++;
    }
    day = (jd - hebrew_to_jd(year, month, 1)) + 1;
    return new Array(year, month, day);
}

var HEBREW_EPOCH = 347995.5;

//  Is a given Hebrew year a leap year ?

function hebrew_leap(year)
{
    return mod(((year * 7) + 1), 19) < 7;
}

//  How many months are there in a Hebrew year (12 = normal, 13 = leap)

function hebrew_year_months(year)
{
    return hebrew_leap(year) ? 13 : 12;
}

//  Test for delay of start of new year and to avoid
//  Sunday, Wednesday, and Friday as start of the new year.

function hebrew_delay_1(year)
{
    var months, days, parts;

    months = Math.floor(((235 * year) - 234) / 19);
    parts = 12084 + (13753 * months);
    day = (months * 29) + Math.floor(parts / 25920);

    if (mod((3 * (day + 1)), 7) < 3) {
        day++;
    }
    return day;
}

//  Check for delay in start of new year due to length of adjacent years

function hebrew_delay_2(year)
{
    var last, present, next;

    last = hebrew_delay_1(year - 1);
    present = hebrew_delay_1(year);
    next = hebrew_delay_1(year + 1);

    return ((next - present) == 356) ? 2 :
                                     (((present - last) == 382) ? 1 : 0);
}

//  How many days are in a Hebrew year ?

function hebrew_year_days(year)
{
    return hebrew_to_jd(year + 1, 7, 1) - hebrew_to_jd(year, 7, 1);
}

//  How many days are in a given month of a given year

function hebrew_month_days(year, month)
{
    //  First of all, dispose of fixed-length 29 day months

    if (month == 2 || month == 4 || month == 6 ||
        month == 10 || month == 13) {
        return 29;
    }

    //  If it's not a leap year, Adar has 29 days

    if (month == 12 && !hebrew_leap(year)) {
        return 29;
    }

    //  If it's Heshvan, days depend on length of year

    if (month == 8 && !(mod(hebrew_year_days(year), 10) == 5)) {
        return 29;
    }

    //  Similarly, Kislev varies with the length of year

    if (month == 9 && (mod(hebrew_year_days(year), 10) == 3)) {
        return 29;
    }

    //  Nope, it's a 30 day month

    return 30;
}

//  Finally, wrap it all up into...

function hebrew_to_jd(year, month, day)
{
    var jd, mon, months;

    months = hebrew_year_months(year);
    jd = HEBREW_EPOCH + hebrew_delay_1(year) +
         hebrew_delay_2(year) + day + 1;

    if (month < 7) {
        for (mon = 7; mon <= months; mon++) {
            jd += hebrew_month_days(year, mon);
        }
        for (mon = 1; mon < month; mon++) {
            jd += hebrew_month_days(year, mon);
        }
    } else {
        for (mon = 7; mon < month; mon++) {
            jd += hebrew_month_days(year, mon);
        }
    }

    return jd;
}

function fx1(varMonth, selYear)
{

var iCurrentMonth = Number(varMonth)+Number(1);;
var iCurrentYear = selYear;

// ***** Getting jewish details for 1st day of month
// ***** Mahesh Kulkarni - 27/Jul/2002

calcGregorian(1, iCurrentMonth, iCurrentYear);

// ***** Deciding last day of month and getting details for last day of the month
// ***** Mahesh Kulkarni - 27/Jul/2002

if ((iCurrentMonth == 1) || (iCurrentMonth == 3) || (iCurrentMonth == 5) || 
(iCurrentMonth == 7) || (iCurrentMonth == 8) || (iCurrentMonth == 10) || 
(iCurrentMonth == 12))
{
	calcGregorian(31, iCurrentMonth, iCurrentYear);		
}
else if ((iCurrentMonth == 4) || (iCurrentMonth == 6) || (iCurrentMonth == 9) || 
(iCurrentMonth == 11))
{
	calcGregorian(30, iCurrentMonth, iCurrentYear);		
}
else if ((iCurrentMonth == 2) && leap_gregorian(iCurrentYear))		
{
	calcGregorian(29, iCurrentMonth, iCurrentYear);		
}
else if ((iCurrentMonth == 2)) 		
{
	calcGregorian(28, iCurrentMonth, iCurrentYear);		
}

//if (sJewishFirstDayYear != sJewishLastDayYear)
	//document.getElementById('jewishDateCalendarYear').innerHTML = sJewishFirstDayYear + ' - ' + sJewishLastDayYear;
//else				
	document.getElementById('jewishDateCalendarYear').innerHTML = sJewishLastDayYear;

document.write('<br>');

//if (sJewishFirstDayMonth != sJewishLastDayMonth)
//	document.getElementById('jewishDateCalendarMonth').innerHTML = (sJewishFirstDayMonth + ' - ' + sJewishLastDayMonth + ' ');
//else
//	document.getElementById('jewishDateCalendarMonth').innerHTML =  sJewishFirstDayMonth  + ' ';
}


