#!/usr/bin/perl

use xml;

my $hpp=20;
my $noless=4;
my @color=("white", "#e5f4e4");
  
# check params
my %in=getcgivars ();
print "Content-type: text/html\n\n";
#$in{country}="austria";
#$in{city}="graz";
$in{page}="1" unless ($in{page});
$in{lang}="en" unless ($in{lang});
exit if ($in{page}!~/\d+/ or $in{page}=~/[\.\,\-\+]/ or $in{page}<=0);
exit unless ($in{country});
exit unless ($in{city});
exit unless ($in{lang} eq "en" or $in{lang} eq "de" or $in{lang} eq "it" or $in{lang} eq "fr" or $in{lang} eq "es");

# prepare page data
my $code=$city{$in{country}}{$in{city}};
exit unless ($code);
my @data=getdata ("list", $code);
my $no=scalar @data;
my $first=($in{page}-1)*$hpp+1;
my $last=$first+$hpp-1;
my $maxpage=sprintf ("%0.0f",$no/$hpp+0.5);
my $lastno=$no-($maxpage-1)*$hpp;
if ($lastno<=$noless) { $maxpage--; if ($in{page}==$maxpage) { $last=$no } }
exit if ($in{page}>$maxpage);

# make up hotel list
my $page=readtext ("template/$in{city}.list.$in{lang}.html");
$page=~s/{hotel}([\w\W\n]*){\/hotel}/{list}/;
my $hotel=$1;
my $list;
my $j;
for my $i (@data) {
  $j++;
  next unless ($j>=$first and $j<=$last);
  my $h=$hotel;
  $h=~s/{country}/$in{country}/g;
  $h=~s/{city}/$in{city}/g;
  for my $k (keys %$i) { $h=~s/{\.$k}/$$i{$k}/g; }
  $h=~s/{currency}/$currency{$in{country}}/g;
  $h=~s/{color}/$color[($j-$first)%2]/g;
  #my @hoteldata=getdata ("hotel",$$i{HotelID},$in{lang});
  #$hoteldata[0]{Description}=substr ($hoteldata[0]{Description},0,100);
  #$hoteldata[0]{c1}=~s/s=0/s=60/;
  #for my $k (keys %{$hoteldata[0]}) { $h=~s/{\.$k}/$hoteldata[0]{$k}/g; }
  $h=~s/{.*}//g;
  $list.=$h;
}
$page=~s/{list}/$list/;

# show page list
if ($maxpage>1) {
  my $pagelist;
  for my $i (1..$maxpage) {
    $pagelist.=" &middot; \n" if ($pagelist);
    $pagelist.=$i==$in{page} ? "<b>$i</b>" : qq{<a href="http://www.hotels-travel-world.com/$in{country}/$in{city}/list/$i/$in{lang}.html">$i</a>};
  }
  $page=~s/{pagelist}/$pagelist<br><br>/g;
} else {
  $page=~s/{pagelist}//;
}

print $page;


