#!/usr/bin/perl
# Items for future implementation
#
#	Birth/Death dates
#	ToddFest attendance record
#	Concert attendance record
#	
#
require "weblib.lib";
require "/home/todd/private/toddweb.env";
use Time::Local;
use POSIX "strftime";
use POSIX "setlocale";
use DB_File;
$style=<<EOF;
style=body {font-family: Verdana, Arial, sans-serif;}
.options	{border: 0px solid blue;}
EOF
$PUBLIC_PRIV=99;
$PRIVATE_PRIV=0;
%PRIVACY=($PRIVATE_PRIV,Private,$PUBLIC_PRIV,Public,"","Field not defined");
$SENDMAIL="/usr/sbin/sendmail";
&setupLanguages;
&checkUpdates;
if($param{"confirm"}&&$admin){&confirmNewMember;}
elsif($param{"fan"}>0){&findFanID;}
elsif($param{"fanlist"}){&displayFanList;}
elsif($fanid){$pubfanid=$FANDIR{"$fanid:PubFanID"};}
if($param{update}){
	$createupdate=1;
	$createupdatetag="/update";
	if($fanid){$pubfanid=$FANDIR{"$fanid:PubFanID"};}
	else{
		$pubfanid=0;
		srand(time||$$);
		while($fanid==0 || defined $FANDIR{$fanid}){
			$fanid=sprintf("%07d",int(rand(9999999))+1);
			$checksum=&checksum($fanid);
			$fanid="$fanid-$checksum";
		}
	}
}
foreach $field(@checkfields){$field="$field:$LANG{$field}";}
%fields=map{$_,1} @fields;
foreach $field(@fields){$DB{$field}=$FANDIR{"$fanid:$field"};}
$DB{Photo}=$FANDIR{"$fanid:Photo"};
foreach $field(@checkfields){
	($fieldname,$fieldtest)=split(/:/,$field,2);
	$DB{$fieldname}=$FANDIR{"$fanid:$fieldname"};
}
if($update){&doUpdate;}
$privacylevel=0;
foreach $field(@checkfields){
	($fieldname,$fieldtest)=split(/:/,$field,2);
	$Checked{$fieldname}=$DB{$fieldname}==1?" checked":"";
}
$photo=qq{<img src="/trc/pictures/fandir/$pubfanid.jpg">} if -f "$PHOTODIR/$pubfanid.jpg";
&setstateprov;
&yearselect;
$cookiedate=&cookiedate(time+365*86400);
$cookiedate=&cookiedate(time-365*86400) if $param{nocookie};
$cookie="cookie=FanID=$fanid; expires=$cookiedate; path=/fandb"
	if $update||$param{nocookie}||$param{setcookie};
$cookie="" if ($idincookie&&!$param{nocookie}&&!$param{setcookie})||$fanid==0;
$Title=$LANG{Title}=~/^<img/?$ENG{Title}:$LANG{Title};
&htmlopen($Title,"$LANG{Title}%",$cookie,$style,"charset=ISO-8859-1");
&displayLangSelection;
&displayCommonInfo;
&displayRequestedInfo;
&displayAcknowledgements;
print $PAGETAIL;
&htmlclose;
sub setupLanguages{
	@language=(
		"dutch:Nederlands",
		"english:English",
		"spanish:Espa&ntilde;ol",
		"french:Fran&ccedil;ais",
		"german:Deutsche",
		"italian:Italiano",
		"japanese:Nihongo",
		"portuguese:Portugues",
		"swedish:Svenska");
	%language=map {split(/:/)} @language;
	@pathinfo=split(m#/#,substr($ENV{PATH_INFO},1));
	$language=$pathinfo[0] if $language{$pathinfo[0]};
	if($language){shift @pathinfo;}
	else{$language="english";}
	foreach $param(@pathinfo){
		($key,$value)=split(/=/,$param);
		$value=-1 unless $value;
		$param{"\L$key"}=$value;
	}
	$ENV{LANG}=$language;
	$ENV{LC_ALL}="LC_ALL";
	$PHOTODIR="/home/todd/www/pictures/fandir";
	dbmopen(%CC,"/home/todd/db/cc",0644);
	dbmopen(%FANDIR,"/home/todd/db/fandir",0644);
	if($language eq "english"){
		dbmopen(%LANG,"/home/todd/db/fandb-$language",0644);
	}
	else{
		dbmopen(%LANGDB,"/home/todd/db/fandb-$language",0644);
		dbmopen(%ENGDB,"/home/todd/db/fandb-english",0644);
		foreach $key(keys %ENGDB){
			$ENG{$key}=$ENGDB{$key};
			$LANG{$key}=$LANGDB{$key}?$LANGDB{$key}:$ENG{$key};
		}
		dbmclose(%LANGDB);
		dbmclose(%ENGDB);
	}
}
sub displayLangSelection{
	print qq{<div style="display: table; border: 2px gold solid; width: auto; margin-left: auto;},
		qq{ margin-right: auto; padding: 0px; margin-bottom: 10px;">\n},
		qq{<ul style="margin: 2px; padding: 0; list-style-type: none;">\n};
	foreach $langcell(@language){
		($lang)=split(/:/,$langcell);
		print qq{<li style="text-align: center; display: inline; border: 1px gold solid; margin: 0px;">};
		if($lang eq $language){print $language{$lang};}
		else{
			print "<a href=$weblib::URL/$lang";
			foreach $param(keys %param){
				print "/$param";
				print "=$param{$param}" if $param{$param} ne "-1";
			}
			print ">$language{$lang}</a>";
		}
		print "</li>\n";
	}
	print "</ul>\n";
	$cols=@language;
	print "</div>\n";
}
sub checkUpdates{
	$update=&ReadParse(*in,*incfn,*inct);
#	&htmlopen("Test");
#	print &PrintVariables(%in);
	$update=0 if $param{"fan"};
	$update=0 if $ENV{'REQUEST_METHOD'} eq "GET";
	$fanid=getcookie("FanID");
	$idincookie=$fanid;
	$fanid=$param{"fanid"} if $param{"fanid"}=~/^\d{7}-\d+$/;
	$fanid=$in{FanID} if $in{FanID}=~/^\d{7}-\d+$/;
	$fanidparam="/fanid=$fanid" unless $idincookie||!$fanid;
	($fid,$cs)=split(/-/,$fanid);
	if(&checksum($fid) != $cs){$fanid=0;}
	if($fanid == 0 && $in{Email}){&SendID($in{Email});}
	$admin=$FANDIR{"$fanid:Admin"};
	@fields=(First,Middle,Last,Address,City,State,Prov,Country,Postal,Email,
		TRTVHandle,TRCFHandle,ICQ,FanSince,PHCC,PHAC,PHNum);
	@checkfields=(News,ToddFest,UT,BW,RockLove,OneWorld,SpecialInterest);
}
sub confirmNewMember{
	foreach $key(grep /:Email/,keys %FANDIR){
		($fanid)=split(/:/,$key);
		next if $FANDIR{"$fanid:Confirmed"};
		($priv,$email)=split(/:/,$FANDIR{$key},2);
		$bcclist.="Confirmed $fanid: $email<br>\n";
		$newsfeed=$FANDIR{"$fanid:News"}?"(and you have)":"(you haven't)";
		open(MAIL,"|$SENDMAIL -t -f roger");
		($priv,$name1)=split(/:/,$FANDIR{"$fanid:First"},2);
		($priv,$name2)=split(/:/,$FANDIR{"$fanid:Middle"},2);
		($priv,$name3)=split(/:/,$FANDIR{"$fanid:Last"},2);
		$name1="(none)" if $name1 =~ /^\s*$/;
		$name2="(none)" if $name2 =~ /^\s*$/;
		$name3="(none)" if $name3 =~ /^\s*$/;
		$salutation=$name1 ne "(none)"?"Hi $name1!\n\n":"";
		print MAIL <<EOF;
From: "The International Todd Rundgren Fan Database" <toddfans\@TRConnection.com>
To: $email
Subject: Confirmation of ITRFDB registration

${salutation}Thanks for registering with The International Todd Rundgren Fan Data 
Base. This e-mail confirms your registry, and is the first of the
generally infrequent administrative messages that I may send from time
to time. As of this writing, there are $FANDIR{HighPubFanID} registered fans.
Not exactly the whole world, but we're getting there!

If you've requested the "Occasional news" option $newsfeed, you'll 
also start receiving those messages as well. Don't worry, it'll be a 
light feed, maybe once or twice a month, more if something's cooking. 
If you've got news to share, send it to toddfans\@TRConnection.com, and 
if it's truly newsworthy, it will be sent on to the subscribers.

Below you will find a summary of your data base entry. You may wish 
to check it for accuracy, and if necessary, visit 
http://TRConnection.com/fandb to make any corrections

If you find that you're in the database more than once, drop me a
line with the private ID# of the duplicate entry (or entries) and
I'll see to it that it gets removed. And don't hesitate to ask
for assistance if something else seems to be not working!

-- 
Roger Linder, Administrator
roger\@TRConnection.com
========================================================================

EOF
		print MAIL "$LANG{FanIDLabel}: $fanid\n";
		printf MAIL "$LANG{PubFanIDLabel}: %d\n",$FANDIR{"$fanid:PubFanID"};
		$dateestab=&formatdate($FANDIR{"$fanid:DateEstab"});
		$dateupdated=&formatdate($FANDIR{"$fanid:DateUpdated"});
		print MAIL "$LANG{DateEstabLabel} - $dateestab\n";
		print MAIL "$LANG{DateUpdatedLabel} - $dateupdated\n\n";
		print MAIL $LANG{"NameLabel"}," (Privacy level: $PRIVACY{$priv})\n";
		print MAIL "Contents: ",
			"  First: $name1, Middle: $name2, Last: $name3\n\n";
		foreach $field(@fields[3..$#fields]){
			($priv,$value)=split(/:/,$FANDIR{"$fanid:$field"});
			if($field eq "State"){ 
				print MAIL $LANG{StateLabel}." (State)";
			}
			elsif($field eq "Prov"){
				print MAIL $LANG{StateLabel}." (Province)";
			}
			elsif($LANG{"${field}Label"}){
				print MAIL $LANG{"${field}Label"};
			}
			else{
				print MAIL $field;
			}
			if($value){
				print MAIL " (Privacy level: $PRIVACY{$priv})\n";
				print MAIL "Contents: ";
				if($field eq "Country"){print MAIL "$value ($CC{$value})\n\n";}
				else{print MAIL "$value\n\n";}
			}
			else{
				print MAIL ": No value given\n\n";
			}
		}
			print MAIL $LANG{PhotoLabel},": ";
		if(-f "$PHOTODIR/".$FANDIR{"$fanid:PubFanID"}.".jpg"){
			print MAIL "Yes (Privacy level: ",
				$PRIVACY{$FANDIR{"$fanid:Photo"}},")\n\n";
		}
		else{
			print MAIL "No\n\n";
		}
		if($translist=$FANDIR{"$fanid:Translator"}){
			print MAIL "You've also agreed to be a translator for the following languages:\n";
			print MAIL "\n\t$translist\n\nThanks!\n\n";
		}
		foreach $checkfield(@checkfields){
			print MAIL $LANG{$checkfield},"\n";
			if(defined $FANDIR{"$fanid:$checkfield"}){
				print MAIL "\t",$FANDIR{"$fanid:$checkfield"}?"Yes":"No","\n";
			}
			else{
				print MAIL "\tNo answer provided yet\n";
			}
		}
		close(MAIL);
		$FANDIR{"$fanid:Confirmed"}=time;
	}
}
sub findFanID{
	$authfanid=$fanid if $FANDIR{"$fanid:DateEstab"};
	$pubfanid=sprintf("%07d",$param{"fan"});
	$pubfanid=0 unless $authfanid;
	$pubfanid=0 unless $pubfanid=~/^\d{1,7}$/&&($fanid=$FANDIR{$pubfanid});
	if($pubfanid==0){$fanid=$authfanid;$authfanid=0;}
}
sub displayCommonInfo{
	print "<b>$error</b><p>\n" if $error;
	$weblib::URL.="/$language";
	print "$LANG{Info}<p>\n";
	print <<EOF unless $param{faq};
<center>
<a href=$weblib::URL/faq>$LANG{FAQLabel}</a>
</center>
$bcclist<p>
EOF
}
sub displayFanList{
	$fanlist="<center><table cellpadding=5>\n";
	for($fan=1;$fan<=$FANDIR{HighPubFanID};$fan++){
		$key=$FANDIR{sprintf("%07d",$fan)};
		if(defined $key){
			($privacy,$name1)=split(/:/,$FANDIR{"$key:First"});
			($privacy,$privacylist)=split(/;/,$privacy,2);
			$private=$privacy<$PUBLIC_PRIV if $name1;
			($privacy,$name2)=split(/:/,$FANDIR{"$key:Middle"});
			($privacy,$privacylist)=split(/;/,$privacy,2);
			$private=$private&&$privacy<$PUBLIC_PRIV if $name2;
			($privacy,$name3)=split(/:/,$FANDIR{"$key:Last"});
			($privacy,$privacylist)=split(/;/,$privacy,2);
			$private=$private&&$privacy<$PUBLIC_PRIV if $name3;
			next if $private && !$admin;
			$name="\u$name3$;\u$name1$;\u$name2";
			$name{$name}=$private?"*$fan":$fan;
			$fancount++ if $admin;
		}
	}
	@names=sort {"\L$a" cmp "\L$b"} keys %name;
	$cols=4;
	$listlen=int(@names/$cols);
	$listlen+=1 unless @names%$cols == 0;
	$fanlist.="<tr><td valign=top>\n";
	$total=$len=0;
	foreach $name(@names){
		$len++;
		$total++;
		$fan=$name{$name};
		$private=$fan=~s/^\*//;
		($name3,$name1,$name2)=split(/$;/,$name);
		$fanlist.="*" if $private;
		$fanlist.="<a href=$weblib::URL/$language/fan=$fan$fanidparam>".
			"$name1 $name2 $name3</a><br>\n";
		if($len == $listlen){
			$fanlist.="</td><td valign=top>\n" unless $total == @names;
			$len=0;
		}
	}
	$fanlist.="</td></tr></table></center>\n";
	$fanlist.="$fancount registered fans<br>\n" if $admin;
}
sub displayRequestedInfo{
	if($param{faq}){print <DATA>;}
	elsif($fanlist){print $fanlist if $FANDIR{"$fanid:DateEstab"};}
	elsif($fanid){
		$dateestab=&formatdate($FANDIR{"$fanid:DateEstab"});
		$dateupdated=&formatdate($FANDIR{"$fanid:DateUpdated"});
		print <<EOF if $createupdate;
<a href=$weblib::URL$fanidparam>$LANG{ReturnLabel}</a><br>
<form action=$weblib::URL method=POST enctype="multipart/form-data" name=UpdateForm>
$LANG{FanIDLabel} #$fanid <input name=FanID type=hidden value="$fanid"><br>
<blockquote>$LANG{FanIDDesc}</blockquote>
EOF
	}
	if($authfanid){
		print "<hr>\n";
		if($photo&&$DB{Photo}>=$PUBLIC_PRIV){
			substr($photo,-1,1)=" align=right>";
			print "$photo\n";
		}
	}
	if($createupdate){
		if($pubfanid){
			print "$LANG{PubFanIDLabel} #",$pubfanid+0,"/$FANDIR{HighPubFanID}<br>\n";
			print "<blockquote>$LANG{PubFanIDDesc}</blockquote>\n" unless $authfanid;
			print <<EOF;
$LANG{DateEstabLabel} - $dateestab<br>
$LANG{DateUpdatedLabel} - $dateupdated<br>
EOF
		}
	}
	else{&options unless $authfanid;}
	if(!$fanlist){
		if($authfanid){&displayUserInfo;}
		elsif($createupdate){&generateUpdateForm;}
	}
}
sub displayUserInfo{
	if($admin){
		$fanidparam="/fanid=$fanid";
		print "<a href=$weblib::URL/update$fanidparam>Update this entry</a><p>";
	}
	@report=("First Middle Last","Email","$LANG{TRTVHandleLabel}:TRTVHandle",
		"$LANG{TRCFHandleLabel}:TRCFHandle",
		"$LANG{ICQLabel}:ICQ",
		"Address","City, State Postal","Country","$LANG{FanSinceLabel}:FanSince",
		"PHCC PHAC PHNum");
	Outer:foreach $field(@report){
		($label,$items)=split(/:/,$field);
		if($items eq ""){$items=$label;$label="";}
		else{$label="<b>$label:</b> ";}
		$reportline="";
		@items=split(/\s+/,$items);
		foreach $item(@items){
			$punct="";
			$punct="," if $item=~s/,$//;
			($privacy,$value)=split(/:/,$DB{$item},2);
			($privacy,$privacylist)=split(/;/,$privacy,2);
			@privacylist=split(/;/,$privacylist);
			if($PRIVATE_PRIV < $privacy && $privacy < $PUBLIC_PRIV){
				$privacy=$PRIVATE_PRIV;
				foreach $fid(@privacylist){
					$fid=$FANDIR{sprintf("%07d",$fid)};
					$privacy=$PUBLIC_PRIV,last if $fid eq $authfanid;
				}
			}
			next Outer unless $privacy>=$PUBLIC_PRIV||$value eq "";
			if($item eq "Email"){$value="<a href=mailto:$value>$value</a>";}
			if($item eq "Country"){$value=$CC{$value};}
			if($item eq "State" && $value eq "" && $DB{Prov} ne ""){
				($privacy,$value)=split(/:/,$DB{Prov},2);
				($privacy,$privacylist)=split(/;/,$privacy,2);
			}
			$reportline.=$value;
			$reportline.=$punct if $punct&&$value;
			$reportline.=" " if $value;
		}
		print "$label $reportline<br>\n" if $reportline;
	}
	print "<br clear=all>\n";
	if($admin){
		$submit=$LANG{MessageLabel}=~/^<img/?
			"<a href='' onClick='document.Message.submit();return false;'>".
				"$LANG{MessageLabel}</a>":
			"<input type=submit value=\"$LANG{MessageLabel}\">";
		($priv,$email)=split(/:/,$FANDIR{"$authfanid:Email"});
		$emailchecked=$priv<$PUBLIC_PRIV?"":" checked";
		($priv,$name)=split(/:/,$FANDIR{"$authfanid:First"});
		$namechecked=$priv>=$PUBLIC_PRIV
			if $FANDIR{"$authfanid:First"};
		($priv,$name)=split(/:/,$FANDIR{"$authfanid:Middle"});
		$namechecked=$priv>=$PUBLIC_PRIV&&$namechecked
			if $FANDIR{"$authfanid:Middle"};
		($priv,$name)=split(/:/,$FANDIR{"$authfanid:Last"});
		$namechecked=$priv>=$PUBLIC_PRIV&&$namechecked
			if $FANDIR{"$authfanid:Last"};
		$namechecked=$namechecked?" checked":"";
		($priv,$toemail)=split(/:/,$DB{Email});
		print <<EOF if $priv>=$PUBLIC_PRIV;
<hr>
<form action=$weblib::URL method=POST name=Message>
<input type=hidden name=FanID value="$authfanid">
<input type=hidden name=PubFanID value="$param{fan}">
<textarea name=Message rows=10 cols=70 wrap=hard></textarea><br>
<input type=checkbox name=AllowEmail$emailchecked>$LANG{AllowEmailLabel}<br>
<input type=checkbox name=AllowName$namechecked>$LANG{AllowNameLabel}<br>
$submit
</form>
EOF
	}
	&options;
}
sub generateUpdateForm{
	print <<EOF;
<table>
<tr><th>$LANG{PrivateLabel}/$LANG{PublicLabel}</th>
<th>$LANG{FieldNameLabel}</th><th>$LANG{FieldEntryLabel}</th></tr>
EOF
	print &DataField($LANG{NameLabel},"First:15%%Middle:15%%Last:15");
	print &DataField($LANG{EmailLabel},"Email:50");
	print &DataField($LANG{TRTVHandleLabel},"TRTVHandle:16");
	print &DataField($LANG{TRCFHandleLabel},"TRCFHandle:24");
	print &DataField($LANG{ICQLabel},"ICQ:16");
	print &DataField($LANG{FanSinceLabel},"FanSince:+$yearselect");
	print &DataField($LANG{AddressLabel},"Address:40");
	$location="$LANG{CityLabel}<br>$LANG{StateLabel}<br>".
		"$LANG{CountryLabel}<br>$LANG{PostalLabel}";
	$locationfield=
		"City+:40%%State:+$state/%%Prov+:+$prov%%Country+:+$country%%Postal+:10";
	print &DataField($location,$locationfield);
	print &DataField($LANG{PhoneLabel},
		"PHCC:3:$LANG{PHCCLabel}%%PHAC:3:$LANG{PHACLabel}%%PHNum:10:$LANG{PHNumLabel}");
	print &DataField($LANG{PhotoLabel},"Photo:+<input type=file name=Photo>:$photo");
	foreach $field(@checkfields){
		($fieldname,$fieldtext)=split(/:/,$field,2);
	print <<EOF;
<tr>
<td colspan=3>
<input type=checkbox name=$fieldname$Checked{$fieldname} value=1> $fieldtext
</td>
</tr>
EOF
	}
	print "</table>\n";
	if($LANG{SubmitLabel}=~/^<img/){
		print "<a href='' onClick='document.UpdateForm.submit();return false;'>$LANG{SubmitLabel}</a>\n";
	}
	else{print "<input type=submit value=\"$LANG{SubmitLabel}\">\n";}
	print "</form>\n";
}
sub displayAcknowledgements{
	print <<EOF;
<div style="width: 400px; margin-left: auto; margin-right: auto; text-align: center; border: 3px black double; padding: 5px;">
<ul style="list-style-type: none; margin: 0px; margin-left: -40px;">
<li>Dutch translation by <a href="/fandb/$language/fan=8">Patrick Muijsers</a></li>
<li>Spanish translation by <a href="/fandb/$language/fan=4">Inaki Prieto</a></li>
<li>French translation by <a href="/fandb/$language/fan=1">Roger Linder</a> & AltaVista</li>
<li>German translation by <a href="/fandb/$language/fan=8">Patrick Muijsers</a></li>
<li>Italian translation by <a href="/fandb/$language/fan=12">Lino Terlati</a></li>
<li>Japanese translation by <a href="/fandb/$language/fan=2">Hiroshi Kawauchi</a></li>
<li>Portuguese translation by <a href="/fandb/$language/fan=80">Rog&eacute;rio Ferraz</a></li>
<li>Swedish translation by <a href="/fandb/$language/fan=7">Stefan L. Pettersson</a></li>
</ul>
<p>
More to come!
</p><p style="text-align: left; margin-bottom: 0px;">
If you are interested in providing a translation from English to your native language,
please contact <a href=mailto:roger\@TRConnection.com>Roger Linder</a>
</p></div>
<a href=/trc/otrn>The Occasional TR News archive</a>
EOF
}
sub doUpdate{
	if($in{Message}){
		$tofanid=$FANDIR{sprintf("%07d",$in{PubFanID})};
		$tomail=$FANDIR{"$tofanid:Email"};
		($priv,$tomail)=split(/:/,$tomail);
		$frommail=$FANDIR{"$fanid:Email"};
		($priv,$frommail)=split(/:/,$frommail);
		$pfanid=sprintf("%d",$pubfanid);
		$name="";
		if($in{AllowName}){
			($priv,$name1)=split(/:/,$FANDIR{"$fanid:First"});
			($priv,$name2)=split(/:/,$FANDIR{"$fanid:Middle"});
			($priv,$name3)=split(/:/,$FANDIR{"$fanid:Last"});
			$name=$name1;
			$name.=" $name2" if $name2!~/^\s*$/;
			$name.=" $name3" if $name3;
		}
		if($tomail&&$frommail){
			open(MAIL,"|$SENDMAIL -t");
			$returnaddress="";
			$returnaddress.=qq{"$name" } if $in{AllowName};
			$returnaddress.=" <$frommail>" if $in{AllowEmail};
			print MAIL "From: $returnaddress\n" if $returnaddress;
			print MAIL <<EOF;
To: $tomail
Subject: A message from a Todd Rundgren Fan

This message was sent via the International Todd Rundgren Fan Database at

   http://TRConnection.com/fandb

It was sent by the fan whose entry can be found at

   http://TRConnection.com/fandb/fan=$pfanid

=========================================================================

$in{Message}
EOF
			close(MAIL);
		}
	}
	elsif($in{Last}){
		if(!defined $FANDIR{"$fanid:DateEstab"}){
			$pubfanid=$FANDIR{"$fanid:PubFanID"}=$DB{PubFanID}=
				sprintf("%07d",++$FANDIR{HighPubFanID});
			$FANDIR{"$fanid:DateEstab"}=time;
			$FANDIR{$pubfanid}=$fanid;
			$FANDIR{"\L$in{Email}"}=$fanid if $in{Email};
		}
		foreach $key(keys %in){
			if($fields{$key}&&$in{$key}){
				$privacylevel=$in{"$key%%Privacy"};
				$privacy=$in{"Privacy%%$privacylevel"};
				if($PRIVATE_PRIV < $privacy && $privacy < $PUBLIC_PRIV){
					$privacy.=";".$in{"RestrictGroup%%$privacylevel"}
						if $in{"RestrictGroup%%$privacylevel"};
				}
				$FANDIR{"$fanid:$key"}=$DB{$key}="$privacy:$in{$key}";
			}
		}
		foreach $field(@checkfields){
			($fieldname,$fieldtest)=split(/:/,$field,2);
			if($in{$fieldname}){
				$FANDIR{"$fanid:$fieldname"}=$DB{$fieldname}=$in{$fieldname};
			}
			else{$FANDIR{"$fanid:$fieldname"}=$DB{$fieldname}=0;}
		}
		$privacylevel=$in{"Photo%%Privacy"};
		$FANDIR{"$fanid:Photo"}=$DB{Photo}=$in{"Privacy%%$privacylevel"};
		if($in{Photo}){
			chdir($PHOTODIR);
			open(FILE,">$pubfanid");
			print FILE $in{Photo};
			close(FILE);
			chop($identity=`/usr/bin/identify $pubfanid`);
			($name,$geometry,$class,$size,$type)=split(/\s+/,$identity);
			system "/usr/bin/convert -geometry '160x160>' $pubfanid jpeg:$pubfanid.jpg";
			if($type eq "JPEG"){
				chop($identity=`/usr/bin/identify $pubfanid.jpg`);
				($name2,$geometry2,$class2,$size2,$type2)=split(/\s+/,$identity);
				$size=~/^(\d+)/;
				$size=$1;
				$size2=~/^(\d+)/;
				$size2=$1;
				if($size<$size2){rename($pubfanid,"$pubfanid.jpg");}
			}
			unlink($pubfanid);
		}
		$FANDIR{"$fanid:DateUpdated"}=time;
	}
}
sub setstateprov{
	local($privacy,$dbvalue)=split(/:/,$DB{State});
	local($privacylist);
	($privacy,$privacylist)=split(/;/,$privacy,2);
	%states=(AK,Alaska,AL,Alabama,AR,Arkansas,AZ,Arizona,CA,California,CO,Colorado,
		CT,Connecticut,DC,"District of Columbia",DE,Delaware,FL,Florida,GA,Georgia,
		HI,Hawaii,IA,Iowa,ID,Idaho,IL,Illinois,IN,Indiana,KS,Kansas,KY,Kentucky,
		LA,Louisiana,MA,Massachusetts,MD,Maryland,ME,Maine,MI,Michigan,MN,Minnesota,
		MO,Missouri,MS,Mississippi,MT,Montana,NC,"North Carolina",ND,"North Dakota",
		"NE",Nebraska,NH,"New Hampshire",NJ,"New Jersey",NM,"New Mexico",NV,Nevada,
		NY,"New York",OH,Ohio,OK,Oklahoma,OR,Oregon,PA,Pennsylvania,RI,"Rhode Island",
		SC,"South Carolina",SD,"South Dakota",TN,Tennessee,TX,Texas,UT,Utah,
		VA,Virginia,VT,Vermont,WA,Washington,WI,Wisconsin,WV,"West Virginia",
		WY,Wyoming);
	$state=qq{<SELECT NAME="State">\n<OPTION VALUE=""> State};
	foreach $st(sort {$states{$a} cmp $states{$b}} keys %states){
		$selected=$st eq $dbvalue?" selected":"";
		$state.=qq{<OPTION VALUE="$st"$selected>$states{$st}\n};
	}
	$state.="</SELECT>\n";
	($privacy,$dbvalue)=split(/:/,$DB{Prov});
	($privacy,$privacylist)=split(/;/,$privacy,2);
	$prov=qq{<SELECT NAME="Prov">\n<OPTION value="">Province\n};
	%Provinces=(AB,Alberta,BC,"British Columbia",MB,Manitoba,NB,"New Brunswick",
		NF,Newfoundland,NS,"Nova Scotia",NWT,"NW Territories",ON,Ontario,
		PEI,"Pr. Edward Isl.",QC,Quebec,SK,Saskatchewan,YT,Yukon);
	foreach $province(sort {$Provinces{$a} cmp $Provinces{$b}} keys %Provinces){
		$selected=$province eq $dbvalue?" selected":"";
		$prov.=qq{<OPTION VALUE="$province"$selected>$Provinces{$province}\n};
	}
	$prov.="</SELECT>\n";
	($privacy,$dbvalue)=split(/:/,$DB{Country});
	($privacy,$privacylist)=split(/;/,$privacy,2);
	$defaultcountry="";
	@addr=split(/\./,$ENV{REMOTE_HOST});
	$defaultcountry=pop(@addr);
	$defaultcountry=$dbvalue if $dbvalue;
	if(length($defaultcountry) > 2){$defaultcountry="us";}
	if($defaultcountry =~/^\d+$/){$defaultcountry="";}
	$country="<select name=Country>\n";
	$country.='<option value="">'."\n";
	foreach $cc(sort {$CC{$a} cmp $CC{$b}} keys %CC){
		$selected=$cc eq "\L$defaultcountry"?" selected":"";
		$country.="<option value=$cc$selected>$CC{$cc}\n";
	}
	$country.="</select>\n";
}
sub yearselect{
	local($privacy,$dbvalue)=split(/:/,$DB{FanSince});
	local($privacylist);
	($privacy,$privacylist)=split(/;/,$privacy,2);
	@time=localtime(time);
	$yearselect=qq{<select name=FanSince>\n<option value="">\n};
	for($year=1965;$year<=$time[5]+1900;$year++){
		$selected=$dbvalue == $year?" selected":"";
		$yearselect.="<option$selected>$year\n";
	}
	$yearselect.="</select>\n";
}
sub DataField{
	local($return);
	local($label,$fields)=@_;
	local(@fields)=split(/%%/,$fields);
	local($field,$return,$returnprefix,$size);
	local($privacy,$oldprivacy,$dbvalue,$checked);
	$oldprivacy=$PUBLIC_PRIV;
	$privacylevel++;
	$return="<th align=right>$label</th>\n<td>";
	foreach $field(@fields){
		($field,$size,$label)=split(/:/,$field);
		$breakline=($field=~s/\+$//);
		$return .="$label " if $label;
		($privacy,$dbvalue)=split(/:/,$DB{$field});
		($privacy,$privacylist)=split(/;/,$privacy,2);
		$privacy=$oldprivacy if $privacy>$oldprivacy;	# Make most restrictive in group
		if($size=~s/^\+//){$return.=$size;} # Not size - Preformatted form element
		else{
			$size=" size=$size" if $size;
			$return.=qq{<input name=$field value="$dbvalue"$size>\n};
		}
		$return.="<br>" if $breakline;
		$return.=
			qq{<input type=hidden name="$field%%Privacy" value="$privacylevel">\n};
	}
	$return.="</td></tr>\n";
	$returnprefix="<tr><th>\n";
	$privacy=$PRIVATE_PRIV unless $privacy;
	foreach $level($PRIVATE_PRIV,$PUBLIC_PRIV){
		$checked=$level==$privacy?" checked":"";
		$returnprefix.=
			"<input type=radio name=Privacy%%$privacylevel value=$level$checked>\n";
	}
	$restrictGroup="<input size=15 name=RestrictGroup%%$privacylevel value=\"$privacylist\">\n";
	$returnprefix.="</th>\n";
	return "$returnprefix$return";
}
sub formatdate{
	if($_[0] == 0){return "Never";}
	return POSIX::strftime("%c",localtime($_[0]));
}
sub checksum{
	local(@digits);
	local($sum,$power,$digit);
	@digits=split('',shift);
	foreach $digit(@digits){
	  $power++;
	  $sum+=$digit*2**$power+$digit;
	}
	return (($sum+62)%100);
}
sub options{
	print qq{<hr><div class="options">\n};
	print "<a href=$weblib::URL/fanlist$fanidparam>$LANG{FanlistLabel}</a><br>\n"
		if $FANDIR{"$fanid:DateEstab"};
	if($fanid){
		print "<a href=$weblib::URL/update$fanidparam>$LANG{UpdateLabel}</a>";
	}
	else{
		print "<form action=$weblib::URL method=POST>";
		print "$LANG{NewVisitorMessage}<br>\n";
		print "<a href=$weblib::URL/update$fanidparam>$LANG{CreateLabel}</a>";
		print "<p>$LANG{InDBMessage}<br>\n";
		print " <input type=submit value=\"$LANG{FanIDLabel}\"> <input name=FanID></form>";
if(0){ # Disabled 11/24/2020 due to misuse by spammers
		print $LANG{AlreadyMessage};
		print "<form action=$weblib::URL method=POST>\n";
		print "<input type=submit value=\"$LANG{EmailLabel}\"> <input name=Email size=40>\n";
		print "</form>\n";
}
	}
	print "</div>\n";
}
sub SendID{
	local($email)=shift;
	local($fanid)=$FANDIR{"\L$email"};
	$sessiondate=strftime("%A %B %d, %Y %X GMT",gmtime(time));
	open(MAIL,"|$SENDMAIL -t");
	print MAIL "To: $email\n";
	print MAIL "From: \"The International Todd Rundgren Database\" <fandb\@TRConnection.com>\n";
$intro=<<EOF;
This message was generated from a session with the International Todd
Rundgren Fan Database on

    $sessiondate
    from host computer $ENV{REMOTE_HOST}
    for address $email

It is an automated response to an inquiry to determine if you are
currently registered in the database. 
EOF
	if($fanid){
		print MAIL <<EOF;
Subject: You are in the database!

$intro
Thank you for you inquiry regarding your International Todd Rundgren Fan
Data Base status. From the e-mail address that was provided from your
session I have determined that your Private Fan ID number is $fanid.
Please use it to enter your update page.

For convenience, you may use this link to return to the database:

    $weblib::URL/fanid=$fanid

EOF
	}
	else{
		print MAIL <<EOF;
Subject: You are not in the database

$intro
The current database indicates that you are not registered with the
E-mail address of "$email."

If you believe you are registered, please drop a line to the data base
administrator, Roger Linder, at

    roger\@TRConnection.com.

If you are not registered, you may do so by visiting 

     $weblib::URL

EOF
	}
	close(MAIL);
	$error=$LANG{RegSentMessage};
	$error="Your registration status has been sent via E-mail";
}
__END__
<h1>Frequently Asked Questions</h1>
Well, no one has actually asked these questions, but I've answered them anyway.

<h2>What are the benefits of registering?</h2>
1) Your name emblazoned on a web page (or not)<br>
2) Your e-mail address and other pertinent information provided to other fans
(or not) <br>
3) Your picture available for all to see (or not) <br>
4) Your ability to join informational mailing lists (or not) <br>
5) Many other capabilities to be developed (or not)

<h2>So what the heck is this (or not) business?</h2>

The database is capable of collecting as much personal information (such as
name, e-mail, home address, phone number, photo) as you care to divulge, but
you are in complete control of who has access to it online, via restriction
options. You can choose to make any portion private (including the fact that
you are even in the database). And you can leave it out altogether if you
don't want it known anywhere.

<h2>Why is it called an "International" data base?</h2>

Since Todd fans are found all over the world, the database is available in
multiple languages, thanks in no small part to the efforts of several
multi-lingual fans. I encourage anyone who can provide additional translations
to let me know.

<h2>So what's the point?</h2>

I get a number of inquiries though the <a href=http://TRConnection.com/>TR Connection</a>
for locating fans world wide, and see similar inquiries on the <a
href=news:alt.music.todd-rundgren>alt.music.todd-rundgren newsgroup</a>, and
the <a href=/todd/join-awizard.pl>awizard mailing list</a>. This database
attempts to meet that need by allowing fans to voluntarily list information
about themselves, thus promoting contact among fans.

<h2>How is my data kept secure?</h2>

Access to your data is controlled by a private ID number that is given to you
when you register, and saved as a cookie in your browser (optional). Your data
is released to other database members by options you control at the time of
entering it, or which can be changed at any later time.

<h2>So nobody can see my data, that's great, but what about you?</h2>

Yes, it's true, I will be able to see all of the data you enter into the
database. If there is something that you don't want me to see, don't enter it.
It's as simple as that.

<h2>Now that you've got my personal information, what're you going to do with
it?</h2>

If you've asked for TR news to be sent to you, you'll get an occasional
e-mail. In fact, this page may also be an alternative page for
subscribing/unsubscribing  to the awizard list (not activated yet, though).
You have the opportunity to respond to these questions:
<ul>
<li>I would like to receive occasional TR-related news items via E-mail
<li>I would like to subscribe to the ToddFest/West information mailing list
<li>I was a subscriber to Utopia Times
<li>I am or was a subscriber to Black & White
<li>I am or was a subscriber to Rock Love
<li>I am or was a subscriber to One World (Japan)
<li>I am or was a subscriber to Special Interest (Italy)
</ul>
The last ones are just informational. I don't even do anything with them right
now, other that have them in the database. They may eventually show up in the
fan profile, but only if you elect it.

<h2>But I don't trust you with my data, aren't you a known control freak?</h2>

Rumors of my freakiness are highly exaggerated. No one is obligated to
register with the data base. Or if you do register, you can provide only
minimal information such as name and E-mail address. I've probably got those
anyway.

<h2>So am I going to get a bunch of spam 'cause I make my address available on
the Net?</h2>

You must be registered in the database in order to view the database, so
automatic address grabbing programs are not going to get the data. A member
could theoretically stop by each entry and save a copy of your address for
developing their own list. If this concerns you, don't make your address
public.

I plan on making another capability that will allow you to exchange messages
with other members without actually requiring an E-mail address (via a web
form). Not there yet, though.

I also plan on migrating some of my TR Connection features (favorite album
cuts, etc.) over to the DB as time goes by.

So, there you go. Take a look if you have an interest, (or not). The choice is
always yours.
