<?php

	$actions = array('add', 'delete');
	
	(key_exists('action', $_GET) && in_array($_GET['action'], $actions)) ? $action = $_GET['action'] : $action = '';
	(key_exists('i', $_GET) && $_GET['i']>0) ? $i = $_GET['i'] : $i = 1;
	$sum = (key_exists('sum', $_POST)) ? $_POST['sum'] : 0;
	$num1 = (key_exists('num1', $_POST)) ? $_POST['num1'] : 0;
	$num2 = (key_exists('num2', $_POST)) ? $_POST['num2'] : 0;
	
	$nastranu = 15;
	$rewrite = false;
	
	if($action == 'delete' && session_is_registered('my_id') && in_array($_SESSION['my_id'], $admins))
	{
		$id = fnAdjustID();
		if($id != '')
		{
			$query = "DELETE FROM mobile_gbook WHERE id='$id' limit 1";
			$dba->ExecuteQuery($query);
		}
	}
	
	if($action=='add' && $sum == $num1 + $num2)
	{
		$meno = (key_exists('meno', $_POST) && $_POST['meno']<>'') ? trim($_POST['meno']) : '';
		$text = (key_exists('text', $_POST) && $_POST['text']<>'') ? trim($_POST['text']) : '';
		
		$query = "SELECT * FROM mobile_gbook ORDER BY ID DESC limit 0, 1";
		$result = $dba->GetResult($query);
		$row = mysql_fetch_array($result);
		
		if($meno!='' && $text!='')
		{
			$lmc = (isset($_SESSION['last_message'])) ? $_SESSION['last_message'] : '';
			if(addslashes(htmlspecialchars($_POST['text']))!=$row['text'] && $lmc != addslashes(htmlspecialchars($_POST['text'])))
			{
				$meno = addslashes(htmlspecialchars($_POST['meno']));
				$mail = addslashes(htmlspecialchars($_POST['mail']));
				$text = addslashes(htmlspecialchars($_POST['text']));
				$datum = date("j.m.Y H:i:s");
				$admin = (fnIsAdmin()) ? 'a' : 'n';
				$query = "INSERT INTO mobile_gbook (meno, mail, datum, text, admin) VALUES ('$meno', '$mail', '$datum', '$text', '$admin')";
				$dba->ExecuteQuery($query);
				$_SESSION['last_message'] = $text;
				$komu = "pesto@peterpech.com";
				$subject = "Nový odkaz na 4mobile";
		   		$sprava = $meno." - ".$mail." : ".$text;
		   		$headers = "From:4mobile <4mobile@4mobile.sk>\n";
		   		$headers .= "Content-Type: text/html; charset=iso-8859-2\n";
		   		mail($komu,  $subject, $sprava, $headers);
	   		
				$action = '';
			}
			else
			{
				$add_err = 'Nemôžete dvakrát pridať ten istý odkaz!';
			}
		}
		else
		{
			$add_err = 'Nezadali ste svoje meno alebo text odkazu!';
			$rewrite = true;
		}
	}
	elseif ($sum != $num1 + $num2)
	{
		$add_err = 'Niekto by si mal zopakovať matematiku..';
		$rewrite = true;
	}
	
	srand(microtime()*10000000);
	$num1 = rand(1, 49);
	$num2 = rand(1, 49);
	
?>
	<script language="javascript">
	function AddSmile(s)
	{
	   document.forms.frm.text.value += ' ' + s + ' ';
	   document.forms.frm.text.focus();
   	}
   	<?php if(session_is_registered('my_id') && in_array($_SESSION['my_id'], $admins)) { ?>
	function DeleteConfirm(link)
	{
		if (confirm('Zmazať?')) window.location = link;
	}
	<?php } ?>
	</script>
    <div id="register_form01">
	  <div id="menu01_head">:: guestbook</div>
	  <div id="melodie_01">
	   <?php if(isset($add_err)) echo "<br /><span id=\"red\">$add_err</span>"; ?>
		<form name="frm" style="padding-top: 10px;" action="index.php?module=gbook&amp;action=add" method="post">
		<table border="0" cellspacing="0" cellpadding="0" align="center">
		  <tr> 
		    <td width="100" align="right">Meno:</td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="meno" size="51" maxlength="30" value="<?php if(key_exists('meno', $_POST) && $action=='add' && $rewrite)
				{
					echo $_POST["meno"]; 
				}elseif(session_is_registered('log'))
				{
					echo $_SESSION['log'];
				}
				
				?>"/>
		    </td>
		  </tr>
		  <tr> 
		    <td align="right">Mail:</td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="mail" size="51" maxlength="30" value="<? if(key_exists('mail', $_POST) && $action=='add' && $rewrite) echo $_POST["mail"]; ?>"/>
		    </td>
		  </tr>
		  <tr> 
		    <td align="right"><?php echo $num1." + ".$num2." =&nbsp;";?></td>
		    <td align="left" colspan="2"> 
		      <input id="form_field01" type="text" name="sum" size="3" maxlength="3" value="???" onclick="this.value='';"/> Povinné!!!
		      <input type="hidden" name="num1" value="<?php echo $num1; ?>">
		      <input type="hidden" name="num2" value="<?php echo $num2; ?>">
		    </td>
		  </tr>
		  <tr> 
		      <td valign="top" align="right">Správa:</td>
		      <td valign="top" align="left"> 
		        <textarea id="form_field01" rows="12" cols="50" name="text"><? if(key_exists('text', $_POST) && $action=='add' && $rewrite) echo $_POST["text"]; ?></textarea><br />
		      </td>
		      <td valign="top" style='line-height: 10px;'><?php  
		      for($j=1;$j<=20;$j++)
		      {
		      	echo"<a href=\"javascript:AddSmile('s".$j."')\"><img src=\"images/smile/".$j.".gif\" border=\"0\" align=\"top\" style=\"padding-bottom: 1.5px;\" alt=\"\"/></a> ";
		      	if($j%3==0) echo "<br />";
		      }
		      ?></td>
		  </tr>
		  <tr> 
		    <td></td>
		    <td align="left" valign="top">
		            <input id="form_button01" type="submit" name="Submit" value="Pošli"/>
		            <input id="form_button01" type="reset" name="reset" value="Reset"/>
		    </td>
		  </tr>
		  <tr> 
		    <td></td>
		    <td align="left" valign="top">
		    </td>
		  </tr>
		</table></form>
		<span class="cervena"><b>PREDTÝM AKO SA ČOKOĽVEK SPÝTATE, PREČÍTAJTE SI ČASŤ FAQ!!!</b></span>
		<?php
		
			$query = "SELECT id FROM mobile_gbook";
			$chunk = $dba->GetLimit($query);
			$lim = $chunk[0];
			$stranok = $chunk[2];
			$query = "SELECT * FROM mobile_gbook ORDER BY ID desc limit $lim, $nastranu";
			$result = $dba->GetResult($query);
			
			fnNavi($i, $stranok, 'module=gbook');
			fnWriteGbook($result);
			fnNavi($i, $stranok, 'module=gbook');
			
		
		?>
	  </div>
	</div>