<?
	require_once 'functions.php';
	require_once 'variables.php';
	require_once 'sql.php';
	
	mysql_connect (SQL_HOST, SQL_USER, SQL_PASS);
	mysql_select_db (SQL_DB);
	
	print "<title>Masters of Endurance - 2h of Kyoto - Live standings</title>";
	print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
	//print '<head><meta http-equiv="refresh" content="30; URL=http://moe.lfs-tracker.de/"></head>'."\n";
	print "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n";
	print "<center>";
	
	 
	
	$r_res = mysql_query("SELECT max(id) AS id FROM " . SQL_RACE . " where 1");
	$race_id = mysql_result($r_res, 0, 'id');

	$chat_qry = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND race_id='" . $race_id . "' ORDER BY id ASC");
	
	while ($chat_res = mysql_fetch_object ($chat_qry))
	{
		print $chat_res->value."<br>";
	}
		
	$timeout_qry = mysql_query("SELECT * FROM " . SQL_LAP . " WHERE type='MSG' AND (value LIKE '%antwortet nicht' OR value LIKE '%nimmt am Rennen teil' OR value LIKE '%verlässt die Box%' OR value LIKE '%geht an die Box') AND race_id='" . $race_id . "' ORDER BY id ASC");
	
	print "<table cellspacing=3 cellpadding=3 border=0>\n";
	print "\t<tr>\n";
		print "\t\t<td width=150>Name</td>\n";		
		print "\t\t<td colspan=12></td>\n";					
	print "\t</tr>\n";
	
	$new_name= "";
	$n = "jada";
	$first = true;
	
	while ($t_res = mysql_fetch_object($timeout_qry))
	{
		$info = getInfo($t_res->value);
		$new_name = $info['name'];
		$error = $info['error'];
		
		$teamInfo = getTeam ($new_name);
		
		//print_r ($teamInfo);
		
		if ($teamInfo['prc'] > 80)
		{
			$driver = trim($new_name);
			$new_name = $teamInfo['match'];
		}
		else
		{
			$driver = "";
		}
		
		if ($n == $new_name)
		{
			print "\t\t<td width=25 bgcolor='#C4C2C2' align=center>" . normalTime((($t_res->created_on - $m)*100), false) . "</td>\n";
			
			if ($driver != "")
				print "\t\t<td width=175 bgcolor='#D6D6D6'>" . $err[$error] . " (" . $driver . ")</td>\n";
			else
				print "\t\t<td width=125 bgcolor='#D6D6D6'>" . $err[$error] . "</td>\n";
				
			$m = $t_res->created_on;
		}
		else
		{
			$n = $new_name;
			
			if (!$first)
				print "\t</tr>\n";				
			$first = false;
			
			print "\t<tr>\n";			
			print "\t\t<td bgcolor='#7DA7D9'>" . $n . "</td>\n";
			
			if ($driver != "")
				print "\t\t<td width=175 bgcolor='#D6D6D6'>" . $err[$error] . " (" . $driver . ")</td>\n";
			else
				print "\t\t<td width=125 bgcolor='#D6D6D6'>" . $err[$error] . "</td>\n";
			
			$m = $t_res->created_on;
		}		
	}
	
	print "</table>\n";
?>