<?php

#--------------------------------------------------------------
# program:    common.php
#
# written by:    John Phillips on 3/5/2003
#
# description:    This script contains common functions.
#
# revisions:    
# 2003.03.09    Latest version
#--------------------------------------------------------------

$HTTP_HOST     "157.62.24.146";

# location of index.php

$DOC_ROOT     "~reserve/reservation";         

# location of main.php and others relative to $DOC_ROOT

$WORKING_DIR    "y03b1";    
$SITE_NAME     "reservation.com";
$DB_SERVER     "localhost";
$DB_LOGIN     "reserve";
$DB_PASSWORD     "reserve";
$DB         "reserve";

# Year/semester prefix y03b1 = academic year 2002-2003 spring semester.

$YSP 'y03b1_'

function 
HTMLStart()
{
  
?>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
            "http://www.w3.org/TR/REC-html40/strict.dtd">
  <html>
  <head>
  <title>Classroom/Lab Reservation System</title>
  <link rel="stylesheet" type="text/css" href="reservation.css">
  </head>
  <body>
  <?php 
}

function 
HTMLEnd()
{
  print 
"</body></html>";
}

function 
PageFooter()
{
  
?>
  <br>
  <hr>
  <p align="center"><font size="1">copyright &copy;2003 by John Phillips</font></p>
  <?php
}

function 
SqlStart()
{
  global 
$DB_SERVER$DB_LOGIN$DB_PASSWORD$DB;

  
$link MYSQL_CONNECT$DB_SERVER$DB_LOGIN$DB_PASSWORD ) ||
    die( 
"Our SQL server is having problems. Try again in a couple of hours.\n\n" );

  
MYSQL_SELECT_DB"$DB" ) || die( "Could not open $DB\n" ); 
}

function 
FormatSQLTimeStamp$ts )
{
  return 
ereg_replace"([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",
  
" \\1-\\2-\\3 \\4:\\5:\\6"$ts );
}

function 
array2select$arr$sel )
{
  
$numElements count$arr );
  for( 
$i=0$i $numElements; ++$i )
  {
    if( 
$arr[$i] == $sel 
    { 
      print 
"<option selected>$arr[$i]</option>\n"
    } 
    else 
    { 
      print 
"<option>$arr[$i]</option>\n"
    }
  }
}

function 
ValidateSession$sessionString$YSP )
{
  
$userId "";

  
# clear out old sessions; 300 = 60*5 or 5 minute delay before auto logout
  
$q "DELETE FROM ${YSP}session WHERE timeStamp < '";
  
$q.= date("Y-m-d H:i:s", (time()-300));
  
$q.= "'";
  
MYSQL_QUERY$q ) || die( "could not delete sessions: $q" );

  
# look for the session
  
$q "SELECT sessionId, userId FROM ${YSP}session WHERE sessionString = '$sessionString'";
  
$qResult MYSQL_QUERY$q );

  if( 
MYSQL_NUM_ROWS$qResult ) )
  {
    
$qRow MYSQL_FETCH_ARRAY$qResult );
    
$userId $qRow['userId'];
    
$sessionId $qRow['sessionId'];

    
# found it so now update the timestamp
    
$q "UPDATE ${YSP}session SET timeStamp = NULL WHERE sessionId = $sessionId";
    
$qResult MYSQL_QUERY$q ) || die( "bad session update: $q" );
    return( 
$userId );
  }
  die( 
'Session has expired: <a href="main.php">Relogin</a>' );
}

function 
calData$q ) {
  global 
$userId$sessionString;
  
$qResult MYSQL_QUERY$q ) or die( "could not select: $q" );
  
$numRows mysql_num_rows$qResult );
  if( 
== $numRows ) {
    print 
"<div class='event'><span class='loc'>&nbsp;</span></div>";
  }
  else {
    while( 
$qRow MYSQL_FETCH_ARRAY$qResult ) ) {
      
$sTime PMTimeNoPM$qRow['startTime'] );
      
$eTime PMTime$qRow['endTime'] );
      
$note $qRow['note'];
      
$initials $qRow['initials'];
      
$reservationId $qRow['reservationId'];
      print 
"<div class='event'><span class='time'>$sTime-$eTime</span>";

      if( 
$qRow['userId'] == $userId ) {
        print 
"<span class='loc2'>$note $initials</span>";
        print 
"<form method='post' id='formdelete'>";
        print 
"<input type='hidden' name='reservationId' value='$reservationId'>";
        print 
"<input type='hidden' name='sessionString' value='$sessionString'>";
        print 
"<input type='submit' name='delete' value='Delete'>";
        print 
"</form>";
      }
      else {
        print 
"<span class='loc'>$note $initials</span>";
      }
      print 
"</div>";
    }
  }
}

function 
PMTime$time ) {
  list( 
$hour$minute$sec ) = split':'$time );

  if( 
$hour >= 12 ) {
    if( 
$hour != 12 ) { $hour $hour 12; }
    
$sTime "$hour:${minute}PM";
  }
  else {
    
$sTime "$hour:$minute";
  }

  return 
$sTime;
}

function 
PMTimeNoPM$time ) {
  list( 
$hour$minute$sec ) = split':'$time );

  if( 
$hour 12 ) {
    
$hour $hour 12;
  }

  return 
"$hour:$minute";
}

function 
militaryHour$hour ) {
    if( 
$hour == '1' ) { return '13'; }
    elseif( 
$hour == '2' ) { return '14'; }
    elseif( 
$hour == '3' ) { return '15'; }
    elseif( 
$hour == '4' ) { return '16'; }
    elseif( 
$hour == '5' ) { return '17'; }
    elseif( 
$hour == '6' ) { return '18'; }
    elseif( 
$hour == '7pm' ) { return '19'; }
    elseif( 
$hour == '8pm' ) { return '20'; }
    elseif( 
$hour == '9pm' ) { return '21'; }
    else { return 
$hour; } 
}

function 
calForm $date ) {
  global 
$sessionString;
  
$hours = array('7','8','9','10','11','12','1','2','3','4','5','6','7pm','8pm','9pm');
  
$startMin = array('00','15','30','45');
  
$endMin = array('00','15','30','45','50');

  print 
"<form method='POST' id='formreserve'>";
  print 
"<div class='reserve'>";
  print 
'Start<br>';
  print 
'<select name="startHour">' "\n";
  
array2select$hours'8' );
  print 
'</select>' "\n";

  print 
'<select name="startMin">' "\n";
  
array2select$startMin'00' );
  print 
'</select><br>' "\n";

  print 
'End<br>';
  print 
'<select name="endHour">' "\n";
  
array2select$hours'8' );
  print 
'</select>' "\n";

  print 
'<select name="endMin">' "\n";
  
array2select$endMin'50' );
  print 
'</select><br>' "\n";

  print 
"Course/Note<br>";
  print 
"<input type=text name=note value='' size=13>";
  print 
"<input type=hidden name=date value='$date'>";
  print 
"<input type=hidden name=sessionString value='$sessionString'>";
  print 
"<br><input type=submit name='post2' value='reserve'>";
  print 
"</div></form>";
}

?>