$do_examples=1;
$color="red";

sub stats_form_query {
  my ($master_ref, $thisdance, $lastdance, $i, $trigger,
      %dances_style, %dances_level, $this_style, $this_level, $key,
      @order, $schools_ref);

  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";

  print Constants::get(HTML_MSG32)."<p>";

  print "<FORM action=\"http://".Constants::get(FORM_URL)."\" method=POST>\n";
  print "<input type=\"hidden\" name=\"openimpetus_major_dispatch\"";
  print " value=\"stats_form\">\n";
  print "<input type=\"hidden\" name=\"openimpetus_minor_dispatch\"";
  print " value=\"process\">\n";
  print "<input type=\"hidden\" name=\"compname\" value=\"";
  print Constants::get(COMPFILE)."\">\n";

  print "<center>";

  print "<h3> ".Constants::get(HTML_MSG34)." </h3>\n";
  print Constants::get(HTML_MSG54)." <font color=$color><b>$color</b></font>".
    Constants::get(HTML_MSG55).
      "(<a href=\"#Examples\">".Constants::get(HTML_MSG56)."</a>).<p>\n";

  print "<table>\n";

  $master_ref=Constants::get(MASTER);
  $lastdance=$$master_ref[0];

  for ($i=0;$i<=$#$master_ref;$i++) {
#    print "foo<P>\n";
    $thisdance=$$master_ref[$i];

    if ($thisdance->getLevel() ne $lastdance->getLevel() || $i==0) {
      if ($i!=0) {
	print "</tr>\n";
      }
      print "<tr><td> <input type=\"checkbox\" ";
      print "name=\"req:".Constants::get($thisdance->getLevel()).
	"\" value=\"1\"> </td>\n";
      print "<th align=left ";
      print "id=\"".Constants::get($thisdance->getLevel())."\">".
	"<font color=$color>".
	  Constants::get($thisdance->getLevel()).
	    "</font></th>";
      $this_level=Constants::get($thisdance->getLevel());
      $trigger=1;
    }

    if ($thisdance->getStyle() ne $lastdance->getStyle() || $i==0 ||
	($thisdance->getStyle() eq $lastdance->getStyle() &&
	 $thisdance->getContinent() ne $lastdance->getContinent())) {
      if ($trigger) {
	$trigger=0;
      } elsif ($i!=0) {
	print "</tr>\n<tr>\n";
	print "<td></td><td></td>\n";
      }
      print "<td id=\"".Constants::get($thisdance->getLevel())."-".
	$thisdance->getStyle2()."\">";
      print "<input type=\"checkbox\" ";
      print "name=\"req:".Constants::get($thisdance->getLevel())."-".
	$thisdance->getStyle2()."\" value=\"1\"> </td>\n";
      print "<th align=left> <font color=$color>".$thisdance->getStyle2().
	" </font></th>";
      $this_style=$thisdance->getStyle2();
    }

    print "<td headers=\"".
      Constants::get($thisdance->getLevel())." ".
        Constants::get($thisdance->getLevel())."-".
	  $thisdance->getStyle2()."\"> <input type=\"checkbox\" ";
    print "name=\"req:".$thisdance->getAbbrev()."\" value=\"1\"> </td>\n";
    print "<td headers=\"".
      Constants::get($thisdance->getLevel())." ".
        Constants::get($thisdance->getLevel())."-".
	  $thisdance->getStyle2()."\"> ";
    print $thisdance->getName()." </td>\n";

    $dances_style{$this_level."-".$this_style}.="/".$thisdance->getAbbrev();
    $dances_level{$this_level}.="/".$thisdance->getAbbrev();

    push(@order, $thisdance->getAbbrev());

    $lastdance=$thisdance;
  } # loop over Master[]

  print "</table><p>\n";

  foreach $key (keys(%dances_style)) {
#  foreach $key (sort(keys(%dances_style))) {
    print "<input type=\"hidden\" name=\"category:$key\"";
    print " value=\"$dances_style{$key}\">\n";
  }

  foreach $key (keys(%dances_level)) {
#  foreach $key (sort(keys(%dances_level))) {
    print "<input type=\"hidden\" name=\"category:$key\"";
    print " value=\"$dances_level{$key}\">\n";
  }

  print "<input type=\"hidden\" name=\"order\" value=\"";
  for ($i=0;$i<=$#order;$i++) {
    print "/$order[$i]";
  }
  print "\">\n";

  print "<h3> ".Constants::get(HTML_MSG33)." </h3>\n";

  print "<table>\n";

  print "<tr><td> <input checked type=\"radio\" name=\"format\" value=\"0\"> ",
    Constants::get(HTML_MSG21)." </td>\n";
  print "<td> <input type=\"radio\" name=\"format\" value=\"1\"> ",
    Constants::get(HTML_MSG22)." </td>\n";
  print "<td> <input type=\"radio\" name=\"format\" value=\"2\"> ",
    Constants::get(HTML_MSG57)." </td></tr>\n";

  print "</table>\n";

  $schools_ref=Database::getSortedSchools();
  print "<input type=\"hidden\" name=\"numSchools\" value=\"",$#$schools_ref,
  "\">\n";
  print "<table><tr><th rowspan=10> ".Constants::get(HTML_MSG52).": </th>\n";
  for ($i=0;$i<=$#$schools_ref;$i++) {
    $school=$$schools_ref[$i];
    $school2=$school;
    $school=~s/\s/+/g;
    print "<td> <input type=\"checkbox\" name=\"school-$i\" value=\"$school\"> ".
      Database::getSchoolAbbrev($school2)." </td>\n";
    if (($i+1)%5==0) {
      print "</tr><tr>\n";
    }
  }
  print "</tr></table><p>\n";

  print "</center>\n";

  print "<input type=\"submit\" value=\"".
    Constants::get(HTML_SUBMIT_FORM)."\">\n";
  print "<input type=\"reset\" value=\"".
    Constants::get(HTML_CLEAR_FORM)."\">\n";

  if ($do_examples) {
    print "<hr width=50%>\n";
    print "<a name=\"Examples\"></a>\n";
    print "<h3 align=center> Examples</h3>\n";
    print "Example 1: This will select all events in all 4 styles in Beginner.<br>\n";
    print "<table>\n";
    print "<tr><td> <input checked type=\"checkbox\" name=\"foo\" value=\"0\"> </td>\n";
    print "<th align=left><font color=$color>Beginner</font></th><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<th align=left><font color=$color>Latin</font></th><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Cha-Cha </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Samba </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Rumba </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Paso Doble </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Jive </td>\n";
    print "</tr>\n";
    print "</table><br>\n";
    
    print "Example 2: This will select all of the Latin events in Beginner.<br>\n";
    print "<table>\n";
    print "<tr><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<th align=left><font color=$color>Beginner</font></th><td> <input checked type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<th align=left><font color=$color>Latin</font></th><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Cha-Cha </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Samba </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Rumba </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Paso Doble </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Jive </td>\n";
    print "</tr>\n";
    print "</table><br>\n";

    print "Example 3: This will select only Beginner Samba and Beginner Paso.<br>\n";
    print "<table>\n";
    print "<tr><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<th align=left><font color=$color>Beginner</font></th><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<th align=left><font color=$color>Latin</font></th><td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Cha-Cha </td>\n";
    print "<td> <input checked type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Samba </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Rumba </td>\n";
    print "<td> <input checked type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Paso Doble </td>\n";
    print "<td> <input type=\"checkbox\" name=\"foo\" value=\"1\"> </td>\n";
    print "<td> Jive </td>\n";
    print "</tr>\n";
    print "</table><br>\n";
  } # do_examples

  print "</form>\n";
  General::PrintHTMLTail();
}

1;
