#!/usr/local/bin/perl
#Allows user to lookup name in a database
push @INC, ("/home/scsfac/downeyt/scripts");
require 'parse-lib.pl';
require 'pictutil.pl';
%query=&get_query;

print "Content-type: text/html\n\n";

&start_html;

&open_databases('writable');

if (!$query{'reset'}) {
    $username=$query{'username'};
    $password=$query{'password'};
    $interest=$query{'interest'};
    $new_interest=$query{'new_interest'};
}

if ($query{'update_password'})
{
    &update_password($username,$password);
}

if ($query{'update_interest'})
{
    &change_field($username,'interest',$new_interest);
    $interest=$new_interest;
}


if ($username)
{
    if (&in_file($username))
    {
	$filepass=&lookup_field($username,'password');
	if ($filepass eq $password)
	{
	    if ($query{'change_password'} eq "Change Password")
	    {
		&change_password($username,$password);
	    } elsif ($query{'change_interest'} eq "Change Interest") {
		&change_interest($username,$password);
	    } else {
		&registered_user($username,$password,"Here is your information");
	    }
	} elsif (!$query{'update_password'}) {
	    &wrong_password($username,"You typed the wrong password");
	}
    } elsif ($query{'add'} ne "Add Me") {
	&wrong_password($username,"You are not a registered user");
    } elsif ($query{'password'} ne $query{'confirm'}) {
	&wrong_password($username,"You typed two different passwords");
    } else {
	&index($username,$password,"");
	&registered_user($username,$password,"You are now entered in the data base");
    }
} else {
    &first_time;
}

sub start_html {
    print <<END;
<html>
<head>
<title>Add info and update a data base</title>
</head>
<body>
END
    ;
}

sub first_time {
    print <<END;
<form method=post>
<p>Username <input type="text" name="username" value="$username">
<p>Password <input type="password" name="password" value=$password>
<p><input type="submit" value="Submit" name="submit">
</body>
</html>
END
    ;
}


sub wrong_password {
    my($username,$title)=@_;
    print <<END;
<h3>$title</h3>
Retype your
password and press 'Submit', or enter a password, confirm it and press 'Add Me'
<form method=get>
<table>
<tr>
<td>Username <input type="text" name="username" value="$username"><td>
<td></td>
</tr>
<tr>
<td>Password <input type="password" name="password" value=$password><td>
<td><input type="submit" value="Submit" name="submit"><td>
</tr>
<tr>
<td>Retype Password <input type="password" name="confirm"><td>
<td><input type="submit" value="Add Me" name="add"><td>
</tr>
</table>
</body>
</html>
END
    ;
}

sub registered_user {
    my($username,$password,$title) = @_;
    my($interest);
    $interest = &lookup_field($username,'interest');
    print <<END;
<h3>$title</h3>
<form method=get>
<input type="hidden" name="username" value="$username">
<input type="hidden" name="password" value="$password">
<input type="hidden" name="interest" value="$interest">
<table>
<tr>
<td>Username</td>
<td>$username</td>
<td><input type="submit" name="change_password" value="Change Password"></td>
</tr>
<tr>
<td>Interest</td>
<td>$interest</td>
<td><input type="submit" value="Change Interest" name="change_interest"></td>
</tr>
</table>
<p><input type="submit" name="reset" value="Log on as new user">
</body>
</html>
END
    ;
}

sub change_password {
    my($username,$password) = @_;
    print <<END;
<h3>Update Password</h3>
Type your new
password, confirm it and press 'Update Password'
<form method=get>
<input type="hidden" name="username" value="$username">
<table>
<tr>
<td>Username</td><td>$username</td>
</tr>
<tr>
<td>New Password</td><td><input type="password" name="password"></td>
</tr>
<tr>
<td>Retype New Password</td><td><input type="password" name="confirm"></td>
</tr>
</table>
<input type="submit" value="Update Password" name="update_password">
<p><input type="submit" name="reset" value="Log on as new user">
</body>
</html>
END
    ;
}

sub change_interest {
    my($username,$password) = @_;
    print <<END;
<h3>Update Interest</h3>
Type your new
interest and press 'Update Interest'
<form method=get>
<input type="hidden" name="username" value="$username">
<input type="hidden" name="password" value=$password>
<table>
<tr><td>Username</td><td>$username</td>
<tr><td>Interest</td><td><textarea name="new_interest">
$interest
</textarea></td>
</table>
<input type="submit" value="Update Interest" name="update_interest">
<p><input type="submit" name="reset" value="Log on as new user">
</body>
</html>
END
    ;
}

sub update_password {
    my($username,$password)=@_;
    if ($password ne $query{'confirm'}) {
    print <<END;
<h3>Passwords Don't Match</h3>
Retype your new
password, confirm it and press 'Update Password'
<form method=get>
<table>
<tr><td>Username</td><td> <input type="text" name="username" value="$username"></td>
<tr><td>New Password</td><td> <input type="password" name="password"></td>
<tr><td>Retype New Password</td><td> <input type="password" name="confirm"></td>
</table>
<input type="submit" value="Update Password" name="update_password"></td>
<p><input type="submit" name="reset" value="Log on as new user">
</body>
</html>
END
    ;
} else {
&change_field($username,'password',$password);
}

}


