./booking.cgi

#! /usr/local/bin/perl -w

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;
use Time::localtime;
use POSIX;
print header;
require "common.pl";

$connection = mysql_connect();
$head = load_template("head");
$head = str_replace('%%BODY%%', '"', $head);
print $head;
$body = load_template("booking/booking_head");

if(param(stage)eq"")
{
    $show = 'booking';
}
if(param(stage)eq"flights")
{
    #First, verify the passed data, all data is manditory and must be checked for validity
    #destination and depart: check for database existance, data validate and check a value is present
    if(!param(departure) || param(departure) =~ /[^a-z][^A-Z]/ || mysql_query("SELECT * FROM flights WHERE departure = '".param(departure)."';")->rows==0)
    {
        $errors .= "<li>Please enter a valid departure point.</li>";
    }

    if(!param(destination) || param(destination) =~ /[^a-z][^A-Z]/ || mysql_query("SELECT * FROM flights WHERE destination = '".param(destination)."';")->rows==0)
    {
        $errors .= "<li>Please enter a valid destination.</li>";
    }

    if(!param(tickets) || param(tickets) =~ /[^0-9]/)
    {
        $errors .= "<li>Please enter a valid number of tickets</li>";
    }

    @depart = split '/', param(depart_date);
    if(booking_checkdate(($depart[2]-1900), ($depart[1]-1), $depart[0]) ne "true" || param(depart_date) =~ '[^0-9]/[^0-9]/[^0-9]')
    {
        $errors .= "<li>Please choose a departure date within the next 3 months.</li>";
    }

    if(param(single) ne "on")
    {
        @return = split '/', param(return_date);
        if(booking_checkdate(($return[2]-1900), ($return[1]-1), $return[0]) ne "true" || param(return_date) =~ '[^0-9]/[^0-9]/[^0-9]')
        {
            $errors .= "<li>Please choose a return date within the next 3 months.</li>";
        }
    }

    if($errors)
    {
        $show = 'booking';    
    }
    else
    {
        $show = 'flights';
    }
}
if(param(stage)eq"details")
{
    #Make sure that they have chosen a flight
    foreach $value (@parameters)
    {
        if($value =~ /out\[/)
        {
            $out = 'true';
        }
    }
    if($out ne 'true')
    {
        $errors .= '<li>Please select an outbound flight.</li>';
    }

    if(param(single) ne "checked")
    {
        foreach $value (@parameters)
        {
            if($value =~ /in\[/)
            {
                $in = 'true';
            }
        }
        if($in ne 'true')
        {
            $errors .= '<li>Please select a return flight.</li>';
        }
    }

    if($errors)
    {
        $show = "flights";
    }
    else
    {
        #todays date
        $tm = localtime;
        $min = $tm->min;
        if(length($min)==1)
        {
            $min = '0'.$min;
        }
        $sql_day = ($tm->year+1900).'-'.($tm->mon+1).'-'.$tm->mday.' '.$tm->hour.':'.$min.':'.$tm->sec;

        #Loop through each outbound flight
        foreach $value (@parameters)
        {
            if($value =~ /\[/)
            {
                @name = split /\[/, $value;
                @value = split /\]/, $name[1];
                
                if($name[0] eq "out")
                {
                    #Found an outbound flight, add to database
                    @data = split /_/, $value[0];
                    $query = "SELECT price FROM flights WHERE Flight_id = ".$data[0].";"; 
                    $result = mysql_query($query);

                    while($row = $result->fetchrow_hashref)
                    {
                        $price = $row->{price};
                    }
                    @flight_date = split '/', $data[1];

                    #Find the corrisponding ticket value

                    $ticket = "tickets[".$value[0]."]";

                    if(param($ticket) != 0)
                    {
                        #Insert the flight details into the database
                        $query = "INSERT INTO bookings (`Flight_id`, `seats`, `booking_date`, `total_price`, `payment_status`, `booking_made`) VALUES (".$data[0].", ".param($ticket).", '".$flight_date[2]."-".$flight_date[1]."-".$flight_date[0]."', '".($price * param($ticket))."', 'unpaid', '$sql_day');";
                        mysql_query($query);    
                        $newid .= $connection->{mysql_insertid}.'_';
                    }
                }
            }
        }


        if(param(single)ne"on")
        {
            #Loop through each outbound flight
            foreach $value (@parameters)
            {
                if($value =~ /\[/)
                {
                    @name = split /\[/, $value;
                    @value = split /\]/, $name[1];

                    if($name[0] eq "in")
                    {
                        #Found an outbound flight, add to database
                        @data = split /_/, $value[0];
                        $query = "SELECT price FROM flights WHERE Flight_id = ".$data[0].";";
                        $result = mysql_query($query);

                        while($row = $result->fetchrow_hashref)
                        {
                            $price = $row->{price};
                        }
                        @flight_date = split '/', $data[1];

                        #Find the corrisponding ticket value

                        $ticket = "tickets[".$value[0]."]";
                    
                        if(param($ticket) != 0)
                        {
                            #Insert the flight details into the database
                            $query = "INSERT INTO bookings (`Flight_id`, `seats`, `booking_date`, `total_price`, `payment_status`, `booking_made`) VALUES (".$data[0].", ".param($ticket).", '".$flight_date[2]."-".$flight_date[1]."-".$flight_date[0]."', '".($price * param($ticket))."', 'unpaid', '$sql_day');";
                            mysql_query($query);    
                            $renewid .= $connection->{mysql_insertid}.'_';
                        }
                    }
                }
            }
        }
        else
        {
            $renewid = 'single';
        }
        $show = "details";
    }
}
if(param(stage)eq"payment")
{
    if(param(new) eq "true")
    {
        #New user, verify info
        if(!param(forenames) || param(forenames) =~ /[^[:space:]a-zA-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid forname.</li>";
        }
        if(!param(surname) || param(surname) =~ /[^[:space:]a-zA-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid surname.</li>";
        }
        if(!param(email) || param(email) =~ /[^a-z0-9A-Z@._-]{1,}/)
        {
            $errors .= "<li>Please enter a valid email address.</li>";
        }
        if(!param(address_line_one) || param(address_line_one) =~ /[:space:][^0-9][^a-z][^A-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid address.</li>";
        }
        if(!param(city) || param(city) =~ /[:space:][^a-z][^A-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid city.</li>";
        }
        if(!param(county) || param(county) =~ /[:space:][^a-z][^A-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid county.</li>";
        }
        if(!param(country) || param(country) eq "select")
        {
            $errors .= "<li>Please select a country.</li>";
        }
        if(!param(postcode) || param(postcode) =~ /[:space:][^a-z][^A-Z]{1,}/)
        {
            $errors .= "<li>Please enter a valid postcode.</li>";
        }
        if(!param(homephone) || param(homephone) =~ /[^0-9]{1,}/)
        {
            $errors .= "<li>Please enter a valid home phone number.</li>";
        }
        if(!param(mobilephone) || param(mobilephone) =~ /[^0-9]{1,}/)
        {
            $errors .= "<li>Please enter a valid mobile phone number.</li>";
        }
        if(!param(password) || !param(repassword))
        {
            $errors .= "<li>Please enter and re-enter your password.</li>";
        }
        if(param(password) ne param(repassword))
        {
            $errors .= "<li>Please make sure that your passwords match.</li>";
        }
        if($errors)
        {
            $show = "details";
        }
        else
        {
            $password = crypt(param(password), 'salt');
            $query = "INSERT INTO `users` (`forename` , `surname` , `email` , `password` , `address_line_one` , `address_line_two` , `city` , `county` , `country` , `post_code` , `landline_number` , `mobile_number`, `rank` ) VALUES ('".param(forenames)."', '".param(surname)."', '".param(email)."', '$password', '".param(address_line_one)."', '".param(address_line_two)."' , '".param(city)."' , '".param(county)."', '".param(country)."' , '".param(postcode)."' , '".param(homephone)."' , '".param(mobilephone)."' , 'normal');";
            mysql_query($query);
            $newid = $connection->{mysql_insertid};

            #Now update the booking table with the new user id
            @ids = split /_/, param(booking_id); 

            foreach $value (@ids)
            {
                $query = "UPDATE bookings SET User_id = ".$newid." WHERE Booking_id = '".$value."';";
                mysql_query($query);

                $using_user_id = $newid;
            }
            @ids = split /_/, param(return_id); 
            foreach $value (@ids)
            {
                if(param(return_id)ne"single")
                {
                    $query = "UPDATE bookings SET User_id = ".$newid." WHERE Booking_id = '".$value."';";
                    mysql_query($query);
                }
            }
        }
    }
    else
    {
        #Already registered user
        $query = "SELECT * FROM users WHERE email = '".param(email)."';";
        $result = mysql_query($query);
        $pass = crypt(param(password), 'salt');
        if($result->rows>0)
        {
            while($row = $result->fetchrow_hashref)
            {
                if($row->{password}eq$pass)
                {
                    #Now update the booking table with the new user id
                    @ids = split /_/, param(booking_id); 

                    foreach $value (@ids)
                    {
                        $query = "UPDATE bookings SET User_id = ".$row->{user_id}." WHERE Booking_id = '".$value."';";
                        mysql_query($query);

                        $using_user_id = $row->{user_id};
                    }
                    @ids = split /_/, param(return_id); 
                    foreach $value (@ids)
                    {
                        if(param(return_id)ne"single")
                        {
                            $query = "UPDATE bookings SET User_id = ".$row->{user_id}." WHERE Booking_id = '".$value."';";
                            mysql_query($query);
                        }
                    }
                }
                else
                {
                    $errors = 'true';
                }
                
            }
        }
        else
        {
            $errors = 'true';
        }    
    }

    if(!$errors)
    {
        $show = "payment";
    }
    else
    {
        $show = "details";
    }
}
if(param(stage)eq"confirm")
{
    if(!param(number) || param(number) =~ /[:space:][0-9]/)
    {
        $errors .= "<li>Please enter a valid card number.</li>";
    }
    if(!param(namr) || param(namr) =~ /[:space:]-[^a-z][^A-Z]/)
    {
        $errors .= "<li>Please enter your name as it appears on the card.</li>";
    }
    if(param(cardtype) eq "select")
    {
        $errors .= "<li>Please select a card type.</li>";
    }
    if(param(expire_month) eq "--")
    {
        $errors .= "<li>Please select the month your card expires.</li>";
    }
    if(param(expire_year) eq "----")
    {
        $errors .= "<li>Please select the year your card expires.</li>";
    }
    if(!param(security_code) || param(security_code) =~ /[^0-9]/)
    {
        $errors .= "<li>Please enter a valid 3 digit security code as it appears on the back of your card.</li>";
    }
    if(!$errors)
    {
        #Update the booking to complete (i.e. all info has been added)
        @ids = split /_/, param(booking_id); 

        foreach $value (@ids)
        {
            $query = "UPDATE bookings SET complete = 1 WHERE Booking_id = '".$value."';";
            mysql_query($query);

            $using_user_id = $row->{user_id};
        }
        @ids = split /_/, param(return_id); 
        foreach $value (@ids)
        {
            if(param(return_id)ne"single")
            {
                $query = "UPDATE bookings SET complete = 1 WHERE Booking_id = '".$value."';";
                mysql_query($query);
            }
        }

        $query = "UPDATE users SET card_number = '".param(number)."', name_on_the_card = '".param(namr)."', expire_date = '".param(expire_year)."-".param(expire_month)."', card_type = '".param(cardtype)."' WHERE user_id = '".param(user_id)."';";
        mysql_query($query);

        $show = "confirm";
    }
    else
    {
        $show = "payment";
    }
}
if(param(stage)eq"finish")
{
    #Imagine there is code here that links to a payment gateway, and let imagine that the payment was ok


    #Update the booking to paid
    @ids = split /_/, param(booking_id); 

    foreach $value (@ids)
    {
        $query = "UPDATE bookings SET payment_status = 1 WHERE Booking_id = '".$value."';";
        mysql_query($query);

        $using_user_id = $row->{user_id};
    }
    @ids = split /_/, param(return_id); 
    foreach $value (@ids)
    {
        if(param(return_id)ne"single")
        {
            $query = "UPDATE bookings SET payment_status = 1 WHERE Booking_id = '".$value."';";
            mysql_query($query);
        }
    }
    $show = "finish";
}
#Display contents
if($show eq "booking")
{
    $body .= load_template("booking/booking");
    $body = str_replace('%OVER>Booking', ' class="over">Booking', $body);

    if(param(stage) eq "flights")
    {
        $body = str_replace('%%ERRORS%%', '<b>Please correct the following errors:</b><ul>'.$errors.'</ul>', $body);
    }
    else
    {
        $body = str_replace('%%DEPARTURE%%', '', $body);
        $body = str_replace('%%DESTINATION%%', '', $body);
        $body = str_replace('%%DEPART_DATE%%', '', $body);
        $body = str_replace('%%RETURN_DATE%%', '', $body);
        $body = str_replace('%%TICKETS%%', '', $body);
        $body = str_replace('%%ERRORS%%', '', $body);
    }
}
if($show eq "flights")
{
    $body .= load_template("booking/flight_form");

    $body = str_replace('%%DEPART_DATE%%', param(depart_date), $body);
    $body = str_replace('%%RETURN_DATE%%', param(return_date), $body);
    $body = str_replace('%%TICKETS%%', param(tickets), $body);

    $tables = load_template("booking/flights"); 
    $tables = str_replace('%%HEADING%%', 'Outbound Flight', $tables);
    $qquery = "SELECT * FROM flights WHERE  flights.departure = '".param(departure)."' AND flights.destination = '".param(destination)."';";
    $result = mysql_query($qquery);
    
    $tickets_left = param(tickets);        
    @udate = split '/', param(depart_date);
    #If the flight is valid
    if($result->rows!=0)
    {
        while($tickets_left > 0)
        {
            while(my $row = $result->fetchrow_hashref)
            {
                $flights .= load_template("booking/flight");

                $tables = str_replace('%%EDEPART_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $tables);
                $tables = str_replace('%%EDEPART_ID%%', $row->{Flight_id}, $tables);

                $flights = str_replace('%%OUT_TIME%%', makenormtime($row->{departure_time}), $flights);
                $flights = str_replace('%%OUT_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $flights);
                $flights = str_replace('%%OUT_POINT%%', tc($row->{departure}), $flights);
                $flights = str_replace('%%LAND_TIME%%', makenormtime($row->{arrival_time}), $flights);
                $flights = str_replace('%%LAND_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $flights);
                $flights = str_replace('%%LAND_POINT%%', tc($row->{destination}), $flights);
                $flights = str_replace('%%PRICE%%', $row->{price}, $flights);
                $flights = str_replace('%%FLIGHT%%', $row->{Flight_id}.'_'.$udate[0].'/'.$udate[1].'/'.$udate[2].'_'.$row->{departure_time}, $flights);

                $ldate = $udate[0].'/'.$udate[1].'/'.$udate[2];
                $lid = $row->{Flight_id};

                $maxq = "SELECT sum(seats) as max FROM bookings WHERE flight_id = ".$row->{Flight_id}." AND booking_date = '".$udate[2]."-".$udate[1]."-".$udate[0]."' AND complete = 1;";
                $results = mysql_query($maxq);
                while(my $rows = $results->fetchrow_hashref)
                {
                    $flights = str_replace('%%MAX%%', $row->{passenger_seats} - $rows->{max}, $flights);

                    if($tickets_left<0)
                    {
                        $flights = str_replace('%%TICKETS%%', 0, $flights);    
                    }
                    else
                    {
                        if($tickets_left>($row->{passenger_seats} - $rows->{max}))
                        {
                            $flights = str_replace('%%TICKETS%%', ($row->{passenger_seats} - $rows->{max}), $flights);    
                        }
                        else
                        {
                            $flights = str_replace('%%TICKETS%%', $tickets_left, $flights);    
                        }
                    }


                    $tickets_left = $tickets_left - ($row->{passenger_seats} - $rows->{max});
                }
            } 
            #Rerun query
            $result = mysql_query($qquery);
            @udate = increase_day($udate[0], $udate[1], $udate[2])
        }
        $tables = str_replace('%%FLIGHTS%%', $flights, $tables);
        $tables = str_replace('%%TYPE%%', 'out', $tables);
        $tables = str_replace('%%LDEPART_DATE%%', $ldate, $tables);
        $tables = str_replace('%%LDEPART_ID%%', $lid, $tables);
    }
    else
    {
        $tables = str_replace('%%FLIGHTS%%', '<tr><td colspan="5">Sorry no flights wherre found.</td></tr>', $tables);
    }
    if(param(single)ne"on" && param(single) ne "checked")
    {
        undef $flights;
        $tables .= load_template("booking/flights");
        $tables = str_replace('%%HEADING%%', 'Return Flight', $tables);

        $qquery = "SELECT * FROM flights WHERE destination = '".param(departure)."' AND departure = '".param(destination)."';";
        $result = mysql_query($qquery);
        $tickets_left = param(tickets);        
        @udate = split '/', param(return_date);
        #If the flight is valid
        if($result->rows!=0)
        {
            while($tickets_left > 0)
            {
                while(my $row = $result->fetchrow_hashref)
                {
                    $flights .= load_template("booking/flight"); 

                    $tables = str_replace('%%EDEPART_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $tables);
                    $tables = str_replace('%%EDEPART_ID%%', $row->{Flight_id}, $tables);

                    $flights = str_replace('%%OUT_TIME%%', makenormtime($row->{departure_time}), $flights);
                    $flights = str_replace('%%OUT_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $flights);
                    $flights = str_replace('%%OUT_POINT%%', tc($row->{departure}), $flights);
                    $flights = str_replace('%%LAND_TIME%%', makenormtime($row->{arrival_time}), $flights);
                    $flights = str_replace('%%LAND_DATE%%', $udate[0].'/'.$udate[1].'/'.$udate[2], $flights);
                    $flights = str_replace('%%LAND_POINT%%', tc($row->{destination}), $flights);
                    $flights = str_replace('%%PRICE%%', $row->{price}, $flights);
                    $flights = str_replace('%%FLIGHT%%', $row->{Flight_id}.'_'.$udate[0].'/'.$udate[1].'/'.$udate[2].'_'.$row->{departure_time}, $flights);

                    $ldate = $udate[0].'/'.$udate[1].'/'.$udate[2];
                    $lid = $row->{Flight_id};

                    @sql_date = split '/', param(return_date);
                    $results = mysql_query("SELECT count(Booking_id) as max FROM bookings WHERE flight_id = ".$row->{Flight_id}." AND booking_date = '".$udate[2]."-".$udate[1]."-".$udate[0]."' AND complete = 1;");
                    while(my $rows = $results->fetchrow_hashref)
                    {
                        $flights = str_replace('%%MAX%%', $row->{passenger_seats} - $rows->{max}, $flights);

                        if($tickets_left<0)
                        {
                            $flights = str_replace('%%TICKETS%%', 0, $flights);    
                        }
                        else
                        {
                            if($tickets_left>($row->{passenger_seats} - $rows->{max}))
                            {
                                $flights = str_replace('%%TICKETS%%', ($row->{passenger_seats} - $rows->{max}), $flights);    
                            }
                            else
                            {
                                $flights = str_replace('%%TICKETS%%', $tickets_left, $flights);    
                            }
                        }


                        $tickets_left = $tickets_left - ($row->{passenger_seats} - $rows->{max});
                    }
                } 
                #Rerun query
                $result = mysql_query($qquery);
                @udate = increase_day($udate[0], $udate[1], $udate[2])
            }
            $tables = str_replace('%%FLIGHTS%%', $flights, $tables);
            $tables = str_replace('%%TYPE%%', 'in', $tables);
            $tables = str_replace('%%LDEPART_DATE%%', $ldate, $tables);
            $tables = str_replace('%%LDEPART_ID%%', $lid, $tables);
        }
        else
        {
            $tables = str_replace('%%FLIGHTS%%', '<tr><td colspan="5">Sorry no flights wherre found.</td></tr>', $tables);
        }
    }
    $body = str_replace('%%TABLES%%', $tables, $body);
    $body = str_replace('%%SELECTED%%', '', $body);
    $body = str_replace('%OVER>Booking', ' class="done">Booking', $body);
    $body = str_replace('%OVER>Flights', ' class="over">Flights', $body);

    if(param(stage) eq "flights")
    {
        $body = str_replace('%%ERRORS%%', '', $body);
    }
    else
    {
        $body = str_replace('%%ERRORS%%', '<b>Please correct the following errors:</b><ul>'.$errors.'</ul>', $body);
    } 
}
if($show eq "details")
{
    $body = str_replace('%OVER>Booking', ' class="done">Booking', $body);
    $body = str_replace('%OVER>Flights', ' class="done">Flights', $body);
    $body = str_replace('%OVER>Your', ' class="over">Your', $body);

    if(param(stage) eq "details")
    {
        $body .= load_template('booking/detail');
        $body = str_replace('%%LOGIN%%', load_template('booking/login'), $body);
        $body = str_replace('%%NEW%%', load_template('booking/new'), $body);
        $body = str_replace('%%ERROR%%', '', $body);
        $body = str_replace('%%BOOKING_ID%%', $newid, $body);
        $body = str_replace('%%RETURN_ID%%', $renewid, $body);

        $body = str_replace('%%SELECTED%%', '', $body);
        $body = str_replace('%%FORENAMES%%', '', $body);
        $body = str_replace('%%SURNAME%%', '', $body);
        $body = str_replace('%%ADDRESS_LINE_ONE%%', '', $body);
        $body = str_replace('%%ADDRESS_LINE_TWO%%', '', $body);
        $body = str_replace('%%CITY%%', '', $body);
        $body = str_replace('%%COUNTY%%', '', $body);
        $body = str_replace('%%POSTCODE%%', '', $body);
        $body = str_replace('%%HOMEPHONE%%', '', $body);
        $body = str_replace('%%MOBILEPHONE%%', '', $body);
        $body = str_replace('%%EMAIL%%', '', $body);
    }
    else
    {
        if(param(new) eq "true")
        {
            $body .= load_template('booking/new');
            $body = str_replace('%%ERROR%%', '<b>Please correct the following errors:</b><ul>'.$errors.'</ul>', $body);

            #Replace values to get the country to be pre selected
            $body = str_replace('%%SELECTED%%>'.param(country), ' selected>'.param(country), $body);
            $body = str_replace('%%SELECTED%%', '', $body);
        }
        else
        {
            $body .= load_template('booking/login');
            $body = str_replace('%%ERROR%%', '<b>You email address of password was not recognised.</b>', $body);
        }
    }
}
if($show eq "payment")
{
    $body = str_replace('%OVER>Booking', ' class="done">Booking', $body);
    $body = str_replace('%OVER>Flights', ' class="done">Flights', $body);
    $body = str_replace('%OVER>Your', ' class="done">Your', $body);
    $body = str_replace('%OVER>Payment', ' class="over">Payment', $body);
    $body .= load_template('booking/pay');

    if(param(stage) eq "payment")
    {
        $body = str_replace('%%USER_ID%%', $using_user_id, $body);
        #Fill in payment details if present
        $query = "SELECT * FROM users WHERE user_id = '".$using_user_id."';";
        $result = mysql_query($query);

        while($row = $result->fetchrow_hashref)
        {
            @date = split /-/, $row->{expire_date};
            if($row->{card_type} ne "")
            {
                $body = str_replace('%%CSELECTED%%>'.$row->{card_type}, ' selected>'.$row->{card_type}, $body);
            }
            if($date[1] ne "")
            {
                $body = str_replace('%%MSELECTED%%>'.$date[1], ' selected>'.$date[1], $body);
            }
            if($date[0] ne "")
            {
                $body = str_replace('%%YSELECTED%%>'.$date[0], ' selected>'.$date[0], $body);
            }

            $body = str_replace('%%NAMR%%', $row->{name_on_the_card}, $body);
            $body = str_replace('%%NUMBER%%', $row->{card_number}, $body);
        }
        $body = str_replace('%%ERROR%%', '', $body);
    }
    else
    {
        $body = str_replace('%%CSELECTED%%>'.param(cardtype), ' selected>'.param(cardtype), $body);
        $body = str_replace('%%MSELECTED%%>'.param(expire_month), ' selected>'.param(expire_month), $body);
        $body = str_replace('%%YSELECTED%%>'.param(expire_year), ' selected>'.param(expire_year), $body);

        $body = str_replace('%%ERROR%%', '<b>Please correct the following errors:</b><ul>'.$errors.'</ul>', $body);
    }

    #Replace whats left over
    $body = str_replace('%%CSELECTED%%', '', $body);
    $body = str_replace('%%MSELECTED%%', '', $body);
    $body = str_replace('%%YSELECTED%%', '', $body);
}
if($show eq "confirm")
{
    $body = str_replace('%OVER>Booking', ' class="done">Booking', $body);
    $body = str_replace('%OVER>Flights', ' class="done">Flights', $body);
    $body = str_replace('%OVER>Your', ' class="done">Your', $body);
    $body = str_replace('%OVER>Payment', ' class="done">Payment', $body);
    $body = str_replace('%OVER>Confirm', ' class="over">Confirm', $body);

    #Display all the info for confirmation
    $body .= load_template('booking/confirm');

    $tables = load_template('booking/con_flight');
    $tables = str_replace('%%TITLE%%', 'Outgoing Flight', $tables);

    @ids = split /_/, param(booking_id); 

    foreach $value (@ids)
    {
        $query = "SELECT * FROM bookings, flights WHERE flights.Flight_id = bookings.flight_id AND bookings.Booking_id = '".$value."';";
        $result = mysql_query($query);
        $flights .= load_template('booking/con_flightt');
        while(my $row = $result->fetchrow_hashref)
        {
            @date = split /-/, $row->{booking_date};
            $flights = str_replace('%%OUT_POINT%%', tc($row->{departure}), $flights);
            $flights = str_replace('%%OUT_DATE%%', $date[2].'/'.$date[1].'/'.$date[0], $flights);
            $flights = str_replace('%%OUT_TIME%%', makenormtime($row->{departure_time}), $flights);

            $flights = str_replace('%%LAND_POINT%%', tc($row->{departure}), $flights);
            $flights = str_replace('%%LAND_DATE%%', $date[2].'/'.$date[1].'/'.$date[0], $flights);
            $flights = str_replace('%%LAND_TIME%%', makenormtime($row->{arrival_time}), $flights);

        $flights = str_replace('%%PRICE%%', $row->{total_price}, $flights);
        $flights = str_replace('%%TICKETS%%', $row->{seats}, $flights);
        }
    }
    $tables = str_replace('%%FLIGHT_T%%', $flights, $tables);
    if(param(return_id) ne "single")
    {
        $tables .= load_template('booking/con_flight');
        $tables = str_replace('%%TITLE%%', 'Return Flight', $tables);
        @ids = split /_/, param(return_id); 
        undef $flights;
        foreach $value (@ids)
        {
            $query = "SELECT * FROM bookings, flights WHERE flights.Flight_id = bookings.flight_id AND bookings.Booking_id = '".$value."';";
            $result = mysql_query($query);
            $flights .= load_template('booking/con_flightt');
            while(my $row = $result->fetchrow_hashref)
            {
                @date = split /-/, $row->{booking_date};
                $flights = str_replace('%%OUT_POINT%%', tc($row->{departure}), $flights);
                $flights = str_replace('%%OUT_DATE%%', $date[2].'/'.$date[1].'/'.$date[0], $flights);
                $flights = str_replace('%%OUT_TIME%%', makenormtime($row->{departure_time}), $flights);

                $flights = str_replace('%%LAND_POINT%%', tc($row->{departure}), $flights);
                $flights = str_replace('%%LAND_DATE%%', $date[2].'/'.$date[1].'/'.$date[0], $flights);
                $flights = str_replace('%%LAND_TIME%%', makenormtime($row->{arrival_time}), $flights);

            $flights = str_replace('%%PRICE%%', $row->{total_price}, $flights);
            $flights = str_replace('%%TICKETS%%', $row->{seats}, $flights);
            }
        }
    } 
    $tables = str_replace('%%FLIGHT_T%%', $flights, $tables);
    $body = str_replace('%%FLIGHTS%%', $tables, $body);
}
if($show eq "finish")
{
    $body = str_replace('%OVER>Booking', ' class="done">Booking', $body);
    $body = str_replace('%OVER>Flights', ' class="done">Flights', $body);
    $body = str_replace('%OVER>Your', ' class="done">Your', $body);
    $body = str_replace('%OVER>Payment', ' class="done">Payment', $body);
    $body = str_replace('%OVER>Confirm', ' class="done">Confirm', $body);

    $body .= load_template('booking/done');
}
#Populate fields with posted data
foreach $value (@parameters)
{
    if($value ne "single")
    {
        $body = str_replace('%%'.uc($value).'%%', param($value), $body);
    }
    else
    {
        if(param(single) eq "checked")
        {
            $body = str_replace('%%'.uc($value).'%%', "checked", $body);
        }
        else
        {
            $body = str_replace('%%'.uc($value).'%%', "", $body);
        }
    }

}
#Single is a checkbox, so may not be passed
$body = str_replace('%%SINGLE%%', '', $body);

#Remove any left %OVER from head
$body = str_replace('%OVER', '', $body);

print $body;
print load_template("foot");

mysql_disconnect();