function generateMonthView_small($month, $year, $view_diary)
{
    global $_GET;
    $OUTPUT = "\r\n\t\t<table width=190 cellspacing=0>\r\n\t\t\t<tr>\r\n\t\t\t\t<td height=20 align=center " . TMPL_calSmallMonthTitleStyle . "\r\n\t\t\t\t\t\tonClick='document.location.href=\"" . SELF . "?key=month&month={$month}&year={$year}\"'>\r\n\t\t\t\t\t<b>" . getMonthText($month) . " {$year}</b>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td align=center " . TMPL_calSmallMonthBodyStyle . ">";
    // generate the titles of the weekdays
    $OUTPUT .= "\r\n\t\t\t<table width='184' cellspacing=0>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td width=23>&nbsp;</td>\r\n\t\t\t\t\t<td width=23 align=center><b>M</b></td>\r\n\t\t\t\t\t<td width=23 align=center><b>T</b></td>\r\n\t\t\t\t\t<td width=23 align=center><b>W</b></td>\r\n\t\t\t\t\t<td width=23 align=center><b>T</b></td>\r\n\t\t\t\t\t<td width=23 align=center><b>F</b></td>\r\n\t\t\t\t\t<td width=23 align=center bgcolor=" . TMPL_calFillSaturday . "><b>S</b></td>\r\n\t\t\t\t\t<td width=23 align=center bgcolor=" . TMPL_calFillSunday . "><b>S</b></td>\r\n\t\t\t\t</tr>";
    // get the weekday number of the first of this month
    $first_wd = getWeekdayNum(1, $month, $year);
    // the following code will generate the first entries on the calendar, which is for the previous month (if any)
    // month and year of previous month
    if ($month == 1) {
        $tmp_month = 12;
        $tmp_year = $year - 1;
    } else {
        $tmp_month = $month - 1;
        $tmp_year = $year;
    }
    // date of last monday in previous month (where the entries will start)
    if ($first_wd > 1) {
        $tmp_day = getDaysInMonth($tmp_month, $tmp_year) - ($first_wd - 2);
    } else {
        $tmp_day = 1;
        $tmp_month = $month;
        $tmp_year = $year;
    }
    // create a view variables
    $selected_month = $_GET["month"];
    // create the previous month's entries
    $c_weeknum = getWeekNumber($tmp_day, $tmp_month, $tmp_year);
    $selected_weeknum = getWeekNumber($_GET["mday"], $_GET["month"], $_GET["year"]);
    // if today's week number = the current generated week's number, hightlight the row, as so with the selected week,
    if ($c_weeknum == getTodayWeekNumber() && $tmp_year == date("Y") && $month == date("m") || getTodayWeekNumber() == 0 && $month == date("m") && $year == date("Y")) {
        // today's week
        $OUTPUT .= "<tr bgcolor='" . TMPL_calSmallMonthCurrentWeek . "'>";
        $ROW_COLORED = 1;
    } else {
        if ($month == $_GET["month"] && $c_weeknum == $selected_weeknum && $month == $_GET["month"] || $c_weeknum == 52 && $selected_weeknum == 0) {
            // selected week, the last check is for the first week in jan
            $OUTPUT .= "<tr bgcolor='" . TMPL_calSmallMonthSelectedWeek . "'>";
            $ROW_COLORED = 1;
        } else {
            // other dates
            $OUTPUT .= "<tr>";
            $ROW_COLORED = 0;
        }
    }
    if ($first_wd != 1) {
        // only if there is a day in this week of previous month, print the week number
        $OUTPUT .= "<td width=23 " . TMPL_calSmallMonthWeekNumberStyle . " align=center>{$c_weeknum}</td>";
    }
    for ($c_wd = 1; $c_wd < $first_wd; $c_wd++, $tmp_day++) {
        // fill differently for saturday and sunday (only when row wasn't already highlighted)
        if ($c_wd == 6 && !$ROW_COLORED) {
            $dayfill = "bgcolor=" . TMPL_calFillSaturday;
        } else {
            if ($c_wd == 7) {
                $dayfill = "bgcolor=" . TMPL_calFillSunday;
            } else {
                $dayfill = "";
            }
        }
        $OUTPUT .= "<td {$dayfill} width=23 align=center>\r\n\t\t\t\t\t\t\t<a id='calSmallMonthOMLink' href='" . SELF . "?mday={$tmp_day}&month={$tmp_month}&year={$tmp_year}'>{$tmp_day}</a>\r\n\t\t\t\t\t\t</td>";
    }
    // start creating this month's entries
    $cm_days = getDaysInMonth($month, $year);
    for ($c_day = 1; $c_day <= $cm_days; $c_day++) {
        $c_weeknum = getWeekNumber($c_day, $month, $year);
        if ($c_wd == 1) {
            // start a new row (it's MONDAY!!!!!)
            // if today's week number = the current generated week's number, hightlight the row, as so with the selected week
            if ($c_weeknum == getTodayWeekNumber() && $year == date("Y") && $month == date("m")) {
                // today's week
                $OUTPUT .= "<tr bgcolor='" . TMPL_calSmallMonthCurrentWeek . "'>";
                $ROW_COLORED = 1;
            } else {
                if ($month == $_GET["month"] && $c_weeknum == $selected_weeknum) {
                    // selected week
                    $OUTPUT .= "<tr bgcolor='" . TMPL_calSmallMonthSelectedWeek . "'>";
                    $ROW_COLORED = 1;
                } else {
                    // other dates
                    $OUTPUT .= "<tr>";
                    $ROW_COLORED = 0;
                }
            }
            // attach the week number
            $OUTPUT .= "<td align=center width=23 " . TMPL_calSmallMonthWeekNumberStyle . ">{$c_weeknum}</td>";
        }
        // change the fill color if it it 2day's date we are printing, or the selected date
        if ($c_day == $_GET["mday"] && $month == $_GET["month"] && $year == $_GET["year"]) {
            // selected date
            $dayfill = "bgcolor=" . TMPL_calSmallMonthSelectedDay;
            $a_id = "calSmallMonthCMLinkSelected";
        } else {
            if (date("d") == $c_day && date("m") == $month && date("Y") == $year) {
                // 2day's date
                $dayfill = "bgcolor=" . TMPL_calSmallMonthCurrentDay;
                $a_id = "calSmallMonthCMLinkToday";
            } else {
                // other dates
                // fill differently for saturday and sunday (only when the row has not already been colored)
                if ($c_wd == 6 && !$ROW_COLORED) {
                    $dayfill = "bgcolor=" . TMPL_calFillSaturday;
                } else {
                    if ($c_wd == 7 && !$ROW_COLORED) {
                        $dayfill = "bgcolor=" . TMPL_calFillSunday;
                    } else {
                        $dayfill = "";
                    }
                }
                $a_id = "calSmallMonthCMLink";
            }
        }
        $OUTPUT .= "<td width=23 {$dayfill} align=center>\r\n\t\t\t\t\t\t\t<a id='{$a_id}' href='" . SELF . "?mday={$c_day}&month={$month}&year={$year}&view_diary={$view_diary}'>{$c_day}</a>\r\n\t\t\t\t\t\t</td>";
        if ($c_wd == 7) {
            // end the output
            $OUTPUT .= "</tr>";
        }
        $c_wd == 7 ? $c_wd = 1 : $c_wd++;
    }
    // next month variables
    if ($month == 12) {
        $tmp_month = 1;
        $tmp_year = $year + 1;
    } else {
        $tmp_month = $month + 1;
        $tmp_year = $year;
    }
    // finish with the next months entries
    for ($c_day = 1; $c_wd <= 7 && $c_wd > 1; $c_wd++, $c_day++) {
        // fill differently for saturday and sunday
        if ($c_wd == 6 && !$ROW_COLORED) {
            $dayfill = "bgcolor=" . TMPL_calFillSaturday;
        } else {
            if ($c_wd == 7 && !$ROW_COLORED) {
                $dayfill = "bgcolor=" . TMPL_calFillSunday;
            } else {
                $dayfill = "";
            }
        }
        $OUTPUT .= "<td {$dayfill} width=23 align=center>\r\n\t\t\t\t\t\t\t<a id='calSmallMonthOMLink' href='" . SELF . "?mday={$c_day}&month={$tmp_month}&year={$tmp_year}'>{$c_day}</a>\r\n\t\t\t\t\t\t</td>";
    }
    // finish the tables and return
    $OUTPUT .= "\r\n\t\t\t\t</tr>\r\n\t\t\t</table>\r\n\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\t";
    return $OUTPUT;
}
function generateMonthView_large($month, $year, $view_diary)
{
    global $_GET;
    $valid_date = mktime(0, 0, 0, $month, 1, $year);
    $month = date("m", $valid_date);
    $year = date("Y", $valid_date);
    $select_month = "<select name=month>";
    for ($i = 1; $i <= 12; $i++) {
        if ($month == $i) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $select_month .= "<option {$sel} value='{$i}'>" . getMonthText($i) . "</option>";
    }
    $select_month .= "</select>";
    $select_year = "<select name=year>";
    for ($i = 1971; $i <= 2028; $i++) {
        if ($year == $i) {
            $sel = "selected";
        } else {
            $sel = "";
        }
        $select_year .= "<option {$sel} value={$i}>{$i}</option>";
    }
    $select_year .= "</select>";
    $OUTPUT = "\r\n\t<table width=600 cellspacing=0>\r\n\t<tr>\r\n\t\t<td align=center style='font-size: 14px; color: #ffffff; font-weight: bold;' colspan=3>\r\n\t\t\t<form method=post action='" . SELF . "'>\r\n\t\t\t<input type=hidden name=key value='month'>\r\n\t\t\tGoto: {$select_month} {$select_year} <input type=submit value='Go'>\r\n\t\t\t</form>\r\n\t\t</td>\r\n\t</tr>\r\n\t<tr><td>&nbsp;</td></tr>\r\n        <tr>\r\n        \t<td colspan=3 " . TMPL_calTimeStyle2 . ">\r\n        \t<table width=100%><tr>\r\n        \t<td width=30% align=center valign=middle>\r\n        \t\t<a href='" . SELF . "?key=month&month=" . ($month - 1) . "&year={$year}&view_diary={$view_diary}'>\r\n        \t\t\t<img src='left_day.gif' border=0>\r\n\t\t\t</a>\r\n\t\t</td>\r\n\t\t<td width=40% valign=middle align=center " . TMPL_calTimeStyleHeader . ">\r\n\t\t\t<b>" . getMonthText($month) . " {$year}</b>\r\n\t\t</td>\r\n\t\t<td width=30% align=center valign=middle>\r\n\t\t\t<a href='" . SELF . "?key=month&month=" . ($month + 1) . "&year={$year}&view_diary={$view_diary}'>\r\n\t\t\t\t<img src='right_day.gif' border=0>\r\n\t\t\t</a>\r\n\t\t</td>\r\n\t\t</tr></table>\r\n\t\t</td>\r\n\t</tr>\r\n\t<tr>\r\n\t\t<td align=center " . TMPL_calLargeMonthBodyStyle . " colspan=3>\r\n\t\t<table width='600' cellspacing=0>\r\n\t\t<tr>\r\n\t\t\t<td width=75 height=60>&nbsp;</td>\r\n\t\t\t<td width=75 height=60 align=center><h1>M</h1></td>\r\n\t\t\t<td width=75 height=60 align=center><h1>T</h1></td>\r\n\t\t\t<td width=75 height=60 align=center><h1>W</h1></td>\r\n\t\t\t<td width=75 height=60 align=center><h1>T</h1></td>\r\n\t\t\t<td width=75 height=60 align=center><h1>F</h1></td>\r\n\t\t\t<td width=75 height=60 align=center bgcolor=" . TMPL_calFillSaturday . "><h1>S</h1></td>\r\n\t\t\t<td width=75 height=60 align=center bgcolor=" . TMPL_calFillSunday . "><h1>S</h1></td>\r\n\t\t</tr>";
    // get the weekday number of the first of this month
    $first_wd = getWeekdayNum(1, $month, $year);
    // the following code will generate the first entries on the calendar, which is for the previous month (if any)
    // and year of previous month
    if ($month == 1) {
        $tmp_month = 12;
        $tmp_year = $year - 1;
    } else {
        $tmp_month = $month - 1;
        $tmp_year = $year;
    }
    // date of last monday in previous month (where the entries will start)
    if ($first_wd > 1) {
        $tmp_day = getDaysInMonth($tmp_month, $tmp_year) - ($first_wd - 2);
    } else {
        $tmp_day = 1;
        $tmp_month = $month;
        $tmp_year = $year;
    }
    // create a view variables
    $selected_month = $_GET["month"];
    // create the previous month's entries
    $c_weeknum = getWeekNumber($tmp_day, $tmp_month, $tmp_year);
    $selected_weeknum = getWeekNumber($_GET["mday"], $_GET["month"], $_GET["year"]);
    $OUTPUT .= "<tr>";
    if ($first_wd != 1) {
        // only if there is a day in this week of previous month, print the week number
        $OUTPUT .= "<td width=75 height=60 " . TMPL_calLargeMonthWeekNumberStyle . " align=center>{$c_weeknum}</td>";
    }
    for ($c_wd = 1; $c_wd < $first_wd; $c_wd++, $tmp_day++) {
        // fill differently for saturday and sunday (only when row wasn't already highlighted)
        if ($c_wd == 6) {
            $dayfill = "bgcolor=" . TMPL_calFillSaturday;
        } else {
            if ($c_wd == 7) {
                $dayfill = "bgcolor=" . TMPL_calFillSunday;
            } else {
                $dayfill = "";
            }
        }
        $OUTPUT .= "<td {$dayfill} width=75 style='border-bottom: 1px solid #000000;' height=60 align=center>\r\n\t\t\t\t<a id='calSmallMonthOMLink' href='" . SELF . "?mday={$tmp_day}&month={$tmp_month}&year={$tmp_year}'>{$tmp_day}</a>\r\n\t\t\t</td>";
    }
    // start creating this month's entries
    $cm_days = getDaysInMonth($month, $year);
    for ($c_day = 1; $c_day <= $cm_days; $c_day++) {
        $c_weeknum = getWeekNumber($c_day, $month, $year);
        if ($c_wd == 1) {
            // start a new row (it's MONDAY!!!!!)
            $OUTPUT .= "<tr>";
            // attach the week number
            $OUTPUT .= "<td align=center width=75 height=60 " . TMPL_calLargeMonthWeekNumberStyle . ">{$c_weeknum}</td>";
        }
        // change the fill color if it is 2day's date we are printing, or the selected date
        if (date("d") == $c_day && date("m") == $month && date("Y") == $year) {
            // 2day's date
            $dayfill = "bgcolor=" . TMPL_calLargeMonthSelectedDay;
            $a_id = "calSmallMonthCMLinkToday";
        } else {
            // other dates
            // fill differently for saturday and sunday (only when the row has not already been colored)
            if ($c_weeknum == getTodayWeekNumber() && $year == date("Y") && $month == date("m")) {
                $dayfill = "bgcolor='" . TMPL_calLargeMonthCurrentWeek . "'";
            } else {
                if ($c_wd == 6) {
                    $dayfill = "bgcolor=" . TMPL_calFillSaturday;
                } else {
                    if ($c_wd == 7) {
                        $dayfill = "bgcolor=" . TMPL_calFillSunday;
                    } else {
                        $dayfill = "";
                    }
                }
            }
            $a_id = "calSmallMonthCMLink";
        }
        $weeknum_first = getWeekNumber(1, $month, $year);
        $weeknum_last = getWeekNumber(getDaysInMonth($month, $year), $month, $year);
        // create the borders for the days
        $b_top = "border-top: 1px solid #000000;";
        $b_bottom = "border-bottom: 1px solid #000000;";
        $b_right = "border-right: 1px solid #000000;";
        $b_left = "border-left: 1px solid #000000;";
        $day_style = "cursor: pointer; cursor: hand; {$b_left} {$b_bottom}";
        // first_week
        if ($c_weeknum == $weeknum_first) {
            $day_style .= "{$b_top} {$b_left}";
        }
        // sundays
        if ($c_wd == 7 || getDaysInMonth($month, $year) == $c_day) {
            $day_style .= "{$b_right}";
        }
        // check appointment, reminders and create the day information
        $checkdate_start = date("Y-m-d", mktime(0, 0, 0, $month, $c_day, $year));
        $checkdate_end = date("Y-m-d", mktime(0, 0, 0, $month, $c_day + 1, $year));
        db_conn("cubit");
        $sql = "SELECT * FROM diary_entries\r\n\t\t\tWHERE (DATE '{$checkdate_start}', DATE '{$checkdate_end}') OVERLAPS (time_start, time_end)";
        $rslt = db_exec($sql) or errDie("Error checking for appointments");
        $entrycount = pg_num_rows($rslt);
        if ($entrycount > 0) {
            $count_entireday = 0;
            $day_information = "<table width=100%>";
            while ($row = pg_fetch_array($rslt)) {
                $appointment_url = "diary-appointment.php?entry_id={$row['entry_id']}&key=view";
                $onClick = "popupOpen(\"{$appointment_url}\",\"appointment_popup\",\"scrollbars=yes,width=500,height=590\")";
                $day_information .= "\r\n\t\t\t\t\t<tr><td onClick='{$onClick}' nowrap>\r\n\t\t\t\t\t\t{$row['title']}\r\n\t\t\t\t\t</td></tr>";
            }
            $day_information .= "</table>";
        } else {
            $day_information = "&nbsp;";
        }
        $OUTPUT .= "\r\n\t\t\t<td width=75 height=60 {$dayfill} valign=top align=center style='{$day_style}'>\r\n\t\t\t\t<table width=100% height=100%>\r\n\t\t\t\t<tr><td valign=top align=right height=0% onClick='document.location.href=\"" . SELF . "?mday={$c_day}&month={$month}&year={$year}&view_diary={$view_diary}\";'>\r\n\t\t\t\t\t<font size=2 color='" . TMPL_calLargeMonthOMLink_a . "'><b>{$c_day}</b></font>\r\n\t\t\t\t</td></tr>\r\n\t\t\t\t<tr><td valign=top align=center height=100%>\r\n\t\t\t\t\t{$day_information}\r\n\t\t\t\t</td></tr>\r\n\t\t\t\t</table>\r\n\t\t\t</td>";
        if ($c_wd == 7) {
            // end the output
            $OUTPUT .= "</tr>";
        }
        $c_wd == 7 ? $c_wd = 1 : $c_wd++;
    }
    // next month variables
    if ($month == 12) {
        $tmp_month = 1;
        $tmp_year = $year + 1;
    } else {
        $tmp_month = $month + 1;
        $tmp_year = $year;
    }
    // finish with the next months entries
    for ($c_day = 1; $c_wd <= 7 && $c_wd > 1; $c_wd++, $c_day++) {
        // fill differently for saturday and sunday
        if ($c_wd == 6) {
            $dayfill = "bgcolor=" . TMPL_calFillSaturday;
        } else {
            if ($c_wd == 7) {
                $dayfill = "bgcolor=" . TMPL_calFillSunday;
            } else {
                $dayfill = "";
            }
        }
        $OUTPUT .= "<td {$dayfill} width=75 height=60 align=center>\r\n\t\t\t\t<a id='calSmallMonthOMLink' href='" . SELF . "?mday={$c_day}&month={$tmp_month}&year={$tmp_year}'>{$c_day}</a>\r\n\t\t\t</td>";
    }
    // finish the tables and return
    $OUTPUT .= "\r\n\t\t\t</tr>\r\n\t\t</table>\r\n\r\n\t\t\t</td>\r\n\t\t</tr>\r\n\t</table>";
    return $OUTPUT;
}