<?php
if ($search != 'Search...' and $search != '') {
	
	$res = mysql_query ("SELECT p.Id FROM page AS p JOIN report AS r ON r.PageId = p.Id WHERE p.Title REGEXP '$search' AND DATE(r.Date) = (SELECT MAX(DATE(Date)) FROM report) ") or die ('error');
	$nr = mysql_num_rows ($res);
	if ($nr==1) {
		$row = mysql_fetch_array ($res);
		extract ($row);
		?>
 		<script type="text/javascript">
		window.location = '?page=page&id=<?php echo $Id; ?>&searchbox=<?php echo $search; ?>';
    	</script>
    	<?php	
	} else {
		?>
 		<script type="text/javascript">
		window.location = '?page=pages';
    	</script>
    	<?php	
	}
} else {
	?>
 	<script type="text/javascript">
	window.location = '?page=home';
    </script>
    <?php	
}
?>