local($debug=0);

sub stats_form_process {
  my $post_data_ref=$_[0];
  my (@req, %category, $key, $i, $delim, %allreqs, @foo,
      @order, @get_dances, $format, $header);

  $delim=Constants::get(DELIM);

  print "<HTML><HEAD><TITLE> ".Constants::get(HTML_MSG31).
    " </TITLE></HEAD>";
  print Constants::get(BODY_TAG);
  print "<h1 align=center> ".Constants::get(HTML_MSG31)." </h1>\n";

  foreach $_ (keys(%$post_data_ref)) {
    if (/req\%3A/) {
      s/req\%3A//;
      push(@req, $_);
    }
    if (/category\%3A/) {
      $key=$_;
      s/category\%3A//;
      $category{$_}=$$post_data_ref{$key};
    }
  }

  for ($i=0;$i<=$#req;$i++) {
    $_=$req[$i];
    if (/^\w+$delim\w+$delim\w+$/) { # normal req
      $allreqs{$_}=1;
    } else {
      @foo=split('/', $category{$_});
      shift(@foo); # delete leading blank
      while($_=shift(@foo)) {
	$allreqs{$_}=1;
      }
    }
  }

  @order=split('/', $$post_data_ref{"order"});

  for ($i=0;$i<=$#order;$i++) {
    if ($allreqs{$order[$i]}) {
      push(@get_dances, $order[$i]);
    }
  }

  $format=$$post_data_ref{"format"};
  if ($format==0) { # by leader
    $header=Constants::get(STATS_CATL_2)."-";
    print Constants::get(HTML_MSG50).":<p>\n";
  } else { # by school; possibly by selected school
    $header=Constants::get(STATS_CATS_2)."-";
    print Constants::get(HTML_MSG51).":<p>\n";
    if ($format==2) {
      print "<table><tr><th> ".Constants::get(HTML_MSG52)." </th>\n";
      $numSchools=$$post_data_ref{"numSchools"};
      for ($i=0;$i<=$numSchools;$i++) {
	if ($$post_data_ref{"school-$i"}) {
	  push(@schools, $$post_data_ref{"school-$i"});
	  print "<td> ",$$post_data_ref{"school-$i"}," </td>\n";
	}
	print "</tr></table>\n";
      }
    }
  }

  print "<p>\n";
  print "<table border>\n<tr>\n";
  for ($i=0;$i<=$#get_dances;$i++) {
    print "<td> $get_dances[$i] </td>";
  }
  print "\n</tr></table>\n";

  for ($i=0;$i<=$#get_dances;$i++) {
    open(IN, $header.$get_dances[$i].".html") ||
      Error::CantOpenFile($header.$get_dances[$i].".html");
    while(<IN>) {
      last if /^<h3/;
    }
    print $_,"\n";
    print "<table>\n";
    if ($format==2) {
      while(<IN>) {
	chop;
#	print "foo: '$_'<br>\n";
	FOO: if (/th valign=top/) { # school start
	  for ($j=0;$j<=$#schools;$j++) {
	    $school=$schools[$j];
	    $school=~s/\(/\\(/;
	    $school=~s/\)/\\)/;

#	print "j: '$j', '".$schools[$j]."', \$_='$_'<br>\n";
	    if (/$school/) { # we're in!
	      print $_,"\n";
	      while(<IN>) {
		chop;
		next FOO if (/th valign=top/);
		last if (m@</table>@);
		print $_,"\n";
	      }
	    }
	  }
	}
      }
    } else { # deal with formats 0 and 1
      $_=<IN>; # pick up the <table>
      while(<IN>) {
	last if m@</table>@;
	print;
      }
    }
    print "</table>\n";
#    while(<IN>) {
#      last if m@Number of couples in this event@;
#    }
    close(IN);
  }
    
  if ($debug) {
    print "<ul>\n";
    for ($i=0;$i<=$#order;$i++) {
      if ($allreqs{$order[$i]}) {
	print "<li> $order[$i]\n";
      }
    }
    print "</ul><p>\n";
    
    print Constants::get(HTML_MSG53).": ".$$post_data_ref{"order"}."<p>\n";

    print "<table border>\n";
    print "<tr><th> Key </th><th> Value </th></tr>\n";
    foreach $key (sort(keys(%$post_data_ref))) {
      print "<tr><td> $key </td><td> ".$$post_data_ref{$key}." </td></tr>\n";
    }
    print "</table><p>\n";
  }

  General::PrintHTMLTail();
}

1;
