#!/usr/bin/perl -w
use CGI qw(:standard);

#both left or right ajustiment, when length is 0, will print the string
#put extra :: at the end of .def file to avoid 
# to print out last field as trash

close STDERR;
open STDERR, '>>', './errlog.txt' 
  or die "Couldn't redirect STDERR: $!";
#paul, did put error msg not to sdout

warn "this will go to errlog.txt\n";

$q = new CGI; open(def_file,"/home/plee1470/http_root/jws/cgi-bin/bid.def") || print "die<p>";

@lines = <def_file>;
@colnames = split(":",$lines[0]);
close(def_file);

my (@ss,$line);

foreach $k (@colnames){
 $v = $q->param("$k");
 if (($k eq "bid") && ($v eq "")){

print header(), start_html("Your Registration");
print "<p> SOME IMPORTANT INFO. MISSING, Please go back to registed AGAIN\n";
#paul: prevent 500 meaningless msg, and fellowing code produce the same
#output as getbid.cgi, it still would go to web,why?
#pasted from getbid.cgi, it worked print header() is important!!!

#   system ("perl /home/chrislu/http_root/jws/cgi-bin/getbid.cgi");
print "<HTML>\n";

print '<body bgcolor="#66FFFF">';
print '<font color="#660066">';


print "<FORM 
ACTION=\"http://bitcoinshell.mooo.com/users/plee1470/jws/cgi-bin/setbid.cgi\">\n";


$i=0;
while (($colnames[${i}]) && ($colnames[${i}] ne "Date")) {
  if ($colnames[${i}] eq "bid") {
    print "Buyer email address<INPUT NAME=\"$colnames[${i}]\" VALUE=\"\"><p>\n";
  }
  else {
  print "$colnames[${i}] <INPUT NAME=\"$colnames[${i}]\" VALUE=\"\"><p>\n";
  }
  $i += 1;
}
print "<p><INPUT TYPE=\"submit\"> </FORM>";

print "</HTML>";


   exit(0);
 }
 if ($v eq "") {
   $v = "null";
 }
 if ($k eq "Date") {
   $v =mydate();
 }
$v="$v:";
push(@ss,$v);
#print "<p>$v\n";
#print "<p>$line\n";
}
pop(@ss); pop(@ss); #get rid of two extra null
foreach (@ss){
# print "$_";
$line="$line$_";

}
#print ("<p>\n");

$line="$line:\n"; #give an extra : and a new line
open(FILE, ">>/home/plee1470/http_root/jws/cgi-bin/bid.dat");
print FILE $line;

print header(), start_html("Thank You For Your Registration"), h1("Thank You For Your Registration");
print "<p> Thank you for registed as following:\n";
print "<p> ------------------------------------\n";
print "<p> $line\n";
close(FILE); # done the append a record/line

print '<p><A HREF="http://bitcoinshell.mooo.com/users/plee1470">Home</A>';

#mydate-- change : to + in date format
sub mydate {
  $s = `date`;
  chomp($s);
  my @ss= split(/:/, $s);
  my $ss = join("+", @ss);
  return $ss;
}
