function in_array_calendar(what, where) {
    for (let i = 0; i < where.length; i++)
        if (what == where[i])
            return true;
    return false;
}

function calendar(year, month, thisDay) {
    if (year != null || month != null) {
        eval(AJAXEngine.notAsyncReq('ajax.php?do=calendar/' + year + ',' + month));
    }

    var year = (year == null) ? new Date().getFullYear() : year;
    var month = (month == null) ? new Date().getMonth() : month;
    const exmonth = new Date().getMonth();
    var thisDay = (thisDay == null) ? new Date().getDate() : thisDay;
    const oldyear = year - 1;
    const dayMonth = new Date(year, month + 1, 0).getDate(); // Количество дней в месяце
    const dayCount = 1;
    let mark = new Array();
    const weekDays = ['вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб']; // Недели. Для отладки
    const rusDays = [6, 0, 1, 2, 3, 4, 5]; // Для русской разметки календаря
    const rusMonths = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
    const firstDay = new Date(year, month, 1).getDay(); // Первый день календаря
    const lastDay = new Date(year, month, dayMonth).getDay(); // Последний день календаря
    mark = new Array();
    const beforeMonth = rusDays[firstDay];
    const affterMonth = (6 - rusDays[lastDay]);
    const lastMonth = (month != 0) ? month - 1 : 11;
    const nextMonth = (month != 11) ? month + 1 : 0;
    const sumMarks = beforeMonth + dayMonth + affterMonth;
    const sumWeeks = parseInt(sumMarks / 7);
    const lastWeek = (sumWeeks - 1);
    mark = 1;
    const dd = new Array();
    let a = '';
    let b = '';
    const lastYear2 = null;
    const nextYear2 = null;
    const nowUnix = new Date().getTime();
    const timeUnix = new Date(year, month, thisDay).getTime();

    if (month == 0) {
        lastGenerate = (year - 1) + ',' + '11';
        nextGenerate = year + ', ' + nextMonth;
    }
    else if (month == 11) {
        lastGenerate = year + ', ' + lastMonth;
        nextGenerate = (year + 1) + ',' + '0';
    }
    else {
        lastGenerate = year + ', ' + lastMonth;
        nextGenerate = year + ', ' + nextMonth;
    }
    let c = '<table width="100%" cellspacing="0" cellpadding="0" border="0" class="calendar">';
    c += '<tr><td colspan="7" align="center" style="padding-bottom:5px; padding-top:5px;"><div style="float:left"><a href="javascript:void(0);" onclick="calendar(' + lastGenerate + ');">&larr; ' + rusMonths[lastMonth].toLowerCase() + '</a></div><div style="float:right"><a href="javascript:void(0);" onclick="calendar(' + nextGenerate + ');">' + rusMonths[nextMonth].toLowerCase() + ' &rarr;</a></div></td></tr>';

    if (timeUnix > nowUnix)
        c += '<td colspan="7" align="center" style="padding-bottom:5px; padding-top:5px;">Далее ничего нового нет</td>';
    else if (newsDays.length == 0 && (year != new Date().getFullYear() || month != new Date().getMonth() || thisDay != new Date().getDate()))
        c += '<tr><td colspan="7" align="center" style="padding-bottom:5px; padding-top:5px;">Новостей не найдено</td></tr>';
    c += '<tr align="center">';
    c += '<th class="day">ПН</th><th class="day"><b>ВТ</b></th><th class="day">СР</th><th class="day"><b>ЧТ</b></th><th class="day">ПТ</th><th class="holiday"><b>СБ</b></th><th class="holiday">ВС</th></tr>';
    c += '<tr  align="center" class="calendarTr">';
    workWeeks = sumWeeks + 1;
    for (let i = 1; i < workWeeks; i++) {
        c += '<tr  align="center" class="calendarTr">';
        for (j = 0; j < 7; j++) {
            if ((j < rusDays[firstDay] && i == 1) || (mark > dayMonth && i == workWeeks - 1)) {
                a += '<td class="rowEmpty">&nbsp;</td>';
            }
            else {
                if (!in_array(newsDays, mark)) {
                    addition = (mark == thisDay) ? 'dayNow' : '';
                    kmark = (mark == thisDay && month == exmonth) ? ((j == 5 || j == 6) ? '<font color="red">' + mark + '</font>' : '<span style="color: blue; ">' + mark + '</span>') : mark;
                    kmark1 = (mark == thisDay && month == exmonth) ? 'id="today"' : '';
                    if (j == 5 || j == 6) b += '<td ' + kmark1 + ' class="isHolid calendarBorder ' + addition + '"><font color="red">' + kmark + '</font></td>';
                    else a += '<td ' + kmark1 + ' class="calendarBorder ' + addition + '">' + kmark + '</td>';
                }
                else {
                    if (j == 5 || j == 6) b += '<td ' + kmark1 + ' class="isNewsH"><a href="news/date/' + year + '-' + (month + 1) + '-' + mark + '"><strong>' + mark + '</strong></a></td>';
                    else a += '<td ' + kmark1 + ' class="isNews"><a href="news/date/' + year + '-' + (month + 1) + '-' + mark + '"><strong>' + mark + '</strong></a></td>';
                }
                mark++;
            }
        }
        c += a + b + '</tr>';
        a = '';
        b = '';
    }
    c += a + b + '</table>';
    c += '<div class="calendar" align="center">' + rusMonths[month] + '  | <a  href="javascript:void(0);" onclick="calendar(' + (year - 1) + ', ' + month + ');">' + year + '</a> | <a href="javascript:void(0);" onclick="calendar();">Обнулить</a></div></div></div>';

    gid('_calendar').innerHTML = c;

}