Chris Pollett >
Old Classes >
PIC40

   ( Print View )

Lecture Notes-PDF

Spring '01 Ad: Enrollment info

Course Info: Homework Assignments:
Practice Exams:
PIC:
                           












HW5 Solutions Page

Return to homework page.

<? if($State=="Save Itinerary"){SetCookies();}
//
// FileName: p40hw5file1.php (Travel Planner program)
//
// Purpose: This file accesses the airtransportation database to allow
//          user to make a travel itinerary based on the flight
//          information stored in this database.
//
// Remark:  A PERL solution is given after this one.
//
?>

<html>
  <head>
    <title>Travel Planner</title>
  </head>
  <body bgcolor="#99FFCC">

  <?
	//
        // Main Loop:
        //
        // $State variable is used to control which page is displayed

	switch($State)
	{
		case "Fetch Now":
		  Itinerary("saved");
		break;

		case "Save Itinerary":

		  Intro("Itinerary Saved");
		break;

		case "Show Flights":
                  $MSG = CKAirport();
                   if($MSG == "Flight")
                     {Flight();}
                   else if($MSG != "done")
                     {Intro($MSG);}
                break;

		case Previous:
		  if($SEAT2) {Flight();}
                  else {SelectSeat();}
		break;

                case Flight:
	          Flight();
		break;

                case "Continue":
		  if($SEAT)
                       {Itinerary("unsaved");}
                  else
                      {SelectSeat();}
		break;

		case "Main Menu":
		default:
		  Intro();
	}


  ?>

  </body>
</html>

<?

//
// Function: Intro
//
// Purpose: This function takes a message $MSG and if it exists
//          outputs in blue followed by the intro screen to
//          the Travel Planner program. If $MSG is false it
//          just outputs the intro screen. If $MSG has some value
//          then any of the form variables is not set it also
//          outputs a star by this element in the intro screen.
//
function Intro($MSG=false) {
  GLOBAL $DEPCITY, $DESCITY, $DEPDATE;



  if ($MSG)   {print "<h3 style=\"color:blue\"> $MSG</h3>";}
 ?>

  <h1>Travel Planner</h1>

  <form method="POST" action="hw5.php">

  <h3>Previous Itinerary:
     <? OutButton("submit","State","Fetch Now");?>
  </h3>
  <hr>

    <table border="0" cellpadding="3" cellspacing="3">
     <tr>
      <td><b>Departure City or Airport:</b></td>
      <td><? OutText("DEPCITY", "$DEPCITY", 15, 80);
             if($MSG && !$DEPCITY) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td><b>Destination City or Airport:</b></td>
      <td><?  OutText("DESCITY", "$DESCITY", 15, 80);
             if($MSG && !$DESCITY) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td><b>Departure Date:</b></td>
      <td><?  OutText("DEPDATE", "$DEPDATE", 15, 80);
             if($MSG && !$DEPDATE) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td></td>
      <td>YYYY-MM-DD
      </td>
     </tr>
    <td align="center" colspan="2">
    <h3>
     <?OutButton("reset","reset","Reset");
       OutButton("submit","State","Show Flights");?>
    </h3>
    </td>
    </table>
  </form>
<?}

//
// Function:CKAirport
//
// Purpose: this function check if the three variables
//          $DEPCITY, $DESCITY, $DEPDATE all have values.
//          If not it returns the string "Sorry, you did not give
//          me enough data. Please fill in the starred fields."
//          Otheriwse, it looks at the Airport table of the airtransportation
//          database to see if $DEPCITY and $DESCITY are codes for
//          airports or are cities. If they are cities but there
//          is only one airport in that city the $DEPCITY or $DESCITY
//          is then set to the code of that airport. If at this
//          point both $DEPCITY and $DESCITY are codes of airports
//          then the value "Flight" is returned. If neither a code nor
//          a city could be found then "Sorry we not service $DEPCITY"
//          or "Sorry we nor service $DESCITY" or both is returned
//          by CKAirport. Finally, if there is more airport for $DESCITY
//          of $DEPCITY of both it produces a form with selection
//          selection gadgets to let the user pick between the possibilities.
//          after generating this form it returns the value "done".
//

function CKAirport()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	$DEPCITY=DeleteSharp($DEPCITY);
	$DESCITY=DeleteSharp($DESCITY);

	if (!($DEPCITY && $DESCITY && $DEPDATE))
	{
		$ERRMSG = "Sorry, you did not give me enough data. Please";
                $ERRMSG .= " fill in the starred fields.";
		return $ERRMSG;
	}

	$dbh=Connect();

	$cur=Prepare("select code from Airport where city='$DEPCITY'
                                                  OR code='$DEPCITY'");
	if(NumRows($cur)==0) { $ERRMSG.=
                                  "Sorry, we do not service $DEPCITY.<br />\n";
                                $DEPCITY = false;
                              }
	if(NumRows($cur)==1) {$row=FetchRow($cur);
                                $DEPCITY = $row[0];
				$isAirport1=true;
                               }
	FreeCursor($cur);

	$cur=Prepare("select code from Airport where city='$DESCITY'
                                                  OR code='$DESCITY'");
	if(NumRows($cur)==0) { $ERRMSG .=
                                  "Sorry, we do not service $DESCITY.<br />\n";
                                $DESCITY = false;
                              }

	if(NumRows($cur)==1) {
                               $row=FetchRow($cur);
                                $DESCITY = $row[0];
				$isAirport2=true;
                               }
	FreeCursor($cur);

	if(!$DEPCITY || !$DESCITY) {Close($dbh); return $ERRMSG;}
	if($isAirport1 && $isAirport2) {Close($dbh); return "Flight";}

	?>

	<form method="POST" action="hw5.php"> <?
	  $cur=Prepare("select code from Airport where city='$DEPCITY'");
	  if(NumRows($cur) > 1) {
		print "<h3> Please select a departure airport:</h3>\n";
		DrawSelect($cur,"DEPCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DEPCITY","$DEPCITY");}

	  FreeCursor($cur);
	  $cur=Prepare("select code from Airport where city='$DESCITY'");
	  if(NumRows($cur) > 1) {
		print "<h3> Please select a destination airport:</h3>\n";
		DrawSelect($cur,"DESCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DESCITY","$DESCITY");}
	  FreeCursor($cur);
        OutButton("hidden","DEPDATE","$DEPDATE");
        OutButton("submit","State","Main Menu");
        OutButton("submit","State","Show Flights");
	?> </form> <?
	Close($dbh);
	return "done";
}

//
// Function: Flight
//
// Purpose: produces the screen that allows the user to
//          select the from a list of available flights
//          between $DEPCITY and $DESCITY on $DEPDATE.
//          Queries the airtransportation database Flight
//          table to get this info.
//

function Flight()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	$dbh=Connect();

	$cur=Prepare("select flightNo, airCraftName, dateTime
                      from Flight
                      where fromAirport='$DEPCITY'
                            AND toAirport='$DESCITY'
                            AND dateTime like '$DEPDATE%'");


 ?>
  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw5.php">

  <table>
  <tr>
  <td>
  <h3>Please select a flight:</h3>
  </td>
  </tr>
  <tr>
  <td>
  <?
	DrawSelect($cur, "FLIGHT", 10);

	Close($dbh);
  ?>
  <td>
  </tr>
  <tr>
  <td>
  <? OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Continue");
  ?>
  </tr>
  </td>
  </table>
  </form>
<?}

//
// Function: SetCookies
//
// Purpose: sets cookies to store the variables
//          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT
//

function SetCookies()
{
  GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT;
  SetCookie("DEPCSAVE","$DEPCITY");
  SetCookie("DESCSAVE","$DESCITY");
  SetCookie("DEPDSAVE","$DEPDATE");
  SetCookie("FLIGHTSAVE","$FLIGHT");
  SetCookie("SEATSAVE","$SEAT");
}

//
// Function: Itinerary
//
// Purpose: if $saved is equal to "saved". It copies the variables
//          stored in the cookies back into the variables
//          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT first.
//          In all cases, it then draws the itinerary that the
//          user has selected so far followed by the buttons
//          Main Menu, Previous, and Save Itinerary.
//          these either return to the Intro screen, select seat
//          screen or returns to Intro screen with saved info
//          message and where the first line of the program has been
//          execute to set the appropriate cookies.
//

function Itinerary($saved)
{ GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT;
  GLOBAL $DEPCSAVE, $DESCSAVE, $DEPDSAVE, $FLIGHTSAVE, $SEATSAVE;

  	if($saved=="saved")
	{
		if(!$DEPCSAVE) return Intro("No previous itinerary");
		$DEPCITY=$DEPCSAVE;
		$DESCITY=$DESCSAVE;
		$DEPDATE=$DEPDSAVE;
		$FLIGHT=$FLIGHTSAVE;
		$SEAT=$SEATSAVE;
	}

	$flArray = split( "#", $FLIGHT);
	$flightNo = $flArray[0];
 ?>
  <h1>Travel Planner</h1>
  <hr>
  <h3>Your current schedule is as follows:</h3>
  <p>You are leaving on: <? print "$DEPDATE."; ?></p>
  <p>You are flying from: <? print "$DEPCITY."; ?></p>
  <p>You are flying to: <? print "$DESCITY."; ?></p>
  <p>The flight number is: <? print "$flightNo."; ?></p>
  <p>Your seat is: <? print "$SEAT."; ?></p>
  <form method="POST" action="hw5.php">
  <?   OutButton("hidden","DEPCITY",$DEPCITY);
       OutButton("hidden","DESCITY",$DESCITY);
       OutButton("hidden","DEPDATE",$DEPDATE);
       OutButton("hidden","FLIGHT",$FLIGHT);
       OutButton("hidden","SEAT",$SEAT);
       OutButton("submit","State","Main Menu");
       OutButton("submit","State","Previous");
       OutButton("submit","State","Save Itinerary"); ?>
  </form>
<?
 }

//
// Function: SelectSeat
//
// Purpose: This function draws the screen to allow the user to pick a
//          from the available seats on a given flight.
//

function SelectSeat()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT;

	$flArray = split( "#", $FLIGHT);
	$flightNo = $flArray[0];
        $aircraft = $flArray[1];
	$dateTime = $flArray[2];

 ?>
  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw5.php">

  <table>
  <tr>
  <td>
  <h3>Please select a seat:</h3>
  </td>
  </tr>
  <tr>
  <td>
  The information below is formatted as follows:
  </td>
  </tr>

  <tr>
  <td>
   Row#Seat#Aisle?#Window?#Class
  </td>
  </tr>

  <tr>
  <td>
  <?
	print "<select name=\"SEAT\" size=\"10\">\n";
	$flag=true;

	$dbh=Connect();

	//
	// Notice I open up two cursors at this point. I wouldn't
        // need to do this on a database
	//

	$cur=Prepare("select rowNo, seat,aisle,window, class
                      from AircraftRow
                      where airCraftName = '$aircraft'");
	while($row = FetchRow($cur))
	{

	   $cur2=Prepare("select rowNo, seat from Bookings
                          where flightNo='$flightNo'
			  and dateTime='$dateTime'
                          and rowNo='$row[0]' and seat='$row[1]' ");
	  if(!FetchRow($cur2))
		{
		   OutOption($flag,$row);
                   if($flag) {$flag=false;}
                }
	}



	Close($dbh);
  ?>
     </select>
  <td>
  </tr>
  <tr>
  <td>
  <? OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("hidden","FLIGHT",$FLIGHT);
     OutButton("hidden","SEAT2","true");
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Previous");
     OutButton("submit","State","Continue");
  ?>
  </tr>
  </td>
  </table>
  </form>

<?}

//
// Function: OutButton
//
// Purpose: draws to screen a button of type $type with name $name
//          and with value $value.
//

function OutButton($type,$name,$value)
{
 print "<input type=\"$type\" name=\"$name\" value =\"$value\" />";
}

//
// Function: OutText
//
// Purpose: draws to screen a textfield of type $type with name $name
//          and with value $value of size $size and max number of char's
//          maxlength.
//


function OutText($name,$value,$size,$maxLength)
{
 print "<input type=\"text\" name=\"$name\" value =\"$value\"";
 print " size=\"$size\" maxlength=\"$maxLength\" />";
}

//
// Function: OutStar
//
// Purpose: draws a bold-face blue star
//

function OutStar()
{
	print "<b style=\"color:blue\">*</b>\n";
}

//
// Function: OutOption
//
// Purpose: Writes out an option of a selection gadget
//          If $selected is true this will be a selected option
//          of the selection gadget. $row is the value of the option.
//

function OutOption($selected,$row)
{
		if($selected) {print "<option selected=\"true\">";
                           $flag=false;
                          }
		else {print "<option>";}
                foreach($row as $field)
                {
			print "$field#";
		}?>
                </option>
<?}

//
// Function: DeleteSharp
//
// Purpose: returns the substring of $string before the first # symbol
//

function DeleteSharp($string)
{
	$row=split("#",$string);
	return $row[0];
}

//
// Function:DrawSelect
//
// Purpose: Draws a selection gadget with name $name where the options
//          are given by cycling over the rows returned by the database
//          cursor $cursor.
//

function DrawSelect($cursor, $name, $rowsVisible)
{
	print "<select name=\"$name\" size=\"$rowsVisible\">\n";
	$flag=true;

	while($row = FetchRow($cursor))
	{
                OutOption($flag,$row);
		if($flag) { $flag=false;}
         }?>
                </select>
<?}

//
// Function:Connect
//
// Purpose: connect to the database system.
//

function Connect()
{
	return mysql_connect("localhost", "pic40", "pic40");

}

//
// Function: FetchRow
//
// Purpose:
//

function FetchRow($cursor)
{
	return mysql_fetch_row($cursor);
}
//
// Function:NumRows
//
// Purpose: returns the numbers of rows left in database cursor $cursor.
//

function NumRows($cursor)
{
	return mysql_num_rows($cursor);
}

//
// Function: Prepare
//
// Purpose: prepares an SQL query to be executed
//

function Prepare($query)
{
	return mysql_db_query("airtransportation", $query);
}

//
// Function:FreeCursor
//
// Purpose: frees a cursor
//

function FreeCursor($cursor)
{
    mysql_free_result($cursor);
}

//
// Function: Close
//
// Purpose: closes a database handle $dbhandle
//

function Close($dbhandle)
{
    mysql_close($dbhandle);

}

?>

#!/usr/local/bin/perl
#
# FileName: p40hw5file2.cgi (Travel Planner program)
#
# Purpose: This file accesses the airtransportation database to allow
#          user to make a travel itinerary based on the flight
#          information stored in this database.
#
# Remark: This is the Perl solution

use DBI;
require "subparseform.lib";

Parse_Form();
$State = $formdata{'State'};
$DEPCITY = $formdata{'DEPCITY'};
$DESCITY = $formdata{'DESCITY'};
$DEPDATE = $formdata{'DEPDATE'};
$FLIGHT = $formdata{'FLIGHT'};
$SEAT = $formdata{'SEAT'};
$SEAT2 = $formdata{'SEAT2'};

if($State eq "Save Itinerary"){SetCookies();}


print<<HTML;
Content-type: text/html


<html>
  <head>
    <title>Travel Planner</title>
  </head>
  <body bgcolor="#99FFCC">
HTML


SWITCH:	{ #there is no switch statement in PERL!

		if($State eq "Fetch Now")
		  {if(Itinerary("saved")=="no"){Intro("No previous Itinerary");}
                   last SWITCH;}


		if($State eq "Save Itinerary")
		{
		   Intro("Itinerary Saved");
		   last SWITCH;
		}

		if($State eq "Show Flights")
		{
                  $MSG = CKAirport();
                  if($MSG eq "Flight")
                     {Flight();}
                   elsif($MSG ne "done")
                     {Intro($MSG);}
		   last SWITCH;
		}

		if($State eq "Previous")
		{
		  if($SEAT2) {Flight();}
                  else {SelectSeat();}
		  last SWITCH;
		}

                if($State eq "Flight")
		{
	          Flight();
		  last SWITCH;
		}

                if($State eq "Continue")
		{
		  if($SEAT)
                       {Itinerary("unsaved");}
                  else
                      {SelectSeat();}
		   last SWITCH;
		}

                Intro();
	}




print<<HTML;

  </body>
</html>
HTML

#
# Function: Intro
#
# Purpose: This sub takes a message $MSG and if it exists
#          outputs in blue followed by the intro screen to
#          the Travel Planner program. If $MSG is false it
#          just outputs the intro screen. If $MSG has some value
#          then any of the form variables is not set it also
#          outputs a star by this element in the intro screen.
#
sub Intro() {
  my $MSG=$_[0];

  #GLOBAL $DEPCITY, $DESCITY, $DEPDATE;



  if ($MSG)   {print "<h3 style=\"color:blue\"> $MSG</h3>";}

print<<HTML;
  <h1>Travel Planner</h1>

  <form method="POST" action="hw5perl.cgi">

  <h3>Previous Itinerary:
HTML
     OutButton("submit","State","Fetch Now");

print<<HTML;

  </h3>
  <hr>

  <form method="POST" action="hw5perl.cgi">
    <table border="0" cellpadding="3" cellspacing="3">
     <tr>
      <td><b>Departure City or Airport:</b></td>
      <td>
HTML

	     OutText("DEPCITY", "$DEPCITY", 15, 80);
             if($MSG && !$DEPCITY) {OutStar();}

print<<HTML;
      </td>
     </tr>
     <tr>
      <td><b>Destination City or Airport:</b></td>
      <td>
HTML
	     OutText("DESCITY", "$DESCITY", 15, 80);
             if($MSG && !$DESCITY) {OutStar();}

print<<HTML;
      </td>
     </tr>
     <tr>
      <td><b>Departure Date:</b></td>
      <td>
HTML

	     OutText("DEPDATE", "$DEPDATE", 15, 80);
             if($MSG && !$DEPDATE) {OutStar();}

print<<HTML;
      </td>
     </tr>
     <tr>
      <td></td>
      <td>YYYY-MM-DD
      </td>
     </tr>
    <td align="center" colspan="2">
    <h3>
HTML

       OutButton("reset","reset","Reset");
       OutButton("submit","State","Show Flights");

print<<HTML;
    </h3>
    </td>
    </table>
  </form>
HTML

}

#
# Function:CKAirport
#
# Purpose: this sub check if the three variables
#          $DEPCITY, $DESCITY, $DEPDATE all have values.
#          If not it returns the string "Sorry, you did not give
#          me enough data. Please fill in the starred fields."
#          Otheriwse, it looks at the Airport table of the airtransportation
#          database to see if $DEPCITY and $DESCITY are codes for
#          airports or are cities. If they are cities but there
#          is only one airport in that city the $DEPCITY or $DESCITY
#          is then set to the code of that airport. If at this
#          point both $DEPCITY and $DESCITY are codes of airports
#          then the value "Flight" is returned. If neither a code nor
#          a city could be found then "Sorry we not service $DEPCITY"
#          or "Sorry we nor service $DESCITY" or both is returned
#          by CKAirport. Finally, if there is more airport for $DESCITY
#          of $DEPCITY of both it produces a form with selection
#          selection gadgets to let the user pick between the possibilities.
#          after generating this form it returns the value "done".
#

sub CKAirport()
{
	#GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	$DEPCITY=DeleteSharp($DEPCITY);
	$DESCITY=DeleteSharp($DESCITY);
	my $ERRMSG, $dbh, $cur, $row, $field, isAirport1, isAirport2;

	if (!($DEPCITY && $DESCITY && $DEPDATE))
	{
		$ERRMSG = "Sorry, you did not give me enough data. Please";
                $ERRMSG .= " fill in the starred fields.";
		return $ERRMSG;
	}

	$dbh=Connect();


	$cur=Prepare($dbh, "select code from Airport where city='$DEPCITY'
                                                  OR code='$DEPCITY'");
	if(NumRows()==0)   { $ERRMSG.=
                                  "Sorry, we do not service $DEPCITY.<br />\n";
                                $DEPCITY = undef;
                              }
	if(NumRows()==1)    {@row=FetchRow($cur);
                                $DEPCITY = $row[0];
				$isAirport1=true;
                               }
	FreeCursor($cur);

	$cur=Prepare($dbh, "select code from Airport where city='$DESCITY'
                                                  OR code='$DESCITY'");
	if(NumRows()==0)    { $ERRMSG .=
                                  "Sorry, we do not service $DESCITY.<br />\n";
                                $DESCITY = undef;
                              }

	if(NumRows()==1) {
                               @row=FetchRow($cur);
                                $DESCITY = $row[0];
				$isAirport2=true;
                               }
	FreeCursor($cur);

	if(!$DEPCITY || !$DESCITY) {Close($dbh); return $ERRMSG;}
	if($isAirport1 && $isAirport2) {Close($dbh); return "Flight";}



	print "<form method=\"POST\" action=\"hw5perl.cgi\">\n";
	  $cur=Prepare($dbh, "select code from Airport where city='$DEPCITY'");
	  if(NumRows() > 1) {
		print "<h3> Please select a departure airport:</h3>\n";
		DrawSelect($cur,"DEPCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DEPCITY","$DEPCITY");}

	  FreeCursor($cur);
	  $cur=Prepare($dbh, "select code from Airport where city='$DESCITY'");
	  if(NumRows() > 1) {
		print "<h3> Please select a destination airport:</h3>\n";
		DrawSelect($cur,"DESCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DESCITY","$DESCITY");}
	  FreeCursor($cur);
        OutButton("hidden","DEPDATE","$DEPDATE");
        OutButton("submit","State","Main Menu");
        OutButton("submit","State","Show Flights");
	print "</form>\n";
	Close($dbh);
	return "done";
}

#
# Function: Flight
#
# Purpose: produces the screen that allows the user to
#          select the from a list of available flights
#          between $DEPCITY and $DESCITY on $DEPDATE.
#          Queries the airtransportation database Flight
#          table to get this info.
#

sub Flight()
{
	#GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	my $dbh, $cur;

	$dbh=Connect();

	$cur=Prepare($dbh,"select flightNo, airCraftName, dateTime
                      from Flight
                      where fromAirport='$DEPCITY'
                            AND toAirport='$DESCITY'
                            AND dateTime like '$DEPDATE%'");


print<<HTML;
  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw5perl.cgi">

  <table>
  <tr>
  <td>
  <h3>Please select a flight:</h3>
  </td>
  </tr>
  <tr>
  <td>
HTML

	DrawSelect($cur, "FLIGHT", 10);

	Close($dbh);

print<<HTML;
  <td>
  </tr>
  <tr>
  <td>
HTML

     OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Continue");

print<<HTML;
  </tr>
  </td>
  </table>
  </form>
HTML
}

#
# Function: SetCookies
#
# Purpose: sets cookies to store the variables
#          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT
#

sub SetCookies()
{
	print "Set-Cookie: DEPCSAVE=$DEPCITY;\n";
	print "Set-Cookie: DESCSAVE=$DESCITY;\n";
	print "Set-Cookie: DEPDSAVE=$DEPDATE;\n";
	print "Set-Cookie: FLIGHTSAVE=$FLIGHT;\n";
	print "Set-Cookie: SEATSAVE=$SEAT;\n";

}

#
# Function: Itinerary
#
# Purpose: if $saved is equal to "saved". It copies the variables
#          stored in the cookies back into the variables
#          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT first.
#          In all cases, it then draws the itinerary that the
#          user has selected so far followed by the buttons
#          Main Menu, Previous, and Save Itinerary.
#          these either return to the Intro screen, select seat
#          screen or returns to Intro screen with saved info
#          message and where the first line of the program has been
#          execute to set the appropriate cookies.
#

sub Itinerary()
{ #GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT;
  #GLOBAL $DEPCSAVE, $DESCSAVE, $DEPDSAVE, $FLIGHTSAVE, $SEATSAVE;

  my ($saved) = @_ ;
  my $flightNo, %cookies;

  	if($saved eq "saved")
	{
		if(!$ENV{'HTTP_COOKIE'})
                    {
		       return "no";
                    }

		%cookies = split(/=|; / , $ENV{'HTTP_COOKIE'});

		$DEPCITY=$cookies{DEPCSAVE};
		$DESCITY=$cookies{DESCSAVE};
		$DEPDATE=$cookies{DEPDSAVE};
		$FLIGHT=$cookies{FLIGHTSAVE};
		$SEAT=$cookies{SEATSAVE};
	}

	($flightNo) = split( /\#/, $FLIGHT);

  print "<h1>Travel Planner</h1>\n";
  print "<hr>\n";
  print "<h3>Your current schedule is as follows:</h3>\n";
  print "<p>You are leaving on: $DEPDATE.</p>\n";
  print "<p>You are flying from: $DEPCITY.</p>\n";
  print "<p>You are flying to: $DESCITY.</p>\n";
  print "<p>The flight number is: $flightNo</p>\n";
  print "<p>Your seat is: $SEAT </p>\n";
  print "<form method=\"POST\" action=\"hw5perl.cgi\">\n";
       OutButton("hidden","DEPCITY",$DEPCITY);
       OutButton("hidden","DESCITY",$DESCITY);
       OutButton("hidden","DEPDATE",$DEPDATE);
       OutButton("hidden","FLIGHT",$FLIGHT);
       OutButton("hidden","SEAT",$SEAT);
       OutButton("submit","State","Main Menu");
       OutButton("submit","State","Previous");
       OutButton("submit","State","Save Itinerary");
  print "</form>";
 }

#
# Function: SelectSeat
#
# Purpose: This sub draws the screen to allow the user to pick a
#          from the available seats on a given flight.
#

sub SelectSeat()
{
	#GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT;


	my ($flightNo, $aircraft, $dateTime) = split( /\#/, $FLIGHT);
	my $flag, $dbh, $cur, $cur2, @row;

print<<HTML;

  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw5perl.cgi">

  <table>
  <tr>
  <td>
  <h3>Please select a seat:</h3>
  </td>
  </tr>
  <tr>
  <td>
  The information below is formatted as follows:
  </td>
  </tr>

  <tr>
  <td>
   Row Seat Aisle Window Class
  </td>
  </tr>

  <tr>
  <td>
HTML

	print "<select name=\"SEAT\" size=\"10\">\n";
	$flag=true;

	$dbh=Connect();

	#
	# Notice I open up two cursors at this point. I wouldn't
        # need to do this on a database
	#

	$cur=Prepare($dbh,"select rowNo, seat,aisle,window, class
                      from AircraftRow
                      where airCraftName = '$aircraft'");
	while(@row = FetchRow($cur))
	{

	   $cur2=Prepare($dbh, "select rowNo, seat from Bookings
                          where flightNo='$flightNo'
			  and dateTime='$dateTime'
                          and rowNo='$row[0]' and seat='$row[1]' ");
	  if(!FetchRow($cur2))
		{
		   OutOption($flag,@row);
                   if($flag) {$flag=false;}
                }
	}



	Close($dbh);

print<<HTML;
     </select>
  <td>
  </tr>
  <tr>
  <td>
HTML

     OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("hidden","FLIGHT",$FLIGHT);
     OutButton("hidden","SEAT2","true");
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Previous");
     OutButton("submit","State","Continue");

print<<HTML;
  </tr>
  </td>
  </table>
  </form>
HTML
}

#
# Function: OutButton
#
# Purpose: draws to screen a button of type $type with name $name
#          and with value $value.
#

sub OutButton()
{
	my ($type,$name,$value) =  @_;

 print "<input type=\"$type\" name=\"$name\" value =\"$value\" />\n";
}

#
# Function: OutText
#
# Purpose: draws to screen a textfield of type $type with name $name
#          and with value $value of size $size and max number of char's
#          maxlength.
#


sub OutText
{

 my ($name,$value,$size,$maxLength) = @_;

 print "<input type=\"text\" name=\"$name\" value =\"$value\"";
 print " size=\"$size\" maxlength=\"$maxLength\" />";
}

#
# Function: OutStar
#
# Purpose: draws a bold-face blue star
#

sub OutStar()
{
	print "<b style=\"color:blue\">*</b>\n";
}



#
# Function: DeleteSharp
#
# Purpose: returns the substring of $string before the first # symbol
#

sub DeleteSharp()
{
	my $string = $_[0];

	@row=split(/\#/,$string);
	return $row[0];
}

#
# Function:DrawSelect
#
# Purpose: Draws a selection gadget with name $name where the options
#          are given by cycling over the rows returned by the database
#          cursor $cursor.
#

sub DrawSelect
{
	my ($cursor, $name, $rowsVisible) = @_;
	my $flag, @row;

	print "<select name=\"$name\" size=\"$rowsVisible\">\n";
	$flag=true;

	while(@row = FetchRow($cursor))
	{
                OutOption($flag, @row);
		if($flag) { $flag=false;}
         }
         print "</select>\n";
}

#
# Function: OutOption
#
# Purpose: Writes out an option of a selection gadget
#          If $selected is true this will be a selected option
#          of the selection gadget. $row is the value of the option.
#

sub OutOption()
{
	my ($selected, @row) = @_;

	if($selected) {print "<option selected=\"true\">";
                        $flag=false;
                          }
		else {print "<option>";}
                foreach $field (@row)
                {
			print "$field#";
		}
                print "</option>\n";
}

#
# Function:Connect
#
# Purpose: connect to the database system.
#

sub Connect()
{
 return DBI->connect("dbi:mysql:dbname=airtransportation;host=hermosa","pic40","pic40")
}

#
# Function: FetchRow
#
# Purpose:
#

sub FetchRow()
{
	my $cursor = $_[0];

	return $cursor->fetchrow();
}
#
# Function:NumRows
#
# Purpose: returns the numbers of rows left in database cursor $cursor.
#

sub NumRows()
{
	return $DBI::rows;
}

#
# Function: Prepare
#
# Purpose: prepares an SQL query to be executed
#

sub Prepare()
{
	my $dbh = $_[0];
	my $query = $_[1];
	my $cursor = $dbh->prepare($query);
	$cursor->execute();
	return $cursor;
}

#
# Function:FreeCursor
#
# Purpose: frees a cursor
#

sub FreeCursor()
{
	$_[0]->finish();
}

#
# Function: Close
#
# Purpose: closes a database handle $dbhandle
#

sub Close()
{
	$_[0]->disconnect();
}