<?php
require("konf.php");
mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($db);
if($submit){
	if($id == -1){
		$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;
		@mysql_query('INSERT INTO bugs SET liik="'.$liik.'", leidja="'.$leidja.'", maaraja="'.$maaraja.'", piirkond="'.$piirkond.'", koht="'.$koht.'", aeg="'.$aeg.'" ') or die("Baasi kirjutamisel viga");
  }else{
		$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;
		@mysql_query('UPDATE bugs SET liik="'.$liik.'", leidja="'.$leidja.'", maaraja="'.$maaraja.'", piirkond="'.$piirkond.'", koht="'.$koht.'", aeg="'.$aeg.'" WHERE id="'.$id.'" ') or die("Baasi kirjutamisel viga");
  }
?>
<html>
<head>
  <title>Putuka andmed sisestatud</title>
	<link rel="stylesheet" href="style.css">
  <Script Type="text/JavaScript" language="JavaScript">
    CounterStart = 1

    function Decrement() {
      CounterStart--;
      if(CounterStart < 0) {
        self.close();
      }else{
        setTimeout('Decrement()',1000);
      }
    }

    function StartTheCounter() {
      opener.location.reload();
      setTimeout('Decrement()',1000);
    }
  </Script>
</head>
<body onLoad="StartTheCounter()" leftmargin="0" topmargin="0">
<table height="100%" width="100%">
<form name="timer">
<tr>
	<td align="center">
		Andmed edukalt sisestatud.<br><a href="javascript:window.close()">Sulge aken</a>
	</form></td>
</tr>
<?php
}else{
	if($id != -1){
		$result = mysql_fetch_array(mysql_query('SELECT liik, aeg, piirkond, koht, leidja, maaraja FROM bugs WHERE id="'.$id.'"'));
		$ka1 = substr($result[1], -4, 2);
		$ka2 = substr($result[1], -2);
    $date = substr($result[1], 6, 2)."/".substr($result[1], 4, 2)."/".substr($result[1], 0, 4);
  }
?>
<html>
<head>
  <title><?php if($id == -1) echo "Lisad uut putukat."; else echo "Muudad ".$result[0]." andmeid"; ?></title>
	<link rel="stylesheet" href="style.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 leftmargin="7" topmargin="7" valign="top">
	<table width="100%" valign="top" align="center">
	<caption>&raquo;&nbsp;Andmed&nbsp;&laquo;</caption>
	<tr><td colspan="2" height="15"></td></tr>
	<form action="<?php echo $PHP_SELF; ?>" method="post">
	<tr>
    <td align="right"><em>Liik:</em></td>
    <td><input type="text" name="liik" value="<?php echo $result[0]; ?>" style="width:250px"></td>
  </tr>
	<tr>
    <td align="right"><em>Leidja:</em></td>
    <td><input type="text" name="leidja" value="<?php echo $result[4]; ?>" style="width:250px"></td>
  </tr>
	<tr>
    <td align="right"><em>Määraja:</em></td>
    <td><input type="text" name="maaraja" value="<?php echo $result[5]; ?>" style="width:250px"></td>
  </tr>
	<tr>
    <td align="right"><em>Piirkond:</em></td>
    <td><input type="text" name="piirkond" value="<?php echo $result[2]; ?>" style="width:250px"></td>
  </tr>
	<tr>
    <td align="right"><em>Koht:</em></td>
    <td><input type="text" name="koht" value="<?php echo $result[3]; ?>" style="width:250px"></td>
  </tr>
  <tr>
		<td align="right" id='f_trigger_c' style='cursor: hand;' title='Vajuta muutmiseks'><em>Kuupäev:</em></td>
		<td>
			<input type="text" name='kp' id='f_date_c' value="<?php echo $date; ?>" 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="hidden" name="id" value="<?php echo $id; ?>">
			<input type="submit" name="submit" value="Salvesta">
      </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          :    'Tl',           // alignment (defaults to 'Bl')
	        singleClick    :    true
		    });
			</script>
		</td>
	</tr>
<?php
}
?>
</table>
</body>
</html>