#!/usr/bin/perl
require "wiz_resources";
$action=param("action");
$action=lc($action);

cookie_read();

if ($action eq "verify") {
  verify();
} else {
  header("Site Details","","edit");
  init();
  print_form();
  footer();
}


sub print_form {
  $temp_phone="01" if ($temp_phone eq "");
  $temp_fax  ="01" if ($temp_fax eq "");
print <<EOF;
<form action="/cgi-bin/sitedetails" method="post">
<input type="hidden" name="action" value="verify">
<table>
<tr valign="top" bgcolor="#eeeeee"><td align="right"><font color="#ff0000">Organisation name, long:</font>&nbsp;</td><td><input type="text" name="longname" value="$temp_longorg" size="32"></td><td><font color="#666666">(e.g. <tt>Forest Fields Sixth College of Tertiary Education</tt> or <tt>Smith and Sons Tractor Parts Ltd.</tt>)</font></td></tr>
<tr valign="top"><td align="right">Organisation name, short:&nbsp;</td><td><input type="text" name="shortname" value="$temp_shortorg" size="32" maxlength="32"></td><td><font color="#666666">(e.g. <tt>Forest Fields</tt> or <tt>Smith and Sons</tt>)</font></td></tr>
<tr valign="top" bgcolor="#eeeeee"><td align="right">Contact name:&nbsp;</td><td><input type="text" name="contactname" value="$temp_contact" size="32" maxlength="64"></td><td><font color="#666666">(e.g. <tt>Sharon</tt> or <tt>us</tt> or <tt>our sales staff</tt>)</font></td></tr>
<tr valign="top"><td align="right"><font color="#ff0000">Address:</font>&nbsp;</td><td><textarea name="address" rows="6" cols="32" wrap="virtual">$temp_address</textarea></td><td></td></tr>
<tr valign="top" bgcolor="#eeeeee"><td align="right">Post code:&nbsp;</td><td><input type="text" name="postcode" value="$temp_postcode" size="12" maxlength="8"></td><td></td></tr>
<tr valign="top"><td align="right">Telephone number:&nbsp;</td><td><input type="text" name="phone" value="$temp_phone" size="12" maxlength="20"></td><td></td></tr>
<tr valign="top" bgcolor="#eeeeee"><td align="right">Fax number:&nbsp;</td><td><input type="text" name="fax" value="$temp_fax" value="01" size="12" maxlength="20"></td><td></td></tr>
<tr valign="top"><td align="right">Email address:&nbsp;</td><td><input type="text" name="email" value="$temp_email" size="32" maxlength="64"></td><td></td></tr>
<tr valign="top" bgcolor="#eeeeee"><td align="right">Description:&nbsp;</td><td><input type="text" name="description" value="$temp_description" size="32"></td><td><font color="#666666">(e.g. <tt>We make the best flux capacitors in the alpha quadrant</tt>)</font></td></tr>
<tr valign="top"><td align="right">Key words:&nbsp;</td><td><input type="text" name="keywords" value="$temp_keywords" size="32"></td><td><font color="#666666">(a list of words and phrases that describe what you do, separated by commas - e.g.<br><tt>locally brewed beers, tea, coffee, bar snacks, sandwiches</tt>)</font></td></tr>

<tr valign="top"><td align="right">Link Preference:&nbsp;</td><td>
<select name="linkprefs">
EOF

print "<option value=\"all\"";
print " selected" if ($temp_linkprefs eq "all");
print ">show all links</options>\n";
print "<option value=\"some\"";
print " selected" if ($temp_linkprefs eq "some");
print ">show only some links</options>\n";

print <<EOF;
</select>
</td><td><font color="#666666">(Do you want all pages linked to on the navigation bar, or just ones that the system recommends?)</font></td></tr>

<tr align="center" bgcolor="#eeeeee"><td colspan="3">
<input type="submit" value="continue">
</td></tr>
</table>
</form>
EOF
}

sub init {
  $temp_longorg    ="";
  $temp_shortorg   ="";
  $temp_contact    ="";
  $temp_address    ="";
  $temp_postcode   ="";
  $temp_phone      ="01";
  $temp_fax        ="01";
  $temp_email      ="";
  $temp_description="";
  $temp_keywords   ="";

  $temp_longorg    =$user_longorg     if ($user_longorg     ne "");
  $temp_shortorg   =$user_shortorg    if ($user_shortorg    ne "");
  $temp_contact    =$user_contact     if ($user_contact     ne "");
  $temp_address    =$user_address     if ($user_address     ne "");
  $temp_postcode   =$user_postcode    if ($user_postcode    ne "");
  $temp_phone      =$user_phone       if ($user_phone       ne "");
  $temp_fax        =$user_fax         if ($user_fax         ne "");
  $temp_email      =$user_email       if ($user_email       ne "");
  $temp_description=$user_description if ($user_description ne "");
  $temp_keywords   =$user_keywords    if ($user_keywords    ne "");
  $temp_linkprefs  =$user_link_prefs  if ($user_link_prefs  ne "");
}

sub verify {
  $temp_longorg    =param("longname");
  $temp_shortorg   =param("shortname");
  $temp_contact    =param("contactname");
  $temp_address    =param("address");
  $temp_postcode   =param("postcode");
  $temp_phone      =param("phone");
  $temp_fax        =param("fax");
  $temp_email      =param("email");
  $temp_description=param("description");
  $temp_keywords   =param("keywords");
  $temp_linkprefs  =param("linkprefs");
  if (($temp_longorg eq "") || ($temp_address eq "")) {
    header("Site Details","","edit");
    headline("Error: required details not submitted","red","","The minimum requirement is that you fill in your organisation name and address; come on, that's not much to ask for is it?");
    print_form();
    footer();
  } else {
    $user_longorg=$temp_longorg;
    $user_shortorg=$temp_shortorg;
    $user_contact=$temp_contact;
    $user_address=$temp_address;
    $user_postcode=$temp_postcode;
    $user_phone=$temp_phone;
    $user_fax=$temp_fax;
    $user_email=$temp_email;
    $user_description=$temp_description;
    $user_keywords=$temp_keywords;
    $user_link_prefs=$temp_linkprefs;
    
    $user_address=~ s~[\n|\r]+~\n~sg;
    $user_address=~ s~\n~,\n~sg;
    $user_address=~ s~,+~,~sg;
    $user_address=~ s~\n~<br>~sg;
    
    my $err=0;
      open OUTPUT, ">${homesite}.wiz_details" or $err=1;
      if ($err==0) {
        print OUTPUT "longorg: $user_longorg\n";
        print OUTPUT "shortorg: $user_shortorg\n";
        print OUTPUT "contact: $user_contact\n";
        print OUTPUT "address: $user_address\n";
        print OUTPUT "postcode: $user_postcode\n";
        print OUTPUT "phone: $user_phone\n";
        print OUTPUT "fax: $user_fax\n";
        print OUTPUT "email: $user_email\n";
        print OUTPUT "description: $user_description\n";
        print OUTPUT "keywords: $user_keywords\n";   
        print OUTPUT "linkprefs: $user_link_prefs\n";
        print OUTPUT "\n";
        close OUTPUT;
        set_change_date() if (-e "${homesite}.wiz_track");
        # -e check so that new site can be auto-generated
        header("Site Details","refresh","edit");
        headline("Details set","","","Your details have been stored.<p>Please <a href=\"/cgi-bin/edit\">click to continue</a>.");
        if (-e "${homesite}.wiz_track") {
        }
        footer();
      } else {
        header("Site Details","","edit");
        headline("Error: can't open details storage file","red","","Unable to open details storage file for writing - this means I cannot store any of your details!  Please contact <a href=\"mailto:$support_email\">$company support</a> and get them to check the permissions for your home directory.");
        footer();
      }
  }
}