<?PHP

$outsideflag = "Europe";

$sqlregionlist = "SELECT gtr.regionlong AS regionlong, gtr.regionid AS regionid, gtr.regionname AS regionname, gts.statetag AS statetag, gts.statelong AS statelong, COUNT(gf.coach_id) AS numclubs
FROM ".$prefix."CoachesStates AS gts
LEFT JOIN ".$prefix."Coaches AS gf
ON gts.statetag = gf.coach_state_tag AND gf.coach_valid = 1
LEFT JOIN ".$prefix."CoachesRegions AS gtr
ON gts.regionid = gtr.regionid
GROUP BY gtr.regionlong, gts.statetag ORDER BY gtr.regionlong ASC";

//echo $sqlregionlist; 

$resultregionlist = mysql_query($sqlregionlist);

?>

<div class="widget">
  <h2>Browse Coaches</h2>
  <div class="region-list">
<?

$currentregion = '';
$regioncount = '';
$tempstring = '';
$currenttag = '';
$showhide = '';
$region_dropdown = '';
$state_string = '';
while($rowregionlist = mysql_fetch_array($resultregionlist)){
  $selected_region = '';
  $selected_state  = '';
  if ($currentregion != $rowregionlist[regionlong]) {
      if ($selected_item == $currentrid) {
          $selected_region = ' selected="seleted"';
      }
      if ($currentregion != '') {
          if ($currentregion == $outsideflag) { echo ("<br /><strong>Outside N. America</strong><br />"); }
          $region_dropdown .= "<option value=\"$site_url/regionlist.php?region=$currentrid\"$selected_region>$currentregion ($regioncount)</option>$state_string";
          echo ("<div class='list-item'><a href='$site_url/regionlist.php?region=$currentrid'>" . $currentregion . " (" . $regioncount . ")</a><span>+</span><div class='sublist' id='". $currenttag . "'>");
          echo $tempstring;
          echo ('</div></div>');
      }
      $currentregion = $rowregionlist[regionlong];
      $currentrid = $rowregionlist[regionid];
      $currenttag = $rowregionlist[regionname];
      $regioncount = 0;
      $tempstring = '';
      $state_string = '';
  }
  if ($selected_item == $rowregionlist[statetag]) {
      $selected_state = ' selected="selected"';
  }
  $state_string .= "<option value=\"$site_url/statelist.php?state=$rowregionlist[statetag]\"$selected_state>&nbsp;&nbsp;&nbsp;$rowregionlist[statelong] ($rowregionlist[numclubs])</option>";
  $tempstring = $tempstring. "<a href='$site_url/statelist.php?state=$rowregionlist[statetag]".get_sid()."' class='lhdr' title='$rowregionlist[statelong]'>$rowregionlist[statelong] ($rowregionlist[numclubs])</a>\n";
  $regioncount += $rowregionlist[numclubs];
}

echo ("<div class='list-item'><a href='$site_url/regionlist.php?region=$currentrid'>".$currentregion." (".$regioncount.")</a><span>+</span><div class='sublist' id='".$currenttag."'>");
echo $tempstring;
echo ('</div></div>');
if ( $selected_item == 'top') { $selected_region = ' selected="selected"'; }
$region_dropdown .= "<option value=\"" .  $site_url . '/top.php' . "\"$selected_region>Top Ranked</option>";

?>
    <div class="advsearch">
        <a href="<? echo $site_url . '/search.php' ?>">Search Coaches</a> <span class="sprite-magnify">&nbsp;</span>
    </div>
  </div>
</div>
