<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<?php

    //error_reporting (E_ALL);
    
	$startTime = array_sum(explode(chr(32), microtime()));
	require_once 'variables.php';
	require_once 'sql.php';
	require_once 'functions.php';

	mysql_connect (SQL_HOST, SQL_USER, SQL_PASS);
	mysql_select_db (SQL_DB);

	$myColor1 = '#A1A1A1';
	$myColor2 = '#EBEBEB';
	$myColor3 = '#FFD600';
	$myColor4 = '#F2F2F2';
	$myColor5 = '#DA1414';

	if ($numServers == 2)
	{
		$rid_string = " AND (";

		if ($useManualIdServer1)
			$race_id = $manualIdServer1;
		else
		{
			$r_res = mysql_query("SELECT max(id) AS id FROM " . SQL_RACE . " where server='1'");
			$race_id = mysql_result($r_res, 0, 'id');
		}

		$rid_string .= " race_id='" . $race_id . "' ";

		if ($useManualIdServer2)
			$race_id = $manualIdServer2;
		else
		{
			$r_res = mysql_query("SELECT max(id) AS id FROM " . SQL_RACE . " where server='2'");
			$race_id = mysql_result($r_res, 0, 'id');
		}

		$rid_string .= " OR race_id='" . $race_id . "' ";
		$rid_string .= " ) ";
	}
	elseif ($numServers == 1)
	{
		if ($useManualIdServer1)
		{
			$race_id = $manualIdServer1;
		}
		else
		{
			$r_res = mysql_query("SELECT max(id) AS id FROM " . SQL_RACE . " where 1");
			$race_id = mysql_result($r_res, 0, 'id');
		}
		$rid_string = "AND ( race_id='" . $race_id . "')";
	}
	else
	{
		print "Incompatible number of servers specified.";
		die;
	}
	
	//include 'header.php';
	print "<table width=3000><tr>";
	
		foreach ($TEAMS as $key => $team)
		{
			print "<td valign=top>";
			print "<table border=1><tr><td valign=top width=300 colspan=3>" . $team['team_name'] . "</td></tr><tr>";
			
			//$pitstop_search = mysql_query ("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND value LIKE '% pit stop'" . $rid_string . " $timeoffset ORDER BY race_id asc");
			//$dt_search = mysql_query ("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND value LIKE '% drive through'" . $rid_string . " $timeoffset ORDER BY race_id asc");

				$drivers = generateDriverString ($team);
				$sql_res = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE ((type='LAP' AND (" . $drivers . ")) OR ( type='MSG' AND value LIKE '" . $team['number'] . " % pit stop')) " . $rid_string . " ORDER BY race_id asc, id ASC");
				$sql_dummy = $sql_res;
				
				print "<td valign=top>";
				while ($res = mysql_fetch_object($sql_res))
				{
					if ($res->lap != 0 && !strstr($res->value, "pit stop"))
						print $res->lap . "<br>";
				}						
				print "</td>";
				
				mysql_data_seek($sql_res, 0);
				
				print "<td valign=top>";
				while ($res = mysql_fetch_object($sql_res))
				{
					if ($res->lap != 0 && !strstr($res->value, "pit stop"))
						print $res->value . "<br>";
				}		
				print "</td>";
				
				mysql_data_seek($sql_res, 0);
				
				print "<td valign=top>";
				while ($res = mysql_fetch_object($sql_res))
				{
					if (strstr($res->value, "pit stop"))
						print "P<br>";
					else
						print "<br>";
				}		
				print "</td>";
					
			print "</tr></table>";
			print "</td>";
		}
	print "</td></tr></table>";
?>