<?php
error_reporting(E_ALL);
include 'config.inc.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
	<head>
		<title><?php echo $grade; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<style type="text/css">
			<!--
				td {border:1px solid black;}
			-->
		</style>
	</head>
	<body>
<?php

if (!isset($settings['orderby']) OR $settings['orderby'] == "orderby" OR !$settings[$settings['orderby']])
{
    $settings['orderby'] = "LastName";
}

if (!isset($settings['orderhow']))
{
    $settings['orderhow'] = 1;
}

if ($settings['orderhow'] == 1)
{
    $orderhow = "DESC";
}
else
{
    $orderhow = "ASC";
}

@mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) OR die(mysql_error());

mysql_select_db(MYSQL_DATABASE) OR die(mysql_error());
										   
$sql = "SELECT
         `FirstName` ,  `LastName` ,  `Mail` , `MailHidden` ,  `ICQ` ,  `Jabber` ,  `MSN` ,  `Handy` ,  `OtherIM` ,  `IRC` ,  `Phone` , `Website` , `AdditionalWebsites` ,  `Blog` ,  `Flickr`, `Skype`, `SchuelerVZ`
        FROM
          ".$grade."
        WHERE
          hidden = 0
        ORDER BY
          ".$settings['orderby']." ".$orderhow.";";

$result = mysql_query($sql) OR die(mysql_error());
        
?>
<table>
<?php		    

$urlstr = "";
$count = 0;
foreach ($header as $id => $name)
{
    $value = 0;
    if ($settings[$id] == 1)
    {
        $value = 1;
    }
    
    if ($count == 0)
    {
        $urlstr .= "?";
    }
    else
    {
        $urlstr .= "&";
    }
    
    $urlstr .= "$id"."="."$value";
    $count++;
}

foreach ($header as $id => $name)
{
	if ($settings[$id] == 0)
		continue;
	if ($settings['orderhow'] == 1)
	{
        $oh = 0;
	}
	else
	{
	    $oh = 1;
	}
	echo "\t\t<td class=\"".$id."\"><em><a href=\"".$urlstr."&orderby=".$id."&orderhow=".$oh."\">".$name."</a></em></td>\n";
}

echo "\t</tr>\n";

$nr = 0;
while($row = mysql_fetch_assoc($result))
{
	echo "\t<tr>\n";
	echo "\t\t<td class=\"Number\">".++$nr."</td>\n";
	
	foreach ($row as $id => $value)
	{
		if ($id != "MailHidden")
		{
			if ($settings[$id] == 0)
			{
				continue;
			}
			if ($id == "Mail")
			{
				if ($row['MailHidden'])
					$value = "<i>".$locale[$settings['currentlocale']]['hidden']."</i>";
				else
				{
					$newval = "<a href=\"mailto:".$value."\">".$value."</a>";
					$value = $newval;
				}
			}
			
				
			if ($id == "SchuelerVZ" && trim($value) != "")
			{
				$newval = "<a href=\"".$value."\">".$row['FirstName']." ".$row['LastName']."</a>";
				$value = $newval;
			}
			
			if ($id == "Website" || $id == "Blog")
			{
				$newval = "<a href=\"".$value."\">".$value."</a>";
				$value = $newval;
			}
			
			if ($id == "AdditionalWebsites" && $value != "")
			{
				$sitearray = split ("[\n\r\t ]+", $value);
				$newval = "";
				foreach ($sitearray as $site)
				{
					$newval = $newval."<a href=\"".$site."\">".$site."</a> ";
				}
				$value = $newval;
			}
			
			if ($id == "Flickr")
			{
				$newval = "<a href=\"".$value."\">".$value."</a>";
				$value = $newval;
			}
			
			echo "\t\t";
			echo "<td class=\"".$id."\">".$value."</td>\n";
		}
    }
    
    echo "\t</tr>\n";
}
echo "</table>\n";
echo "<br />";
print_show ($header, $settings, $locale);
?>
	</body>
</html>