#!/usr/bin/perl

#####! c:\software\perl\bin\perl

# 
#
# Issue record
#
#	25/2/01		1.0	First Release
#

$width=191;
$tabwidth=616;
$cols=0;

&init();
&get_form_data();
&process_request();
&log_request();

sub init()
{
   $standalone=0;
   $version=1;
   $basepath="/websites/LinuxPackage04/te/xt/ur/textura.f2s.com/public_html/lcr-cgi" if ($standalone == 0);
   # $basepath="." if ($standalone == 0);
   $basepath="." if ($standalone == 1);
   $logdir="$basepath/logs";
   $updatelog="$basepath/update/";
   $datafile="$basepath/lcr.txt";
   $webmaster="webmaster\@londoncountertrade.org";
   $generate_out="members_list.htm" if ($standalone == 0);
   $reference="$logdir/lcr.log";
}

sub get_form_data
{
   # Determine the form's REQUEST_METHOD (GET or POST) and split the form
   #
   # fields up into their name-value pairs. If the REQUEST_METHOD was
   #
   # not GET or POST, send an error.
   #
   if ($ENV{'REQUEST_METHOD'} eq 'GET')
   {
      # Split the name-value pairs
      @pairs = split(/&/, $ENV{'QUERY_STRING'});
   }
   elsif ($ENV{'REQUEST_METHOD'} eq 'POST') 
   {
      # Get the input
      read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
      # Split the name-value pairs
      @pairs = split(/&/, $buffer);
   }
   else
   {
      # Not GET or POST. 
      print "content-type: text/html\n\n";
      print "<HTML><HEAD>Inform - Error</HEAD><BODY><H1>INFORM Error</H1>\n";
      print "<P>Request method ($ENV{'REQUEST_METHOD'}) is invalid. Please contact webmaster.</P>\n";
      print "</BODY></HTML>\n";
   }
   foreach $pair(@pairs)
   {
      ($name, $value) = split(/=/, $pair);
      # un-webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;
      $FORM{$name} = $value;
   }
}

sub process_request()
{
   $countries = 0;
   &generate_header();
   $format = sprintf ("%d", $FORM{'format'});
   $search = sprintf ("%d", $FORM{'search'});
   print "<table summary=\"Member\" CELLSPACING=1>\n" if ($format > 0);
   $class=0;
   open DATA, $datafile;
   @data=<DATA>;
   close DATA;
   # Now sort data
   @sortdata=sort(@data);
   #while (<DATA>)
   foreach $dataitem(@sortdata)
   {
      @line = split (/[|]/, $dataitem);
      chomp @line;
      # Check if entry required
      if (substr($line[0],0,1) ne '#')
      {
         # Now display line
         if ($search > 0)
         {
            if (lc($line[13]) =~ lc($FORM{'country'}))
            {
               &show_country($format,$class,1,@line);
               $countries++;
               $class=1-$class;
            }
         }
         elsif ($FORM{'name'} ne "")
         {
            if ($line[0] eq $FORM{'name'})
            {
               &show_entry($format,$class,1,@line);
               $class=1-$class;
            }
         }
         else
         {
            &show_entry($format,$class,0,@line);
            $class=1-$class;
         }

      }
   }
   if ($search > 0)
   {
      print "<tr><td><br>No entries for $FORM{'country'} found</td></tr>\n" if ($countries == 0);
   }
   else
   {
      # print "<tr><td colspan=3>$countries entries found</td></tr>\n"
   }
   &generate_footer($format);
}

#################################
##### SUPPORT ROUTINES ##########
#################################

sub show_country()
{
   $scriptid="http://www.textura.f2s.com/lcr-cgi/members.pl";
   $detailstr="$scriptid?format=0&name=";
   my ($format,$class,$index,@line)= @_;
   $style = sprintf ("line%d", $class);
   if ($format > 0) 
   {
      print "<tr class=$style><td width=31%>";
      print "$line[13]</td><td>&nbsp;&nbsp;</td><td>";
      if ($line[9] eq "")
      {
         print "$line[0]</td>"; 
      }
      else
      {
         print "<a href=mailto:$line[9]>$line[0]</a></td>"; 
      }
      print "<td>";
      if ($line[11] eq "")
      {
         print "$line[2]</td>"; 
      }
      else
      {
         print "<a href=$line[11]>$line[2]</a></td>"; 
      }
      print "<td><a href=\"$detailstr$line[0]\">Details</a></td></tr>\n";
   }
}

sub show_entry()
{
   $scriptid="http://www.textura.f2s.com/lcr-cgi/members.pl";
   $indexstr="memberdir.htm";
   $detailstr="$scriptid?format=0&name=";
   my ($format,$class,$index,@line)= @_;
   $style = sprintf ("line%d", $class);
   if ($format > 0) 
   {
      print "<tr class=$style><td>";
      if ($line[9] eq "")
      {
         print "$line[0]</td>"; 
      }
      else
      {
         print "<a href=mailto:$line[9]>$line[0]</a></td>"; 
      }
      print "<td>";
      if ($line[11] eq "")
      {
         print "$line[2]</td>"; 
      }
      else
      {
         print "<a href=$line[11]>$line[2]</a></td>"; 
      }
      print "<td><a href=\"$detailstr$line[0]\">Details</a></td></tr>\n";
   }
   else
   {
      print "<table summary=\"Member\" CELLSPACING=0 BORDER=2 WIDTH=$tabwidth>\n";
      &show_member("MEMBERS NAME",$line[0],$line[1]);
	  &show("COMPANY NAME",$line[2]) if ($line[2] ne "");
      &show("",$line[3]) if ($line[3] ne "");
      &show_address("ADDRESS",$line[4]) if ($line[4] ne "");
      &show("POSTCODE",$line[5]) if ($line[5] ne "");
      &show("TELEPHONE",$line[6]) if ($line[6] ne "");
      &show("MOBILE",$line[8]) if ($line[8] ne "");
      &show("FAX",$line[7]) if ($line[7] ne "");
      &show_email("EMAIL",$line[10]) if ($line[10] ne "");
      &show_website("WEBSITE",$line[11]) if ($line[11] ne "");
      &show("BUSINESS",$line[12]) if ($line[12] ne "");
      &show("COUNTRIES",$line[13]) if ($line[13] ne "");
      &show("SPECIALISATION",$line[14]) if ($line[14] ne "");
      print "</table><p><a href=\"#top\">Top</a></p>\n" if ($index == 0);
      print "</table><p><a href=\"$indexstr\">Index</a></p>\n" if ($index > 0);
   }
}

sub show()
{
   my ($title,$value) = @_;
   $value="&nbsp;" if ($value eq "");
   $title="$title:" if ($title ne "");
   $title="&nbsp;" if ($title eq "");
   &show_title($title);
   print "$value</td></tr>\n"; 
}

sub show_member()
{
   my ($title,$value,$image) = @_;
   $value="&nbsp;" if ($value eq "");
   $title="$title:";
   @name = split (/[,]/, $value);
   $name[0]=uc($name[0]);
   &show_title($title);   
   print "$name[1] $name[0]";
   if ($image ne "")
   {
   		&show_image($image,$name[1]." ".$name[0]);
   }
   print "</td></tr>\n"; 
}

sub show_address()
{
   my ($title,$value) = @_;
   $value="&nbsp;" if ($value eq "");
   $title="$title:";
   @line = split (/[,]/, $value);
   &show_title($title);
   foreach $line(@line) { print "$line<BR>";}
   print "</td></tr>\n"; 
}

sub show_email()
{
   my ($title,$value) = @_;
   &show_title($title);
   if ($value eq "")
   {
      print "&nbsp;</td></tr>\n"; 
   }
   else
   {
      print "<a href=mailto:$value>$value</a></td></tr>\n"; 
   }
}

sub show_website()
{
   my ($title,$value) = @_;
   &show_title($title);
   if ($value eq "")
   {
      print "&nbsp;</td></tr>\n"; 
   }
   else
   {
      print "<a href=$value>$value</a></td></tr>\n"; 
   }
}

sub show_title()
{
   my ($title) = @_;
   print "<tr><td WIDTH=$width>$title</td><td colspan=2>";
}

sub show_image()
{
	my ($image,$name) = @_;
	$imagedir="http://www.londoncountertrade.org/members/";
	#print "<td>Image : $imagedir.$image alt $name</td>";	
	print "&nbsp;&nbsp;<img align=middle src=$imagedir$image alt=\"$name\" width=90></td>";
}

sub generate_header
{
  if ($standalone == 1)
  {
     open (OUTFILE, ">$generate_out") || warn "Failed to create file\n";
     select OUTFILE;
  }
  else
  {
     print "Content-type: text/html\n\n";
  }
        print <<"(END HTML HEADER)";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>London Countertrade Roundtable - Membership</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<META name="author" content="Textura">
<base href="http://www.londoncountertrade.org">
<link type="text/css" href="css/lcr.css" rel=stylesheet>
<script type="text/javascript" language="Javascript" src="js/lcr.js"></script>
</HEAD>
<BODY>
<A NAME="TOP"></A>
<script type="text/javascript" language="Javascript">pageHeader();</script>
<noscript>
<p>[<a href="index.html"></a>]&nbsp;[<a href="members.htm"></a>]&nbsp;[<a href="activity.htm"></a>]&nbsp;
[<a href="meeting.htm"></a>]&nbsp;[<a href="seminars.htm"></a>]&nbsp;[<a href="offset_register.htm"></a>]&nbsp;
[<a href="press.htm"></a>]&nbsp;[<a href="links.htm"></a>]&nbsp;[<a href="contacts.htm"></a>]</p>
</noscript>
<h1>Membership</h1>

(END HTML HEADER)


  #print "<HTML><HEAD><TITLE>LCR Membership</TITLE>\n";
  # print "<script language=JavaScript type=text/javascript src=http://www.fast-thales.net/fast.js></script>\n";
  # print "<LINK HREF=\"http://www.fast-thales.net/fast.css\" REL=\"stylesheet\" TYPE=\"text/css\">\n";
  # Version 2 website change   
  #print "</HEAD><BODY>\n" if ($version == 1);
  if ($standalone == 1)
  {
    close (OUTFILE);
    select STDOUT;
  }
}

sub generate_footer
{
  my ($format)= @_;
  if ($standalone == 1)
  {
     open (OUTFILE, ">>$generate_out") || warn "Failed to extend file\n";
     select OUTFILE;
  }
   print "</table>\n" if ($format > 0);

        print <<"(END HTML FOOTER)";
<script type="text/javascript" language="Javascript">pageFooter();</script>
<noscript><p>&copy;2002 <a href="http://www.textura.co.uk">Textura</a></p></noscript>
</BODY></HTML>

(END HTML FOOTER)


  if ($standalone == 1)
  {
    close (OUTFILE);
    select STDOUT;
  }
}

sub log_request
{
   open (OUTFILE, ">>$reference") || warn "Failed to open logfile\n";
   &timenow(1);
   print OUTFILE "$time_now|$FORM{'format'}|$ENV{'REMOTE_ADDR'}|$ENV{'HTTP_REFERER'}|$ENV{'HTTP_USER_AGENT'}\n";
   close OUTFILE;
}

sub timenow
{
   my($format) = @_; 
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdist)=gmtime();
   $dayname = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];
   $monname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
   $monnamefull = (January, February, March, April, May, June, July, August, September, October, November, December)[$mon];
   $year +=1900;
   # BOTCH adjustment to French time!!
   $hour +=1 if ($hour < 22);
   if ($format == 1)
   {
      $time_now=sprintf ("%02d %s %d %02d:%02d:%02d",$mday,$monname,$year,$hour,$min,$sec);
   }
   elsif ($format == 2)
   {
      $time_now=sprintf ("%s %d, %d",$monnamefull,$mday,$year);
   }
   elsif ($format < 0)
   {
      $time_now=sprintf ("%d%02d%02d",$year,$mon+1,$mday);
   }
   else
   {
      # $time_now=sprintf ("%s %s %02d %d %02d:%02d:%02d",$dayname,$monname,$mday,$year,$hour,$min,$sec);
      $time_now=sprintf ("%02d %s %d ",$mday,$monname,$year);
   }
}

