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

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

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

my (@ss,$line);

 $from = $q->param("eid");
 $to = $q->param("To");
 $msgbody= $q->param("MsgBody");
foreach $k (@colnames){

 $v = $q->param("$k");
 if ((($k eq "eid") && ($v eq "")) || (($k eq "To") && ($v eq ""))){
   system ("perl /home/plee/jws/cgi-bin/geteid.pl 'Some important fields were not filled, please fill the form again\'");
   exit(0);
 }
 if ($v eq "") {
   $v = "null";
 }
 if ($k eq "Date") {
   $v =mydate();
 }

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

}

$line="$line:\n"; #give an extra : and a new line
open(FILE, ">>/home/plee/jws/cgi-bin/eid.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

#sending email now
 # dir only work with full, not relative with ~
 # shell email now only can send to  unix email, not web mail.
 #open (tmpfile,  ">/home/plee/jws/cgi-bin/mail.tmp") || print "can not open";
 #print tmpfile $msgbody;
 #close tmpfile;
# system "cd /home/plee/jws/cgi-bin; java -classpath .:mail.jar:activation.jar mastermail $to $from $msgbody" || print "can not mail";
print "to $to\n";
print "from $from\n";
$body=mypara($msgbody);
print "msgbody $body\n";

 #-s test $to \< mail.tmp" || print "something is not right";
#system "cd /home/plee/jws/cgi-bin; java  -classpath .:mail.jar:activation.jar  mastermail plee1143\@yahoo.com  someone\@some.com \"line1\nline2\nline3\n\"";
system "cd /home/plee/jws/cgi-bin; java  -classpath .:mail.jar:activation.jar  mastermail $to  $from $body\n";

print '<p><p><A HREF="http://school.hzclake.net:8888">Home</A>';

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

}
