#!/usr/bin/kitasperlas

print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
 ($name, $value) = split(/=/, $pair);
 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/~!/ ~!/g;
 $FORM{$name} = $value;
}

$tipas = $FORM{'TIPAS'};

if ($tipas eq 'Atvirutes') {
  $kiekis = $FORM{'T5'};
  if ($kiekis <= 50) {
    $suma = $kiekis * 100;
  }elsif ($kiekis <= 100) {
    $suma = $kiekis * 95;
  }elsif ($kiekis <= 500) {
    $suma = $kiekis * 90;
  }else {
    $suma = $kiekis * 80;
  }
}elsif ($tipas eq 'Dzukija') {
  $kiekis = $FORM{'kiekis'};
  $suma = $kiekis * 400;
}

$litu = $suma / 100;
$cnt  = $suma % 100;

print "<html><body>";
print "Jusu uzsakytu leidiniu verte yra $litu.$cnt Lt.<p>";
print "Ar tikrai norite isigyti siuos leidinius?<p><p>";
print '<form method="POST" action="http://www.elnet.lt/cgi-bin/vija/order.pl">';
if ($tipas eq 'Atvirutes') {
  print '<input type="hidden" name="TIPAS" value="Atvirutes">';
  print '<input type="hidden" name="NR" value="';
  print "$FORM{'Atvirutes_nr'}";
  print '">';
  print '<input type="hidden" name="Kiekis" value="';
  print "$FORM{'T5'}";
  print '">';
}elsif ($tipas eq 'Dzukija') {
  print '<input type="hidden" name="TIPAS" value="Dzukija">';
  print '<input type="hidden" name="Kiekis" value="';
  print "$FORM{'kiekis'}";
  print '">';
}elsif ($tipas eq 'www') {
  print '<input type="hidden" name="TIPAS" value="www">';
  print '<input type="hidden" name="Variant" value="';
  print "$FORM{'Pasirinkite:'}";
  print '">';
}

print '<input type="hidden" name="Pavarde" value="';
print "$FORM{'Pavarde'}";
print '">';

print '<input type="hidden" name="Vardas" value="';
print "$FORM{'Vardas'}";
print '">';

print '<input type="hidden" name="Adresas" value="';
print "$FORM{'adresas_uzsakymui'}";
print '">';

print '<input type="hidden" name="E-mail" value="';
print "$FORM{'e_pastas'}";
print '">';

print '<input type="hidden" name="Telefonas" value="';
print "$FORM{'Telefonas'}";
print '">';

print '<input type="hidden" name="Mobilus" value="';
print "$FORM{'mob_tel'}";
print '">';

print '<input type="hidden" name="Firma" value="';
print "$FORM{'Firma'}";
print '">';

print '<input type="hidden" name="T1" value="';
print "$FORM{'T1'}";
print '">';

print '<input type="hidden" name="i_kodas" value="';
print "$FORM{'i_kodas'}";
print '">';

print '<input type="hidden" name="pvm_kodas" value="';
print "$FORM{'Pvm_kodas'}";
print '">';

print '<input type="hidden" name="T2" value="';
print "$FORM{'T2'}";
print '">';

print '<input type="hidden" name="T3" value="';
print "$FORM{'T3'}";
print '">';

print "<center>";
print '<input type="submit" name="submit" value="Patvirtinti">';
print "</center>";
print "<p><p>Jei norite pakeisti savo uzsakyma, spauskite narsykles ";
print "'Back' mygtuka.";

print "</form></body></html>";