function sqlRefresh()
{
    $season = getSeason();
    /// /////////////////////////////////////////////////
    /// TRUNCATE SCHEDULE
    $sql = <<<SQL

\t\ttruncate table fanta66_joomla.fch_schedule;
SQL;
    $scheduleTruncateResults = executeGenericSQL($sql);
    /// /////////////////////////////////////////////////
    /// REBUILD SCHEDULE
    $sql = <<<SQL

\t\tinsert into fanta66_joomla.fch_schedule
\t\t(dateStamp,visitor,home)
\t\tselect date_stamp,visitor,home from fanta66_zudnicfchmaster.schedule_master;
SQL;
    $scheduleResults = executeGenericSQL($sql);
    /// /////////////////////////////////////////////////
    /// TRUNCATE SCHEDULE
    $sql = <<<SQL

\t\ttruncate table fanta66_joomla.fch_schedule_lastseason;
SQL;
    $scheduleTruncateResultsLY = executeGenericSQL($sql);
    /// /////////////////////////////////////////////////
    /// REBUILD SCHEDULE
    $sql = <<<SQL

\t\tinsert into fanta66_joomla.fch_schedule_lastseason
\t\t(dateStamp,visitor,home)
\t\tselect date_stamp,visitor,home from fanta66_zudnicfchmaster.schedule_lastseason;
SQL;
    $scheduleResultsLY = executeGenericSQL($sql);
    /// /////////////////////////////////////////////////
    /// TRUNCATE RESULTS
    $sql = <<<SQL

\t\tDELETE FROM fanta66_joomla.fch_consolidated_results where season = "{$season}";
SQL;
    $out .= "<HR><B>Truncate Results SQL: </B><br/>{$sql}<p/>\n";
    $truncateResultsResults = executeGenericSQL($sql);
    /// /////////////////////////////////////////////////
    /// REBUILD RESULTS - SKATERS
    $sql = <<<SQL

insert into fanta66_joomla.fch_consolidated_results
(dateStamp
,totalPoints
,season
,playerID
,schoolID
,opponent
,opponentDisplayName
,playerDisplayName
,position
,schoolDisplayName
,statLine
,dateLine
,atVs)

SELECT DISTINCT 
\t`fanta66_zudnicfchmaster`.`results_skaters`.`date_stamp` AS `date_stamp`

\t,`fanta66_zudnicfchmaster`.`results_skaters`.`total_points` AS `totalPoints`
\t,convert(CONCAT (
\t\t\tsubstr(`fanta66_zudnicfchmaster`.`results_skaters`.`season`, 1, 4)
\t\t\t,"-"
\t\t\t,substr(`fanta66_zudnicfchmaster`.`results_skaters`.`season`, 5, 4)
\t\t\t) using utf8) AS `season`
\t,replace(replace(replace(replace(replace(`fanta66_zudnicfchmaster`.`results_skaters`.`player`, "'", ""), "-", ""), ".", ""), " ", ""), "~", "") AS `playerID`
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_id` AS `schoolID`
\t,

IF (
\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_skaters`.`visitor`)
\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`home`
\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`visitor`
\t\t) AS `opponent`
\t,`fanta66_zudnicfchmaster`.`school_master`.`school_shortname` AS `opponentDisplayName`
\t,CONCAT (
\t\t`fanta66_zudnicfchmaster`.`player_master`.`first_name`
\t\t," "
\t\t,`fanta66_zudnicfchmaster`.`player_master`.`last_name`
\t\t,", "
\t\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name`
\t\t) AS `playerDisplayName`
\t,`fanta66_zudnicfchmaster`.`player_master`.`position` as position
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name` as schoolDisplayName

\t,CONCAT (
\t\tconvert(IF (
\t\t\t\t(1 > `fanta66_zudnicfchmaster`.`results_skaters`.`goals`)
\t\t\t\t,"0"
\t\t\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`goals`
\t\t\t\t) using latin1 )
\t\t\t," G, "
\t\t\t,convert(IF (
\t\t\t\t\t(1 > `fanta66_zudnicfchmaster`.`results_skaters`.`assists`)
\t\t\t\t\t,"0"
\t\t\t\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`assists`
\t\t\t\t\t) using latin1 )
\t\t\t\t," A "
\t\t\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`info` ) AS `statLine`
\t\t\t\t,CONCAT (convert(date_format(`fanta66_zudnicfchmaster`.`results_skaters`.`date_stamp`, "%c/%e") using latin1)) AS `dateLine`
\t\t\t\t,convert(IF (
\t\t\t\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_skaters`.`visitor`)
\t\t\t\t\t\t," at "
\t\t\t\t\t\t," vs "
\t\t\t\t\t\t) using latin1 ) AS `atVs` FROM (
\t\t\t\t\t(
\t\t\t\t\t\t`fanta66_zudnicfchmaster`.`results_skaters` JOIN `fanta66_zudnicfchmaster`.`player_master`
\t\t\t\t\t\t) JOIN `fanta66_zudnicfchmaster`.`school_master`
\t\t\t\t\t) WHERE (
\t\t\t\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`text_id` = `fanta66_zudnicfchmaster`.`results_skaters`.`player`)
\t\t\t\t\t\tAND (
\t\t\t\t\t\t\t`fanta66_zudnicfchmaster`.`school_master`.`school_id` = IF (
\t\t\t\t\t\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_skaters`.`visitor`)
\t\t\t\t\t\t\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`home`
\t\t\t\t\t\t\t\t,`fanta66_zudnicfchmaster`.`results_skaters`.`visitor`
\t\t\t\t\t\t\t\t) ) ) ORDER BY `fanta66_zudnicfchmaster`.`results_skaters`.`date_stamp` DESC;
SQL;
    $skaterInsertResults = executeGenericSQL($sql);
    $out .= "<HR><B>Skater Results SQL: </B><br/>{$sql}<p/>\n";
    /////////////////////////////////////////
    // GOALIE RESULTS
    $sql = <<<SQL

\tinsert into fanta66_joomla.fch_consolidated_results
(dateStamp
,totalPoints
,season
,playerID
,schoolID
,opponent
,opponentDisplayName
,playerDisplayName
,position
,schoolDisplayName
,statLine
,dateLine
,atVs)
\tSELECT DISTINCT `fanta66_zudnicfchmaster`.`results_goalies`.`date_stamp` AS `date_stamp`
\t,`fanta66_zudnicfchmaster`.`results_goalies`.`points` AS `total_points`
\t,convert(CONCAT (
\t\t\tsubstr(`fanta66_zudnicfchmaster`.`results_goalies`.`season`, 1, 4)
\t\t\t,"-"
\t\t\t,substr(`fanta66_zudnicfchmaster`.`results_goalies`.`season`, 5, 4)
\t\t\t) using utf8) AS `season`
\t,replace(replace(replace(replace(replace(`fanta66_zudnicfchmaster`.`results_goalies`.`player`, "'", ""), "-", ""), ".", ""), " ", ""), "~", "") AS `playerID`
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_id` AS `schoolID`
\t

,IF (
\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_goalies`.`visitor`)
\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`home`
\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`visitor`
\t\t) AS `opponent`
\t,`fanta66_zudnicfchmaster`.`school_master`.`school_shortname` AS `opponentDisplayName`
\t,CONCAT (
\t\t`fanta66_zudnicfchmaster`.`player_master`.`first_name`
\t\t," "
\t\t,`fanta66_zudnicfchmaster`.`player_master`.`last_name`
\t\t, ", "
\t\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name`

\t\t) AS `playerDisplayName`
\t,`fanta66_zudnicfchmaster`.`player_master`.`position` AS position
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name` as schoolDisplayName
\t,CONCAT (
\t\t`fanta66_zudnicfchmaster`.`results_goalies`.`goals_allowed`
\t\t," GA, "
\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`saves`
\t\t," saves, "
\t\t,round(`fanta66_zudnicfchmaster`.`results_goalies`.`gaa`, 3)
\t\t," GAA, "
\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`decision`
\t\t) AS `statLine`
\t,CONCAT (convert(date_format(`fanta66_zudnicfchmaster`.`results_goalies`.`date_stamp`, "%c/%e") using latin1)) AS `dateLine`
\t,convert(IF (
\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_goalies`.`visitor`)
\t\t\t," at "
\t\t\t," vs "
\t\t\t) using latin1 ) AS `atVs` FROM (
\t\t(
\t\t\t`fanta66_zudnicfchmaster`.`results_goalies` JOIN `fanta66_zudnicfchmaster`.`player_master`
\t\t\t) JOIN `fanta66_zudnicfchmaster`.`school_master`
\t\t) WHERE (
\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`text_id` = `fanta66_zudnicfchmaster`.`results_goalies`.`player`)
\t\t\tAND (
\t\t\t\t`fanta66_zudnicfchmaster`.`school_master`.`school_id` = IF (
\t\t\t\t\t(`fanta66_zudnicfchmaster`.`player_master`.`school_id` = `fanta66_zudnicfchmaster`.`results_goalies`.`visitor`)
\t\t\t\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`home`
\t\t\t\t\t,`fanta66_zudnicfchmaster`.`results_goalies`.`visitor`
\t\t\t\t\t) ) ) ORDER BY `fanta66_zudnicfchmaster`.`results_goalies`.`date_stamp` DESC;
SQL;
    $goalieInsertResults = executeGenericSQL($sql);
    $out .= "<HR><B>Goalie Results SQL: </B><br/>{$sql}<p/>\n";
    $out .= "Skater Results Insert: " . $skaterInsertResults["msg"] . "\n<br/>";
    $out .= "Goalie Results Insert: " . $goalieInsertResults["msg"] . "\n<br/>";
    $playerTruncateResults = executeGenericSQL("truncate table fanta66_joomla.fch_players");
    $out .= "Player Truncate: " . $playerTruncateResults["msg"] . "\n";
    $sql = <<<SQL

\tinsert into fanta66_joomla.fch_players
(playerID, lastName, firstName, schoolDisplayName, schoolID, playerYear, position, season)
select distinct
\treplace(replace(replace(replace(replace(`fanta66_zudnicfchmaster`.`player_master`.`text_id`, "'", ""), "-", ""), ".", ""), " ", ""), "~", "") AS `playerID`
\t,`fanta66_zudnicfchmaster`.`player_master`.`last_name` as lastName
\t,`fanta66_zudnicfchmaster`.`player_master`.`first_name` as firstName
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name` as schoolDisplayName
\t, `fanta66_zudnicfchmaster`.`player_master`.`school_id` as schoolID
\t, `fanta66_zudnicfchmaster`.`player_master`.`year_in_school` as playerYear
\t, `fanta66_zudnicfchmaster`.`player_master`.`position` as position
\t, convert(CONCAT (
\t\t\tsubstr(`fanta66_zudnicfchmaster`.`player_master`.`season`, 1, 4)
\t\t\t,"-"
\t\t\t,substr(`fanta66_zudnicfchmaster`.`player_master`.`season`, 5, 4)
\t\t\t) using utf8) AS `season`
\tfrom `fanta66_zudnicfchmaster`.`player_master`
\twhere `fanta66_zudnicfchmaster`.`player_master`.`test` = "prod"
SQL;
    $sql .= " and season = \"" . preg_replace("/[^A-Za-z0-9 ]/", '', getSeason()) . "\";";
    $playerInsertResults = executeGenericSQL($sql);
    $out .= "Player refresh insert: " . $playerInsertResults["msg"] . "\n";
    echo "Full Insert playerInsertResults: {$sql}<p>\n\n";
    $lastSeason = preg_replace("/[^A-Za-z0-9 ]/", '', getLastSeason());
    $season = getSeason();
    $sql = <<<SQL

\tinsert into fanta66_joomla.fch_players
(playerID, lastName, firstName, schoolDisplayName, schoolID, playerYear, position, season)
select distinct
\treplace(replace(replace(replace(replace(`fanta66_zudnicfchmaster`.`player_master`.`text_id`, "'", ""), "-", ""), ".", ""), " ", ""), "~", "") AS `playerID`
\t,`fanta66_zudnicfchmaster`.`player_master`.`last_name` as lastName
\t,`fanta66_zudnicfchmaster`.`player_master`.`first_name` as firstName
\t,`fanta66_zudnicfchmaster`.`player_master`.`school_name` as schoolDisplayName
\t, `fanta66_zudnicfchmaster`.`player_master`.`school_id` as schoolID
\t, "?" as playerYear
\t, `fanta66_zudnicfchmaster`.`player_master`.`position` as position
\t, {$season} AS `season`
\tfrom `fanta66_zudnicfchmaster`.`player_master`
\twhere `fanta66_zudnicfchmaster`.`player_master`.`year_in_school` != "Sr" 
SQL;
    $sql .= " and `fanta66_zudnicfchmaster`.`player_master`.`season` = \"{$lastSeason}\" ";
    $sql .= " and `fanta66_zudnicfchmaster`.`player_master`.`school_id` not in (select distinct schoolID from fanta66_joomla.fch_players where season = \"" . getSeason() . "\");";
    SQL;
    echo "Delta Insert: {$sql}<BR>\n\n";
    $deltaInsertResults = executeGenericSQL($sql);
    $out .= "Player delta insert: " . $deltaInsertResults["msg"] . "\n";
    $deltaUpdateResults = executeGenericSQL("UPDATE fch_players SET season = \"" . getSeason() . "\", playerYear = NULL where playerYear = \"?\"");
    $out .= "Player delta update: " . $deltaInsertResults["msg"] . "\n";
    // Draft times are in eastern time. No offset.
    $sql = "SELECT u.email as email, DATE_FORMAT(l.draftTime ,'%W, %b %d %h:%i %p, Eastern time') as draftTime, l.id as id\r\r\n\tFROM fch_users u, fch_leagues l, fch_league_membership lm\r\r\n\tWHERE l.draftTime > DATE_ADD( NOW( ) , INTERVAL +24 HOUR ) \r\r\n\tAND l.draftTime < DATE_ADD( NOW( ) , INTERVAL +48 HOUR ) \r\r\n\tAND lm.leagueID = l.id\r\r\n\tAND l.draftReminderSent != TRUE\r\r\n\tAND lm.userID = u.id";
    /*
    $sql = "
    SELECT u.email AS email, DATE_FORMAT( l.draftTime,  '%W, %b %d %h:%i %p, Eastern time' ), l.id AS draftTime
    FROM fch_users u, fch_leagues l, fch_league_membership lm
    WHERE lm.leagueID = l.id
    AND lm.userID = u.id
    AND l.id =35";
    */
    $con = initializeDB();
    $result = $con->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $body = "Hi,\n";
            $body .= "This is a reminder that your Fantasy College Hockey draft is tomorrow, " . $row[draftTime] . ".\n\n";
            $body .= "The draft is live on fantasycollegehockey.com. The draft is available in the League menu and includes a live chat.\n\n";
            $body .= "One hour prior to draft time, the system will close new registrations to the league and generate the draft order. The order will be e-mailed to you.\n\n";
            $body .= "Your league administrator will need to open the draft. When the draft is over, the administrator will close the draft and your drafted players will be moved onto your roster.\n\n";
            $body .= "** DO NOT REPLY ** to this email. Please contact us at fantasycollegehockey@gmail.com if you have questions.\n\n";
            $body .= "Have fun!\n";
            $updateDraftReminder = "UPDATE fch_leagues SET draftReminderSent = TRUE WHERE id = " . $row[id];
            $out .= "<HR>Draft Reminder, SQL: {$updateDraftReminder}";
            executeGenericSQL($updateDraftReminder);
            sendEmail($row[email], "Reminder: Your Draft Is Tomorrow", $body);
            $emailSentTo .= $row[email] . ", ";
        }
    }
    $truncateByTeamResult = executeGenericSql("DELETE FROM fanta66_joomla.fch_schedule_byteam");
    $out .= "<hr/>Delete all from fch_scheduleByTeam: " . $truncateByTeamResult["msg"];
    $sql = "\r\r\n\t\tinsert into fch_schedule_byteam (id,dateStamp,schoolID,atVs,opponent) \r\r\n\t\t(select NULL, dateStamp, home, \"vs\" , visitor  from fch_schedule)\r\r\n\t\tunion all\r\r\n\t\t(select NULL, dateStamp, visitor as schoolID, \"at\" as atVs, home as opponent from fch_schedule)";
    $rebuildByTeamResult = executeGenericSql($sql);
    $out .= "<hr>Rebuild fch_scheduleByTeam: <p/>\n{$sql}<p/>\nResult:" . $rebuildByTeamResult["msg"];
    closeDB($con);
    $out .= "<p/>\n\tEmail sent to: {$emailSentTo}";
    sendEmail("*****@*****.**", "FCH Cron Results", $out);
    echo $out;
}
}
// If we did a POST, purge GET.
if (strlen($_POST["action"]) > 0) {
    $_GET = null;
}
////////// GET
if ($_GET["action"] == "rescind") {
    $r = executeGenericSQL("UPDATE fch_draft SET playerID = \"WithdrawnPick\" , position = \"X\" , status = \"W\" WHERE leagueID = " . $_GET[leagueID] . " AND overallPick = " . intval($_GET[pickID]));
    if ($r["status"] == 1) {
        echo displayResults(getReturnCode(1, "Your pick was successfully withdrawn."));
    } else {
        echo displayResults(getReturnCode(0, "There has been an error and your pick was not withdrawn."));
    }
}
if ($_GET["action"] == "closeDraft") {
    executeGenericSql("UPDATE fch_leagues SET draftStatus = \"closed\" WHERE id = {$leagueID} and season = \"{$season}\"");
    closeDraft($leagueID);
    echo displayResults(getReturnCode(1, "The draft is officially closed."));
}
if (selectCount("fch_players", " WHERE playerID = \"WithdrawnPick\"") == 0) {
    executeGenericSQL("INSERT INTO fch_players SET playerID = \"WithdrawnPick\", schoolID = \"pc\" , season = \"{$season}\"");
}
if (selectCount("fch_players", " WHERE playerID = \"PassedPick\"") == 0) {
    executeGenericSQL("INSERT INTO fch_players SET playerID = \"PassedPick\", schoolID = \"pc\" , season = \"{$season}\"");
}
$leagueAdmin = getSingleton("fch_leagues", "admin_user", " WHERE id = {$leagueID} and season = \"{$season}\"");
// Is user the admin?
if (intval($leagueAdmin) == intval($userID)) {
    $userIsAdmin = true;
} else {
    $userIsAdmin = false;