/* Original code (c)2002 Paul Kershaw. All rights reserved. Code 
   is offered in the spirit of open source, and should not be modified or resold 
   for commercial purposes without the explicit prior consent of the author.
   Contact the author via brighn@yahoo.com */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class numerology extends Applet 
implements TextListener {

    TextArea txtWords = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
    TextArea txtValue = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
    Label lblTitle = new Label("Numerology Counter");
    Label lblWord = new Label("Per word: ");
    TextField txtWord = new TextField(50);
    Label lblTotal = new Label("Total: ");
    TextField txtTotal = new TextField(10);
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints gridcons = new GridBagConstraints();

    public numerology() {

        setLayout(gridbag);
        buildConstraints(gridcons, 0, 0, 4, 1, 100, 5);
	gridcons.fill = GridBagConstraints.NONE;
        gridcons.anchor = GridBagConstraints.NORTHWEST;
        gridbag.setConstraints(lblTitle, gridcons);
        add(lblTitle);
        buildConstraints(gridcons, 0, 1, 4, 1, 100, 10);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(txtWords, gridcons);
        add(txtWords);
        buildConstraints(gridcons, 0, 2, 4, 1, 100, 100);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(txtValue, gridcons);
        add(txtValue);
        buildConstraints(gridcons, 0, 3, 1, 1, 10, 5);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.EAST;
        gridbag.setConstraints(lblWord, gridcons);
        add(lblWord);
        buildConstraints(gridcons, 1, 3, 1, 1, 70, 5);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(txtWord, gridcons);
        add(txtWord);
        buildConstraints(gridcons, 2, 3, 1, 1, 10, 5);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.EAST;
        gridbag.setConstraints(lblTotal, gridcons);
        add(lblTotal);
        buildConstraints(gridcons, 3, 3, 1, 1, 10, 5);
	gridcons.fill = GridBagConstraints.BOTH;
        gridcons.anchor = GridBagConstraints.CENTER;
        gridbag.setConstraints(txtTotal, gridcons);
        add(txtTotal);
	txtWords.addTextListener(this); 
    }
    
    public void textValueChanged(TextEvent evt) {
        int asc;
        String sTextString;
        boolean bInWord = false;
        int I;
        char sInQuest;
        int CurrWord = 0;
        int CurrTotal = 0;
        String sWordCount = "";
        String sLetterCount = "";

        sTextString = txtWords.getText();
        for (I = 1; I <= sTextString.length(); I++) {
            sInQuest = sTextString.charAt(I - 1);
            asc = (int)sInQuest;
            asc = NumShift(asc);
            if (IsAlpha(sInQuest)) {
                if (!bInWord) {
                    bInWord = true;
                    if (sWordCount.length() > 0) {
                        sWordCount += ", ";
                    }
                    if (CurrWord > 0) {
                        sWordCount += NumVal(CurrWord);
                    }
                    CurrWord = 0;
                }
                if (sLetterCount.length() > 0) {
                    sLetterCount += ", ";
                }
                sLetterCount += NumVal(asc);
                CurrWord += asc;
                CurrTotal += asc;
                }
            else {
                bInWord = false;
            }
            CurrTotal = NumVal(CurrTotal);
        }
        if (sWordCount.length() > 0) {
            sWordCount += ", ";
        }
        sWordCount += NumVal(CurrWord);
        txtValue.setText(sLetterCount);            
        txtWord.setText(sWordCount);            
        txtTotal.setText(String.valueOf(CurrTotal));            
        repaint();
    }

    void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw,
    int gh, int wx, int wy) {
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;
    }

    public boolean IsAlpha(char InputString) {
        int ascii;
        ascii = (int)InputString;
        if ((ascii >= 65 & ascii <= 90) || (ascii >= 97 & ascii <= 122)
        || (ascii >= 48 & ascii <= 57))
            return true;
        else  
            return false;
    }
    
    public int NumShift(int InputInt) {
        int calc = InputInt;
        if (calc < 58) {
            calc = calc - 48;
        } else if (calc < 95) {
            calc = calc - 64;
        } else {
            calc = calc - 96;
        }
        return calc;
    }
            
    public int NumVal(int InputInt) {
        int calc = InputInt;
        int poplast;
        while (calc > 9) {
            poplast = calc % 10;
            calc = (calc - poplast)/10 + poplast;
        }
        return calc;
    }

}
