<?php
// NFL ~ Current week - last game as tie breaker
// last updated: Sep 09 9:15pm ~ 2014

if (!defined('SMF'))
	die('Hacking attempt...');

function nfl_football_pool()
{
	global $user_info, $scripturl, $smcFunc, $sourcedir, $context, $modSettings, $settings, $txt;
	require_once($sourcedir . '/Subs-Package.php');
	db_extend('packages');

	if (empty($modSettings['nfl_installed']))
		installNflTables();

	$games = nfl_gameYear();
	$lastGamesArray = $games[0];
	$gamesTotals = $games[1];
	$feed = 'http://www.nfl.com/liveupdate/scorestrip/scorestrip.json';
	$json = fetch_web_data($feed) ? fetch_web_data(utf8_encode($feed)) : false;
	$json = preg_replace(array('~,,~', '~,,~', '~,,~'), array(',"",', ',"",', ',"",'), $json);
	$html = json_decode($json, true);
	$html = !empty($html['ss']) ? $html['ss'] : array();
	$gameData = array('day', 'time', 'type', 'a', 'away', 'away_score', 'home', 'home_score', 'b', 'c', 'id', 'd', 'code', 'year');
	$currentPage = currentPageNflPickem();
	list($userName, $userNow, $data, $nfl_games, $all_nfl_games, $picks, $user_picks, $weekArray, $spreads, $games, $nfl_pick, $finals, $gameCount, $allGameCount) = array('Invalid User ID', 'Invalid User Id', array(), array(), array(), array(), array(), array(), '', '', 0, 0, 0, 0);

	if (!empty($html))
	{
		$checkWeek = !empty($html[0]['12']) ? getInt($html[0]['12']) : 1;
		$_SESSION['nfl_otherUser'] = !empty($_SESSION['nfl_otherUser']) ? (int)$_SESSION['nfl_otherUser'] : $user_info['id'];
		$user = !empty($_REQUEST['member']) ? (int)$_REQUEST['member'] : ($_SESSION['nfl_otherUser'] != $user_info['id'] ? $_SESSION['nfl_otherUser'] : $user_info['id']);
		$_SESSION['nfl_otherUser'] = $user;
		$allowedEdit = $user == $user_info['id'] || $user_info['is_admin'] ? true : false;
		$currentPage = stripos($currentPage, 'member=' . $user) === false ? $currentPage .= ';member=' . $user: $currentPage;
		$placement = $lastGamesArray[$checkWeek][2];
		$cutoff = $lastGamesArray[$checkWeek][$placement+4];
		$gameCutOff = !empty($checkWeek) ? $lastGamesArray[$checkWeek][2] : 1;
		$allowedView = time() - $cutoff == abs(time() - $cutoff) || $user_info['is_admin'] ? true : false;
		$error = !empty($_REQUEST['error']) ? $_REQUEST['error'] : false;
		$original = $html;
		$context['nfl_mode'] = '';

		if ((!empty($gameCutOff)) && count($html) > $gameCutOff)
		{
			for ($i=0; $i<$gameCutOff; $i++)
				array_shift($html);
		}

		foreach ($html as $key => $game)
		{
			for ($i=0; $i <=13; $i++)
				$nfl_games[$gameCount][$gameData[$i]] = !empty($game[$i]) ? $game[$i] : '';

			$gameCount++;
		}
		foreach ($original as $key => $game)
		{
			for ($i=0; $i <=13; $i++)
			{
				$all_nfl_games[$allGameCount][$gameData[$i]] = !empty($game[$i]) ? $game[$i] : '';
				if ($gameData[$i] === 'type' && stripos($all_nfl_games[$allGameCount][$gameData[$i]], 'final') !== false)
					$finals++;
			}

			$allGameCount++;
		}
		$week_final = $nfl_games[count($nfl_games) - 1];
		$actualTotal = (is_numeric($week_final['away_score']) || is_numeric($week_final['home_score'])) ? (int)$week_final['home_score'] + (int)$week_final['away_score'] : '??';

		if ($finals+1 >= $gamesTotals[$checkWeek])
		{
			$finals = 1;
			$now = time();
			$tiebreaker = (int)$actualTotal;
			foreach ($all_nfl_games as $gameData)
			{
				$finalScores['game' . $finals] = $all_nfl_games[$finals-1]['away'] . '_' . $all_nfl_games[$finals-1]['away_score'] . '@' . $all_nfl_games[$finals-1]['home'] . '_' . $all_nfl_games[$finals-1]['home_score'];
				$finals++;
			}
			for ($i=$finals; $i<=16; $i++)
				$finalScores['game' . $i] = '';

			$smcFunc['db_query']('', "
				DELETE FROM {db_prefix}nfl_finals
				WHERE week = {int:current}",
				array('current' => $checkWeek)
			);

			$smcFunc['db_insert']('insert', "
				{db_prefix}nfl_finals",
				array(
					'week' => 'int',
					'logged' => 'int',
					'tiebreaker' => 'int',
					'game1' => 'string',
					'game2' => 'string',
					'game3' => 'string',
					'game4' => 'string',
					'game5' => 'string',
					'game6' => 'string',
					'game7' => 'string',
					'game8' => 'string',
					'game9' => 'string',
					'game10' => 'string',
					'game11' => 'string',
					'game12' => 'string',
					'game13' => 'string',
					'game14' => 'string',
					'game15' => 'string',
					'game16' => 'string'
				),
				array(
					$checkWeek,
					$now,
					$tiebreaker,
					$finalScores['game1'],
					$finalScores['game2'],
					$finalScores['game3'],
					$finalScores['game4'],
					$finalScores['game5'],
					$finalScores['game6'],
					$finalScores['game7'],
					$finalScores['game8'],
					$finalScores['game9'],
					$finalScores['game10'],
					$finalScores['game11'],
					$finalScores['game12'],
					$finalScores['game13'],
					$finalScores['game14'],
					$finalScores['game15'],
					$finalScores['game16']
				),
				array('week')
			);
		}

		date_default_timezone_set('EST');
		$myTime = time();
		$time = $lastGamesArray[$checkWeek][3];
		$displayCutOff = date('D M d h:iA', $cutoff) . ' EST';
		$displayTime = date('D M d h:iA', $time) . ' EST';
		$notAllowed = time() - $cutoff >= 0 ? true : false;
		$currentWeekApi = getInt($week_final['code']);
		$currentMatchUp = $lastGamesArray[$checkWeek];
		$confirm =  (in_array($week_final['home'], $currentMatchUp) && in_array($week_final['away'], $currentMatchUp)) ? true : false;
		$checkQueryStatus = $user_info['is_admin'] || $notAllowed ? true : false;

		if (isset($_REQUEST['NflMember']) && isset($_REQUEST['submit']) && $checkQueryStatus)
		{
			$queryName = $smcFunc['htmlspecialchars']($_REQUEST['NflMember']);
			$result =  $smcFunc['db_query']('', '
				SELECT id_member, real_name, member_name
				FROM {db_prefix}members
				WHERE real_name = {string:member}
				LIMIT 1',
				array(
					'member' => $smcFunc['htmltrim']($queryName),
				)
			);

			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				list($_SESSION['nfl_otherUser'], $user) = array($row['id_member'], $row['id_member']);
				$userName = $row['real_name'];
				$userNow = !empty($row['real_name']) ? 'Editing week ' . $checkWeek . ' picks for ' . $row['real_name'] : (!empty($row['member_name']) ? $row['member_name'] : 'Invalid User ID');
			}

			$smcFunc['db_free_result']($result);

		}
		elseif ($user != $user_info['id'])
		{
			$result =  $smcFunc['db_query']('', '
				SELECT id_member, real_name, member_name
				FROM {db_prefix}members
				WHERE id_member = {int:member}
				LIMIT 1',
				array(
					'member' => $user,
				)
			);

			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				list($_SESSION['nfl_otherUser'], $user) = array($row['id_member'], $row['id_member']);
				$userName = $row['real_name'];
				$userNow = !empty($row['real_name']) ? 'Editing week ' . $checkWeek . ' picks for ' . $row['real_name'] : (!empty($row['member_name']) ? $row['member_name'] : 'Invalid User ID');
			}

			$smcFunc['db_free_result']($result);
		}
		else
		{
			list($_SESSION['nfl_otherUser'], $user) = array($user_info['id'], $user_info['id']);
			$userName = $user_info['name'];
			$userNow = 'Editing week ' . $checkWeek . ' picks for ' . $user_info['name'];
		}
	}

	if ((!empty($html)) && !$notAllowed && $confirm && stripos($week_final['code'], 'PRE') === false && $allowedEdit)
	{
		if (isset($_REQUEST['admin']))
			$_SESSION['nfl_admin'] = !empty($_SESSION['nfl_admin']) ? false : true;
		if (isset($_REQUEST['stats']))
			$_SESSION['nfl_stats'] = !empty($_SESSION['nfl_stats']) ? false : true;

		if (!$user_info['is_admin'])
			list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);

		$userNow = !empty($_SESSION['nfl_admin']) ? 'Editing spreads for week ' . $checkWeek : $userNow;
		if (isset($_REQUEST['points']) && isset($_REQUEST['submit']) && empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']))
		{
			list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
			$request = $smcFunc['db_query']('', "
				SELECT id_member
				FROM {db_prefix}nfl_weekly
				WHERE id_member = {int:member}
				LIMIT 1",
				array('member' => $user)
			);

			$row = $smcFunc['db_fetch_row']($request);
			for ($i=1; $i <=17; $i++)
				$weekArray['week' . $i] = !empty($row['week' . $i]) ? $row['week' . $i] : '';

			$smcFunc['db_free_result']($request);

			$current = !empty($weekArray['week' . $checkWeek]) ? array_pop(explode('_', $weekArray['week' . $checkWeek])) : array();
			$current = implode('_', $current);
			for ($i=1; $i< count($nfl_games)+1; $i++)
				$games .= !empty($_REQUEST['game_' . ($i-1)]) ? $_REQUEST['game_' . ($i-1)] . '_': (!empty($current) ? $current . '_' : '');

			$games .= !empty($_REQUEST['points']) ? abs((int)$_REQUEST['points']) : '';
			$weekArray['week' . $checkWeek] = trim($games, '_');

			$smcFunc['db_query']('', "
				DELETE FROM {db_prefix}nfl_weekly
				WHERE id_member = {int:member}",
				array('member' => $user, 'current' => $checkWeek)
			);

			$smcFunc['db_insert']('insert', "
				{db_prefix}nfl_weekly",
				array(
					'id_member' => 'int',
					'week1' => 'string',
					'week2' => 'string',
					'week3' => 'string',
					'week4' => 'string',
					'week5' => 'string',
					'week6' => 'string',
					'week7' => 'string',
					'week8' => 'string',
					'week9' => 'string',
					'week10' => 'string',
					'week11' => 'string',
					'week12' => 'string',
					'week13' => 'string',
					'week14' => 'string',
					'week15' => 'string',
					'week16' => 'string',
					'week17' => 'string'
				),
				array(
					$user,
					(!empty($weekArray['week1']) ? $weekArray['week1'] : ''),
					(!empty($weekArray['week2']) ? $weekArray['week2'] : ''),
					(!empty($weekArray['week3']) ? $weekArray['week3'] : ''),
					(!empty($weekArray['week4']) ? $weekArray['week4'] : ''),
					(!empty($weekArray['week5']) ? $weekArray['week5'] : ''),
					(!empty($weekArray['week6']) ? $weekArray['week6'] : ''),
					(!empty($weekArray['week7']) ? $weekArray['week7'] : ''),
					(!empty($weekArray['week8']) ? $weekArray['week8'] : ''),
					(!empty($weekArray['week9']) ? $weekArray['week9'] : ''),
					(!empty($weekArray['week10']) ? $weekArray['week10'] : ''),
					(!empty($weekArray['week11']) ? $weekArray['week11'] : ''),
					(!empty($weekArray['week12']) ? $weekArray['week12'] : ''),
					(!empty($weekArray['week13']) ? $weekArray['week13'] : ''),
					(!empty($weekArray['week14']) ? $weekArray['week14'] : ''),
					(!empty($weekArray['week15']) ? $weekArray['week15'] : ''),
					(!empty($weekArray['week16']) ? $weekArray['week16'] : ''),
					(!empty($weekArray['week17']) ? $weekArray['week17'] : '')
				),
				array('id_member')
			);
		}
		elseif ($user_info['is_admin'] && isset($_REQUEST['submit']) && !empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']))
		{
			$_SESSION['nfl_stats'] = false;
			for ($i=0; $i<count($html); $i++)
			{
				$team = !empty($_REQUEST['game_' . $i]) ? $_REQUEST['game_' . $i] : $html[$i+1]['home'];
				$spreads .= $team . (!empty($_REQUEST['spread_' . $i]) ? $_REQUEST['spread_' . $i] : '0.5') . '_';
			}

			$spreads = trim($spreads, '_');
			$logged = time();

			$smcFunc['db_query']('', "
				DELETE FROM {db_prefix}nfl_tiebreakers
				WHERE week = {int:week}",
				array('week' => $checkWeek)
			);

			$smcFunc['db_insert']('insert', "
				{db_prefix}nfl_tiebreakers",
				array(
					'week' => 'int',
					'logged' => 'int',
					'home' => 'string',
					'away' => 'string',
					'picks' => 'string'
				),
				array(
					$checkWeek,
					$logged,
					(!empty($week_final['home']) ? $week_final['home'] : ''),
					(!empty($week_final['away']) ? $week_final['away'] : ''),
					$spreads
				),
				array('week')
			);
		}

		if ($user_info['is_admin'] && !empty($_SESSION['nfl_stats']))
		{
			$_SESSION['nfl_stats'] = true;
			$_SESSION['nfl_admin'] = false;
			nfl_user_stats($currentPage, $checkWeek);
		}
		else
		{
			$request = $smcFunc['db_query']('', "
				SELECT n.week, w.id_member, n.logged, n.home, n.away, n.picks, {raw:week_num}
				FROM {db_prefix}nfl_tiebreakers as n
				LEFT JOIN {db_prefix}nfl_weekly as w ON (w.id_member = {int:member})
				WHERE week = {int:week}
				LIMIT 1",
				array('week' => $checkWeek, 'member' => $user, 'week_num' => 'w.week' . $checkWeek)
			);

			while ($row = $smcFunc['db_fetch_assoc']($request))
			{
				$data = array(
					'week' => !empty($row['week']) ? (int)$row['week'] : 0,
					'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
					'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
					'home' => !empty($row['home']) ? $row['home'] : '',
					'away' => !empty($row['away']) ? $row['away'] : '',
					'picks' => !empty($row['picks']) ? $row['picks'] : '',
				);

				$picks = !empty($row['picks']) ? explode('_', $row['picks']) : array();
				$user_picks = !empty($row['week' . $checkWeek]) ? explode('_', $row['week' . $checkWeek]) : array();
				$context['nfl_user_points'] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
				array_pop($user_picks);
			}
			$smcFunc['db_free_result']($request);

			if ((!empty($gameCutOff)) && count($picks) > count($html))
			{
				for ($i=count($picks); $i>count($html); $i--)
					array_shift($picks);
			}

			if ((!empty($gameCutOff)) && count($user_picks) > count($html))
			{
				for ($i=count($user_picks); $i>count($html); $i--)
					array_shift($user_picks);
			}

			foreach ($picks as $pickem)
			{
				$context['spread_' . $nfl_pick] = abs((int)getInt($pickem)) . '.5';
				$pickem = trim(str_replace(range(0,9), '', $pickem), '.');
				$context['game_' . $nfl_pick] = (!empty($pickem)) && $pickem === trim($nfl_games[$nfl_pick]['home']) ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
				$nfl_pick++;
			}
			$nfl_pick = 0;
			foreach ($user_picks as $user_pickem)
			{
				$context['user_game_' . $nfl_pick] = (!empty($user_pickem)) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
				$nfl_pick++;
			}

			echo '
<script type="text/javascript"><!-- // --><![CDATA[
	function NFL_confirmSubmit()
	{
		var agree=confirm("Do you confirm this action?");
		if (agree)
			return true ;
		else
			return false ;
	}
// ]]></script>
<form id="nfl_pick" name="nfl_pick" action="' . $currentPage . '" method="post">
	<div class="windowbg">
		<div class="cat_bar">
			<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
		</div>
		<table width="100%">
			<tr>
				<td class="centertext titlebg" colspan="5">
					Week ', (int)$currentWeekApi, '<br />
				</td>
			</tr>
			<tr>
				<td class="centertext titlebg" colspan="5">
					', $userNow, '<br />
				</td>
			</tr>
			', $user_info['is_admin'] && empty($_SESSION['nfl_admin']) ? '
			<tr>
				<td class="centertext titlebg" colspan="5">
					Game Mode<br />
				</td>
			</tr>' : ($user_info['is_admin'] && !empty($_SESSION['nfl_admin']) ? '
			<tr>
				<td class="centertext titlebg" colspan="5">
					Admin Mode<br />
				</td>
			</tr>' : ''), '
			<tr>
				<td class="centertext" style="padding:8px 8px 0px 8px;">AWAY</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">HOME</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">DATE</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">Point Spread</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">Select Team</td>
			</tr>
			<tr>
				<td colspan="5">&nbsp;</td>
			</tr>';
			foreach ($nfl_games as $key => $game)
			{
				$gameTime = date('D M d h:iA', (strtotime($game['day'] . ', ' . $game['time'] . 'pm'))) . ' EST';
				$windowbg = (!empty($windowbg)) && $windowbg === 'windowbg' ? 'windowbg2' : 'windowbg';
				echo '
			<tr class="' . $windowbg . '">
				<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['away'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['away'], '</td>
				<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['home'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['home'], '</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">', $gameTime, '</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">
					', !empty($_SESSION['nfl_admin']) ? '
					<input name="spread_' . $key . '" type="number" maxlength="4" size="3" min="0" max="999" step="0.5" value="' . (isset($context['spread_' . $key]) ? abs((int)$context['spread_' . $key]) + 0.5 : 0.5) . '" />' : '
					<span>' . (isset($context['spread_' . $key]) ? abs((int)$context['spread_' . $key]) + 0.5 : 0.5) . '</span>', '
				</td>
				<td class="centertext" style="padding:8px 8px 0px 8px;">
					<select name="game_' . $key . '">
						<option ', ($user_info['is_guest'] ? 'disabled ' : ''), 'value="' . $game['away'] . '" ', (empty($_SESSION['nfl_admin']) && !empty($context['user_game_' . $key]) && $context['user_game_' . $key] === $game['away'] ? 'selected' : (!empty($_SESSION['nfl_admin']) && !empty($context['game_' . $key]) && $context['game_' . $key] === $game['away'] ? 'selected' : '')), '>', $game['away'], '</option>
						<option ', ($user_info['is_guest'] ? 'disabled ' : ''), 'value="' . $game['home'] . '" ', (empty($_SESSION['nfl_admin']) && !empty($context['user_game_' . $key]) && $context['user_game_' . $key] === $game['home'] ? 'selected' : (!empty($_SESSION['nfl_admin']) && !empty($context['game_' . $key]) && $context['game_' . $key] === $game['home'] ? 'selected' : '')), '>', $game['home'], '</option>
					</select>
				</td>
			</tr>';
			}
			echo '
		</table>
		<div class="windowbg"><br /></div>
		<table width="100%">
			<tr>
				<td class="centertext titlebg" colspan="7">
				Tie Breaker<br />
				</td>
			</tr>
			<tr style="padding:3px;">
				<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
				<td style="padding:0px 8px 0px 8px;">Game Date</td>
				<td style="padding:0px 8px 0px 8px;">Away</td>
				<td style="padding:0px 8px 0px 8px;">Home</td>
				<td style="padding:0px 8px 0px 8px;">Username</td>
				<td style="padding:0px 8px 0px 8px;">Points Total</td>
				<td colspan="2">Status</td>
			</tr>
			<tr style="padding:3px;">
				<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
				<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
				<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
				<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
				<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user . '">', $userName, '</a></td>
				<td style="padding:0px 8px 0px 8px;" class="left">
					<input ', (!empty($_SESSION['nfl_admin']) ? 'disabled ' : ''), 'name="points" type="number" maxlength="4" size="3" min="0" max="999" value="', (isset($context['nfl_user_points']) ? (int)$context['nfl_user_points'] : 0), '" />
					', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
				</td>
				<td>
					', (isset($context['nfl_user_points']) && empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']) ? '<span style="color:green;">SAVED' : (empty($_SESSION['nfl_admin']) && empty($_SESSION['nfl_stats']) ? '<span style="color:red;">Pending' : (!empty($_SESSION['nfl_stats']) ? 'Stats Mode' : 'Admin Mode'))), '</span>
				</td>
			</tr>
			<tr>
				<td style="border:0px;" colspan="7"><hr /><br /></td>
			</tr>
			<tr>
				<td colspan="7">
					', (!empty($error) ? '<span class="error">Warning: You are not permitted to view other user picks until after the cut-off time.</span><br /><br />' : '&nbsp;'), '
				</td>
			</tr>
			<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
				<td>
					<input class="submit" type="submit" name="submit" value="Submit" onclick="var confirm = NFL_confirmSubmit(); if (confirm) {this.form.submit();} else {return false;}" />
				</td>
				<td colspan="4">
					<div class="content">
						<strong>Search Member:</strong>
						<input type="text" name="NflMember" id="NflMember" value="" class="input_text" ' . (!empty($_SESSION['nfl_admin']) ? 'disabled ' : '') . '/>
						<div id="NflQueryUserContainer"></div>
					</div>
				</td>
				', empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="stats" value="View Overall" />
				</td>' : (!empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="stats" value="Exit Overall" />
				</td>' : '
				<td>
					&nbsp;
				</td>'), '
				', empty($_SESSION['nfl_admin']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="admin" value="Admin Mode" />
				</td>' : (!empty($_SESSION['nfl_admin']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="admin" value="Game Mode" />
				</td>' : '
				<td>
					&nbsp;
				</td>'), '
			</tr>
		</table>
	</div>
</form>
<script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/suggest.js?fin20"></script>
<script type="text/javascript"><!-- // --><![CDATA[
	var oAddMemberSuggest = new smc_AutoSuggest({
		sSelf: \'oAddMemberSuggest\',
		sSessionId: \'', $context['session_id'], '\',
		sSessionVar: \'', $context['session_var'], '\',
		sSuggestId: \'to_suggest\',
		sControlId: \'NflMember\',
		sSearchType: \'member\',
		sPostName: \'nfl_pick\',
		sURLMask: \'', !empty($_REQUEST['page']) ? 'page=' . $smcFunc['htmlspecialchars']($_REQUEST['page']) : (!empty($context['current_action']) ? 'action=' . $context['current_action'] : ''), ';member=%item_id%\',
		sTextDeleteItem: \'', $txt['autosuggest_delete_item'], '\',
		bItemList: false,
		sItemListContainerId: \'NflQueryUserContainer\'
	});
// ]]></script>';
		}
	}
	elseif (!empty($html) && $confirm && stripos($week_final['code'], 'PRE') === false && $allowedView)
	{
		if (isset($_REQUEST['stats']))
			$_SESSION['nfl_stats'] = !empty($_SESSION['nfl_stats']) ? false : true;

		if (!$user_info['is_admin'])
			list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
		$userNow = !empty($_SESSION['nfl_admin']) ? 'Viewing spreads for week ' . $checkWeek : str_replace('Editing', 'Viewing', $userNow);

		if ($user_info['is_admin'] && !empty($_SESSION['nfl_stats']))
		{
			$_SESSION['nfl_stats'] = true;
			$_SESSION['nfl_admin'] = false;
			nfl_user_stats($currentPage, $checkWeek);
		}
		else
		{
			list($_SESSION['nfl_admin'], $_SESSION['nfl_stats']) = array(false, false);
			$request = $smcFunc['db_query']('', "
				SELECT n.week, w.id_member, n.logged, n.home, n.away, n.picks, w.{raw:week_num}
				FROM {db_prefix}nfl_tiebreakers as n
				LEFT JOIN {db_prefix}nfl_weekly as w ON (w.id_member = {int:member})
				WHERE week = {int:week}
				LIMIT 1",
				array('week' => $checkWeek, 'member' => $user, 'week_num' => 'week' . (string)$checkWeek)
			);

			while ($row = $smcFunc['db_fetch_assoc']($request))
			{
				$data = array(
					'week' => !empty($row['week']) ? (int)$row['week'] : 0,
					'id_member' => !empty($row['id_member']) ? (int)$row['id_member'] : 0,
					'logged' => !empty($row['logged']) ? (int)$row['logged'] : 0,
					'home' => !empty($row['home']) ? $row['home'] : '',
					'away' => !empty($row['away']) ? $row['away'] : '',
					'picks' => !empty($row['picks']) ? $row['picks'] : '',
				);

				$picks = !empty($row['picks']) ? explode('_', $row['picks']) : array();
				$user_picks = !empty($row['week' . $checkWeek]) ? explode('_', $row['week' . $checkWeek]) : array();
				$context['nfl_user_points'] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
				array_pop($user_picks);
			}
			$smcFunc['db_free_result']($request);

			if ((!empty($gameCutOff)) && count($picks) > count($html))
			{
				for ($i=count($picks); $i>count($html); $i--)
					array_shift($picks);
			}

			if ((!empty($gameCutOff)) && count($user_picks) > count($html))
			{
				for ($i=count($user_picks); $i>count($html); $i--)
					array_shift($user_picks);
			}

			foreach ($picks as $pickem)
			{
				$context['spread_' . $nfl_pick] = abs((int)getInt($pickem)) . '.5';
				$pickem = trim(str_replace(range(0,9), '', $pickem), '.');
				$context['game_' . $nfl_pick] = (!empty($pickem)) && $pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
				$nfl_pick++;
			}
			$nfl_pick = 0;
			foreach ($user_picks as $user_pickem)
			{
				$context['user_game_' . $nfl_pick] = (!empty($user_pickem) && !empty($nfl_games[$nfl_pick]['home'])) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : (!empty($nfl_games[$nfl_pick]['away']) ? $nfl_games[$nfl_pick]['away'] : '');
				$nfl_pick++;
			}

			echo '
<script type="text/javascript"><!-- // --><![CDATA[
	function NFL_confirmSubmit()
	{
		var agree=confirm("Do you confirm this action?");
		if (agree)
			return true ;
		else
			return false ;
	}
// ]]></script>
<div style="display:inline;" class="windowbg">
	<div class="cat_bar">
		<h3 class="centertext catbg">NFL Pool Weekly Picks</h3>
	</div>
	<table width="100%">
		<tr>
			<td class="centertext titlebg" colspan="5">
				Week ', (int)$currentWeekApi, '<br />
			</td>
		</tr>
		<tr>
			<td class="centertext titlebg" colspan="5">
				', $userNow, '<br />
			</td>
		</tr>
		<tr>
			<td class="centertext" style="padding:8px 8px 0px 8px;">AWAY</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">HOME</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">DATE</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">Point Spread</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">Your Pick</td>
		</tr>
		<tr>
			<td colspan="5">&nbsp;</td>
		</tr>';
			foreach ($nfl_games as $key => $game)
			{
				$gameTime = date('D M d h:iA', (strtotime($game['day'] . ', ' . $game['time'] . 'pm'))) . ' EST';
				$windowbg = (!empty($windowbg)) && $windowbg === 'windowbg' ? 'windowbg2' : 'windowbg';
				$fu = (!empty($context['game_' . $key]) && !empty($context['user_game_' . $key])) && $context['game_' . $key] !== $context['user_game_' . $key] ? '<span style="float:right;">[U]</span>' : '<span style="float:right;">[F]</span>';
				echo '
		<tr class="' . $windowbg . '">
			<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['away'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['away'], '</td>
			<td style="padding:8px 8px 0px 8px;" ' . ((!empty($context['game_' . $key])) && $context['game_' . $key] === $game['home'] ? 'class="alert catbg2 centertext"' : 'class="centertext"') . '>', $game['home'], '</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">', $gameTime, '</td>
			<td class="centertext" style="padding:8px 8px 0px 8px;">', (!empty($context['spread_' . $key]) ? $context['spread_' . $key] : '0.5'), '</td>
			<td class="centertext alert" style="padding:8px 8px 0px 8px;">', (!empty($context['user_game_' . $key]) ? $context['user_game_' . $key] . ' ' . $fu: '???'), '</td>
		</tr>';
			}
			echo '
	</table>
	<div><br /></div>
	<table width="100%">
		<tr>
			<td class="centertext titlebg" colspan="7">
				Tie Breaker<br />
			</td>
		</tr>
		<tr style="padding:3px;">
			<td style="padding:0px 8px 0px 8px;">Cut-off Date</td>
			<td style="padding:0px 8px 0px 8px;">Game Date</td>
			<td style="padding:0px 8px 0px 8px;">Away</td>
			<td style="padding:0px 8px 0px 8px;">Home</td>
			<td style="padding:0px 8px 0px 8px;">Username</td>
			<td style="padding:0px 8px 0px 8px;">Points Total</td>
			<td colspan="2">Status</td>
		</tr>
		<tr style="padding:3px;">
			<td style="padding:0px 8px 0px 8px;">', $displayCutOff, '</td>
			<td style="padding:0px 8px 0px 8px;">', $displayTime, '</td>
			<td style="padding:0px 8px 0px 8px;">', $week_final['away'], '</td>
			<td style="padding:0px 8px 0px 8px;">', $week_final['home'], '</td>
			<td style="padding:0px 8px 0px 8px;"><a href="' . $scripturl. '?action=profile;u=' . $user . '">', $userName, '</a></td>
			<td style="padding:0px 8px 0px 8px;" class="left">
				', (isset($context['nfl_user_points']) ? $context['nfl_user_points'] : '???'), '
				', (isset($actualTotal) ? '<span title="Actual Total" style="padding:0px 3px 0px 3px;">[' .  $actualTotal . ']</span>' : ''), '
			</td>
			<td>
				', (isset($context['nfl_user_points']) ? '<span style="color:green;">SAVED' : '<span style="color:red;">No Data'), '</span>
			</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td style="border:0px;" colspan="7"><hr /><br /></td>
		</tr>
		<tr>
			<td colspan="7">
				', (!empty($error) ? '<span class="error">Warning: You are not permitted to view other user picks until after the cut-off time.</span><br /><br />' : '&nbsp;'), '
			</td>
		</tr>
	</table>
	<form id="nfl_pick" name="nfl_pickem" action="' . $currentPage . '" method="post">
		<table width="100%">
			<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
				<td>
					<input class="submit" type="submit" name="submit" value="Submit" onclick="var confirm = NFL_confirmSubmit(); if (confirm) {this.form.submit();} else {return false;}" />
				</td>
				<td colspan="5">
					<div class="content">
						<strong>Search Member:</strong>
						<input type="text" name="NflMember" id="NflMember" value="" class="input_text" ' . (!empty($_SESSION['nfl_admin']) ? 'disabled ' : '') . '/>
						<div id="NflQueryUserContainer"></div>
					</div>
				</td>
				', empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="stats" value="Overall" />
				</td>' : (!empty($_SESSION['nfl_stats']) && $user_info['is_admin'] ? '
				<td>
					<input class="submit" type="submit" name="stats" value="Exit Overall" />
				</td>' : '
				<td>
					&nbsp;
				</td>'), '
			</tr>
		</table>
	</form>
</div>
<script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/suggest.js?fin20"></script>
<script type="text/javascript"><!-- // --><![CDATA[
	var oAddMemberSuggest = new smc_AutoSuggest({
		sSelf: \'oAddMemberSuggest\',
		sSessionId: \'', $context['session_id'], '\',
		sSessionVar: \'', $context['session_var'], '\',
		sSuggestId: \'to_suggest\',
		sControlId: \'NflMember\',
		sSearchType: \'member\',
		sPostName: \'nfl_pick\',
		sURLMask: \'', !empty($_REQUEST['page']) ? 'page=' . $smcFunc['htmlspecialchars']($_REQUEST['page']) : (!empty($context['current_action']) ? 'action=' . $context['current_action'] : ''), ';member=%item_id%\',
		sTextDeleteItem: \'', $txt['autosuggest_delete_item'], '\',
		bItemList: false,
		sItemListContainerId: \'NflQueryUserContainer\'
	});
// ]]></script>
';
		}
	}
	elseif (!empty($html) && $confirm && stripos($week_final['code'], 'PRE') === false && !$allowedView)
	{
		$currentPage = currentPageNflPickem() . ';error=1;member=' . $user_info['id'];
		list($_SESSION['nfl_stats'], $_REQUEST['exit_stats']) = array(false, false);
		header('Location: ' . $currentPage);
		die();
	}
	elseif ((!empty($week_final)) && stripos($week_final['code'], 'PRE') !== false)
		echo 'Preseason games still in effect.';
	else
		echo 'No data at this time.';
}

/* Current url */
function currentPageNflPickem($pageURL = 'http://')
{
	global $context, $modSettings, $scripturl;

	if (!empty($_SERVER["HTTPS"]))
		$pageURL = "https://";

	if ($_SERVER["SERVER_PORT"] != "80")
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	else
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

	$urlArray = explode('/', $pageURL);

	return $pageURL;
}

function getInt($str)
{
	preg_match("/([0-9]+[\.,]?)+/",$str,$matches);
	return !empty($matches[0]) ? $matches[0] : 0;
}

/* Create NFL Tables */
function installNflTables()
{
	global $smcFunc, $modSettings;

	$smcFunc['db_create_table']('{db_prefix}nfl_tiebreakers',
		array(
			array(
				'name' => 'week',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
				'auto' => false
			),
			array(
				'name' => 'logged',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'home',
				'type' => 'varchar',
				'size' => 255,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'away',
				'type' => 'varchar',
				'size' => 255,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'picks',
				'type' => 'varchar',
				'size' => 255,
				'null' => false,
				'unsigned' => true,
			),
		),
		array(
			array(
				'type' => 'primary',
				'columns' => array('week')
			),
		),
		array(),
		'ignore'
	);

	$smcFunc['db_create_table']('{db_prefix}nfl_finals',
		array(
			array(
				'name' => 'week',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
				'auto' => false
			),
			array(
				'name' => 'logged',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'tiebreaker',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game1',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game2',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game3',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game4',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game5',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game6',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game7',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game8',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game9',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game10',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game11',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game12',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game13',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game14',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game15',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			),
			array(
				'name' => 'game16',
				'type' => 'varchar',
				'size' => 20,
				'null' => false,
				'unsigned' => true,
			)
		),
		array(
			array(
				'type' => 'primary',
				'columns' => array('week')
			),
		),
		array(),
		'ignore'
	);

	$smcFunc['db_create_table']('{db_prefix}nfl_weekly',
		array(
			array(
				'name' => 'id_member',
				'type' => 'int',
				'size' => 10,
				'null' => false,
				'unsigned' => true,
			),
		),
		array(
			array(
				'type' => 'primary',
				'columns' => array('id_member')
			),
		),
		array(),
		'ignore'
	);

	for ($i=1; $i<=17; $i++)
		$smcFunc['db_add_column']('{db_prefix}nfl_weekly',
			array(
				'name' => 'week' . (string)$i,
				'type' => 'varchar',
				'size' => 255,
				'null' => false,
				'unsigned' => true,
			),
			array(),
			'ignore',
			'fatal'
		);

	$smcFunc['db_add_column']('{db_prefix}nfl_tiebreakers',
		array(
			'name' => 'picks',
			'type' => 'varchar',
			'size' => 255,
			'null' => false,
			'unsigned' => true,
		),
		array(),
		'ignore',
		'fatal'
	);

	// set the install flag
	$setArray['nfl_installed'] = 1;
	$modSettings['nfl_installed'] = 1;
	updateSettings($setArray);

	return;
}

function nfl_gameYear()
{
	// 2014 weekly confirmation
	$lastGamesArray = array(
		0 => array('1', '1', '1', '1', '1', '1'. '1'),
		1 => array('SD', 'ARI', '1', '1410232800', '1409880600', '1410112800'),
		2 => array('PHI', 'IND', '1', '1410831000', '1410485100', '1410717600'),
		3 => array('CHI', 'NYJ', '1', '1411435800', '1411089900', '1411322400'),
		4 => array('NE', 'KC', '1', '1412040600', '1411694700', '1411927200'),
		5 => array('SEA', 'WSH', '1', '1412645400', '1412299500', '1412532000'),
		6 => array('SF', 'STL', '1', '1413250200', '1412904300', '1413136800'),
		7 => array('HOU', 'PIT', '1', '1413855000', '1413509100', '1413741600'),
		8 => array('WSH', 'DAL', '1', '1414459800', '1414113900', '1414346400'),
		9 => array('IND', 'NYG', '1', '1415064600', '1414718700', '1414951200'),
		10 => array('CAR', 'PHI', '1', '1415669400', '1415323500', '1415556000'),
		11 => array('PIT', 'TEN', '1', '1416274200', '1415928300', '1416160800'),
		12 => array('BAL', 'NO', '1', '1416879000', '1416533100', '1416765600'),
		13 => array('MIA', 'NYJ', '3', '1417483800', '1417109100', '1417123800', '1417138200', '1417370400'),
		14 => array('ATL', 'GB', '1', '1418088600', '1417742700', '1417975200'),
		15 => array('NO', 'CHI', '1', '1418693400', '1418347500', '1418580000'),
		16 => array('DEN', 'CIN', '1', '1419298200', '1418952300', '1419098400'),
		17 => array('STL', 'SEA', '0', '1419801900', '1419789600', '1419789600'),
	);

	$numberGames = array(
		0 => 0,
		1 => 16,
		2 => 16,
		3 => 16,
		4 => 13,
		5 => 15,
		6 => 15,
		7 => 15,
		8 => 15,
		9 => 13,
		10 => 13,
		11 => 14,
		12 => 15,
		13 => 16,
		14 => 16,
		15 => 16,
		16 => 16,
		17 => 16
	);
	return array($lastGamesArray, $numberGames);
}

function NFL_pool_pagination($content, $redirect, $count = 10)
{
	/* PHP pagination - max 7 visible integers and 6 periods (all links) - current page encircled with square brackets
	 * This php pagination code was developed by Underdog copyright 2013, 2014
	 * http://web-develop.ca
	 * Licensed under the GNU Public License: http://www.gnu.org/licenses/gpl.html
	*/

	// This particular function is only used when opting an entire table else it is not necessary
	global $context, $scripturl;

	/*  Set the $context variables for the display template  */
	$context['current_count'] = count($content);
	$context['current_pages'] = ((int)($context['current_count'] / $count)) + 1;
	$redirect = !empty($redirect) ? $redirect : $scripturl;
	$maxPages = 1;

	if (count($content) <= $count || $count < 2)
	{
		$context['current_pages'] = 0;
		return $content;
	}

	// Did they enter an invalid page via the url?
	if ((int)($context['current_count']/$count) == $context['current_count']/$count)
		$maxPages = $context['current_count']/$count;
	elseif($context['current_count'] %$count != 0)
		$maxPages += ($context['current_count'] - ($context['current_count'] % $count)) / $count;

	if ((int)$context['current_page']+1 > $maxPages)
		redirectexit($redirect . 'current_page=' . $maxPages . ';#page_top');
	elseif ((int)$context['current_page'] < 0)
		redirectexit($redirect . 'current_page=1;#page_top');

	// Now calculate the part of the array to display
	if (($context['current_count'] / $count) == (int)($context['current_count'] / $count))
		$context['current_pages'] = ($context['current_count'] / $count);

	$context['current_showResults'] = array(((int)$context['current_page'] * $count), (((int)$context['current_page'] + 1) * $count) - 1);

	if ((int)$context['current_page']+1 == (int)$context['current_pages'])
	    $context['current_showResults'][1] = count($content);
	else
	    $context['current_showResults'][1] = (int)$context['current_page']*$count + ($count-1);

	foreach($content as $key => $var)
	{
		$counter = array_search($key , array_keys($content));
		if ($counter >= (int)$context['current_showResults'][0] && $counter <= (int)$context['current_showResults'][1])
			$new_content[$key] = $var;
	}

	if (!empty($new_content))
		$context['current_showResults'][1] = ((int)$context['current_page']*$count) + count($new_content);
	else
		$new_content = $content;

	return $new_content;
}

function NFL_pool_pages($lang, $anchor, $link, $pages, $sort=false, $order=false)
{
	/* PHP pagination - max 7 visible integers and 6 periods (all links) - current page encircled with square brackets
	 * This php pagination code was developed by Underdog copyright 2013, 2014
	 * http://web-develop.ca
	 * Licensed under the GNU Public License: http://www.gnu.org/licenses/gpl.html
	*/
	global $context;

	$pageCount = 1;
	$display = array('page' => false, 'pages' => '0');
	$page = !empty($context['current_page']) ? (int)$context['current_page'] : 0;
	$display['pages'] = !empty($pages) ? (int)$pages : 1;

	if ($display['pages'] > 1)
	{
	    $display['page'] =  '
    <script type="text/javascript"><!-- // --><![CDATA[
	function changeColor(s)
	{
		document.getElementById("link"+s).style.color = "red";
	}
	function changeColorBack(s)
	{
		document.getElementById("link"+s).style.color = "blue";
	}
    // ]]></script>
    <span style="text-align:center;position:relative;width:99%;display:inline-block;">
	' . $lang . '<br />';

	    while ($pageCount < (int)$display['pages']+1)
	    {
		$current_page = (int)$page+1;
		$total = (int)$display['pages'];

		if ($pageCount == 1 || $pageCount == $total || $pageCount == $current_page || $pageCount == $current_page+1 ||
		    $pageCount == $current_page+2 || $pageCount == $current_page-1 || $pageCount == $current_page-2)
		{
		    if ((int)$pageCount == (int)$page+1)
			$display['page'] .= '
	<a onclick="this.href=\'javascript: void(0)\';" onmouseout="changeColor('. $pageCount . ')" onmouseover="changeColorBack(' . $pageCount . ')" id="link' . $pageCount . '" style="color:red;text-decoration:none;" href="'. $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">[' . $pageCount . ']</a> ';
		    else
			$display['page'] .= '
	<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">' . $pageCount . '</a> ';
		}
		elseif ($pageCount < $current_page-2 && $pageCount > $current_page-6)
			$display['page'] .= '
	<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">.</a> ';
		elseif ($pageCount > $current_page+2 && $pageCount < $current_page+6)
			$display['page'] .= '
	<a onmouseout="changeColorBack(' . $pageCount . ')" onmouseover="changeColor(' . $pageCount . ')" id="link' . $pageCount . '" style="color:blue;text-decoration:none;" href="' . $link . ';current_page=' . $pageCount . ';' . $sort . $order . $anchor . '">.</a> ';

		$pageCount++;
	    }

	    $display['page'] .= '
    </span>';
	}

	return $display;
}

function nfl_user_stats($currentPage, $checkWeek)
{
	global $user_info, $scripturl, $smcFunc;
	$nfl_pick = 0;

	if (isset($_REQUEST['exit_stats']))
	{
		list($_SESSION['nfl_stats'], $_REQUEST['exit_stats']) = array(false, false);
		header('Location: ' . $currentPage);
		die();
	}

	$timeAlotted = !$user_info['is_admin'] ? time() : time() + 691200;
	$member = isset($_REQUEST['nfl_member']) ? abs((int)$_REQUEST['nfl_member']) : 0;
	$week = isset($_REQUEST['nfl_week']) ? abs((int)$_REQUEST['nfl_week']) : $checkWeek;
	$context['current_page'] = !empty($context['current_page']) ? (int)$context['current_page'] : 1;
	$context['current_page'] = (!empty($_REQUEST['current_page']) ? (int)$_REQUEST['current_page'] : $context['current_page']) -1;
	$limit = ($context['current_page'] + 1) * 10;
	$spreads = array();

	$request = $smcFunc['db_query']('', "
		SELECT week, picks
		FROM {db_prefix}nfl_tiebreakers
		ORDER BY week",
		array()
	);
	while ($row = $smcFunc['db_fetch_assoc']($request))
	{
		$queryWeek = !empty($row['week']) ? (int)$row['week'] : 0;
		$spreads['week' . $queryWeek] = !empty($row['picks']) ? $row['picks'] : array();
	}
	$smcFunc['db_free_result']($request);

	foreach ($spreads as $weekCount => $pickem)
	{
		$weekFavs = explode('_', $pickem);
		$weekSpreads[$weekCount] = array();
		foreach ($weekFavs as $weekFav)
		{
			$spread = abs((int)getInt($pickem)) . '.5';
			$team = trim(str_replace(range(0,9), '', $pickem), '.');
			$weekSpreads[$weekCount][][$team] = $spread;
		}

	}

	if ($member < 1)
	{
		$request = $smcFunc['db_query']('', "
			SELECT u.id_member
			FROM {db_prefix}nfl_weekly AS u
			WHERE id_member >= {int:page}
			ORDER BY id_member
			LIMIT 10",
			array('week' => $checkWeek, 'page' => $limit)
		);
		while ($row = $smcFunc['db_fetch_assoc']($request))
		{
			$memberQuery = !empty($row['id_member']) ? (int)$row['id_member'] : 0;
			for ($i=1; $i <=17; $i++)
			{
				$data['user_' . $memberQuery]['week' . $i] = array(
					'user_picks' => !empty($row['week' . $i]) ? explode('_', $row['week' . $i]) : array(),
					'tie_points' => (int)getInt($user_picks[(count($user_picks) -1)]),
					'picks' => !empty($row['week' . $i]) ? explode('_', $row['week' . $i]) : array(),
				);
			}
			/*
			$userPoints[] = (!empty($user_picks)) ? (int)getInt($user_picks[(count($user_picks) -1)]) : '';
			array_pop($user_picks);
			*/
		}
		$smcFunc['db_free_result']($request);

		/*
		foreach ($user_picks as $user_pickem)
		{
			$context['user_game_' . $nfl_pick] = (!empty($user_pickem)) && $user_pickem === $nfl_games[$nfl_pick]['home'] ? $nfl_games[$nfl_pick]['home'] : $nfl_games[$nfl_pick]['away'];
			$nfl_pick++;
		}
		*/
	}
	else
	{
		$request = $smcFunc['db_query']('', "
			SELECT u.id_member
			FROM {db_prefix}nfl_weekly AS u
			WHERE id_member = {int:member}
			LIMIT 1",
			array('week' => $checkWeek, 'member' => $member)
		);
	}

	echo '
<div style="display:inline;" class="windowbg">
	<div class="cat_bar">
		<h3 class="centertext catbg">NFL Pool Overall Sheet</h3>
	</div>
	<form id="nfl_pick" name="nfl_pickem" action="' . $currentPage . '" method="post">
		<table width="100%">
			<tr ', ($user_info['is_guest'] ? 'style="display: none;"' : ''),'>
				', $user_info['is_admin'] ? '
				<td colspan="6">
					<input class="submit" type="submit" name="submit" value="Submit" />
				</td>
				' : '
				<td colspan="6">
					&nbsp;
				</td>', '
				<td style="float:right;">
					<input class="submit" type="submit" name="exit_stats" value="Exit Overall" />
				</td>
			</tr>
		</table>
	</form>
</div>';
	return;
}
?>