<div id="register_form01">
	<div id="menu01_head">:: priatelia</div>
	<form name='friends' method='POST' action='index.php?module=friends'  style='margin: 4px;'>
		Zadaj meno priateľa, ktorého si chceš pridať: <input type='text' name='name' class='form_field01' /> 
		<input type='submit' name='submit' value='Pridaj' class='form_button01'/>
	</form>
	<?php
	
		$delete_friend = (isset($_GET['delete_friend'])) ? intval($_GET['delete_friend']) : 0;
		
		if($delete_friend != 0)
		{
			$query = "DELETE FROM mobile_friends WHERE kto = '".$_SESSION['my_id']."' AND koho = '$delete_friend' limit 1";
			if($dba->ExecuteQuery($query))
			{
				echo '<span class=\"red\"><b>Priateľ úspešne zmazaný</b></span>';
			}
			else 
			{
				echo '<span class=\"red\"><b>Chyba pri mazaní priateľa!</b></span>';
			}
		}
		
		if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']))
		{
				$contact = (isset($_POST['name']) && trim($_POST['name']) != '') ? $_POST['name'] : '';
				if(strtoupper($contact) == strtoupper($_SESSION['log']))
				{
					$add_err = 'Nemôžete pridať sám seba';
				}
				else 
				{
					if($contact != '')
					{
						$kontakt_id = $dba->GetOne2('mobile_users', 'ID', 'login', $contact);
						if($kontakt_id != 0)
						{
							$query = "SELECT * FROM mobile_friends WHERE kto = '".$_SESSION['my_id']."' AND koho = '$kontakt_id'";
							$exists_check = $dba->GetNums($query);
							if($exists_check == 0)
							{
								$query = "INSERT INTO mobile_friends (kto, koho) VALUES (".$_SESSION['my_id'].", $kontakt_id)";
								$check = $dba->ExecuteQuery($query);
								if($check)
								{
									$add_err = 'Priateľ pridaný!';
									$predmet = 'Boli ste pridaný do mojich priateľov';
									$sprava = 'Užíveteľ s nickom '.$_SESSION['log'].' si Vás pridal medzi svojich priateľov. <br/><br/>Pridajte si aj Vy svojich priateľov a získate prehľad o tom kedy sú online a budete sa môcť zabávať spolu s nimi!';
									$query = "INSERT INTO mobile_posta (predmet, text, user_from, user_to, cas) values ('$predmet', '$sprava', ".$_SESSION['my_id'].", ".$kontakt_id.", NOW())";
									$dba->ExecuteQuery($query);
								}
								else 
								{
									$add_err = 'Chyba pri pridávaní priateľa!';
								}
							}
							else 
							{
								$add_err = 'Zadaný priateľ už je vo vašom adresári!';
							}
						}
						else 
						{
							$add_err = 'Zadaný užívateľ neexistuje!';
						}
					}
					else 
					{
						$add_err = 'Zadaný kontakt neexistuje!';
						$action = 'contact_add_form';
					}
				}
				if(isset($add_err)) echo '<span id=\"red\"><b>'.$add_err.'</b></span>';
		}
	
		$image_gender = array
		(
			0 => 'male',
			1 => 'female'
		);
	
		$query = "
      			SELECT 
      				*
				FROM (
					mobile_friends a, 
					mobile_users b,
					mobile_mobil d
				)
					LEFT OUTER JOIN mobile_logged_users c ON b.ID = c.user_id
      			WHERE
      				a.kto = '".$_SESSION['my_id']."' AND
      				a.koho = b.ID AND
      				b.moj_mobil = d.ID
      			ORDER BY
      				b.login ASC
      			";
      		$data = $dba->GetArray($query);
      		if(is_array($data) && sizeof($data) > 0)
      		{
      			echo "<table width='100%' align='center' border='0' cellpadding='2' cellspacing='1'>";
      			echo "<tr>
					    <td class='friends_cell' width='20'>&nbsp;</td>
					    <td class='friends_cell'><b>meno</b></td>
					    <td class='friends_cell'><b>mesto</b></td>
					    <td class='friends_cell'><b>mobil</b></td>
					    <td class='friends_cell' width='22'><b>ICQ</b></td>
					    <td class='friends_cell' width='30'><b>MSN</b></td>
					    <td class='friends_cell' width='35'><b>mail</b></td>
					    <td class='friends_cell' width='40'><b>pošta</b></td>
					    <td class='friends_cell' width='20%'><b>posledné prihlásenie</b></td>
					    <td class='friends_cell' width='20'></td>
					  </tr>";
      			foreach ($data as $friend)
      			{
//      				$online = (!is_null($friend['last_act'])) ? 'online' : $friend['last_login'];
//      				echo "<tr>
//      						<td class='friends_cell'><img src=\"images/".$image_gender[$friend['gender']].".gif\" alt=''/></td>
//      						<td class='friends_cell'>".$friend['login']."</td>
//      						<td class='friends_cell'><a href='index.php?module=posta&action=form&komu=".$friend['login']."'>pošli</a></td>
//      						<td class='friends_cell'>$online</td>
//      						<td class='friends_cell'><a href='index.php?module=friens&amp;action=delete_friend&amp;id=".$friend['ID']."'>odstráň priateľa</a></td>
//      					</tr>
//      					";
      				
      				echo "<tr>\n";
				      echo "<td class='friends_cell' align='center'><img src='images/".$image_gender[$friend['gender']].".gif' alt=''	/></td>\n";
				      echo "<td class='friends_cell'>".$friend['login']."</td>\n";
				      echo "<td class='friends_cell'>".$friend['mesto']."</td>\n";
				      echo "<td class='friends_cell'>".$friend['nazov']."</td>\n";
				      echo "<td class='friends_cell'>"; if($friend['icq']!='') echo "<img src='http://status.icq.com/online.gif?icq=".$friend['icq']."&amp;img=5' align='top' title='".$friend['icq']."' width='14' height='14' alt=''/>"; echo "</td>\n";
				      echo "<td class='friends_cell'>"; if($friend['msn']!='') echo "<span title='".$friend['msn']."'>msn</span>"; echo "</td>\n";
				      echo "<td class='friends_cell'>";
				      if($friend['mail']!='') echo "<a href='mailto:".$friend['mail']."' class='users_link'>mail</a>";
				      echo "</td>\n";
				      echo "<td class='friends_cell'><a href='index.php?module=posta&amp;action=form&amp;komu=".$friend['login']."' class='users_link'>pošli</a></td>\n";
				      $online = (!is_null($friend['last_act'])) ? 'online' : $friend['last_login'];
				      echo "<td class='friends_cell'>$online</td>\n";
				      echo "<td class='friends_cell' align='center'><a href='index.php?module=friends&amp;delete_friend=".$friend['koho']."' style='border:0;' title='Odstráň priateľa'><img src='images/remove-user-red.gif' alt='' border='0'/></a></td>";
				      echo "</tr>\n";
      			}
      			echo "</table>";
      		}
	
	?>
</div>