<?
$page = "view_friends.php";
include "manager_header.php";
if($group_info[allow_friends] != "1") { exit(); }
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }


//SET ERROR VAR
$is_error = "";
$general_error = "";
$username1_error = "";
$username2_error = "";
$username3_error = "";
$username4_error = "";
$username5_error = "";


if($task == "cancel") {
header("Location: view_friends.php");
exit();
}


if($task == "doadd") {
$new_username1 = $_POST['new_username1'];
$new_username2 = $_POST['new_username2'];
$new_username3 = $_POST['new_username3'];
$new_username4 = $_POST['new_username4'];
$new_username5 = $_POST['new_username5'];


if(str_replace(" ", "", $new_username1) == "" & str_replace(" ", "", $new_username2) == "" & str_replace(" ", "", $new_username3) == "" & str_replace(" ", "", $new_username4) == "" & str_replace(" ", "", $new_username5) == "") { $is_error = "yes"; $general_error = "$view_friends2<br><br>"; }

if(str_replace(" ", "", $new_username1) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username1'")) == 0) { $is_error = "yes"; $username1_error = "$view_friends1"; }
 if(strtolower($new_username1) == strtolower($user_info[username])) { $is_error = "yes"; $username1_error = "$view_friends3"; }
 $friend_info1 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username1'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info1[u_id]'")) != 0) { $is_error = "yes"; $username1_error = "$view_friends4"; }
}

if(str_replace(" ", "", $new_username2) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username2'")) == 0) { $is_error = "yes"; $username2_error = "$view_friends1"; }
 if(strtolower($new_username2) == strtolower($user_info[username])) { $is_error = "yes"; $username2_error = "$view_friends3"; }
 $friend_info2 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username2'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info2[u_id]'")) != 0) { $is_error = "yes"; $username2_error = "$view_friends4"; }
}

if(str_replace(" ", "", $new_username3) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username3'")) == 0) { $is_error = "yes"; $username3_error = "$view_friends1"; }
 if(strtolower($new_username3) == strtolower($user_info[username])) { $is_error = "yes"; $username3_error = "$view_friends3"; }
 $friend_info3 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username3'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info3[u_id]'")) != 0) { $is_error = "yes"; $username3_error = "$view_friends4"; }
}

if(str_replace(" ", "", $new_username4) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username4'")) == 0) { $is_error = "yes"; $username4_error = "$view_friends1"; }
 if(strtolower($new_username4) == strtolower($user_info[username])) { $is_error = "yes"; $username4_error = "$view_friends3"; }
 $friend_info4 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username4'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info4[u_id]'")) != 0) { $is_error = "yes"; $username4_error = "$view_friends4"; }
}

if(str_replace(" ", "", $new_username5) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username5'")) == 0) { $is_error = "yes"; $username5_error = "$view_friends1"; }
 if(strtolower($new_username5) == strtolower($user_info[username])) { $is_error = "yes"; $username5_error = "$view_friends3"; }
 $friend_info5 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_username5'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info5[u_id]'")) != 0) { $is_error = "yes"; $username5_error = "$view_friends4"; }
}

if($is_error != "yes") {
$result = "";
 if(str_replace(" ", "", $new_username1) != "") {
  mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info1[u_id]')");
  if($result != "") { $result .= ", "; }
  $result .= "<b>$new_username1</b>";
 }
 if(str_replace(" ", "", $new_username2) != "") {
  mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info2[u_id]')");
  if($result != "") { $result .= ", "; }
  $result .= "<b>$new_username2</b>";
 }
 if(str_replace(" ", "", $new_username3) != "") {
  mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info3[u_id]')");
  if($result != "") { $result .= ", "; }
  $result .= "<b>$new_username3</b>";
 }
 if(str_replace(" ", "", $new_username4) != "") {
  mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info4[u_id]')");
  if($result != "") { $result .= ", "; }
  $result .= "<b>$new_username4</b>";
 }
 if(str_replace(" ", "", $new_username5) != "") {
  mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info5[u_id]')");
  if($result != "") { $result .= ", "; }
  $result .= "<b>$new_username5</b>";
 }
$result = "<h2>$view_friends27</h2>".$result." $view_friends5";


echo $head;
echo "
$result
<br><br>
<form action='view_friends.php' method='POST'>
<input type='submit' class='button' value='$view_friends26'>
</form>
";
echo $foot;
exit();
} else {
$task = "addfriend";
}
}






if($task == "adddyn") {
$new_u = $_GET['new_u'];


if(str_replace(" ", "", $new_u) == "") { $is_error = "yes"; }

if(str_replace(" ", "", $new_u) != "") {
 if(mysql_num_rows(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_u'")) == 0) { $is_error = "yes"; }
 if(strtolower($new_u) == strtolower($user_info[username])) { $is_error = "yes"; }
 $friend_info1 = mysql_fetch_assoc(mysql_query("SELECT u_id FROM bhost_users WHERE username='$new_u'"));
 if(mysql_num_rows(mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_info1[u_id]'")) != 0) { $is_error = "yes"; }
}

if($is_error == "yes") { header("Location: view_friends.php"); exit(); }

mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_info1[u_id]')");

$result = "<h2>$view_friends27</h2><b>$new_u</b> $view_friends5";


echo $head;
echo "
$result
<br><br>
<form action='view_friends.php' method='POST'>
<input type='submit' class='button' value='$view_friends26'>
</form>
";
echo $foot;
exit();
exit();
}






if($task == "addfriend") {
if($is_error != "yes") {
 $new_username1 = "";
 $new_username2 = "";
 $new_username3 = "";
 $new_username4 = "";
 $new_username5 = "";
}
echo $head;
echo "
<h2>$view_friends6</h2>
$view_friends7
<br><br>
";
show_error($general_error); 
echo "
<table cellpadding='0' cellspacing='0' class='form'>
<form name='addfriend' action='view_friends.php' method='POST'>
<tr>
<td class='form1' valign='top'>$view_friends8</td>
<td class='form2'>
<input type='text' name='new_username1' class='text' value='$new_username1'><br>"; show_error($username1_error); echo "
<input type='text' name='new_username2' class='text' value='$new_username2'><br>"; show_error($username2_error); echo "
<input type='text' name='new_username3' class='text' value='$new_username3'><br>"; show_error($username3_error); echo "
<input type='text' name='new_username4' class='text' value='$new_username4'><br>"; show_error($username4_error); echo "
<input type='text' name='new_username5' class='text' value='$new_username5'>"; show_error($username5_error); echo "
</td>
</tr>
</table>

<br>

<table cellspacing='0' cellpadding='0'>
<tr>
<td><input type='submit' class='button' value='$view_friends9'></td>
<input type='hidden' name='task' value='doadd'>
</form>
<form action='view_friends.php' method='POST'>
<td>&nbsp;<input type='submit' class='button' value='$view_friends29'></td>
<input type='hidden' name='task' value='cancel'>
</form>
</tr>
</table>
";
echo $foot;
exit;
}











if($task == "add") {
$friend_u_id = $_GET['u_id'];
if($friend_u_id == "" OR $friend_u_id == "$user_info[u_id]") { exit(); }
$newfriend_rows = mysql_num_rows(mysql_query("SELECT * FROM bhost_users WHERE u_id='$friend_u_id'"));
$lookfordupe = mysql_num_rows(mysql_query("SELECT * FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend_u_id'"));
if($newfriend_rows != 1 OR $lookfordupe > 0) { exit(); }
mysql_query("INSERT INTO bhost_friends (u_id, friend_u_id) VALUES ('$user_info[u_id]', '$friend_u_id')");
}









if($task == "delete") {
$fr_id = $_GET['fr_id'];
mysql_query("DELETE FROM bhost_friends WHERE u_id='$user_info[u_id]' AND fr_id='$fr_id'");
}



if($task == "dodelete") {
$friends = mysql_query("SELECT fr_id FROM bhost_friends WHERE u_id='$user_info[u_id]'");

while($friend = mysql_fetch_assoc($friends)) {
$var = "delete_".$friend[fr_id];
$delete = $_POST[$var];
if($delete == "yes") {
mysql_query("DELETE FROM bhost_friends WHERE u_id='$user_info[u_id]' AND fr_id='$friend[fr_id]'");
}}
}










// FELLOW FRIENDS
$fellowfriends = mysql_query("SELECT bhost_friends.u_id, bhost_friends.fr_id, bhost_friends.friend_u_id, bhost_users.username, bhost_users.gender FROM bhost_friends LEFT JOIN bhost_users ON bhost_friends.u_id=bhost_users.u_id WHERE bhost_friends.friend_u_id='$user_info[u_id]' ORDER BY bhost_users.username");
$totalfellowfriends = mysql_num_rows($fellowfriends);

echo $head;
echo "
<script language='JavaScript'> 
<!-- 
var checkboxcount = 1;
function doCheckAll() {
if(checkboxcount == 0) {
with (document.list) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = false;
}}
checkboxcount = checkboxcount + 1;
window.document.list.check.alt='$view_friends11';
}
} else
with (document.list) {
for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox') {
elements[i].checked = true;
}}
checkboxcount = checkboxcount - 1;
window.document.list.check.alt='$view_friends12';
}}
function changeAlt(imgObj,newAlt){
imgObj.alt = newAlt;
}
// -->
</script>
";

$listhtml = "";
$count = 0;
$rows = 0;
while($friend = mysql_fetch_assoc($fellowfriends)) {
$count++;
$mut = mysql_num_rows(mysql_query("SELECT * FROM bhost_friends WHERE u_id='$user_info[u_id]' AND friend_u_id='$friend[u_id]'"));
if($mut == 0) {
$rows++;
if($rows % 2) { $c = "2"; } else { $c = "1"; }
if($friend[gender] == "Female") { $icon = "../images/user_female.gif"; } elseif($friend[gender] == "Male") { $icon = "../images/user_male.gif"; } else { $icon = "../images/user_nogender.gif"; }
$icon = "<a href='".url("profile", "", "$friend[username]")."' target='_blank'><img src='$icon' border='0' style='vertical-align: middle'></a>";
$listhtml .= "
<tr>
<td class='list"."$c"."a' NOWRAP>$icon <a href='".url("profile", "", "$friend[username]")."' target='_blank'>$friend[username]</a></td>
<td class='list"."$c"."b' align='right' NOWRAP><a href='view_friends.php?task=add&u_id=$friend[u_id]'>$view_friends24</a></td>
</tr>
";
}
}

$listhtml = "
<table cellpadding='0' cellspacing='0'>
<tr>
<td class='header2' width='280'>$view_friends22 </td>
<td class='header' NOWRAP>&nbsp;</td>
</tr>
$listhtml
";

if($rows > 0) {
echo "
<h2>$view_friends22</h2>
$view_friends23
<br><br>
$listhtml
</table>
<br>
";
}




// SHOW FRIENDS
$friends = mysql_query("SELECT fr_id, friend_u_id FROM bhost_friends WHERE u_id='$user_info[u_id]'");
$totalfriends = mysql_num_rows($friends);

echo "
<h2>$view_friends13</h2>
$view_friends25
<br><br>
";


if($totalfriends == 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr><td colspan='3' class='empty' width='353'>$view_friends16</td></tr>
</table>
<br>
";
} else {

echo "
<table cellpadding='0' cellspacing='0'>
<form name='list' action='view_friends.php' method='POST'>
<tr>
<td width='1' class='header2'><a href='javascript:doCheckAll()'><img src='../images/check.gif' id='check' border='0' alt='$view_friends11'></a></td>
<td class='header2' width='200'>$view_friends13 ($totalfriends)</td>
<td class='header2' width='50' align='center'>$view_friends14</td>
<td class='header' width='45'>&nbsp;</td>
</tr>
";

$count = 0;
while($friend = mysql_fetch_assoc($friends)) {
$mut = 0;
$count++;
if($count % 2) { $c = "2"; } else { $c = "1"; }
$friend_info = mysql_fetch_assoc(mysql_query("SELECT u_id, username, gender FROM bhost_users WHERE u_id='$friend[friend_u_id]'"));
$mut = mysql_num_rows(mysql_query("SELECT * FROM bhost_friends WHERE u_id='$friend_info[u_id]' AND friend_u_id='$user_info[u_id]'"));
if($mut > 0) { $mutual = "$view_friends17"; } else { $mutual = "$view_friends18"; }

// SHOW ICON ACCORDING TO FRIENDS GENDER
if($friend_info[gender] == "Male") {
$icon = "../images/user_male.gif";
} elseif($friend_info[gender] == "Female") {
$icon = "../images/user_female.gif";
} else {
$icon = "../images/user_nogender.gif";
}
$icon = "<a href='".url("profile", "", "$friend_info[username]")."' target='_blank'><img src='$icon' border='0' class='icon'></a>";

echo "
<tr>
<td class='list"."$c"."a' style='padding: 3px;'><input type='checkbox' name='delete_$friend[fr_id]' value='yes'></td>
<td class='list"."$c"."a' NOWRAP>$icon <a href='".url("profile", "", "$friend_info[username]")."' target='_blank'>$friend_info[username]</a></td>
<td class='list"."$c"."a' NOWRAP align='center'>$mutual</td>
<td class='list"."$c"."b' align='right' NOWRAP><a href='view_friends.php?task=delete&fr_id=$friend[fr_id]'>$view_friends20</a></td>
</tr>
";
}
echo "</table>";
}

if($totalfriends > 0) {
echo "
<br>
<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='submit' class='button' value='$view_friends21'>&nbsp;</td>
<input type='hidden' name='task' value='dodelete'>
</form>
";
}

echo "
<form action='view_friends.php' method='POST'>
<td><input type='submit' class='button' value='$view_friends9'></td>
<input type='hidden' name='task' value='addfriend'>
</form>
";

echo "
</tr>
</table>

<script language='JavaScript'>
<!-- 
function windowonload() { window.document.addfriend.new_username.focus(); } 
// -->
</script>
";



echo $foot;










?>