<?php
#######################################################|#
#|                    HabboMCMS - TheRox
#|                    Powered by HoloCMS 
#|            Copyright 2011-2012 ALL RIGHTS RESERVED
#######################################################|#

require_once('../global.php');

$id = Filter($_POST['accountId']);
$sql = mysql_query("SELECT * FROM messenger_friendships WHERE user_one_id = '".$id."' AND user_two_id = '".$my_id."'");
$rows = mysql_num_rows($sql);
if($rows <> 0){
$error = 1;
$message = "This person is already your friend.";
}

$sql = mysql_query("SELECT * FROM messenger_friendships WHERE user_one_id = '".$my_id."' AND user_two_id = '".$id."'");
$rows = mysql_num_rows($sql);
if($rows <> 0){
$error = 1;
$message = "This person is already your friend.";
}

$sql = mysql_query("SELECT * FROM messenger_friendrequests WHERE user_one_id_from = '".$my_id."' AND user_one_id_to = '".$id."'");
$rows = mysql_num_rows($sql);
if($rows <> 0){
$error = 1;
$message = "You already requested a friendship from this person.";
}

$sql = mysql_query("SELECT * FROM messenger_friendrequests WHERE user_one_id_to = '".$my_id."' AND user_one_id_from = '".$id."'");
$rows = mysql_num_rows($sql);
if($rows <> 0){
$error = 1;
$message = "This person already requested you to be their friend.";
}

if($id == $my_id){
$error = 1;
$message = "You cannot friend request yourself.";
}

if($error <> 1){
$sql = mysql_query("SELECT MAX(requestid) FROM messenger_friendrequests WHERE user_one_id_to = '".$id."'");
$requestid = mysql_result($sql, 0);
$requestid = $requestid + 1;

mysql_query("INSERT INTO messenger_friendrequests (user_one_id_from,user_one_id_to) VALUES ('".$my_id."','".$id."')");

$message = "Friend request has been sent successfully.";
} ?>
	Dialog.showInfoDialog("add-friend-messages",  
		"<?php echo $message; ?>",
		"OK");