<?
$sacha_ajax_url 
"include/server.php";
if (!
class_exists ('xajax'))
{
  require (
'xajax.inc.php');
  
$xajax = new xajax($sacha_ajax_url);
  
$xajax->registerFunction("sacha_calendar");
  
$xajax->processRequests();
}

$this_page "";
function 
pad($s$n)
{
    
$r $s;

    while (
strlen($r) < $n)
    {
        
$r "0".$r;
    }
    return 
$r;
}

function 
getDirName($day$month$year)
{
    return 
pad($year4) . "." pad($month2) . "." pad($day2) . ".php";
}

class 
PlannerCalendar extends Calendar
{
    function 
PlannerCalendar($dates)
    {
        
$this->dates $dates;
    }
    
    function 
getDateLink($day$month$year)
    {
        
$dir getDirName($day$month$year);
        if (
in_array($dir$this->dates))
        {
            return 
$dir;
        }
        else
        {
            return 
"";
        }
    }

  
// A function to specify the link of the month name in the calendar, as opposed the previous and next
  // month links (which still use getCalendarLink, see below).
  //
  // I had to slightly modify calendar.php for this to work (see getMonthHTML definition).
  
function getMonthNameLink($month$year)
  {
    
$dir pad($year4) . "." pad($month2) . ".php";
    if (
in_array($dir$this->dates))
    {
      return 
$dir;
    }
    else
    {
      return 
"";
    }
  }

  
// Now, it simply reloads the same script, but with a different month/name parameters set (for the previous
  // and next month calendar links).
    
function getCalendarLink($month$year)
    {
      if (
$month == 0)
      {
         return 
"Year" $year ".php";
      }
      else
      {
         return 
getenv('SCRIPT_NAME') . "?month=" urlencode($month) . "&amp;year=" urlencode($year);
      }
    }


  function 
isDaySelected ($year$month$day)
  {
    
// The third element of the split needs to be the day number, not the rest of the string.
    // So i set the limit to 4 here.
    
$split preg_split("/\./"basename (getenv('SCRIPT_NAME')), 4);
    return (
$year == $split[0] && $month == $split[1] && $day == $split[2]) ? true false;
  }
}


function 
binsearch($display$dates)
{
    
// Binary search through the dates to find the one being looked for 
    
$left 0;
    
$right count($dates) - 1;
    
$mid = ($left $right) / 2;
    while (
$left <= $right && $dates[$mid] != $display)
    {
        if (
$display $dates[$mid]) { $right $mid 1; }
        else if (
$display $dates[$mid]) { $left $mid 1; }
        
$mid = (int) (($left $right) / 2);
    }
    return 
$mid;
}

function 
getdates($dir)
{
    
$dates = array();       
    if (
$dir opendir("$dir"))
    {
        while ((
$file readdir($dir)) !== false)
        {      
      if (
preg_match("/^\d{4}\.\d\d(\.\d\d)?\.php$/"$file)) // this one also match "2005.01.php"-like files
            
{
               
$dates[] = $file;
            }
        }
        
closedir($dir);
    }
   
sort($dates);
   return 
$dates;
}

$dates getdates(".");
$cal = new PlannerCalendar($dates);
?>