<?php
	require("konf.php");
	mysql_connect($mysql_host, $mysql_user, $mysql_password);
	mysql_select_db($db);
  if(empty($aeg)){
	  if(empty($date)) $kp = date("d")."/".date("m")."/".date("Y"); else $kp = $date;
	  if(empty($ka1)) $ka1 = date("H");
	  if(empty($ka2)) $ka2 = date("i");
	}else{
		$kp = substr($aeg, 6, 2)."/".substr($aeg, 4, 2)."/".substr($aeg, 0, 4);
    $ka1 = substr($aeg, -4, 2);
    $ka2 = substr($aeg, -2);
  }
?>
<html>
<head>
  <title>Otsing</title>
	<link href="style.css" rel="stylesheet" type="text/css">
	<link href='jscalendar/calendar-blue.css' rel='stylesheet' type='text/css'>
	<script type='text/javascript' src='jscalendar/calendar.js'></script>
	<script type='text/javascript' src='jscalendar/lang/calendar-ee.js'></script>
	<script type='text/javascript' src='jscalendar/calendar-setup.js'></script>
</head>
<body>
	<table width="100%">
	  <form action="<?php echo $PHP_SELF; ?>" method="post">
		<caption>Otsing putukate andmebaasist</caption>
  	<tr>
    	<td colspan="2" width="100%" align="center">
				<a href="index.php">Tagasi</a>
      </td>
    </tr>
	  <tr>
	    <td width="50%" align="right" id='f_trigger_c' style='cursor: hand;' title='Vajuta muutmiseks'><em>Kuupäev:</em></td>
	    <td>
	      <input type="text" name='date' id='f_date_c' value="<?php echo $kp; ?>" style="width: 88px">
	    </td>
	  </tr>
	  <tr>
	    <td align="right"><em>Kellaaeg:</em></td>
	    <td>
	      <select name="ka1">
	      <?php for($x = 0; $x < 24; $x++){
        	if(strlen($x) == 1) $x = "0".$x; ?>
	        <option<?php if($x == $ka1) echo " selected"; ?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
	      <?php } ?>
	      </select>:
	      <select name="ka2">
	      <?php for($x = 0; $x < 60; $x++){
        	if(strlen($x) == 1) $x = "0".$x; ?>
	        <option<?php if($x == $ka2) echo " selected"; ?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
	      <?php } ?>
	      </select>
	    </td>
	  </tr>
	  <tr height="5"><td colspan="2"></td></tr>
	  <tr>
	    <td align="center" colspan="2">
	      <input type="submit" name="submit" value="Otsi">
	      </form>
	      <script type="text/javascript">
	        Calendar.setup({
	          inputField     :    'f_date_c',     // id of the input field
	          ifFormat       :    'dd/mm/y',      // format of the input field
	          button         :    'f_trigger_c',  // trigger for the calendar (button ID)
	          align          :    'Bl',           // alignment (defaults to 'Bl')
	          singleClick    :    true
	        });
	      </script>
	    </td>
	  </tr>
  </table>
<?php
if($submit){
?>
	<table width="100%">
  	<tr>
    	<td width="100%" align="center">
      	<table>
<?php
  if(empty($aeg)){
		$aeg = substr($kp, -4).substr($kp, 3, 2).substr($kp, 0, 2);
	  if(strlen($ka1) == 1) $aeg .= "0".$ka1; else $aeg .= $ka1;
	  if(strlen($ka2) == 1) $aeg .= "0".$ka2; else $aeg .= $ka2;
	}
	$lae = mysql_query("SELECT id, liik, aeg FROM bugs WHERE aeg <= '".$aeg."' ORDER BY aeg DESC");
  while($row = mysql_fetch_array($lae)){
	  $row[2] = substr($row[2], 6, 2)."/".substr($row[2], 4, 2)."/".substr($row[2], 0, 4)."&nbsp;".substr($row[2], -4, 2).":".substr($row[2], -2);
    $MASS[$row[0]] = array('id' => $row[0], 'liik' => $row[1], 'aeg' => $row[2]);
	}
  if(!empty($MASS)){
?>
          <caption>Otsingu tulemused</caption>
          <form action="results.php" method="post">
<?php
	  foreach($MASS as $m){
?>
        	<tr>
          	<td>
            	<input type="checkbox" name="ruut[]" value="<? echo $m[id] ?>" style="border:none">
            </td>
          	<td>
              <?php echo $m[liik]; ?>&nbsp;(<?php echo $m[aeg]; ?>)
            </td>
          </tr>
<?php
		}
?>
	        <tr height="5"><td colspan="2"></td></tr>
	        <tr>
	          <td align="center" colspan="2">
							<input type="hidden" name="time" value="<?php echo $aeg; ?>">
	            <input type="submit" name="submit" value="Kuva">
	            </form>
						</td>
	        </tr>
<?php
	}else{
?>
					<tr>
          	<td class="NB">
            	Ei leitud ühtegi sisestust!
            </td>
          </tr>
<?php
	}
?>
        </table>
			</td>
    </tr>
  </table>
<?php
}
?>
</body>
</html>