#!/usr/bin/perl
use CGI;
$q = new CGI;
$DEBUG=0;
#undef ( %sections, $template );
print $q->header if $DEBUG;
@cities = $q->param('city');
if( $ENV{'HTTP_REFERER'} =~ /.*biztravel\.com.*/ )
{
parseTemplate("biztemplate.htm");
}
else
{
parseTemplate("template_ft.shtml");
}
#added by Tim on 2/2/00 to make the airport codes uppercase
foreach $x ( 0 .. $#cities )
{
$len = length $cities[$x];
if( $len eq 3 )
{
$cities[$x] = uc $cities[$x];
}
}
#end added by Tim 2/2/00
foreach $x ( 0 .. $#cities ) { CleanCity( \$cities[$x] ); }
foreach $x ( 0 .. $#cities ) { $fullCity[$x] = findCity( $cities[$x], $x ); }
do
{
undef ( $line );
$sections{'results'} =~ s|([\s\S]*?)||;
$rotation = $1;
foreach $x ( 0 .. $#fullCity )
{
my ( $temp ) = $rotation;
last unless ( $fullCity[$x] );
($code, $airportName, $city, $state, $country, $Lat, $Long) = split(/\t/, $fullCity[$x]);
$airportName = $city . " Airport" unless $airportName;
$loc = ( $state ) ? $state : $country;
$temp =~ s//${$1}/g;
$line .= $temp;
$line .= qq!
to\n! if ( $fullCity[$x + 1] );
}
$sections{'results'} =~ s//$line/;
$sections{'results'} =~ s//${$1}/g;
printOutput ( "results" );
} unless $DEBUG;
sub CleanCity {
my ($cityref) = shift;
open (STATE, "abriviations.df");
while ()
{
m!^([A-Z]{2})\t([A-Z]*)\t([0-9]*)\t(.+)!;
$cit = $4;
$cit =~ tr/a-z/A-Z/;
$twoChar{$cit} = $1;
# $twoChar{$2} = $1 if $2;
$twoChar{$3} = $1 if $3;
}
close (STATE);
$$cityref =~ tr/(A-z)|(0-9)|!|@|#|$|%|^|&|*/ /c; # Translate illegal to space
$$cityref =~ tr/ //s; # Squash mult spaces
$$cityref =~ s/(^ )|( $)//; # Remove leading and trailing space
my ( @parts ) = split ( / /, $$cityref );
foreach $x ( 0 .. $#parts )
{
$ct = $parts[$x];
$ct =~ tr/a-z/A-Z/;
$$cityref =~ s/$parts[$x]/$twoChar{$ct}/i if $twoChar{$ct};
}
}
sub findCity
{
my ($city, $index) = @_;
$found = 0;
return unless $city;
print $city, " --
" if $DEBUG;
@parts = split( / /, $city );
open DAT, "cities.df" or print "Unable to open data file: $!\n";
@cityList = ;
close DAT;
if ($city =~ /^[A-Z]{3}$/)
{
( $airport ) = grep( /^$city/, @cityList );
chomp ( $airport );
noMatch( $city, $index ) unless ($airport);
return $airport;
}
foreach $part (@parts)
{
@cityList = grep(/$part/i, @cityList);
}
toMany( $city, $index, @cityList ) if ($cityList[1]);
noMatch( $city, $index ) unless (@cityList);
chomp ($cityList[0]);
return $cityList[0];
}
sub toMany
{
local ($city, $index, @list) = @_;
($city, @rest) = split(/\t/, $city);
undef ( $formentries );
foreach $x ( 0 .. $#cities )
{
if ( $x eq $index )
{
$formentries .= " \n";
foreach (@list)
{
my ($code, $airportName, $cityName, $state, $country, $Lat, $Long) = split(/\t/);
($airportName = $cityName . " Airport") unless $airportName;
$loc = ( $state ) ? $state : $country;
$formentries .= qq! $airportName ($code) - $cityName, $loc
\n!;
}
$formentries .= " \n";
}
else { $formentries .= " "; }
}
$sections{'tomany'} =~ s//${$1}/g;
printOutput ( "tomany" );
}
sub noMatch
{
local ( $city, $index ) = @_;
undef ( $formentries );
foreach $x ( 0 .. $#cities )
{
if ( $x eq $index )
{ $formentries .= "
\n"; }
else
{ $formentries .= "
\n"; }
}
$sections{'nomatch'} =~ s//${$1}/g;
printOutput ( "nomatch" );
}
sub blankField
{
my ($item, $place, $missing) = @_;
my $x = ($item eq 1) ? 2 : 1;
($code, @rest) = split(/\t/, $place);
$code =~ s/ /+/g;
exit(0);
}
sub round_to_3
{
my ( $num ) = @_;
my ( $lg, $round );
if ( $num == 0 ) { return 0; }
$lg = int( log( abs($num ) ) / log( 10.0 ) ); # log base 10 of num
$round = 10 ** ($lg - 2);
return int( $num / $round + 0.5 ) * $round;
}
sub parseTemplate
{
my ( $file ) = @_;
open (TMP, $file) || print "Unable to open teplate file $file - $!";
while ()
{
if ( // )
{
$sec = $1;
while ( $sections{$sec} !~ // )
{ ( $sections{$sec} ) .= ; }
$template .= "";
}
else { $template .= $_; }
}
close (TMP);
}
sub printOutput
{
my ( $sec ) = @_;
$template =~ s//$sections{$sec}/;
print $q->header;
print $template;
exit (0);
}