/* PassForm v1.0 (C) 2000 Matt Arriola
   All Rights Reserved

   Usage: <applet code=passform.class width=180 height=80>
          <param name=target1 value="user 1's web site">
          <param name=target1 value="user 2's web site">
          ...
          </applet>
   The users[] and password[] arrays should have entries for each username and
   password.

   Legal disclaimer: I am not to be held responsible for any damages this
   applet may cause, be it due to disuse, inability to use this applet or
   incompatibilities with this applet. And besides, I'm only 13. Would it be
   worth it to sue me for only 21 dollars?

*/

import java.applet.*;
import java.awt.*;
import java.net.URL;
public class passform extends Applet {
  // Declare our controls
  private Label lbl1,lbl2;
  private TextField ed1,ed2;
  private Button bt1;
  // Declare an array of URLs
  URL[] webpage;
  /* A status array. If an index is true, then the URL in the corresponding
     index is not null */
  boolean[] cando={false};

  // User-definable parameters here. Both arrays should be of equal length
  String[] users={"user1"};   // An array of user names
  String[] psws={"password"}; // An array of passwords

  public void paint(Graphics g) {
    // Show copyright notice
    getAppletContext().showStatus("PassForm v1.0 © 2000 Matt Arriola");
  }
  public void init() {
    String thepage;
    // Create URL array of the same length as the list of users
    webpage=new URL[users.length];
    for (int i=1;i<=users.length;i++) {
      /* Get value of parameter targetX, where X is a number. For example:
         <PARAM NAME=target1 NAME=mysite.com>
         would define "mysite.com" as the target for user #1 */
      thepage=getParameter("target"+i);
      // If the param exists
      if (thepage != null) {
        // Set status variable for this URL to true
        cando[i-1]=true;
        // Set target URL
        webpage[i-1]=new URL(thepage);
      }
    }
    // Create labels
    lbl1=new Label("User name:");
    lbl2=new Label("Password:");
    // Create user name and password fields
    ed1=new TextField(10);
    ed2=new TextField(10);
    // Create button
    bt1=new Button("&OK");
    // Add "User name:" then the user name field
    add(lbl1); add(ed1);
    // Add "Password:" then the password field
    add(lbl2); add(ed2);
    // Add OK button
    add(bt1);
    /* Set echo character of the password field to '*'(the echo character is
       the character that is put in the text field when a key is pressed */
    ed2.setEchoCharacter('*');
  }
  public void passformError(String errtext) {
    // Set status bar
    getAppletContext().showStatus(errtext);
  }
  public boolean action (Event evt, Object what) {
    String usr,psw;
    int indx=-1;
    // OK button was pressed
    if (evt.target==bt1) {
      // Get entered user name and password
      usr=ed1.getText(); psw=ed2.getText();
      for (int i=0;i<=users.length-1;i++) {
        // If users[i] is equal to the entered user name
        if (usr.equals(users[i])) {
          // This user exists. Exit loop and set index variable
          indx=i; break;
        }
      }
      if (indx==-1) {
        // User does not exist
        passformError("User name \""+usr+"\" not recognized"); return false;
      } else if (!psw.equals(psws[indx])) { // Compare password
        // Bad password
        passformError("Bad password for user \""+usr+"\"!"); return false;
      }
      if (cando[indx]) {
        // Tell the user that the applet is going to the web site
        getAppletContext().showStatus("Going to web site...");
        // Go to the web site
        getAppletContext().showDocument(webpage[indx]);
      } else {
        /* The user name and password check out, but a target hasn't been
           defined */
        passformError("Target URL not defined for \""+usr+"\"!"); return false;
      }
    }
    return true;
  }
}
