<?php

  /*

      calendar.inc

      Calender of find.php. [Commented]

      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
  
      ----
  
      Modified by:
        Copyright (C) 2005  1stBnA GmbH 
        Copyright (C) 2005  Stephan Kaufhold (s.kauhold@1stbna.com)

      From original (calendar.js)
        Copyright (C) 2003  Central Manchester CLC
        Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

  // Includes
  require_once 'system/db.php';
  require_once 'system/lang.php';
  require_once 'system/user_preferences.php';
 
  // Language selection
  set_text_domain("calendar");  
?>

<script language="javascript" type="text/javascript">
//<![CDATA[
<!--

  function showCalendar(element, formfield) {
    showCalendarOnDate(element, formfield, '*', '*');
  }

  function showCalendarOnDate(element, formfield, month, year) {
    days = ["<?php echo gettext("Sun")?>", "<?php echo gettext("Mon")?>", "<?php echo gettext("Tue")?>", "<?php echo gettext("Wed")?>", "<?php echo gettext("Thu")?>", "<?php echo gettext("Fri")?>", "<?php echo gettext("Sat")?>"];
    months = ["<?php echo gettext("January")?>", "<?php echo gettext("February")?>", "<?php echo gettext("March")?>", "<?php echo gettext("April")?>", "<?php echo gettext("May")?>", "<?php echo gettext("June")?>", "<?php echo gettext("July")?>",
      "<?php echo gettext("August")?>", "<?php echo gettext("September")?>", "<?php echo gettext("October")?>", "<?php echo gettext("November")?>", "<?php echo gettext("December")?>"];
    today = new Date();
    // Note - months > 11 (december in 0 based months) will advance the year by
    // 1 - hence the apparant weirdness of the method below.
    if (year != '*') today.setYear(year);
    if (month != '*') today.setMonth(month);
    year = today.getYear();
    if (year < 2000) { year += 1900; }
    month = today.getMonth();
    /* Nice trick to get number of days in the month
    */
    daysinmonth = 32 - new Date(year, month, 32).getDate();
    output = "<table class=\"calendartable\">";

    // Month header
    output += "<tr><th colspan=\"1\"><a href=\"javascript:showCalendarOnDate('"
    + element + "','" + formfield + "'," + (month - 1) + "," + year + ")\">" +
    "&lt;</a></th>" +
    "<th colspan=\"5\">" + months[month] + " " + year + "</th>" +
    "<th colspan=\"1\"><a href=\"javascript:showCalendarOnDate('" +
    element + "','" + formfield + "'," + (month + 1) + "," + year + ")\">" +
    "&gt;</a></th></tr>";

    // Days of the week header
    output += "<tr>";
    for (d = 0; d< 7; d++) {
        output += "<th>" + days[d] + "</th>";
    }
    output += "</tr>"

    // Pad the beginning of the month with blank cells
    daymonthstarts = new Date(year, month, 1).getDay();
    if (daymonthstarts != 0) {
        output += "<tr>";
        for (d = 0; d< daymonthstarts; d++) {
            output += "<td>&nbsp;</td>";
        }
    }

    // Now put the actual dates in
    currentday = daymonthstarts;
    for (d = 0; d < daysinmonth; d++) {
        if (currentday == 0) output += "<tr>";
        // TODO - generate table here
        output += "<td><a href=\"javascript:writeDate('" + element + "','" +
        formfield + "','" + (d + 1) + "/" + (month + 1)+ "/" + year + "')\">"
        + (d + 1) + "</a></td>";
        if (currentday == 6) {
            output += "</tr>";
            currentday = 0;
        } else {
            currentday++;
        }
    }

    // Pad the end of the month
    if (currentday != 0) {
        for (d = currentday; d<7; d++) {
            output += "<td>&nbsp;</td>";
        }
    }
    output += "</tr>";
    output += "</table>";
    document.getElementById(element).innerHTML = output
    document.getElementById(element).style.visibility = "visible";
  }

  function writeDate(element, formelement, date) {
    document.getElementById(formelement).value = date;
    document.getElementById(element).style.visibility = "hidden";
  }

//-->
//]]>
</script>