#! /usr/bin/perl 


$| = 1;

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type:text/html\n\n";

  # Read in text
  $input = $ENV{'QUERY_STRING'};

  @input = split(/&/,$input);

  foreach $i (0 .. $#input) {
    # Convert plus's to spaces
    $input[$i] =~ s/\+/ /g;

    # Convert %XX from hex numbers to alphanumeric
    $input[$i] =~ s/%(..)/pack("c",hex($1))/ge;

    # Split into key and value.
    $loc = index($input[$i],"=");
    $key = uc substr($input[$i],0,$loc);     ### uc function added by E.T.
    $val = substr($input[$i],$loc+1);
    $input{$key} .= '\0' if (defined($input{$key})); # \0 is the multiple separator
    $input{$key} .= $val;
#    print "key = $key, $input{$key}";
  }

# print "$input<P>";
# print "no. variables = $#input";

# Print a title and initial heading
print "<HTML>";
print "<Head><Title></Title></Head>";
print "<Body BGCOLOR =#FFFFFF>\n";


use DBI;
use Config;
use Cwd;
#use strict;

my $dbh = DBI->connect('DBI:mysql:computercar', 'rclee', '0419') || 
	die ("can't connect to the database: $!");

my $cursor_s = $dbh->prepare("select unit_no, unit_type, rental,
		bed_bath, list_price, furnished, mls_no, location,
		new_construction, image_name, long_descr, username,
		image_name2, image_name3, image_name4, image_name5
		from real_estate
		where unit_no = $input{'ENTRY'} ");

     $cursor_s->execute or die "can't execute the query";

     my $table = $cursor_s->fetchall_arrayref or die "$cursor_s->errstr\n";

     my($i, $j);

$username = $table->[0][11];

my $cursor_b = $dbh->prepare("select name, business_name, business_address,
			business_city_sz, business_phone, business_email, url,
			trim(listing_form), login_id
		from login
		where username='$username'
		and acct_status = 'A' ");

     $cursor_b->execute or die "can't execute the query";

     my $table_b = $cursor_b->fetchall_arrayref or die "$cursor_b->errstr\n";

     if ($input{'IMGNO'} == 2)
       { $img_index = 12; } 
     elsif ($input{'IMGNO'} == 3) 
       { $img_index = 13; }
     elsif ($input{'IMGNO'} == 4)
       { $img_index = 14; }
     elsif ($input{'IMGNO'} == 5)
       { $img_index = 15; } 
     else 
       { $img_index = 9; 
         $input{'IMGNO'} = 1; 
       }

     $min_img = 1;
     $this_img = $input{'IMGNO'};
     $prev_img = $input{'IMGNO'} - 1;
     $next_img = $input{'IMGNO'} + 1;

     if ($table->[0][15])
       { $max_img = 5; }
     elsif ($table->[0][14])
       { $max_img = 4; }
     elsif ($table->[0][13])
       { $max_img = 3;}
     elsif ($table->[0][12])
       { $max_img = 2;}
     else
       { $max_img = 1;}


     print "<table cellspacing=8>";
     print "<tr><td><img src=\"/re_images/$table->[0][$img_index]\" border=0 alt=unit_picture><P>";

     if ($this_img > $min_img)
       { print "<a href=\"/cgi-bin/show_detail.cgi?";
         print "entry=$input{'ENTRY'}&imgno=$prev_img\">";
         print "PreviousImage</a> ";
       }
 
     if ($this_img < $max_img)
       { print "<a href=\"/cgi-bin/show_detail.cgi?";
         print "entry=$input{'ENTRY'}&imgno=$next_img\">";
         print "NextImage</a> "; }

     print "</td><td><center><h2>$table_b->[0][1]</h2><BR>\n";
     print "<b>$table_b->[0][0]<P>\n";
     print "$table_b->[0][2]<BR>\n";
     print "$table_b->[0][3]<BR>\n";
     print "Phone: $table_b->[0][4]<BR>\n";

     if ("$table_b->[0][5]" ne "") {
         print "<A HREF=mailto:$table_b->[0][5]>$table_b->[0][5]</A></b>\n";
     }

     print "<a href=$table_b->[0][6]>$table_b->[0][6]</a>\n";
     print "</center></td></tr></table>";   

     print "<center>";
     print "<h3>$table->[0][1] / $table->[0][7]</h3><P>"; 
     print "<table>";
     print "<TR><TD>Rental</TD><TD>$table->[0][2]</TD><td><img src=/images/spacer.gif></td>\n";
     print "<TD>List Price</TD><TD>$table->[0][4]</TD></TR>\n";
     print "<TR><TD>Bed/Bath</TD><TD>$table->[0][3]</TD><td><img src=/images/spacer.gif></td>\n";
     print "<TD>New Construction</TD><TD>$table->[0][8]</TD></TR>\n";
     print "<TR><TD>Furnished </TD><TD>$table->[0][5]</TD></TR>\n";

     print "</table>";
     print "$table->[0][10] <P>\n";

      
     my $rc1 = $cursor_s->finish;

     $login_id = $table_b->[0][8];

     $ra4 = $dbh->do("insert into page_hit
		(login_id) values ($login_id)");     


my $rc2 = $dbh->disconnect;

     if ("$table_b->[0][7]" eq "Y") {

	print "<form METHOD=POST ACTION=\"http://www.buyresortproperties.com/cgi-bin/brp-mail.cgi\">\n";
        print "<input type=hidden name=recipient value=\"$table_b->[0][5]\">\n";

  	$file = "/home/httpd/buyresortproperties/new-form.html";
	open(F,$file) || die "Cannot open $file: $!";
	while (<F>) {
          print;
        }
        close(F);
	

     } 

     if ("$table_b->[0][7]" eq "1") {

        $file = "/home/httpd/buyresortproperties/resortime-form.html";
        open(F,$file) || die "Cannot open $file: $!";
        while (<F>) {
          print;
        }
        close(F);


     } 


