//echo "fred";
/*
 +-------------------------------------------------------------------+
 |                  H T M L - C A L E N D A R   (v2.11)              |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: May 27, 2003                Last modified: Nov. 29, 2009 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+
 EXAMPLE #1:  $myCal = new CALENDAR();
              echo $myCal->create();
 EXAMPLE #2:  $myCal = new CALENDAR(2004, 12);
              echo $myCal->create();
 EXAMPLE #3:  $myCal = new CALENDAR();
              $myCal->year = 2004;
              $myCal->month = 12;
              echo $myCal->create();
 Returns HTML code
==========================================================================================================
*/
  error_reporting(E_WARNING);
  $cal_ID = 0;
  class CALENDAR {
//========================================================================================================
// Configuration
//========================================================================================================
    var $tFontFace = 'Arial, Helvetica'; // title: font family (CSS-spec, e.g. "Arial, Helvetica")
    var $tFontSize = 12;                 // title: font size (pixels)
    var $tFontColor = '#FFFFFF';         // title: font color
    var $tBGColor = '#304B90';           // title: background color
    var $hFontFace = 'Arial, Helvetica'; // heading: font family (CSS-spec, e.g. "Arial, Helvetica")
    var $hFontSize = 10;                 // heading: font size (pixels)
    var $hFontColor = '#FFFFFF';         // heading: font color
    var $hBGColor = '#304B90';           // heading: background color
    var $dFontFace = 'Arial, Helvetica'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
    var $dFontSize = 10;                 // days: font size (pixels)
    var $dFontColor = '#000000';         // days: font color
    var $dBGColor = '#FFFFFF';           // days: background color
    var $wFontFace = 'Arial, Helvetica'; // weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
    var $wFontSize = 10;                 // weeks: font size (pixels)
    var $wFontColor = '#FFFFFF';         // weeks: font color
    var $wBGColor = '#304B90';           // weeks: background color
    var $saFontColor = '#0000D0';        // Saturdays: font color
    var $saBGColor = '#F6F6FF';          // Saturdays: background color
    var $suFontColor = '#D00000';        // Sundays: font color
    var $suBGColor = '#FFF0F0';          // Sundays: background color
    var $tdBorderColor = '#FF0000';      // today: border color
    var $borderColor = '#304B90';        // border color
    var $hilightColor = '#FFFF00';       // hilight color (works only in combination with link)
    var $link = '';                      // page to link to when day is clicked
    var $offset = 1;                     // week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
    var $weekNumbers = true;             // view week numbers: true = yes, false = no
//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
    // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
    var $weekdays = array('Sa', 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr');
    // months: must start with January
    var $months = array('January', 'February', 'March', 'April', 'May', 'June',
                        'July', 'August', 'September', 'October', 'November', 'December');
    // error messages
    var $error = array('Year must be 1 - 3999!', 'Month must be 1 - 12!');
//--------------------------------------------------------------------------------------------------------
// Don't change from here:
//--------------------------------------------------------------------------------------------------------
    var $year, $month, $size;
    var $mDays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    var $specDays = array();
//========================================================================================================
// Functions
//========================================================================================================
    function CALENDAR($year = '', $month = '', $week = '') {
      if($year == '' && $month == '') {
        $year = date('Y');
        $month = date('n');
      }
      else if($year != '' && $month == '') $month = 1;
      $this->year = (int) $year;
      $this->month = (int) $month;
      $this->week = (int) $week;
    }
    function set_styles() {
      global $cal_ID;
      $cal_ID++;
      $html = '';
      return $html;
    }
    function leap_year($year) {
      return (!($year % 4) && ($year < 1582 || $year % 100 || !($year % 400))) ? true : false;
    }
    function get_weekday($year, $days) {
      $a = $days;
      if($year) $a += ($year - 1) * 365;
      for($i = 1; $i < $year; $i++) if($this->leap_year($i)) $a++;
      if($year > 1582 || ($year == 1582 && $days >= 277)) $a -= 10;
      if($a) $a = ($a - $this->offset) % 7;
      else if($this->offset) $a += 7 - $this->offset;
      return $a;
    }
    function get_week($year, $days) {
      $firstWDay = $this->get_weekday($year, 0);
      return floor(($days + $firstWDay) / 7) + ($firstWDay <= 3);
    }
    function table_cell($content, $class, $date = '', $style = '') {
      global $cal_ID;
      $size = round($this->size * 1.5);
      $html = '
link;
        if($this->specDays[$content]) {
          if($this->specDays[$content][0]) {
            $style .= 'background-color:' . $this->specDays[$content][0] . ';';
          }
          if($this->specDays[$content][1]) {
            $html .= ' title="' . $this->specDays[$content][1] . '"';
          }
          if($this->specDays[$content][2]) $link = $this->specDays[$content][2];
        }
        if($link) {
          $link .= strstr($link, '?') ? "&date=$date" : "?date=$date";
          $html .= ' onMouseOver="this.className=\'cssHilight' . $cal_ID . '\'"';
          $html .= ' onMouseOut="this.className=\'' . $class . '\'"';
          $html .= ' onClick="document.location.href=\'' . $link . '\'"';
        }
      }
      if($style) $html .= ' style="' . $style . '"';
      $html .= '>' . $content . ' | ';
      return $html;
    }
    function table_head($content) {
      global $cal_ID;
      $cols = $this->weekNumbers ? 8 : 7;
      $html = '
| ' .
              $content . ' | 
';
      for($i = 0; $i < count($this->weekdays); $i++) {
        $ind = ($i + $this->offset) % 7;
        $wDay = $this->weekdays[$ind];
        $html .= $this->table_cell($wDay, 'cssHeading' . $cal_ID);
      }
      if($this->weekNumbers) $html .= $this->table_cell(' ', 'cssHeading' . $cal_ID);
      $html .= '
';
      return $html;
    }
    function viewEvent($from, $to, $color, $title, $link = '') {
      if($from > $to) return;
      if($from < 1 || $from > 31) return;
      if($to < 1 || $to > 31) return;
      while($from <= $to) {
        $this->specDays[$from] = array($color, $title, $link);
        $from++;
      }
    }
    function create() {
      global $cal_ID;
      $this->size = ($this->hFontSize > $this->dFontSize) ? $this->hFontSize : $this->dFontSize;
      if($this->wFontSize > $this->size) $this->size = $this->wFontSize;
      list($curYear, $curMonth, $curDay) = explode('-', date('Y-m-d'));
      if($this->year < 1 || $this->year > 3999) $html = '
' . $this->error[0] . '';
      else if($this->month < 1 || $this->month > 12) $html = '
' . $this->error[1] . '';
      else {
        $this->mDays[1] = $this->leap_year($this->year) ? 29 : 28;
        for($i = $days = 0; $i < $this->month - 1; $i++) $days += $this->mDays[$i];
        $start = $this->get_weekday($this->year, $days);
        $stop = $this->mDays[$this->month-1];
        $html = $this->set_styles();
        $html .= '
';
        $html .= '| borderColor ? ' bgcolor=' . $this->borderColor  : '') . '>';
        $html .= ' ';
        $title = htmlentities($this->months[$this->month-1]) . ' ' . $this->year;
        $html .= $this->table_head($title);
        $daycount = 1;
        if(($this->year == $curYear) && ($this->month == $curMonth)) $inThisMonth = true;
        else $inThisMonth = false;
        if($this->weekNumbers || $this->week) $weekNr = $this->get_week($this->year, $days);
        while($daycount <= $stop) {
          if($this->week && $this->week != $weekNr) {
            $daycount += 7;
            $weekNr++;
            continue;
          }
          $html .= '';
          for($i = $wdays = 0; $i <= 6; $i++) {
            $ind = ($i + $this->offset) % 7;
            if($ind == 0) $class = 'cssSaturdays';
            else if($ind == 1) $class = 'cssSundays';
            else $class = 'cssDays';
            $style = '';
            $date = $this->year . '-' . $this->month . '-' . $daycount;
            if(($daycount == 1 && $i < $start) || $daycount > $stop) $content = ' ';
            else {
              $content = $daycount;
              if($inThisMonth && $daycount == $curDay) {
                $style = 'padding:0px;border:3px solid ' . $this->tdBorderColor . ';';
              }
              else if($this->year == 1582 && $this->month == 10 && $daycount == 4) $daycount = 14;
              $daycount++;
              $wdays++;
            }
            $html .= $this->table_cell($content, $class . $cal_ID, $date, $style);
          }
          if($this->weekNumbers) {
            if(!$weekNr) {
              if($this->year == 1) $content = ' ';
              else if($this->year == 1583) $content = 52;
              else $content = $this->get_week($this->year - 1, 365);
            }
            else if($this->month == 12 && $weekNr >= 52 && $wdays < 4) $content = 1;
            else $content = $weekNr;
            $html .= $this->table_cell($content, 'cssWeeks' . $cal_ID);
            $weekNr++;
          }
          $html .= '';
        }
        $html .= ' | 
';
      }
      return $html;
    }
  }
?>