#!/usr/bin/perl
use lib '.';
use lib '/home/dave/perl';
use japanese;
require "cgi-lib.pl";
ReadParse(*params);
print "Content-Type: text/html\n\n";
print qq`
<html lang="en" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>`;
my $c = japanese::get_chars();
@chart = (
    [ 'a','i','u','e','o', ],
    [ 'ka', 'ki', 'ku', 'ke', 'ko', ],
    [ 'sa', 'shi', 'su', 'se', 'so', ],
    [ 'ta', 'chi', 'tsu', 'te', 'to', ],
    [ 'na', 'ni', 'nu', 'ne', 'no', ],
    [ 'ha', 'hi', 'fu', 'he', 'ho', ],
    [ 'ma', 'mi', 'mu', 'me', 'mo', ],
    [ 'ya', '', 'yu', '', 'yo', ],
    [ 'ra', 'ri', 'ru', 're', 'ro', ],
    [ 'wa', 'wi', '', 'we', 'wo', ],
    [ 'n', '', '', '', '', ],
    );
print "<table border=1>";
foreach my $row (@chart) {
    print "<tr>";
    foreach my $sound (@{$row}) {
	print "<td id=\"td_$sound\" style=\"font-size: 40px;\" onclick=\"var t=document.getElementById('td_$sound');if(t.style.backgroundColor!=''){ t.style.backgroundColor=''; } else { t.style.backgroundColor='#9090FF';}\">";
	if($sound) {
	    #print "$sound: ";
	    my $found = 0;
	    foreach my $char (keys %{$c}) {
		next if($params{type} =~ /h/i && $c->{$char}{type} !~ /h/i);
		next if($params{type} =~ /k/i && $c->{$char}{type} !~ /k/i);
		if($c->{$char}{letter} eq $sound && !$c->{$char}{old}) {
		    print chr($char);
		    $found = 1;
		}
	    }
	    if(!$found) {
		foreach my $char (keys %{$c}) {
		    next if($params{type} =~ /h/i && $c->{$char}{type} !~ /h/i);
		    next if($params{type} =~ /k/i && $c->{$char}{type} !~ /k/i);
		    foreach my $match (@{$c->{$char}{match}}) {
			if($match eq $sound) {
			    print chr($char);
			    $found = 1;
			}
		    }
		}
		if(!$found) {
		    print "???";
		}
	    }
	}
	print "</td>";
    }
    print "</tr>\n";
}
print "</table>\n";
print "</body></html>\n";
