#!/usr/bin/perl

require 'session.cgi';

my %form = map { $_ => join "\0",$cgi->param($_) } $cgi->param();

if($form{'value'}){
  $sets = &GetManSets($form{'value'});
}else{
  $sets = &GetSet($form{'setID'});
}
&ShowDetail($sets);




##---------sub routines-----------------------------

sub GetManSets{

my($id) = shift;

my($statement, $sth, $rv, $set);

$statement = "SELECT * FROM product_set,set_link,dept_relations WHERE dept_relations.deptid='$id' AND dept_relations.setid=product_set.id AND product_set.available<>'3' AND product_set.id=set_link.setid AND set_link.siteid='2' ORDER BY product_set.setname";
$sth = $dbh->prepare($statement) || die "Can't prepare $statement: $dbh->errstr\n";
$rv = $sth->execute || die "Can't execute the query: $sth->errstr\n";

return $sth; 

}

# -----------------------------

sub GetSet{

my($id) = shift;

my($statement, $sth, $rv, $set);

$statement = "SELECT * FROM product_set,set_link WHERE product_set.id='$id' AND product_set.available<>'3' AND product_set.id=set_link.setid AND set_link.siteid='2' ORDER BY product_set.setname";
$sth = $dbh->prepare($statement) || die "Can't prepare $statement: $dbh->errstr\n";
$rv = $sth->execute || die "Can't execute the query: $sth->errstr\n";

return $sth; 

}

# -----------------------------

sub GetBrandName{

my $id = shift;

my($statement, $sth, $rv);

$statement = "SELECT top_manufacture.name FROM top_manufacture,dept_link,dept_relations WHERE dept_relations.setid='$id' AND dept_relations.deptid=dept_link.deptid AND dept_link.top_manid=top_manufacture.id";
$sth = $dbh->prepare($statement) || die "Can't prepare statement: $!\n";
$rv = $sth->execute || die "Can't execute statement: $!\n"; 

@names = $sth->fetchrow_array;

return @names;

}

# -----------------------------

sub ShowDetail{

my($sth) = shift;
my($header) = "header_can.inc";
my($footer) = "footer_new.inc";
my $something = 0;
&IncludeFile($header,\%form);

print "<table border=\"0\" width=\"600\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n"; 
while($set = $sth->fetchrow_hashref){
   $something++;
   ($manufacture) = &GetBrandName($set->{'id'});
  print "<tr><td align=\"center\" valign=\"top\" colspan=\"3\">\n";
    print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"300\" align=\"center\">\n";
    print "<tr><td bgcolor=\"660000\" height=\"1\"><img src=\"images/pixel_clear.gif\" height=\"1\"></td></tr>\n";
    print "<tr><td bgcolor=\"\#dddddd\" align=\"center\"><font face=\"arial, helvetica, sans-serif\" size=\"2\"><b>$manufacture $set->{'setname'}</b><br>";
    print "</td></tr>\n";
    print "</table>\n";
   print "</font></td></tr>\n";
print "<tr><td align=\"center\">\n";
    if($set->{'image'}){
      print "<a href=\"new_thumb.cgi?SID=$SID\&setID=$set->{'id'}\"><img src=\"images/$set->{'image'}\" alt=\"\" border=\"0\"></a>\n";
    }
    else{
      print "<a href=\"new_thumb.cgi?SID=$SID\&setID=$set->{'id'}\"><img src=\"images/no_piece_image.gif\" alt=\"$set->{'setname'}\" border=\"0\"></a>\n";
    }
   print "<br><font face=\"arial,helvetica,sans-serif\" size=\"2\"><b>Click Image for Details</b></font></td></tr>\n";
   print "<tr><td align=\"center\"><font face=\"arial, helvetica, sans-serif\" size=\"2\">$set->{'description'}</font></td></tr>\n";
   print "<tr><td height=\"2\"><img src=\"images/pixel_clear.gif\" height=\"2\"></td></tr>\n";
   if(($set->{'sale'} < $set-{'retail'}) && ($set->{'sale'} > 0)){
	print "<tr><td>\n";
	print "<center>\n";
	print "<font color=\"red\" face=\"arial, helvetica, sans-serif\">\n";
	print "<b>On Sale Now\!</b>\n";
	print "</font>\n";
	print "</center>\n";
	print "</td></tr>\n";
   }
  print "<tr><td colspan=\"3\" align=\"center\"><font face=\"arial, helvetica, sans-serif\" size=\"2\"><b>Warranty: $set->{'warranty'}</b></font></td></tr>\n" if($set->{'warranty'});
	print "<tr><td><hr width=\"60\%\" noshade></td></tr>\n";
}
unless($something){ &NoSets(1); }
print "</table>\n";

&IncludeFile($footer);

}




# --------------------------

sub NoSets{

my($value) = shift;
my($word);

if($value == 1){
    $word = "sets for that manufacturer";
}
elsif($value == 2){
    $word = "Currently there are no books in the bookstore<br>Please <b>bookmark this page now</b> as it will change frequently.";
}
else{
    $word = "pieces for that set";
}

print "<tr><td><font face=\"arial, helvetica, sans-serif\" size=\"2\"><center><h3>Sorry!</h3></center></font></td></tr>\n";
print "<tr><td><font face=\"arial, helvetica, sans-serif\" size=\"2\">\n";
print "<center>The $word are coming soon.</center></font><p></td></tr>\n" if($value < 2);
print "<center>$word</center></font><p></td></tr>\n" if($value == 2);

}

