var PERSIAN_EPOCH = 1948320.5;
var PERSIAN_WEEKDAYS = new Array("يک شنبه", "دو شنبه","سه شنبه", "چهار شنبه","پنج شنبه", "جمعه", "شنبه");
	var PERSIAN_MONTHS = new Array("فروردين","ارديبهشت","خرداد","تير","مرداد","شهريور","مهر","آبان","آذر","دي","بهمن","اسفند");

//  LEAP_PERSIAN  --  Is a given year a leap year in the Persian calendar ?
function leap_persian(year)
{
    return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682;
}
	
//  PERSIAN_TO_JD  --  Determine Julian day from Persian date
function persian_to_jd(year, month, day)
{
    var epbase, epyear;

    epbase = year - ((year >= 0) ? 474 : 473);
    epyear = 474 + mod(epbase, 2820);

    return day +
            ((month <= 7) ?
                ((month - 1) * 31) :
                (((month - 1) * 30) + 6)
            ) +
            Math.floor(((epyear * 682) - 110) / 2816) +
            (epyear - 1) * 365 +
            Math.floor(epbase / 2820) * 1029983 +
            (PERSIAN_EPOCH - 1);
}

//  JD_TO_PERSIAN  --  Calculate Persian date from Julian day
function jd_to_persian(jd)
{
    var year, month, day, depoch, cycle, cyear, ycycle,
        aux1, aux2, yday;


    jd = Math.floor(jd) + 0.5;

    depoch = jd - persian_to_jd(475, 1, 1);
    cycle = Math.floor(depoch / 1029983);
    cyear = mod(depoch, 1029983);
    if (cyear == 1029982) {
        ycycle = 2820;
    } else {
        aux1 = Math.floor(cyear / 366);
        aux2 = mod(cyear, 366);
        ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) +
                    aux1 + 1;
    }
    year = ycycle + (2820 * cycle) + 474;
    if (year <= 0) {
        year--;
    }
    yday = (jd - persian_to_jd(year, 1, 1)) + 1;
    month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
    day = (jd - persian_to_jd(year, month, 1)) + 1;
    return new Array(year, month, day);
}

//  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);
}

function ShowPersianDate()
{
    var j, year, mon, mday, weekday ;

    var today = new Date();

    year = today.getYear();
    if (year  < 1000)
        year  += 1900;

    mon = today.getMonth();
    mday = today.getDate();

    j = gregorian_to_jd(year, mon + 1, mday);
    weekday = jwday(j);
    perscal = jd_to_persian(j);
    //document.getElementById("CalendarTD").innerHTML = PERSIAN_WEEKDAYS[weekday] + "  " + perscal[2] + "  " + PERSIAN_MONTHS[perscal[1]-1] + "  " + perscal[0];
    document.getElementById("CalendarTD").innerHTML = PERSIAN_WEEKDAYS[weekday] + "  " + perscal[2] + "  " + PERSIAN_MONTHS[perscal[1]-1];
}

function jwday(j)
{
    return mod(Math.floor((j + 1.5)), 7);
}

function mod(a, b)
{
    return a - (b * Math.floor(a / b));
}
