<?php

/***************************************************************************

 minicalendar.php
 -----------------
 copyright : (C) 2002-2003 The ezContents Development Team

 ***************************************************************************/

/***************************************************************************
 The ezContents Development Team offers no warranties on this script.
 The owner/licensee of the script is solely responsible for any problems
 caused by installation of the script or use of the script.

 All copyright notices regarding ezContents must remain intact on the
 scripts and in the HTML for the scripts.

 For more info on ezContents,
 visit http://www.ezcontents.org/

/***************************************************************************

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the License which can be found within the
 *   zipped package.
 *
 ***************************************************************************/

global $HTTP_SERVER_VARS;
if ( (substr($HTTP_SERVER_VARS["PHP_SELF"],-11) == 'control.php') ||
	 (substr($HTTP_SERVER_VARS["PHP_SELF"],-10) == 'module.php') ||
	 (substr($HTTP_SERVER_VARS["PHP_SELF"],-16) == 'showcontents.php') ) {
	 require_once('./modules/moduleSec.php');
} else {
	require_once('../moduleSec.php');
}

if (!isset($GLOBALS["gsLanguage"])) { Header("Location: ".$GLOBALS["rootdp"]."module.php?link=".$GLOBALS["modules_home"].$GLOBALS["ModuleRef"]."/minicalendar.php"); }

$GLOBALS["ModuleName"] = 'calendar';

include_once ($GLOBALS["language_home"].$GLOBALS["gsLanguage"].'/lang_admin.php');
include_once ($GLOBALS["language_home"].$GLOBALS["gsLanguage"].'/lang_main.php');
include_languagefile ($GLOBALS["modules_home"].$GLOBALS["ModuleRef"].'/',$GLOBALS["gsLanguage"],'lang_calendar.php');


$todaydate = strftime("%d");
$todaymonth = strftime("%m");
$todayyear = strftime("%Y");

if (!isset($HTTP_POST_VARS["month"]))
{
   $HTTP_POST_VARS["month"] = $todaymonth;
   if ($HTTP_POST_VARS["month"] < 10) { $HTTP_POST_VARS["month"] = substr($HTTP_POST_VARS["month"],1,1); }
}
if (!isset($HTTP_POST_VARS["year"])) $HTTP_POST_VARS["year"] = $todayyear;

$month_titles = $GLOBALS["tMonth_Array"];
$week_titles = $GLOBALS["tWeek_Array"];
$month_text = $month_titles[$HTTP_POST_VARS["month"]];

SubFormHeader('NewDate');
?>
<table border="0" class="headercontent">
    <tr><td valign="top" align="<?php echo $GLOBALS["right"]; ?>" class="header" colspan="7">
            <table border="0" align="<?php echo $GLOBALS["right"]; ?>" cellpadding="4" cellspacing="1" class="tablecontent">
                <tr><td align="center">
		        <table border=0><tr><td valign="bottom" align="center">
			    <tr><td align="<?php echo $GLOBALS["right"]; ?>"><b><?php echo $GLOBALS["calendar_month"]; ?>:</b></td>
			        <td><select name="month"><?php RenderMonths($HTTP_POST_VARS["month"]); ?></select></td>
			    </tr>
			    <tr><td align="<?php echo $GLOBALS["right"]; ?>"><b><?php echo $GLOBALS["calendar_year"]; ?>:</b></td>
			        <td><select name="year"><?php RenderYears($HTTP_POST_VARS["year"]); ?></select></td>
			    </tr>
			</table>
    			<input type="submit" name="submit" value="<?php echo $GLOBALS["calendar_button"]; ?>">
		    </td>
		</tr>
	    </table>
	</td>
    </tr>
<?php
SubFormFooter();


// Determine the total number of days in the month
$totaldays = 31;
while (!checkdate( $HTTP_POST_VARS["month"], $totaldays + 1, $HTTP_POST_VARS["year"] )) { $totaldays--; }
$totaldays++;

// Build table
?><tr border="1" cellpadding="5" cellspacing="2" class="tablecontent"><?php
for ( $x = 0; $x < 7; $x++ ) echo '<th><b>'.substr($week_titles[$x],0,1).'</b></th>';
echo '</tr>';

$day = 1;
// Ensure that a number of blanks are put in so that the first day of the month lines up with the proper day of the week
// $offset = date( "w", mktime( 0, 0, 0, $month, $day, $year )) + 1;
$offset = date( "w", mktime( 0, 0, 0, $HTTP_POST_VARS["month"], $day, $HTTP_POST_VARS["year"] ));
if ($offset == 7) { $offset = 0; }
echo '<tr>';
if ($offset > 0) echo str_repeat( '<td>&nbsp;</td>', $offset );

// Start entering in the information
for ( $day = 1; $day <= $totaldays; $day++ )
{
   if (($HTTP_POST_VARS["year"] == $todayyear) && ($HTTP_POST_VARS["month"] == $todaymonth) && ($day == $todaydate))
   {
      echo '<td align="center"><b><font color="Red">'.$day.'</font></b></td>';
   }
   else
   {
      if (($offset == 0) || ($offset == 6))
      {
         echo '<td align="center"><b><font color="Blue">'.$day.'</font></b></td>';
      }
      else
      {
         echo '<td align="center">'.$day.'</td>';
      }
   }
   $offset++;
   // If we're on the last day of the week, wrap to the other side
   if ($offset > 6)
   {
      $offset = 0;
      echo '</tr>';
      if ( $day < $totaldays ) echo '<tr>';
   }
}

// Fill in the remaining spaces for the end of the month, just to make it look pretty
if ($offset > 0) $offset = 7 - $offset;
if ($offset > 0) echo str_repeat( '<td>&nbsp;</td>', $offset );

?>
</tr></table>
<?php


function RenderMonths($sDate)
{
   global $month_titles;
   for ( $sMonth = 1; $sMonth <= 12; $sMonth++ ) {
      $s = "";
      if ($sMonth == $sDate) $s = " selected";
      echo '<option value="'.$sMonth.'"'.$s.'>'.substr($month_titles[$sMonth],0,3).'</option>';
   }
} // function RenderMonths()


function RenderYears($sDate)
{
   $sYear = strftime("%Y") - 2;
   $eYear = $sYear + 32;
   while ($sYear <= $eYear) {
      $s = "";
      if ($sYear == $sDate) $s = " selected";
      echo '<option value="'.$sYear.'"'.$s.'>'.$sYear.'</option>';
      $sYear++;
   }
} // function RenderYears()

?>
