$isDraftOpen = false;
} else {
    $isDraftOpen = true;
}
if (!$isDraftOpen && $draftOpenStatus != "closed") {
    echo "Your administrator has not yet opened the draft. {$tryagain}";
    return;
}
if ($draftOpenStatus == "closed") {
    echo "The draft is now closed. {$tryagain}";
    return;
}
$playerQuery = $_GET['q'];
$limit_f = getPositionLimit("F", "a", $leagueID) + getPositionLimit("F", "b", $leagueID);
$limit_d = getPositionLimit("D", "a", $leagueID) + getPositionLimit("D", "b", $leagueID);
$limit_g = getPositionLimit("G", "a", $leagueID) + getPositionLimit("G", "b", $leagueID);
debug("Limits {$limit_f} {$limit_d} {$limit_g}");
$roster_f = selectCount("fch_draft", " Where   leagueID = {$leagueID} and userID = {$userID} and position = \"F\" ");
//$roster_f+=selectCount("fch_draft_customplayer"," Where   leagueID = $leagueID and userID = $userID and position = \"F\" ");
$roster_d = selectCount("fch_draft", " Where   leagueID = {$leagueID} and userID = {$userID} and position = \"D\" ");
//$roster_d+=selectCount("fch_draft_customplayer"," Where   leagueID = $leagueID and userID = $userID and position = \"D\" ");
$roster_g = selectCount("fch_draft", " Where   leagueID = {$leagueID} and userID = {$userID} and position = \"G\" ");
//$roster_g+=selectCount("fch_draft_customplayer"," Where   leagueID = $leagueID and userID = $userID and position = \"G\" ");
//echo "Looking for $playerQuery";
$sql = <<<SQL
SELECT CONCAT( firstName,  " ", lastName,  ", ", schoolDisplayName ) as playerDisplayName, playerID as playerID, position, 
if (playerID  in (select playerID as dPlayerID from fch_draft where leagueID = {$leagueID} ),true,false) as taken
FROM  `fch_players` 
where 
CONCAT( firstName,  " ", lastName ) like "%{$playerQuery}%"
AND playerID not in (select playerID from fch_players_leftearly)
require 'fch-lib.php';
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
$mainframe = JFactory::getApplication('site');
$season = getSeason();
$userID = getUserID();
$leagueID = leagueHandler($_POST, $userID);
$playerQuery = $_GET['q'];
$limit_f_a = getPositionLimit("F", "a", $leagueID);
$limit_f_b = getPositionLimit("F", "b", $leagueID);
$limit_d_a = getPositionLimit("D", "a", $leagueID);
$limit_d_b = getPositionLimit("D", "b", $leagueID);
$limit_g_a = getPositionLimit("G", "a", $leagueID);
$limit_g_b = getPositionLimit("G", "b", $leagueID);
$roster_f_a = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"F\" and statusCode = \"act\" and season = \"{$season}\"");
$roster_f_b = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"F\" and statusCode = \"ben\" and season = \"{$season}\"");
$roster_d_a = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"D\" and statusCode = \"act\" and season = \"{$season}\"");
$roster_d_b = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"D\" and statusCode = \"ben\" and season = \"{$season}\"");
$roster_g_a = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"G\" and statusCode = \"act\" and season = \"{$season}\"");
$roster_g_b = selectCount("fch_rosters", " Where userID = {$userID} and leagueID = {$leagueID} and releaseDate is  null and position = \"G\" and statusCode = \"ben\" and season = \"{$season}\"");
//echo "Looking for $playerQuery";
$sql = <<<SQL

SELECT CONCAT( firstName,  " ", lastName,  ", ", schoolDisplayName ) as playerDisplayName, playerID as playerID, position, IF( schoolID
IN (
SELECT schoolid
FROM v_blacklist
),  true,  false ) as blacklist,
if (playerID  in (select playerID from fch_rosters where leagueID = {$leagueID} and  releaseDate is  NULL and season = "{$season}" ),true,false) as taken
function draftPlayer($userID, $leagueID, $playerID, $position, $transactionID, $passedInStatus)
{
    $season = getSeason();
    debug("passed in status {$passedInStatus}");
    if (!okToTransact($transactionID)) {
        return getReturnCode(0, "This transaction has already been completed. Please do not use the refresh button.");
    }
    // If PassedPick, skip validation and just insert 'em
    if ($playerID == "PassedPick") {
        $status = "P";
        $overallPick = getSingleton("fch_draft", "max(overallPick)", " where leagueID = {$leagueID}") + 1;
    } else {
        /////////// IS NOT PASSED PICK
        $status = $passedInStatus;
        $check = selectCount("fch_draft", " where leagueID = {$leagueID} and playerID = \"{$playerID}\"");
        $r = array();
        if ($check > 0) {
            // 0 is available
            $r["status"] = 0;
            $r["msg"] = "Error: Player has already been drafted.";
            return $r;
        }
        $rosterCount = selectCount("fch_draft", " Where userID = {$userID} and position = \"{$position}\" and leagueID = {$leagueID}");
        $limit = getPositionLimit($position, "b", $leagueID) + getPositionLimit($position, "a", $leagueID);
        debug("Roster Slot calc: {$rosterCount} >= {$limit}");
        if ($rosterCount >= $limit) {
            debug("no slots, returning failure");
            $r["status"] = 0;
            $r["msg"] = "You have already drafted a full complient at this position.";
            return $r;
        }
        debug("passed validation, inserting");
        $overallPick = getSingleton("fch_draft", "max(overallPick)", " where leagueID = {$leagueID}") + 1;
        $passedPick = getSingleton("fch_draft", "overallPick", " WHERE leagueID = {$leagueID} and userID = {$userID} and status = \"P\" ORDER BY overallPick DESC Limit 0,1");
        if ($passedPick != "") {
            $overallPick = $passedPick;
            debug("Replacing Passed Pick at Overall pick # {$passedPick}");
            $replace = "  We have replaced your previously passed pick at overall pick # {$overallPick}.";
            executeGenericSQL("DELETE FROM fch_draft WHERE leagueID = {$leagueID} and userID = {$userID} and status = \"P\" and overallPick = {$overallPick}");
        } else {
            $withdrawnPick = getSingleton("fch_draft", "overallPick", " WHERE leagueID = {$leagueID} and userID = {$userID} and status = \"W\" ORDER BY overallPick DESC Limit 0,1");
            if ($withdrawnPick != "") {
                $overallPick = $withdrawnPick;
                debug("Replaceing Withdrawn Pick at Overall pick # {$withdrawnPick}");
                $replace = "  We have replaced your previously withdrawn pick at overall pick # {$overallPick}.";
                $delete = "DELETE FROM fch_draft WHERE leagueID = {$leagueID} and userID = {$userID} and status = \"W\" and overallPick = {$overallPick}";
                debug("DELETE " + $delete);
                executeGenericSQL($delete);
            }
        }
    }
    $sql = <<<SQL

\tINSERT INTO  fanta66_joomla.fch_draft (
\t\tid ,
\t\tleagueID ,
\t\tuserID ,
\t\tplayerID,
\t\tposition,
\t\tdateStamp,
\t\toverallPick,
\t\tstatus

\t\t)
\t\tVALUES (
\t\t\tNULL ,  {$leagueID},  {$userID}, "{$playerID}",  "{$position}", DATE_ADD(CURRENT_TIMESTAMP, INTERVAL +3 HOUR), {$overallPick}, "{$status}"
\t\t);
\t\t
SQL;
    debug($sql);
    $rosterID = executeGenericInsertSQL($sql);
    $r["status"] = 1;
    $r["msg"] = "Your transaction was successful. {$replace}";
    $r["id"] = $rosterID;
    setTransactionComplete($transactionID);
    return $r;
}