use XML::Simple;
use Data::Dumper;

($sec, $min, $hout, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();

my $inputFile = "techniques.xml";

my $locationFile = "TechniquesByLocation.html";
my $typeFile = "TechniquesByType.html";
my $ingredientFile = "TechniquesByIngredient.html";
my $itemFile = "TechniquesByItem.html";

%files = (
    "Location", $locationFile,
    "Type", $typeFile,
    "Ingredient", $ingredientFile,
    "Item", $itemFile
);


my $xs1 = XML::Simple->new();
my $doc = $xs1->XMLin($inputFile, ForceArray => ['ingredient', 'on']);

foreach $name (keys(%files)) {
    print "Writing Techniques by $name...";
    eval("&doBy" . $name . "File");
    print "done\n";
}


sub doByLocationFile {
    #print "\nBEGIN : doByLocationFile\n";

    if (!open(BY_LOCATION, ">" . $locationFile)) {
        print "Unable to open file '$locationFile' for writing.\n";
        exit 1;
    }

    # Get a list of technique locations
    %locationList = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        my $locationType = $doc->{technique}->{$technique}->{source}->{type};
        my $location  = $doc->{technique}->{$technique}->{source}->{place};
        if ($locationType eq "quartermaster") {
            $locationList{$location} = $doc->{technique}->{$technique}->{source}->{person};
        }
    }
    my @locations = sort (keys(%locationList));

    # Print HTML header
    print BY_LOCATION "<html>\n";
    print BY_LOCATION "<head>\n";
    print BY_LOCATION "\t<title>Techniques by Location</title>\n";
    print BY_LOCATION "\t<link href=\"techniques.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print BY_LOCATION "\t<script src=\"techniques.js\"></script>\n";
    print BY_LOCATION "</head>\n";
    print BY_LOCATION "<body>\n";
    print BY_LOCATION "<span class=\"h3\">Techniques by Location</span>\n";
    &seeAlso(BY_LOCATION, "Location");

    print BY_LOCATION "<table class=\"wrapper\">\n";

    # For each location
    foreach my $location (@locations) {
        &doLocation($location, $locationList{$location});
    }
    &doLocationOther(@locations);

    print BY_LOCATION "</table>";

    &doFooter(BY_LOCATION);

    close(BY_LOCATION);
} # END doByLocationFile

sub doLocation {
    local($location, $person) = @_;

    print BY_LOCATION "<tr>\n";
    print BY_LOCATION "<td class=\"location\">$location ($person)</td>\n";
    print BY_LOCATION "</tr>\n";

    # Get a list of technique types for this location
    %typeList = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        if ($doc->{technique}->{$technique}->{source}->{place} eq $location) {
            $typeList{$doc->{technique}->{$technique}->{type}} = 1;
        }
    }

    print BY_LOCATION "<tr>\n";
    print BY_LOCATION "<td class=\"data\">\n";

    # Technique types
    foreach my $type (keys (%typeList)) {
        &doLocationType($location, $type);
    }
    print BY_LOCATION "</td>\n";
    print BY_LOCATION "</tr>\n";
} # END doLocation

sub doLocationOther {
    local(@locations) = @_;

    print BY_LOCATION "<tr>\n";
    print BY_LOCATION "<td class=\"location\">Other</td>\n";
    print BY_LOCATION "</tr>\n";

    # Get a list of technique types for this location
    %typeList = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        if (!&member($doc->{technique}->{$technique}->{source}->{place}, @locations)) {
            $typeList{$doc->{technique}->{$technique}->{type}} = 1;
        }
    }

    print BY_LOCATION "<tr>\n";
    print BY_LOCATION "<td class=\"data\">\n";

    # Technique types
    foreach my $type (keys (%typeList)) {
        &doLocationType($location, $type);

    }
    print BY_LOCATION "</td>\n";
    print BY_LOCATION "</tr>\n";
} # END doLocation

sub member {
    local($location, @locations) = @_;
    foreach $loc (@locations) {
        if ($location eq $loc) {
            return 1;
        }
    }
    return 0;
} # END member

sub doLocationType {
    local($location, $type) = @_;

    # Header with expand/collapse control
    print BY_LOCATION "<table class=\"tech\">";
    print BY_LOCATION "<tr>";
    print BY_LOCATION "<td class=\"header\">";
    print BY_LOCATION "<a href=\"#\" onclick=\"toggleShow('$location|$type');return false\">";
    print BY_LOCATION "<img id=\"" . $location . "|" . $type . "Img\" src=\"plus.jpg\">";
    print BY_LOCATION "</a>";
    print BY_LOCATION "&nbsp;$type Techniques";
    print BY_LOCATION "</td>";
    print BY_LOCATION "</tr>";
    print BY_LOCATION "</table>\n";

    # Header2
    print BY_LOCATION "<table id=\"$location|$type\" class=\"tech collapsed\">\n";
    print BY_LOCATION "<tr>\n";
    print BY_LOCATION "<td class=\"header2\">Type</td>\n";
    print BY_LOCATION "<td class=\"header2\">Technique</td>\n";
    print BY_LOCATION "<td class=\"header2\">Effect</td>\n";
    print BY_LOCATION "<td class=\"header2\">I</td>\n";
    print BY_LOCATION "<td class=\"header2\">II</td>\n";
    print BY_LOCATION "<td class=\"header2\">III</td>\n";
    print BY_LOCATION "</tr>\n";

    # Get a list of techniques for this location and type
    %techList = ();
    $color = 0;
    foreach my $technique (sort (keys (%{$doc->{technique}}))) {
        if (($doc->{technique}->{$technique}->{source}->{place} eq $location)
            && ($doc->{technique}->{$technique}->{type} eq $type)) {

            # Print one technique
            if ($color) {
                print BY_LOCATION "<tr class=\"even\">\n";
            } else {
                print BY_LOCATION "<tr class=\"odd\">\n";
            }
            $color = !$color;

            print BY_LOCATION "<td class=\"type\">$type</td>\n";
            print BY_LOCATION "<td class=\"name\"><a href=\"$typeFile#$type|$technique\">$technique</a></td>\n";
            print BY_LOCATION "<td class=\"effect\">" . $doc->{technique}->{$technique}->{effect} . "</td>\n";

            if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
                # Technique has tier ALL
                print BY_LOCATION "<td class=\"I\" colspan=\"3\">" . $doc->{technique}->{$technique}->{tier}->{$_}->{result} . "</td>\n";
            } else {
                foreach ('I', 'II', 'III') {
                    print BY_LOCATION "<td class=\"$_\">" . $doc->{technique}->{$technique}->{tier}->{$_}->{result} . "</td>\n";
                }
            }
            print BY_LOCATION "</tr>\n";
        }
    }
    print BY_LOCATION "</table>\n";
} # END doLocationType

sub doByTypeFile {
    if (!open(BY_TYPE, ">" . $typeFile)) {
        print "Unable to open file '$typeFile' for writing.\n";
        exit 1;
    }

    # Print HTML header
    print BY_TYPE "<html>\n";
    print BY_TYPE "<head>\n";
    print BY_TYPE "\t<title>Techniques by Type</title>\n";
    print BY_TYPE "\t<link href=\"techniques.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print BY_TYPE "\t<script src=\"techniques.js\"></script>\n";
    print BY_TYPE "</head>\n";
    print BY_TYPE "<body>\n";
    print BY_TYPE "<span class=\"h3\">Techniques by Type</span>\n";
    &seeAlso(BY_TYPE, "Type");

    print BY_TYPE "<table class=\"wrapper\">\n";

    # Get a list of technique types
    %types = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        $types{$doc->{technique}->{$technique}->{type}} = 1;
    }

    # For each technique type
    foreach my $type (sort (keys %types)) {
        &doType($type);
    }

    print BY_TYPE "</table>";
    print BY_TYPE "\t<script>doAnchor();</script>\n";

    &doFooter(BY_TYPE);

    close(BY_TYPE);
} # END doByTypeFile


sub doType {
    local($type) = @_;

    print BY_TYPE "<tr>\n";
    print BY_TYPE "<td class=\"data\">\n";

    # Header with expand/collapse control
    print BY_TYPE "<table class=\"tech\">";
    print BY_TYPE "<tr>";
    print BY_TYPE "<td class=\"header\">";
    print BY_TYPE "<a href=\"#\" onclick=\"toggleShow('$type');return false\">";
    print BY_TYPE "<img id=\"" . $type . "Img\" src=\"plus.jpg\">";
    print BY_TYPE "</a>";
    print BY_TYPE "&nbsp;$type Techniques";
    print BY_TYPE "</td>";
    print BY_TYPE "</tr>";
    print BY_TYPE "</table>\n";

    print BY_TYPE "<table id=\"$type\" class=\"tech collapsed\">\n";
    print BY_TYPE "<tr>\n";
    print BY_TYPE "<td class=\"header2\">Type</td>\n";
    print BY_TYPE "<td class=\"header2\">Technique</td>\n";
    print BY_TYPE "<td class=\"header2\">Effect</td>\n";
    print BY_TYPE "<td class=\"header2\">I</td>\n";
    print BY_TYPE "<td class=\"header2\">II</td>\n";
    print BY_TYPE "<td class=\"header2\">III</td>\n";
    print BY_TYPE "<td class=\"header2\">Applicable To</td>\n";
    print BY_TYPE "<td class=\"header2\">Source</td>\n";
    print BY_TYPE "</tr>\n";

    # Get a list of techniques for this type
    %techList = ();
    $color = 0;
    foreach my $technique (sort (keys (%{$doc->{technique}}))) {
        if ($doc->{technique}->{$technique}->{type} eq $type) {

            # Print one technique
            if ($color) {
                print BY_TYPE "<tr class=\"even\">\n";
            } else {
                print BY_TYPE "<tr class=\"odd\">\n";
            }
            $color = !$color;

            print BY_TYPE "<td class=\"type\"><a name=\"$type|$technique\"></a>$type</td>\n";
            print BY_TYPE "<td class=\"name\">$technique</td>\n";
            print BY_TYPE "<td class=\"effect\">" . $doc->{technique}->{$technique}->{effect} . "</td>\n";

            # Technique doesn't have 3 tiers
            if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
                # Technique has tier ALL
                print BY_TYPE "<td class=\"I\" colspan=\"3\">\n";
                print BY_TYPE $doc->{technique}->{$technique}->{tier}->{result} . "\n";
                if ($doc->{technique}->{$technique}->{tier}->{recipie}) {
                    print BY_TYPE "<table class=\"recipie\">\n";
                    my $count = 0;
                    while ($doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]) {
                        print BY_TYPE "<tr>\n";
                        print BY_TYPE "<td class=\"ingredient" . ($count + 1) . "\">\n";
                        print BY_TYPE $doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]->{count};
                        print BY_TYPE "&nbsp;";
                        my $ingredient = $doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]->{text};
                        print BY_TYPE "<a href=\"$ingredientFile#$ingredient|\">$ingredient</a>";
                        print BY_TYPE "</td>\n";
                        print BY_TYPE "</tr>\n";
                        $count++;
                    }
                    print BY_TYPE "</table>\n";
                }
                print BY_TYPE "</td>\n";
            } else {
                # Technique has three tiers
                foreach $tier ('I', 'II', 'III') {
                    print BY_TYPE "<td class=\"$tier\">\n";
                    print BY_TYPE $doc->{technique}->{$technique}->{tier}->{$tier}->{result} . "\n";
                    if ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}) {
                        print BY_TYPE "<table class=\"recipie\">\n";
                        my $count = 0;
                        while ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]) {
                            print BY_TYPE "<tr>\n";
                            print BY_TYPE "<td class=\"ingredient" . ($count + 1) . "\">\n";
                            print BY_TYPE $doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{count};
                            print BY_TYPE "&nbsp;";
                            my $ingredient = $doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{text};
                            print BY_TYPE "<a href=\"$ingredientFile#$ingredient|\">$ingredient</a>";
                            print BY_TYPE "</td>\n";
                            print BY_TYPE "</tr>\n";
                            $count++;
                        }
                        print BY_TYPE "</table>\n";
                    }
                    print BY_TYPE "</td>\n";
                }
            }

            print BY_TYPE "<td class=\"on\">";
            my $first = 1;
            foreach $on (@{$doc->{technique}->{$technique}->{onlist}->{on}}) {
                if ($first) {
                    $first = 0;
                } else {
                    print BY_TYPE ", ";
                }
                print BY_TYPE $on->{content};
            }
            print BY_TYPE "</td>\n";

            print BY_TYPE "<td class=\"source\"><a href=\"$locationFile\">";
            if (($doc->{technique}->{$technique}->{source}->{person}) && ($doc->{technique}->{$technique}->{source}->{place})) {
                print BY_TYPE $doc->{technique}->{$technique}->{source}->{person} . " @ " . $doc->{technique}->{$technique}->{source}->{place};
            } else {
                print BY_TYPE $doc->{technique}->{$technique}->{source}->{person};
                print BY_TYPE $doc->{technique}->{$technique}->{source}->{place};
            }
            print BY_TYPE "</a></td>\n";
            print BY_TYPE "</tr>\n";
        }
    }
    print BY_TYPE "</table>\n";
    print BY_TYPE "</td>\n";
    print BY_TYPE "</tr>\n";
} # END doType

sub doFooter {
    local($file) = @_;

    print $file "<hr>";
    print $file "<div align=\"center\">Last updated : " . ($year+1900) . "/" . ($mon+1) . "/" . $mday . "</div>";
    print $file "From posts on <a href=\"http://vnboards.ign.com/\">VN Boards</a> and <a href=\"http://www.tazoon.com\">Tazoon</a>.\n";
    print $file "<br>";
    print $file "Comments and corrections <a href=\"http://vnboards.ign.com/message.asp?topic=61681469\">here</a>.\n";

    print $file "<div align=\"right\">\n";

    print $file "<!-- Start of StatCounter Code -->\n";
    print $file "<script type=\"text/javascript\" language=\"javascript\">\n";
    print $file "var sc_project=275976; \n";
    print $file "</script>\n";
    print $file "<script type=\"text/javascript\" language=\"javascript\" src=\"http://www.statcounter.com/counter/counter.js\"></script><noscript><a href=\"http://www.statcounter.com\" target=\"_blank\"><img src=\"http://c1.statcounter.com/counter.php?sc_project=275976&amp;java=0\" alt=\"website analysis software\" border=\"0\"></a> </noscript>\n";
    print $file "<!-- End of StatCounter Code -->\n";
    
    print $file "</div>\n";

} # END doFooter

sub doByIngredientFile {
    if (!open(BY_INGREDIENT, ">" . $ingredientFile)) {
        print "Unable to open file '$ingredientFile' for writing.\n";
        exit 1;
    }

    # Print HTML header
    print BY_INGREDIENT "<html>\n";
    print BY_INGREDIENT "<head>\n";
    print BY_INGREDIENT "\t<title>Techniques by Ingredient</title>\n";
    print BY_INGREDIENT "\t<link href=\"techniques.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print BY_INGREDIENT "\t<script src=\"techniques.js\"></script>\n";
    print BY_INGREDIENT "</head>\n";
    print BY_INGREDIENT "<body>\n";
    print BY_INGREDIENT "<span class=\"h3\">Techniques by Ingredient</span>\n";
    &seeAlso(BY_INGREDIENT, "Ingredient");

    print BY_INGREDIENT "<table class=\"wrapper\">\n";

    # Get a list of technique ingredients
    %ingredients = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        # Technique doesn't have 3 tiers
        if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
            if ($doc->{technique}->{$technique}->{tier}->{recipie}) {
                my $count = 0;
                while ($doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]) {
                    #print $doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]->{text} . "\n";
                    $ingredients{$doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]->{text}} = 1;
                    $count++;
                }
            }
        } else {
            # Technique has three tiers
            foreach $tier ('I', 'II', 'III') {
                my $count = 0;
                while ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]) {
                    #print $doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{text} . "\n";
                    $ingredients{$doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{text}} = 1;
                    $count++;
                }
            }
        }
    }

    # For each technique ingredient
    foreach my $ingredient (sort (keys %ingredients)) {
        #print "\n" . $ingredient . " (" . %ingredients->{$ingredient} . ") ";
        &doIngredient($ingredient);
    }

    print BY_INGREDIENT "</table>";
    print BY_INGREDIENT "\t<script>doAnchor();</script>\n";

    &doFooter(BY_INGREDIENT);

    close(BY_INGREDIENT);
} # END doByIngredientFile

sub doIngredient {

    local($ingredient) = @_;

    print BY_INGREDIENT "<tr>\n";
    print BY_INGREDIENT "<td class=\"data\">\n";

    # Header with expand/collapse control
    print BY_INGREDIENT "<table class=\"tech\">";
    print BY_INGREDIENT "<tr>";
    print BY_INGREDIENT "<td class=\"header\">";
    print BY_INGREDIENT "<a name=\"Ingredient|$ingredient\"></a>";
    print BY_INGREDIENT "<a href=\"#\" onclick=\"toggleShow('$ingredient');return false\">";
    print BY_INGREDIENT "<img id=\"" . $ingredient . "Img\" src=\"plus.jpg\">";
    print BY_INGREDIENT "</a>";
    print BY_INGREDIENT "&nbsp;$ingredient";
    print BY_INGREDIENT "</td>";
    print BY_INGREDIENT "</tr>";
    print BY_INGREDIENT "</table>\n";

    print BY_INGREDIENT "<table id=\"$ingredient\" class=\"tech collapsed\">\n";
    print BY_INGREDIENT "<tr>\n";
    print BY_INGREDIENT "<td class=\"header2\">Type</td>\n";
    print BY_INGREDIENT "<td class=\"header2\">Technique</td>\n";
    print BY_INGREDIENT "<td class=\"header2\">Effect</td>\n";
    print BY_INGREDIENT "<td class=\"header2\">Source</td>\n";
    print BY_INGREDIENT "</tr>\n";

    # Get a list of techniques for this ingredient
    %techList = ();
    $color = 0;
    foreach my $technique (sort (keys (%{$doc->{technique}}))) {
        if (&uses($technique, $ingredient)) {
            my $type = $doc->{technique}->{$technique}->{type};

            # Print one technique
            if ($color) {
                print BY_INGREDIENT "<tr class=\"even\">\n";
            } else {
                print BY_INGREDIENT "<tr class=\"odd\">\n";
            }
            $color = !$color;

            print BY_INGREDIENT "<td class=\"type\">$type</td>\n";
            print BY_INGREDIENT "<td class=\"name\">";
            print BY_INGREDIENT "<a href=\"$typeFile#$type|$technique\">";
            print BY_INGREDIENT "$technique " . &getTier($technique, $ingredient);
            print BY_INGREDIENT "</a>";
            print BY_INGREDIENT "</td>\n";
            print BY_INGREDIENT "<td class=\"effect\">" . $doc->{technique}->{$technique}->{effect} . "</td>\n";
            print BY_INGREDIENT "<td class=\"source\"><a href=\"$locationFile\">";
            if (($doc->{technique}->{$technique}->{source}->{person}) && ($doc->{technique}->{$technique}->{source}->{place})) {
                print BY_INGREDIENT $doc->{technique}->{$technique}->{source}->{person} . " @ " . $doc->{technique}->{$technique}->{source}->{place};
            } else {
                print BY_INGREDIENT $doc->{technique}->{$technique}->{source}->{person};
                print BY_INGREDIENT $doc->{technique}->{$technique}->{source}->{place};
            }
            print BY_INGREDIENT "</a></td>\n";
            print BY_INGREDIENT "</tr>\n";
        }
    }
    print BY_INGREDIENT "</table>\n";
    print BY_INGREDIENT "</td>\n";
    print BY_INGREDIENT "</tr>\n";
} # END doIngredient

sub getTier {
    local($technique, $ingredient) = @_;

    if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
        return "";

    } else {
        # Technique has three tiers
        foreach $tier ('I', 'II', 'III') {
            my $count = 0;
            while ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]) {
                if ($ingredient eq $doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{text}) {
                    return $tier;
                }
                $count++;
            }
        }
    }
    print "\nError, technique '$technique' does not contain ingredient '$ingredient'!";
    return "";
} # END getTier

sub uses {
    local($technique, $ingredient) = @_;

    if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
        # Technique doesn't have 3 tiers
        if ($doc->{technique}->{$technique}->{tier}->{recipie}) {
            my $count = 0;
            while ($doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]) {
                if ($doc->{technique}->{$technique}->{tier}->{recipie}->{ingredient}->[$count]->{text} eq $ingredient) {
                    return 1;
                }
                $count++;
            }
        }
    } else {
        # Technique has three tiers
        foreach $tier ('I', 'II', 'III') {
            my $count = 0;
            while ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]) {
                if ($doc->{technique}->{$technique}->{tier}->{$tier}->{recipie}->{ingredient}->[$count]->{text} eq $ingredient) {
                    return 1;
                }
                $count++;
            }
        }
    }
    return 0;
} # END uses

sub doByItemFile {
    if (!open(BY_ITEM, ">" . $itemFile)) {
        print "Unable to open file '$itemFile' for writing.\n";
        exit 1;
    }

    # Print HTML header
    print BY_ITEM "<html>\n";
    print BY_ITEM "<head>\n";
    print BY_ITEM "\t<title>Techniques by Item</title>\n";
    print BY_ITEM "\t<link href=\"techniques.css\" rel=\"stylesheet\" type=\"text/css\">\n";
    print BY_ITEM "\t<script src=\"techniques.js\"></script>\n";
    print BY_ITEM "</head>\n";
    print BY_ITEM "<body>\n";
    print BY_ITEM "<span class=\"h3\">Techniques by Item</span>\n";
    &seeAlso(BY_ITEM, "Item");

    print BY_ITEM "<table class=\"wrapper\">\n";

    # Get a list of technique application crafts
    %crafts = ();
    foreach my $technique (keys (%{$doc->{technique}})) {
        foreach $on (@{$doc->{technique}->{$technique}->{onlist}->{on}}) {
            $crafts{$on->{skill}} = ();
        }
    }

    # For each technique application craft
    foreach my $craft (sort (keys %crafts)) {
        # Get a list of technique items craftable using the craft
        %items = ();
        foreach my $technique (keys (%{$doc->{technique}})) {
            foreach $item (@{$doc->{technique}->{$technique}->{onlist}->{on}}) {
                if ($craft eq $item->{skill}) {
                    $items{$item->{content}} = 1;
                }
            }
        }

        print BY_ITEM "<tr>\n";
        print BY_ITEM "<td class=\"craft\"><a href=\"#\" onclick=\"toggleShow('$craft');return false\"><img id=\"" . $craft . "Img\" src=\"plus.jpg\"></a>&nbsp;$craft Items</td>\n";
        print BY_ITEM "</tr>\n";

        print BY_ITEM "<tbody id=\"$craft\" class=\"collapsed\">\n";
        foreach my $item (sort (keys %items)) {
            &doItem($craft, $item);
        }
        print BY_ITEM "</tbody>\n";
    }
    print BY_ITEM "<tr></tr>\n";

    print BY_ITEM "</table>";

    &doFooter(BY_ITEM);

    close(BY_ITEM);
} # END doByItemFile

sub doItem {

    local($craft, $on) = @_;

    print BY_ITEM "<tr>\n";
    print BY_ITEM "<td class=\"data\">\n";

    # Header with expand/collapse control
    print BY_ITEM "<table class=\"tech\">";
    print BY_ITEM "<tr>";
    print BY_ITEM "<td class=\"header\">";
    print BY_ITEM "<a href=\"#\" onclick=\"toggleShow('$craft|$on');return false\">";
    print BY_ITEM "<img id=\"" . $craft . "|" . $on . "Img\" src=\"plus.jpg\">";
    print BY_ITEM "</a>";
    print BY_ITEM "&nbsp;$on";
    print BY_ITEM "</td>";
    print BY_ITEM "</tr>";
    print BY_ITEM "</table>\n";

    print BY_ITEM "<table id=\"$craft|$on\" class=\"tech collapsed\">\n";
    print BY_ITEM "<tr>\n";
    print BY_ITEM "<td class=\"header2\">Type</td>\n";
    print BY_ITEM "<td class=\"header2\">Technique</td>\n";
    print BY_ITEM "<td class=\"header2\">Effect</td>\n";
    print BY_ITEM "<td class=\"header2\">I</td>\n";
    print BY_ITEM "<td class=\"header2\">II</td>\n";
    print BY_ITEM "<td class=\"header2\">III</td>\n";
    print BY_ITEM "<td class=\"header2\">Source</td>\n";
    print BY_ITEM "</tr>\n";

    # Get a list of techniques for this on
    %techList = ();
    $color = 0;
    foreach my $technique (sort (keys (%{$doc->{technique}}))) {
        if (&isOn($technique, $on)) {
            my $type = $doc->{technique}->{$technique}->{type};

            # Print one technique
            if ($color) {
                print BY_ITEM "<tr class=\"even\">\n";
            } else {
                print BY_ITEM "<tr class=\"odd\">\n";
            }
            $color = !$color;

            print BY_ITEM "<td class=\"type\">$type</td>\n";
            print BY_ITEM "<td class=\"name\"><a href=\"$typeFile#$type|$technique\">$technique</a></td>\n";
            print BY_ITEM "<td class=\"effect\">" . $doc->{technique}->{$technique}->{effect} . "</td>\n";

            if ($doc->{technique}->{$technique}->{tier}->{name} eq 'ALL') {
                # Technique has tier ALL
                print BY_ITEM "<td class=\"I\" colspan=\"3\">" . $doc->{technique}->{$technique}->{tier}->{$_}->{result} . "</td>\n";
            } else {
                foreach ('I', 'II', 'III') {
                    print BY_ITEM "<td class=\"$_\">" . $doc->{technique}->{$technique}->{tier}->{$_}->{result} . "</td>\n";
                }
            }

            print BY_ITEM "<td class=\"source\"><a href=\"$locationFile\">";
            if (($doc->{technique}->{$technique}->{source}->{person}) && ($doc->{technique}->{$technique}->{source}->{place})) {
                print BY_ITEM $doc->{technique}->{$technique}->{source}->{person} . " @ " . $doc->{technique}->{$technique}->{source}->{place};
            } else {
                print BY_ITEM $doc->{technique}->{$technique}->{source}->{person};
                print BY_ITEM $doc->{technique}->{$technique}->{source}->{place};
            }
            print BY_ITEM "</a></td>\n";
            print BY_ITEM "</tr>\n";
        }
    }
    print BY_ITEM "</table>\n";
    print BY_ITEM "</td>\n";
    print BY_ITEM "</tr>\n";
} # END doItem

sub isOn {
    local($technique, $on) = @_;

    foreach $onIter (@{$doc->{technique}->{$technique}->{onlist}->{on}}) {
        if ($on eq $onIter->{content}) {
            return 1;
        }
    }

    return 0;
} # END isOn

sub seeAlso {
    local($file, $current) = @_;

    print $file "(See also ";

    my $first = 1;
    foreach $name (keys(%files)) {
        if ($name ne $current)  {
            if ($first) {
                $first = 0;
            } else {
                print $file ", ";
            }
            print $file "<a href=\"" . $files{$name} . "\">Techniques by $name</a>";
        }
    }

    print $file ")\n";
}
