my $events_tally; # Hashtable
local ($debug=1);

sub do_stats {
  my $error=5;
#  logmsg("do_stats") if $debug;
$date=&ctime(time);
#logmsg("do_names starting: $date");
#  if (&do_leaders()) {
  if (&do_names()) { # 5
    $error--;
$date=&ctime(time);
#logmsg("do_schools starting: $date");
    if (&do_schools()) { # 4
      $error--;
$date=&ctime(time);
#logmsg("do_catl starting: $date");
      if (&do_catl()) { # 3
	$error--;
$date=&ctime(time);
#logmsg("do_cats starting: $date");
	if (&do_cats()) { # 2
	  $error--;
$date=&ctime(time);
#logmsg("do_stats_page starting: $date");
	  return(&do_stats_page()); # 1
	}
      }
    }
  }
  logmsg("error in server_stats: $error");
  return($error); # sorry, we fucked up!
}
 
####

sub do_names {
  my ($alphaindex, $alpha_ref, @alphabet, $ret_ref, $keeplooping,
      $master_ref, $s, $comp, $i, $lschool, $fschool,
      $ret2_ref, $j, $partners_ref, $k, $leader, $follower,
      $coup, $dances_ref, $dance, $blank_file, $url, $head_url);

  my $hlink;
  my ($foo1, $foo2);

#  print "do_names: begin\n";
  General::log("do_names") if $debug;

  $master_ref=Constants::get(MASTER);

  $events_tally=Hashtable->new();
  for ($i=0;$i<=$#$master_ref;$i++) {
    $events_tally->put($$master_ref[$i], 0);
  }
  
  $alphaindex=0;
  $alpha_ref=Constants::get(ALPHABET);
  
  # print out the master leader page
  
  if (!open(OUT, ">".Constants::get(STATS_N))) {
    logmsg("Can't open ".Constants::get(STATS_N));
    return(0);
  }
  General::PrintTemplate(Constants::get(TEMPLATE_N), \*OUT) || return(0);
  $hlink=Constants::get(STATS_N_ALL);
  $foo1=Constants::get(STATS_DIR);
  $foo2=Constants::get(STATS_DIR_HREF);
  $hlink=~s@$foo1@$foo2@;
  print OUT "<a href=\"$hlink\">".Constants::get(HTML_MSG39)."</a> ".
    Constants::get(HTML_MSG40)."<p>\n";
  print OUT "<ul>\n";
  for ($i=0;$i<=$#$alpha_ref;$i++) {
    $hlink=Constants::get(STATS_N_2)."-$$alpha_ref[$i].html";
    $foo1=Constants::get(STATS_DIR);
    $foo2=Constants::get(STATS_DIR_HREF);
    $hlink=~s@$foo1@$foo2@;
    print OUT "<li><a href=\"$hlink\">$$alpha_ref[$i]</a>\n";
  }
  print OUT "</ul><p>\n";
  General::PrintHTMLTail(\*OUT);
  close(OUT);

  if (!open(OUT2, ">".Constants::get(STATS_N_ALL))) {
    logmsg("Can't open ".Constants::get(STATS_N_ALL));
    return(0);
  }
  if (!General::PrintTemplate(Constants::get(TEMPLATE_N_ALL), \*OUT2)) {
    logmsg("Can't print template: ".Constants::get(TEMPLATE_N_ALL));
    return(0);
  }

  $ret_ref=getNameSortedCompetitors();

  $head_url="http://".Constants::get(FORM_URL)."?";
  $head_url.="openimpetus_major_dispatch=registration&";
  $head_url.="openimpetus_minor_dispatch=show_registration&";
  $head_url.="compname=".Constants::get(COMPFILE)."&";

  my $file_started=0;
  for ($i=0;$i<=$#$ret_ref;$i++) {
    $comp=$$ret_ref[$i];

#    print "Examining: ".$comp->getName()."\n";
    $s=$comp->getLName();

#    if (($alphaindex==0) && ($s gt $$alpha_ref[0])) { # special case
#      if ($file_started) {
#	print OUT "</table>\n";
#        if ($blank_file) {
#          print OUT Constants::get(HTML_MSG18)."<p>\n";
#        }
#	General::PrintHTMLTail(\*OUT);
#	close(OUT);
#	$file_started=0;
#      } else {
#	open(OUT, ">".Constants::get(STATS_N_2)."-$$alpha_ref[$alphaindex].html") ||
#	  return(0);
#	General::PrintTemplate(Constants::get(TEMPLATE_N_2), OUT) || return(0);
#	print OUT "<h1> ".$$alpha_ref[$alphaindex]." </h1>\n";
#        print OUT Constants::get(HTML_MSG18)."<p>\n";
#	General::PrintHTMLTail(\*OUT);
#	close(OUT);
#      }
#      $alphaindex++;
#    }

    LOOP: while ($alphaindex<$#$alpha_ref) {
      if ($s gt $$alpha_ref[$alphaindex+1]) {
        if ($file_started) {
  	  print OUT "</table>\n";
  	  print OUT2 "</table>\n";
          if ($blank_file) {
            print OUT Constants::get(HTML_MSG18)."<p>\n";
            print OUT2 Constants::get(HTML_MSG18)."<p>\n";
          }
  	  General::PrintHTMLTail(\*OUT);
    	  close(OUT);
          $file_started=0;
        } else {
  	  if (!open(OUT, ">".Constants::get(STATS_N_2).
		    "-$$alpha_ref[$alphaindex].html")) {
	    logmsg("Can't open: ".Constants::get(STATS_N_2));
	    return(0);
	  }
	  if (!General::PrintTemplate(Constants::get(TEMPLATE_N_2), \*OUT)) {
	    logmsg("Can't print template: ".Constants::get(TEMPLATE_N_2));
	    return(0);
	  }
  	  print OUT "<h1> ".$$alpha_ref[$alphaindex]." </h1>\n";
  	  print OUT2 "<h1> ".$$alpha_ref[$alphaindex]." </h1>\n";
          print OUT Constants::get(HTML_MSG18)."<p>\n";
          print OUT2 Constants::get(HTML_MSG18)."<p>\n";
  	  General::PrintHTMLTail(\*OUT);
  	  close(OUT);
        }
	$alphaindex++;
      } else {
	last LOOP;
      }
    }

#     if (($s gt $$alpha_ref[$#alpha]) && $file_started) {
#       print OUT "</table>\n";
#       if ($blank_file) {
#         print OUT Constants::get(HTML_MSG18)."<p>\n";
#       }
#       General::PrintHTMLTail(\*OUT);
#       close(OUT);
#       $file_started=0;
#     }

#    if (($s gt $$alpha_ref[$alphaindex+1]) && ($alphaindex==$#$alpha_ref

    if (!($file_started)) {
      open(OUT, ">".Constants::get(STATS_N_2)."-$$alpha_ref[$alphaindex].html") ||
	return(0);
$|=1;
      General::PrintTemplate(Constants::get(TEMPLATE_N_2), \*OUT) || return(0);
      print OUT "<h1> ".$$alpha_ref[$alphaindex]." </h1>\n";
      print OUT2 "<h1> ".$$alpha_ref[$alphaindex]." </h1>\n";
      print OUT "<table>\n";
      print OUT2 "<table>\n";
      $file_started=1;
      $blank_file=1;
    }

    $partners_ref=getFollowers($comp);
#    print "follower partners: $partners_ref, ref=".ref($partners_ref)."\n";
#    if ($partners_ref and $#$partners_ref>-1) {
    if ($partners_ref) {
      for ($k=0;$k<=$#$partners_ref;$k++) {
	$follower=$$partners_ref[$k];
	$lschool=$comp->getSchoolAbbrev();
	$fschool=$follower->getSchoolAbbrev();

	$coup=Couple->new($comp, $follower);
	$dances_ref=getDancesByCouple($coup);

	if ($#$dances_ref==-1) {
	  General::log("-1 sizeof: ".$comp->getName()." ".
		       $follower->getName());
          next;
	}

        $blank_file=0;
	print OUT "<tr valign=top><td> \n";
	print OUT2 "<tr valign=top><td> \n";

	$url=$head_url;
	$url.="leader_fname=".$comp->getFName()."&";
	$url.="leader_lname=".$comp->getLName()."&";
	$url.="leader_email=".$comp->getEmail()."&";
	if ($comp->doesEmailExist()==0) {
	  $url.="leader_email_exists=false&";
	} else {
	  $url.="leader_email_exists=&";
	}
	if ($comp->isTBA()) {
	  $url.="leaderTBA=true&";
	} else {
	  $url.="leaderTBA=&";
	}
	$url.="leader_school=".$comp->getSchool()."&";
	$url.="leader_other_school=&";

	$url.="follower_fname=".$follower->getFName()."&";
	$url.="follower_lname=".$follower->getLName()."&";
	$url.="follower_email=".$follower->getEmail()."&";
	if ($follower->doesEmailExist()==0) {
	  $url.="follower_email_exists=false&";
	} else {
	  $url.="follower_email_exists=&";
	}
	if ($follower->isTBA()) {
	  $url.="followerTBA=true&";
	} else {
	  $url.="followerTBA=&";
	}
	$url.="follower_school=".$follower->getSchool()."&";
	$url.="follower_other_school=";

	$url=~s/\s/+/g;

	print OUT "<a href=\"$url\">\n";
	print OUT2 "<a href=\"$url\">\n";
	print OUT "<img border=0 src=\"/openimpetus/Images/bullet_green.gif\"></a> </td>\n";
	print OUT2 "<img border=0 src=\"/openimpetus/Images/bullet_green.gif\"></a> </td>\n";
	print OUT "<td valign=top> <b>".$comp->getName().
	  "</b> </td><td valign=top> $lschool </td><td valign=top> ".
	  $follower->getName()." </td><td valign=top> $fschool </td>\n";
	print OUT2 "<td valign=top> <b>".$comp->getName().
	  "</b> </td><td valign=top> $lschool </td><td valign=top> ".
	  $follower->getName()." </td><td valign=top> $fschool </td>\n";
	
	print OUT "<td valign=top> ".$$dances_ref[0]->getAbbrev();
	print OUT2 "<td valign=top> ".$$dances_ref[0]->getAbbrev();
	for ($l=1;$l<=$#$dances_ref;$l++) {
	  print OUT ", ".$$dances_ref[$l]->getAbbrev();
	  print OUT2 ", ".$$dances_ref[$l]->getAbbrev();
	}
	print OUT " </td></tr>\n";
	print OUT2 " </td></tr>\n";
      }
    }
    
    $partners_ref=getLeaders($comp);
#    print "leader partners: $partners_ref, ref=".ref($partners_ref)."\n";
#    if ($partners_ref and $#$partners_ref>-1) {
    if ($partners_ref) {
      for ($k=0;$k<=$#$partners_ref;$k++) {
	$leader=$$partners_ref[$k];
	$lschool=$leader->getSchoolAbbrev();
	$fschool=$comp->getSchoolAbbrev();

	$coup=Couple->new($leader, $comp);
	$dances_ref=getDancesByCouple($coup);

	if ($#$dances_ref==-1) {
	  General::log("-1 sizeof: ".$comp->getName()." ".
		       $leader->getName());
          next;
	}

        $blank_file=0;
	print OUT "<tr valign=top><td> \n";
	print OUT2 "<tr valign=top><td> \n";

	$url=$head_url;
	$url.="leader_fname=".$leader->getFName()."&";
	$url.="leader_lname=".$leader->getLName()."&";
	$url.="leader_email=".$leader->getEmail()."&";
	if ($leader->doesEmailExist()==0) {
	  $url.="leader_email_exists=false&";
	} else {
	  $url.="leader_email_exists=&";
	}
	if ($leader->isTBA()) {
	  $url.="leaderTBA=true&";
	} else {
	  $url.="leaderTBA=&";
	}
	$url.="leader_school=".$leader->getSchool()."&";
	$url.="leader_other_school=&";

	$url.="follower_fname=".$comp->getFName()."&";
	$url.="follower_lname=".$comp->getLName()."&";
	$url.="follower_email=".$comp->getEmail()."&";
	if ($comp->doesEmailExist()==0) {
	  $url.="follower_email_exists=false&";
	} else {
	  $url.="follower_email_exists=&";
	}
	if ($comp->isTBA()) {
	  $url.="followerTBA=true&";
	} else {
	  $url.="followerTBA=&";
	}
	$url.="follower_school=".$comp->getSchool()."&";
	$url.="follower_other_school=";
	$url=~s/\s/+/g;

	print OUT "<a href=\"$url\">\n";
	print OUT2 "<a href=\"$url\">\n";
	print OUT "<img border=0 src=\"/openimpetus/Images/bullet_green.gif\"></a> </td>\n";
	print OUT2 "<img border=0 src=\"/openimpetus/Images/bullet_green.gif\"></a> </td>\n";
	print OUT "<td valign=top> ".$leader->getName().
	  " </td><td valign=top> $lschool </td><td valign=top> <b>".
	  $comp->getName()."</b> </td><td valign=top> $fschool </td>\n";
	print OUT2 "<td valign=top> ".$leader->getName().
	  " </td><td valign=top> $lschool </td><td valign=top> <b>".
	  $comp->getName()."</b> </td><td valign=top> $fschool </td>\n";
	
	print OUT "<td valign=top> ".$$dances_ref[0]->getAbbrev();
	print OUT2 "<td valign=top> ".$$dances_ref[0]->getAbbrev();
	if ($#$dances_ref>0) {
	  for ($l=1;$l<=$#$dances_ref;$l++) {
	    print OUT ", ".$$dances_ref[$l]->getAbbrev();
	    print OUT2 ", ".$$dances_ref[$l]->getAbbrev();
	  }
	}
	print OUT " </td></tr>\n";
	print OUT2 " </td></tr>\n";
      }
    }    
  } # for
  
  if ($file_started) {
    print OUT "</table>\n";
    print OUT2 "</table>\n";
    if ($blank_file) {
      print OUT Constants::get(HTML_MSG18)."<p>\n";
      print OUT2 Constants::get(HTML_MSG18)."<p>\n";
    }
    General::PrintHTMLTail(\*OUT);
    close(OUT);
  }
  $i=$alphaindex+1;
  while($i<=$#$alpha_ref) {
    open(OUT, ">".Constants::get(STATS_L_2)."-$$alpha_ref[$i].html") ||
      return(0);
    General::PrintTemplate(Constants::get(TEMPLATE_L_2), \*OUT) || return(0);
    print OUT "<h1> ".$$alpha_ref[$i]." </h1>\n";
    print OUT2 "<h1> ".$$alpha_ref[$i]." </h1>\n";
    print OUT Constants::get(HTML_MSG18)."<p>\n";
    print OUT2 Constants::get(HTML_MSG18)."<p>\n";
    General::PrintHTMLTail(\*OUT);
    close(OUT);
    $i++;
  }
  
  General::PrintHTMLTail(\*OUT2);
  close(OUT2);
#  print "do_names: end\n";
  General::log("do_names: returning success") if $debug;
  return(1);
} # do_names
 
# ################################################################ #

sub do_schools {
  
#  print "do_schools: begin\n";
  General::log("do_schools") if $debug;

  my ($schools_ref, $i, $school, $comps_ref, $j,
      $comp, $partners_ref, $k, $follower, $coup, $l,
      $dances_ref, $hlink, $foo1, $foo2, $schoolref, %num);

  $schools_ref=getSortedSchools();
#  print "sizeof schools_ref: ".$#$schools_ref."\n";

  for ($i=0;$i<=$#$schools_ref;$i++) {
    $school=$$schools_ref[$i];
#    print "school: $school\n";
    $schoolref=$school; # this is different -- there's the school ref, the
    # school name, and then there's just what was ever in the indexing.
    if ($schoolref==-1) {
      $schoolref="noschoolaffiliation";
      $schoolname="<i>No School Affiliation</i>";
    } else {
      $schoolname=$school;
      $_=$schoolref;
      if (/\(/) {
        ($crap, $_)=split('\(', $_);
        ($_, $crap)=split('\)', $_);
      }
      s/\s//g;
      $schoolref=$_;
    }
#    General::log("schoolref: '$schoolref'");
    open(OUT, ">".Constants::get(STATS_S_2)."-$schoolref.html") ||
      return(0);
    General::PrintTemplate(Constants::get(TEMPLATE_S_2), \*OUT) || return(0);
    print OUT "<h1 align=center> ".$schoolname." </h1>\n";
    print OUT "<table>\n";
    $comps_ref=getNameSortedCompetitorsBySchool($school);
    $num{$school}=$#$comps_ref+1;
    print OUT "<p>".Constants::get(HTML_MSG47).": ".($#$comps_ref+1)."<p>\n";
# General::log("school=$school");
    for ($j=0;$j<=$#$comps_ref;$j++) {
      $comp=$$comps_ref[$j];

# General::log("[$j] comp=".$comp->getName());

      $partners_ref=getFollowers($comp);
#General::log("school: $school\tj: $j\tleader: ".$comp->getName()."\tsizeof partners: $#partners");
      if ($partners_ref) {
	for ($k=0;$k<=$#$partners_ref;$k++) {
	  $follower=$$partners_ref[$k];
#General::log("leader: ".$comp->getName()."\tfollower:".$follower->getName());
	  $lschool=$comp->getSchoolAbbrev();
	  $fschool=$follower->getSchoolAbbrev();
	  $number=$comp->getNumber();

	  $coup=Couple->new($comp, $follower);
	  $dances_ref=getDancesByCouple($coup);
	  if ($#$dances_ref==-1) {
	    General::log("-1 sizeof: ".$comp->getName()." ".
			 $follower->getName()."; school=".$comp->getSchool());
	    next;
	  }

	  if ($fschool eq $lschool) {
	    if ($number==-1) {
	      print OUT "<tr><td valign=top> <b>".$comp->getName().
		"</b> </td><td valign=top> ".
		  $follower->getName()." </td>\n";
	    } else {
	      print OUT "<tr><td valign=top> ".$number.
		" </td><td valign=top> ".
		$comp->getName()." </td><td valign=top> ".
		  $follower->getName()." </td>\n";
	    }
	  } else {
	    if ($number==-1) {
	      print OUT "<tr><td valign=top> <b>".$comp->getName().
		"</b> </td><td valign=top> ".
		  $follower->getName()." ($fschool) </td>\n";
	    } else {
	      print OUT "<tr><td valign=top> ".$number.
		" </td><td valign=top> ".$comp->getName().
		" </td><td valign=top> ".
		  $follower->getName()." ($fschool) </td>\n";
	    }
	  }

	  print OUT "<td valign=top> ".$$dances_ref[0]->getAbbrev() if
	    $#$dances_ref>-1;
	  if ($#$dances_ref>0) {
	    for ($l=1;$l<=$#$dances_ref;$l++) {
	      print OUT ", ".$$dances_ref[$l]->getAbbrev();
	    }
	  }
	  print OUT " </td></tr>\n";
	}
      }

      $partners_ref=getLeaders($comp);
#      print "comp: ".$comp->getName()."\tpartners_ref=$partners_ref\n";
      if ($partners_ref) {
	for ($k=0;$k<=$#$partners_ref;$k++) {
	  $leader=$$partners_ref[$k];
#	  print "leader: ".$leader->getName()."\n";
	  $lschool=$leader->getSchoolAbbrev();
	  $fschool=$comp->getSchoolAbbrev();
	  $number=$leader->getNumber();

	  $coup=Couple->new($leader, $comp);
#	  print "getting dances for ".$coup->getNames()."\n";
	  $dances_ref=getDancesByCouple($coup);
#	  print "sizeof dances: ".$#$dances_ref."\n";
#	  print "ref(dances[0]): ".ref($$dances_ref[0])."\n";
	  if ($#$dances_ref==-1) {
	    General::log("-1 sizeof: ".$comp->getName().", following ".
			 $leader->getName()."; school=".$comp->getSchool());
	    next;
	  }

	  if ($lschool eq $fschool) {
	    if ($number==-1) {
	      print OUT "<tr><td valign=top> ".$leader->getName().
		" </td><td valign=top> <b>".
		  $comp->getName()."</b> </td>\n";
	    } else {
	      print OUT "<tr><td valign=top> ".$number.
		" </td><td valign=top> ".$leader->getName().
		" </td><td valign=top> ".
		  $comp->getName()." </td>\n";
	    }
	  } else {
	    if ($number==-1) {
	      print OUT "<tr><td valign=top> ".$leader->getName().
		" ($lschool)</td><td valign=top> <b>".
		  $comp->getName()."</b> </td>\n";
	    } else {
	      print OUT "<tr><td valign=top> ".$number.
		" </td><td valign=top> ".$leader->getName().
		" ($lschool) </td><td valign=top> <b>".
		  $comp->getName()."</b> </td>\n";
	    }
	  }

	  if ($#$dances_ref>-1) {
	    print OUT "<td valign=top> ".$$dances_ref[0]->getAbbrev();
	    for ($l=1;$l<=$#$dances_ref;$l++) {
#	      $dance=$dances[$l];
	      print OUT ", ".$$dances_ref[$l]->getAbbrev();
	    }
	  }
	  print OUT " </td></tr>\n";
	}
      }
    }
    print OUT "</table>\n";

    General::PrintHTMLTail(\*OUT);
    close(OUT);
  }
  
  # print out the master schools page
  
  open(OUT, ">".Constants::get(STATS_S)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_S), \*OUT) || return(0);
 
  print OUT "<ul>\n";
  
  for ($i=0;$i<=$#$schools_ref;$i++) {
    $school=$$schools_ref[$i];
    $schoolref=$school;
    if ($schoolref==-1) {
      $schoolref="noschoolaffiliation";
      $schoolname="<i>".Constants::get(HTML_MSG29)."</i>";
    } else {
      $schoolname=$school;
      $_=$schoolref;
      if (/\(/) {
        ($crap, $_)=split('\(', $_);
        ($_, $crap)=split('\)', $_);
      }
      s/\s//g;
      $schoolref=$_;
    }
    $hlink=Constants::get(STATS_S_2)."-$schoolref.html";
    $foo1=Constants::get(STATS_DIR);
    $foo2=Constants::get(STATS_DIR_HREF);
    $hlink=~s@$foo1@$foo2@;
    print OUT "<li><a href=\"$hlink\">$schoolname</a> (".$num{$school}.")\n";
  }
  print OUT "</ul><p>\n";
  General::PrintHTMLTail(\*OUT);
  close(OUT);

#  General::PrintTemplate(Constants::get(STATS_END_TEMPLATE), OUT) || return(0);
#  General::PrintHTMLTail(\*OUT);
#  close(OUT);
#  print "do_schools: end\n";
  General::log("do_schools: returning success") if $debug;
  return(1);
} # do_schools

# ################################################################ #

sub do_catl {
  General::log("do_catl") if $debug;
  my ($i, $master_ref, $dance, $vec_ref, $coup,
      $lschool, $fschool, $j, $comp, $hlink, $foo1, $foo2);

  $master_ref=Constants::get(MASTER);
  
  open(OUT, ">".Constants::get(STATS_CATL)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_CATL), \*OUT) || return(0);
  print OUT "<ul>\n";
  for ($i=0;$i<=$#$master_ref;$i++) {
    $dance=$$master_ref[$i];
    $hlink=Constants::get(STATS_CATL_2)."-".Dance::getAbbrev($dance).".html";
    $foo1=Constants::get(STATS_DIR);
    $foo2=Constants::get(STATS_DIR_HREF);
    $hlink=~s@$foo1@$foo2@;
    print OUT "<li> <a href=\"$hlink\">".$dance->getFullName."</a>\n";
  }
  print OUT "</ul>\n";
  General::PrintHTMLTail(\*OUT);
  close(OUT);

  for ($i=0;$i<=$#$master_ref;$i++) {
    $dance=$$master_ref[$i];
    open(OUT, ">".Constants::get(STATS_CATL_2)."-".Dance::getAbbrev($dance).
	 ".html") || return(0);
    General::PrintTemplate(Constants::get(TEMPLATE_CATL_2), \*OUT) || return(0);
    print OUT "<h3 align=center>".$dance->getFullName."</h1>\n";
    
    $vec_ref=getNameSortedCouplesByDance($dance);
#    print "dance=".$dance->getAbbrev()."; sizeof vec_ref: ".$#$vec_ref."\n";
    if ($vec_ref) {
      print OUT "<table>\n";
      for ($j=0;$j<=$#$vec_ref;$j++) {
	$coup=$$vec_ref[$j];
	print OUT "<tr><td> ".$coup->getLeader()->getName()." </td><td> ".
	  $coup->getFollower->getName()." </td>\n";
	$lschool=$coup->getLeader()->getSchoolAbbrev();
	$fschool=$coup->getFollower()->getSchoolAbbrev();
	if ($lschool eq $fschool) {
	  print OUT "<td> $lschool </td>\n";
	} else {
	  print OUT "<td> $lschool / $fschool </td>\n";
	}
	print OUT "</tr>\n";
      }
      print OUT "</table>\n";
    }
    print OUT "<p>".Constants::get(HTML_MSG48).": ".($#$vec_ref+1)."<p>\n";
    General::PrintHTMLTail(\*OUT);
    close(OUT);
  } # for
  
#  General::PrintTemplate(Constants::get(STATS_END_TEMPLATE), OUT) || return(0);
  General::log("do_catl: returning success") if $debug;
  return(1);
} # do_catl

# ################################################################ #

sub do_cats {
  General::log("do_cats") if $debug;

  my ($schools_ref, $couples_ref, $master_idx, $i,
      $master_ref, $school, $coup, $schools_idx,
      $couples_idx, @printVector, $firstOne, $dance);

  $master_ref=Constants::get(MASTER);
  
  open(OUT, ">".Constants::get(STATS_CATS)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_CATS), \*OUT) || return(0);
  print OUT "<ul>\n";
  for ($i=0;$i<=$#$master_ref;$i++) {
    $dance=$$master_ref[$i];
    $hlink=Constants::get(STATS_CATS_2)."-".Dance::getAbbrev($dance).".html";
    $foo1=Constants::get(STATS_DIR);
    $foo2=Constants::get(STATS_DIR_HREF);
    $hlink=~s@$foo1@$foo2@;
    print OUT "<li> <a href=\"$hlink\">".$dance->getFullName."</a>\n";
  }
  print OUT "</ul>\n";
  General::PrintHTMLTail(\*OUT);
  close(OUT);

  $schools_ref=getSortedSchools();

  for ($master_idx=0;$master_idx<=$#$master_ref;$master_idx++) {
    $dance=$$master_ref[$master_idx];
    open(OUT, ">".Constants::get(STATS_CATS_2)."-".Dance::getAbbrev($dance).
	 ".html") || return(0);
    General::PrintTemplate(Constants::get(TEMPLATE_CATS_2), \*OUT) || return(0);
#    print OUT "<h1 align=center>".Constants::get(HTML_MSG22)."</h1>\n";
    print OUT "<h3 align=center>".$dance->getFullName()."</h3>\n";
    print OUT "<table>\n";
    
    $couples_ref=getNameSortedCouplesByDance($dance);
    if ($#$couples_ref>-1) {
      for ($schools_idx=0;$schools_idx<=$#$schools_ref;$schools_idx++) {
	$school=$$schools_ref[$schools_idx];
	@printVector=();
	for ($couples_idx=0;$couples_idx<=$#$couples_ref;$couples_idx++) {
	  $coup=$$couples_ref[$couples_idx];
	  if (($school eq $coup->getLeader()->getSchool()) ||
	      ($coup->getLeader()->isTBA() && $coup->getFollower()->getSchool() eq $school)) {
	    push(@printVector, $coup);
	  }
	}
	next if ($#printVector==-1);
	$firstOne=1;
	for ($i=0;$i<=$#printVector;$i++) {
	  $coup=$printVector[$i];
	  if ($firstOne) {
            if ($school==-1) {
	      print OUT "<tr><th valign=top rowspan=".($#printVector+1)."> ".Constants::get(HTML_MSG29)." </th>\n";
	    } else {
	      print OUT "<tr><th valign=top rowspan=".($#printVector+1)."> ".$school." </th>\n";
	    }
	    $firstOne=0;
	  } else {
	    print OUT "<tr>";
	  }
	  
	  if ($coup->getLeader()->getSchool() eq $school) {
	    print OUT "<td> ".$coup->getLeader()->getName()." </td><td> ".
	      $coup->getFollower->getName()." </td>\n";
	    $lschool=$coup->getLeader()->getSchoolAbbrev();
	    $fschool=$coup->getFollower()->getSchoolAbbrev();
	    if ($lschool eq $fschool) {
              if ($lschool==-1) {
	        print OUT "<td> ".Constans::get(HTML_MSG29)." </td>\n";
	      } else {
	        print OUT "<td> $lschool </td>\n";
              }
	    } else {
	      $lschool=~s/^-1$/Constants::get(HTML_MSG29)/;
	      $fschool=~s/^-1$/Constants::get(HTML_MSG29)/;
	      print OUT "<td> $lschool / $fschool </td>\n";
	    }
	    print OUT "</tr>\n";
# This gets traversed for leader=TBA
	  } else {
	    print OUT "<td><i> ".$coup->getLeader()->getName().
	      " </i></td><td> ".
		$coup->getFollower->getName()." </td>\n";
	    $lschool=$coup->getLeader()->getSchoolAbbrev();
	    $fschool=$coup->getFollower()->getSchoolAbbrev();
	    print OUT "<td>  / $fschool </td>\n";
	  }	  
	}
      }
    }
    print OUT "</table>\n";
    print OUT "<p>".Constants::get(HTML_MSG48).": ".($#$couples_ref+1)."<p>\n";
    General::PrintHTMLTail(\*OUT);
    close(OUT);
  } # for
  
#  General::PrintTemplate(Constants::get(STATS_END_TEMPLATE), OUT) || return(0);
  General::log("do_cats: returning success") if $debug;
  return(1);
} # do_cats

sub do_stats_page {
  General::log("do_stats_page: ".Constants::get(STATS)) if $debug;
  my ($lastLevel, $lastStyle, $i, $master_ref, @master, $dance, $foo);
   
  $master_ref=Constants::get(MASTER);

  open(OUT, ">".Constants::get(STATS)) || return(0);
  General::PrintTemplate(Constants::get(TEMPLATE_STATS), \*OUT) || return(0);
    
  print OUT Constants::get(HTML_MSG26).$date."<p>\n";
  print OUT "<h3>".Constants::get(HTML_MSG14).": ".
    getNumberOfCompetitors()."</h3>\n";
  print OUT "<h3 align=center>\n";
  $foo="[ <a href=\"".Constants::get(STATS_N)."\">".
    Constants::get(HTML_MSG27)."</a> | ".
      "<a href=\"".Constants::get(STATS_S)."\">".
	Constants::get(HTML_MSG20)."</a> | ".
	  "<a href=\"".Constants::get(STATS_CATL)."\">".
	    Constants::get(HTML_MSG21)."</a> | ".
	      "<a href=\"".Constants::get(STATS_CATS)."\">".
		Constants::get(HTML_MSG22)."</a> | "."<a href=\"http://".
		  Constants::get(FORM_URL).
		    "?openimpetus_major_dispatch=stats_form&openimpetus_minor_dispatch=query&compname=".Constants::get(COMPDIR)."\">".
		    Constants::get(HTML_MSG58)."</a> ]<p>\n";
  my $a=Constants::get(STATS_DIR);
  my $b=Constants::get(STATS_DIR_HREF);
  $foo=~s@$a@$b@g;
  print OUT $foo;
  print OUT "</h3>\n";
  
#  print OUT "<img src=\"http://web.mit.edu/endeavor/www/Graphics/Small/new.gif\"> <a href=\"http://".Constants::get(FORM_URL)."?openimpetus_major_dispatch=stats_form&openimpetus_minor_dispatch=query&compname=".Constants::get(COMPDIR)."\">Click here for the Stats Form</a><p>\n";

  $lastLevel="";
  $lastStyle=-1;
  $lastContinent="";
  for ($i=0;$i<=$#$master_ref;$i++) {
    $dance=$$master_ref[$i];
    $level=$dance->getLevel();
    $style=$dance->getStyle();
    $continent=$dance->getContinent();
    if ($lastLevel ne $level) {
      $lastLevel=$level;
      $lastStyle=$style;
      $lastContinent=$continent;
      print OUT "<hr width=50%>\n";
    } elsif ($style ne $lastStyle ||
	     (($style eq $lastStyle) && ($continent ne $lastContinent))) {
      $lastStyle=$style;
      $lastContinent=$continent;
      print OUT "<p>\n";
    }
    $hlink=Constants::get(STATS_CATS_2)."-".Dance::getAbbrev($dance).".html";
    $foo1=Constants::get(STATS_DIR);
    $foo2=Constants::get(STATS_DIR_HREF);
    $hlink=~s@$foo1@$foo2@;
    print OUT Constants::get($dance->getLevel())." ".
      Constants::get($dance->getContinent())." ".
	$dance->getName().": <a href=\"$hlink\">".
		getNumberOfCouplesInDance($dance)."</a><br>\n";
  }
  

  General::PrintHTMLTail(\*OUT);

  close(OUT);
  General::log("do_stats_page: returning success") if $debug;
  return(0); # not 1
} # do_stats_page
 
# ################################################################ #
 
################ Obsolete ################
# 
# sub printCouples {
#   General::log("printCouples") if $debug;
#   my($in_ref, $OUT)=@_;
#   my (@in, $i, $coup, $leader, $follower, $coup_dances, $master_ref, @master);
#  
#   $master_ref=Constants::get(MASTER);
#   @master=@$master_ref;
# 
#   @in=@$in_ref;
# 
#   for ($i=0;$i<=$#in;$i++) {
# #    $coup=Couple->blessme($in[$i]);
#     $coup=$in[$i];
#     $leader=$coup->getLeader();
#     $follower=$coup->getFollower();
# 
#     if ($leader->getSchool() eq $follower->getSchool()) {
#       print $OUT "<tr><td> ".$leader->getName()." </td><td> ".
# 	$follower->getName()." </td><td> ".
# 	  $leader->getSchoolAbbrev()." </td>\n";
#     } else {
#       print $OUT "<tr><td> ".$leader->getName()." </td><td> ".
# 	$follower->getName()." </td><td> ".
# 	  $leader->getSchoolAbbrev()."/".$follower->getSchoolAbbrev().
# 	    " </td>\n";
#     }
#     $coup_dances=Vector->new(Database::getDancesByCouple($coup));
#     for ($i=0;$i<$#master;$i++) {
#       if ($coup_dances->contains($master[$i])) {
# 	# events_tally.add(Constants.MASTER[i],
# 	#	       events_tally.get(Constants.MASTER[i])+1);
# 	print $OUT "<td> ".$master[$i]->getName(). "</td>\n";
#       }
#     }
#     print $OUT "</tr>\n";
#   }
#   General::log("printCouples: returning") if $debug;
# } # printCouples
# 
# #   # Just name and school
# #   private static void printCompetitors2(Enumeration enum, PrintWriter out) {
# #     Competitor comp, f_partner;
# #     Enumeration f_partners_enum;
# #     int i;
# # 
# #     while(enum.hasMoreElements()) {
# #       comp=(Competitor)enum.nextElement();
# #       printCompetitors2(comp, out);
# #     } # while(enum.hasMoreElements())
# #   } # printCompetitors2(Enumeration, PrintWriter)
#  
# 
# sub printCompetitors {
#   my ($comps_ref, $OUT)=@_;
#   my (@comps, $x, $comp, $i, @f_partners, $f_partner);
#   
#   @comps=@$comps_ref;
#   if ($comps[0]==null) {return;}
#   for ($x=0;$x<=$#comps;$x++) {
# #    $comp=Competitor->blessme($comps[$x]);
#     $comp=$comps[$x];
#     if ($comp->hasFPartners()) { # only list leaders
#       @f_partners=$comp->getFPartners();
#       for ($i=0;$i<=$#f_partners;$i++) {
# #	$f_partner=Competitor->blessme($f_partners[$i]);
# 	$f_partner=$f_partners[$i];
# 	if ($comp->getSchool() eq $f_partner->getSchool()) {
# 	  print $OUT "<tr><td> ".$comp->getName()." </td><td> ".
# 	    $f_partner->getName()." </td><td> ".$comp->getSchoolAbbrev().
# 	      " </td>\n";
# 	} else {
# 	  print $OUT "<tr><td> ".$comp->getName()." </td><td> ".
# 	    $f_partner->getName()." </td><td> ".$comp->getSchoolAbbrev()."/".
# 	      $f_partner->getSchoolAbbrev()." </td>\n";
# 	}
# 	print $OUT "</tr>\n";
#       } # while(f_partners_enum.hasMoreElements())
#     } # if (comp.hasF_Partners)
#   } # for
# } # printCompetitors
# 
# sub printCouples1 {
#   General::log("printCouples1") if $debug;
#   my ($coup, $OUT)=@_;
#   
#   print $OUT "<tr><td> ".$coup->getLeader()->getName()." </td><td> ".
#     $coup->getFollower->getName()." </td>\n";
#   my $lschool=$coup->getLeader()->getSchoolAbbrev();
#   my $fschool=$coup->getFollower()->getSchoolAbbrev();
#   if ($lschool eq $fschool) {
#     print $OUT "<td> $lschool </td></tr>\n";
#   } else {
#     print $OUT "<td> $lschool / $fschool </td></tr>\n";
#   }
#   General::log("printCouples1: returning") if $debug;
# }
#  
# sub printCouples2 {
#   General::log("printCouples2") if $debug;
#   my ($vec_ref, $OUT)=@_;
#   my (@vec, $i);
#   @vec=@$vec_ref;
#   for ($i=0;$i<=$#vec;$i++) {
# #    &printCouples1(Couple->blessme($vec[$i]), $OUT);
#     &printCouples1($vec[$i], $OUT);
#   }
#   General::log("printCouples2: returning") if $debug;
# }
#   
# sub do_leaders {
#   my ($alphaindex, $alpha_ref, @alphabet, $ret_ref, @ret, $keeplooping, $comp,
#       $master_ref, @master, $s, $comp, $i, $lschool, $fschool,
#       @ret2, $ret2_ref, $j);
# 
#   General::log("do_leaders") if $debug;
# 
#   $events_tally=Hashtable->new();
#   for ($i=0;$i<$#master;$i++) {
#     $events_tally->put($master[$i], 0);
#   }
#   
#   $alphaindex=0;
#   $alpha_ref=Constants::get(ALPHABET);
#   @alpha=@$alpha_ref;  
#   $master_ref=Constants::get(MASTER);
#   @master=@$master_ref;
#   
#   # print out the master leader page
#   
#   open(OUT, ">".Constants::get(STATS_L)) || return(0);
#   General::PrintTemplate(Constants::get(TEMPLATE_L), OUT) || return(0);
#   print OUT "<ul>\n";
#   my $hlink;
#   my ($foo1, $foo2);
#   for ($i=0;$i<=$#alpha;$i++) {
#     $hlink=Constants::get(STATS_L_2)."-$alpha[$i].html";
#     $foo1=Constants::get(STATS_DIR);
#     $foo2=Constants::get(STATS_DIR_HREF);
#     $hlink=~s@$foo1@$foo2@;
#     print OUT "<li><a href=\"$hlink\">$alpha[$i]</a>\n";
#   }
#   print OUT "</ul><p>\n";
#   General::PrintHTMLTail(\*OUT);
#   close(OUT);
# 
#   $ret_ref=Database::getNameSortedCouples();
#   @ret=@$ret_ref;
# 
#   # Degenerate case; worry about it later.  I think this can be ignored.
# #  if ($#ret==-1) {
# #    for ($i=0;$i<=$#alpha;$i++) {
# #      print OUT "<a name=\"".$alpha[++$alphaindex]."\"></a>\n";
# #      print OUT "<h1> ".$alpha[$alphaindex]." </h1>\n";
# #    }
# ##    General::PrintTemplate(Constants::get(STATS_END_TEMPLATE), OUT) || return(0);
# #    General::PrintHTMLTail(\*OUT);
# #    close(OUT);
# #    return(1);
# #  }
# 
#   my $file_started=0;
#   for ($i=0;$i<=$#ret;$i++) {
#     $coup=Couple->blessme($ret[$i]);
#     $s=$coup->getLeader()->getLName();
# 
#     while ($s gt $alpha[$alphaindex+1]) {
#       if ($file_started) {
# 	print OUT "</table>\n";
# 	General::PrintHTMLTail(\*OUT);
# 	close(OUT);
# 	$file_started=0;
#       } else {
# 	$alphaindex++;
# 	open(OUT, ">".Constants::get(STATS_L_2)."-$alpha[$alphaindex].html") ||
# 	  return(0);
# 	General::PrintTemplate(Constants::get(TEMPLATE_L_2), OUT) || return(0);
# 	print OUT "<h1> ".$alpha[$alphaindex]." </h1>\n";
# 	General::PrintHTMLTail(\*OUT);
# 	close(OUT);
#       }
#     }
# 
#     if (!($file_started)) {
#       open(OUT, ">".Constants::get(STATS_L_2)."-$alpha[$alphaindex].html") ||
# 	return(0);
# $|=1;
#       General::PrintTemplate(Constants::get(TEMPLATE_L_2), OUT) || return(0);
#       print OUT "<h1> ".$alpha[$alphaindex]." </h1>\n";
#       print OUT "<table>\n";
#       $file_started=1;
#     }
# 
#     print OUT "<tr><td> ".$coup->getLeader()->getName()." </td><td> ".
#       $coup->getFollower->getName()." </td>\n";
#     $lschool=$coup->getLeader()->getSchoolAbbrev();
#     $fschool=$coup->getFollower()->getSchoolAbbrev();
#     if ($lschool eq $fschool) {
#       print OUT "<td> $lschool </td>\n";
#     } else {
#       print OUT "<td> $lschool / $fschool </td>\n";
#     }
# 
#     $ret2_ref=Database::getDancesByCouple($coup);
#     @ret2=@$ret2_ref;
#     for ($j=0;$j<=$#ret2;$j++) {
#       $dance=$ret2[$j];
#       print OUT "<td> ".$dance->getAbbrev()." </td>";
#     }
#     print OUT "</tr>\n";
#   } # for
#   
#   if (++$alphaindex <= $#alpha) {
#     if ($file_started) {
#       print OUT "</table>\n";
#       General::PrintHTMLTail(\*OUT);
#       close(OUT);
#     }
#     for ($i=$alphaindex;$i<=$#alpha;$i++) {
#       open(OUT, ">".Constants::get(STATS_L_2)."-$alpha[$alphaindex].html") ||
# 	return(0);
#       General::PrintTemplate(Constants::get(TEMPLATE_L_2), OUT) || return(0);
#       print OUT "<h1> ".$alpha[$i]." </h1>\n";
#       General::PrintHTMLTail(\*OUT);
#       close(OUT);
#     }
#   }
#   
#   General::log("do_leaders: returning success") if $debug;
#   return(1);
# } # do_leaders
 
1;
