<?php
include("includes/site_info.php");
include("session_handler.inc");
include('lib/lib.Functions.php');
include_once __INCLUDE_CLASS_PATH."/class.ItemManager.php";
$option_manager = new OptionManager();
$CONST_MAIL = $option_manager->GetValue('mail'); 
$CONST_SUPPMAIL = $option_manager->GetValue('suppmail'); 

$WUserID = $_SESSION['Sess_UserId'];
$WUserName = $_SESSION['Sess_UserName'];

$mode = $_REQUEST['mode'];
$boxID = $_REQUEST['boxID'];
$FUserID = $_REQUEST['FUserID'];

$RUserID = $_REQUEST['RUserID'];
$RUserName = $_REQUEST['RUserName'];
$AUserID = $_REQUEST['AUserID'];
$AUserName = $_REQUEST['AUserName'];

$new_type = $_REQUEST['new_type'];
$folder = $_REQUEST['folder'];

function GetProPic ($userid, $username){
global $SITE_MAIN_WWW_ROOT, $SITE_WWW_ROOT;

if ($WUserID==$userid) {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$userid' and pic_default='Y'") or die(mysql_error()); 
} else {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$userid' and pic_default='Y' and pic_private='N'") or die(mysql_error()); 
}
$pics_count = mysql_num_rows($photo_query);
$aPicture = mysql_fetch_object($photo_query);
if ($pics_count > 0) {
	$medium = $aPicture->pic_picture."_small.".$aPicture->ext;
	$male1_picture1 = "$SITE_MAIN_WWW_ROOT/members/$username/picture/$medium";
             }else{
$male1_picture1=$SITE_WWW_ROOT."/".GetProfilePicture($userid);
}
return $male1_picture1;
}

function GetProfilePicture($USERID){
$quer=mysql_query("select mem_sex from members where mem_userid='$USERID'");
$show=mysql_fetch_object($quer);

if($show->mem_sex=='M') $main_pic='images/hahaper_m.jpg'; else $main_pic='images/hahaper_f.jpg';

return $main_pic; 
        
}
?>
<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('input[type=submit]').removeAttr("disabled");
	jQuery('input[type=submit]').click(function() {
	jQuery('#mode').val(jQuery(this).attr("value"));
	jQuery('#boxID').val(jQuery(this).parents(".facebox").attr("id"));
    jQuery("#form_label_me").submit(function(){
		jQuery('#loading1').show();

	var str = jQuery(this).serialize();

   		jQuery.ajax({
   		type: "POST",
   		url: "label-me.php",
   		data: str,
   		success: function(result){
		jQuery('#loading1').hide();	
		jQuery('#msg_content_form').hide();	
 		jQuery("#msg_content").fadeIn('slow');
 		jQuery('#msg_content').html(result);
		}

 });
return false;
    });
    });
});
   </script>

<div id="loading1" style="display:none;" align="center"><img src="images/loader.gif"></div>
<div id="msg_content" style="display:none;"></div>
<?php
if((isset($new_type) || isset($folder))  && ($mode=="Send Request")) {
		if (isset($new_type) && ($new_type!='')) {
		$query="INSERT INTO my_friend_folder (name, user_id) VALUES ('$new_type','$WUserID')";
		$result=mysql_query($query,$link) or die(mysql_error());
		$folder[] = mysql_insert_id();
		}
		
		$query="INSERT INTO my_friends (uid, friend_uid, status, approved) VALUES ('$WUserID', '$RUserID', 'P', '0')";
		$result=mysql_query($query,$link) or die(mysql_error());
		$id = mysql_insert_id();

		if (isset($folder) && ($folder!='')) {
		foreach ($folder as $folder_list) {
		$query="INSERT into my_friend_folder_list set fid='$id',uid='$WUserID', friend_uid='$RUserID', folder='$folder_list'";
		$result=mysql_query($query,$link) or die(mysql_error());
			}
		}
		
		
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center">Wow, Your request was sent!</strong></td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>";
	echo "document.getElementById('send_request_".$RUserID."').className = 'none'; document.getElementById('confirm_request_".$RUserID."').className = 'padding_b_10';"; 
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 
	
die;
}

if (isset($RUserID) && isset($WUserID) && ($mode=="request")) {
	if ($RUserID == $WUserID) {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center">Don\'t be silly!</td>';
	echo '</tr>';
	echo '</table>';
	die;
	} else {
	$retval=mysql_query("SELECT * FROM members WHERE mem_userid=$WUserID",$link);
	$sql_array = mysql_fetch_object($retval);
	$retval=mysql_query("SELECT * FROM my_friends WHERE (uid='$sql_array->mem_userid' AND friend_uid='$RUserID')",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);

	if ($result == 0) {
	
	$retval=mysql_query("SELECT * FROM members WHERE mem_userid=$WUserID",$link);
	$sql_array = mysql_fetch_object($retval);
	$retval2=mysql_query("SELECT * FROM my_friends WHERE friend_uid='$sql_array->mem_userid' AND uid='$RUserID'",$link);
	$sql_array2 = mysql_fetch_object($retval2);
	$result2=mysql_num_rows($retval2);

	if (($result2 != 0) && ($sql_array2->approved == 0)) {
	?>
<div id="msg_content_form">
	<form action="javascript:void(null);" name='form_label_me' id='form_label_me' style="padding:0; margin:0; border:0;">
		<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">
			<tr>
				<td rowspan="2" align="left" valign="top" class="content_right"><img src='<?=GetProPic ($RUserID, $RUserName)?>' hspace="5" vspace="5" class="img_b_p3" title='<?=ucwords($RUserName)?>' border="0"/></td>
				<td align="left">Confirm <strong>
					<?=$RUserName?>
					</strong>'s request!</td>
			</tr>
			<tr>
				<td align="left">Label: <br />
					<?php 
	$folder_query=mysql_query("SELECT * FROM my_friend_folder where (user_id=0 or user_id='$WUserID')order by name ASC ") or die(mysql_error());
	while($folder=mysql_fetch_array($folder_query)){?>
					<div style="float:left; width:50%; padding-bottom:5px;">
						<input type='checkbox' name='folder[]' value='<?=$folder['folder_id']; ?>' onclick='checkAllFields(2);' id='folder'>
						<?=$folder['name']; ?>
					</div>
					<?php }?>
					<div style="float:left; width:100%; padding-top:5px; padding-left:5px">Others,
						<input name="new_type" type="text" class="button"/>
					</div></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><div class="left solid_line"><img src="images/spacer.gif" width="1" height="1" /></div>
					<input type='hidden' name='AUserID' value='<?=$RUserID?>' />
					<input type='hidden' name='AUserName' value='<?=$RUserName?>' />
					<input type='hidden' id='mode' name='mode' value='' />
					<input type='hidden' id='boxID' name='boxID' value='' />
					<?php if (!empty($FUserID)) { ?>
					<input type='hidden' id='FUserID' name='FUserID' value='<?=$FUserID?>' />
					<?php } ?>
					<input name="bmode" type="submit" id="bmode" value="Approve" class="button" disabled="disabled"/>
					<input name="bmode" type="submit" id="bmode" value="Reject" class="button" disabled="disabled"/>
					<input name="cancel" type="button" id="cancel" value="Cancel" class="close button" onclick="javascript: jQuery.facebox.close(jQuery(this).parents('.facebox').attr('id')); return false;"/>
				</td>
			</tr>
		</table>
	</form>
</div>
<?php
	die;
		
		} else {

	?>
<div id="msg_content_form">
	<form action="javascript:void(null);" name='form_label_me' id='form_label_me' style="padding:0; margin:0; border:0; width:600px;">
		<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">
			<tr>
				<td rowspan="2" align="left" valign="top" class="content_right"><img src='<?=GetProPic($RUserID, $RUserName);?>' hspace="5" vspace="5" class="img_b_p3" title='<?=ucwords($RUserName)?>' border="0"/></td>
				<td align="left">Please allow <strong>
					<?=$RUserName?>
					</strong> to confirm your request!</td>
			</tr>
			<tr>
				<td align="left">Label: <br />
					<?php 
	$folder_query=mysql_query("SELECT * FROM my_friend_folder where (user_id=0 or user_id='$WUserID')order by name ASC ") or die(mysql_error());
	while($folder=mysql_fetch_array($folder_query)){?>
					<div style="float:left; width:50%; padding-bottom:5px;">
						<input type='checkbox' name='folder[]' value='<?=$folder['folder_id']; ?>' onclick='checkAllFields(2);' id='folder'>
						<?=$folder['name']; ?>
					</div>
					<?php }?>
					<div style="float:left; width:100%; padding-top:5px; padding-left:5px">Others,
						<input name="new_type" type="text" class="button"/>
					</div></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><div class="left solid_line"><img src="images/spacer.gif" width="1" height="1" /></div>
					<input type='hidden' name='RUserID' value='<?=$RUserID?>' />
					<input type='hidden' name='RUserName' value='<?=$RUserName?>' />
					<input type='hidden' id='mode' name='mode' value='' />
					<input type='hidden' id='boxID' name='boxID' value='' />
					<?php if (!empty($FUserID)) { ?>
					<input type='hidden' id='FUserID' name='FUserID' value='<?=$FUserID?>' />
					<?php } ?>
					<input name="bmode" type="submit" id="bmode" value="Send Request" class="button" disabled="disabled"/>
					<input name="cancel" type="button" id="cancel" value="Cancel" class="button" onclick="javascript:  jQuery.facebox.close(jQuery(this).parents('.facebox').attr('id')); return false;"/>
				</td>
			</tr>
		</table>
	</form>
</div>
<?php
	die;
	}
		}	elseif (($result != 0) && ($sql_array->approved == 0)) {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center"><strong>'.$RUserName.'</strong> was received your request already!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>";
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 
die;
		
		} else {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center"><strong>'.$RUserName.'</strong> was your friend already!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>";
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 

die;
		}	
	}
	
}
if (isset($AUserID) && isset($WUserID) && ($mode=="respond")) {
	if ($AUserID == $WUserID) {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center">Don\'t be silly!</td>';
	echo '</tr>';
	echo '</table>';
	die;
	} else {
	$retval=mysql_query("SELECT * FROM members WHERE mem_userid=$WUserID",$link);
	$sql_array = mysql_fetch_object($retval);
	$retval=mysql_query("SELECT * FROM my_friends WHERE friend_uid='$sql_array->mem_userid' AND uid='$AUserID'",$link);
	$sql_array = mysql_fetch_object($retval);
	$result=mysql_num_rows($retval);

	if ($result == 0) {
	?>
<div id="msg_content_form">
	<form action="javascript:void(null);" name='form_label_me' id='form_label_me' style="padding:0; margin:0; border:0; width:600px;">
		<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">
			<tr>
				<td rowspan="2" align="left" valign="top" class="content_right"><img src='<?=GetProPic ($AUserID, $AUserName)?>' hspace="5" vspace="5" class="img_b_p3" title='<?=ucwords($AUserName)?>' border="0"/></td>
				<td align="left">Please allow <strong>
					<?=$AUserName?>
					</strong> to confirm your request!</td>
			</tr>
			<tr>
				<td align="left">Label: <br />
					<?php 
	$folder_query=mysql_query("SELECT * FROM my_friend_folder where (user_id=0 or user_id='$WUserID')order by name ASC ") or die(mysql_error());
	while($folder=mysql_fetch_array($folder_query)){?>
					<div style="float:left; width:50%; padding-bottom:5px;">
						<input type='checkbox' name='folder[]' value='<?=$folder['folder_id']; ?>' onclick='checkAllFields(2);' id='folder'>
						<?=$folder['name']; ?>
					</div>
					<?php }?>
					<div style="float:left; width:100%; padding-top:5px; padding-left:5px">Others,
						<input name="new_type" type="text" class="button"/>
					</div></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><div class="left solid_line"><img src="images/spacer.gif" width="1" height="1" /></div>
					<input type='hidden' name='RUserID' value='<?=$AUserID?>' />
					<input type='hidden' name='RUserName' value='<?=$AUserName?>' />
					<input type='hidden' id='mode' name='mode' value='' />
					<input type='hidden' id='boxID' name='boxID' value='' />
					<?php if (!empty($FUserID)) { ?>
					<input type='hidden' id='FUserID' name='FUserID' value='<?=$FUserID?>' />
					<?php } ?>
					<input name="bmode" type="submit" id="bmode" value="Send Request" class="button" disabled="disabled"/>
					<input name="cancel" type="button" id="cancel" value="Cancel" class="button" onclick="javascript: jQuery.facebox.close(jQuery(this).parents('.facebox').attr('id')); return false;"/></td>
			</tr>
		</table>
	</form>
</div>
<?php
	die;
		}	elseif ($sql_array->approved == 0) {
	?>
<div id="msg_content_form">
	<form action="javascript:void(null);" name='form_label_me' id='form_label_me' style="padding:0; margin:0; border:0; width:600px;">
		<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">
			<tr>
				<td rowspan="2" align="left" valign="top" class="content_right"><img src="<?=GetProPic ($AUserID, $AUserName)?>" hspace="5" vspace="5" class="img_b_p3" title="<?=ucwords($AUserName)?>" border="0"/></td>
				<td align="left">Confirm <strong>
					<?=$AUserName?>
					</strong>'s request!</td>
			</tr>
			<tr>
				<td align="left">Label: <br />
					<?php 
	$folder_query=mysql_query("SELECT * FROM my_friend_folder where (user_id=0 or user_id='$WUserID')order by name ASC ") or die(mysql_error());
	while($folder=mysql_fetch_array($folder_query)){?>
					<div style="float:left; width:50%; padding-bottom:5px;">
						<input type='checkbox' name='folder[]' value='<?=$folder['folder_id']; ?>' onclick='checkAllFields(2);' id='folder'>
						<?=$folder['name']; ?>
					</div>
					<?php }?>
					<div style="float:left; width:100%; padding-top:5px; padding-left:5px">Others,
						<input name="new_type" type="text" class="button"/>
					</div></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><div class="left solid_line"><img src="images/spacer.gif" width="1" height="1" /></div>
					<input type='hidden' name='AUserID' value='<?=$AUserID?>' />
					<input type='hidden' name='AUserName' value='<?=$AUserName?>' />
					<input type='hidden' id='mode' name='mode' value='' />
					<input type='hidden' id='boxID' name='boxID' value='' />
					<?php if (!empty($FUserID)) { ?>
					<input type='hidden' id='FUserID' name='FUserID' value='<?=$FUserID?>' />
					<?php } ?>
					<input name="bmode" type="submit" id="bmode" value="Approve" class="button" disabled="disabled" />
					<input name="bmode" type="submit" id="bmode" value="Reject" class="button" disabled="disabled" />
					<input name="cancel" type="button" id="cancel" value="Cancel" class="button" onclick="javascript: jQuery.facebox.close(jQuery(this).parents('.facebox').attr('id')); return false;"/></td>
			</tr>
		</table>
	</form>
</div>
<?php
	die;
		
		} else {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center"><strong>'.$AUserName.'</strong> was your friend already!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>";
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 

die;
		}	
	}
	
}

if($mode=="Reject"){
	$app=mysql_fetch_object(mysql_query("SELECT mem_username, mem_email from members where mem_userid='$AUserID'"));
	$del=mysql_query("Delete from my_friends where uid='$AUserID' AND friend_uid='$WUserID'");
	$del=mysql_query("Delete from my_friend_folder_list where uid='$AUserID' AND friend_uid='$WUserID'");

			 $data['ReceiverName'] = $app->mem_username;
			 $data['Member'] = $WUserName;
             $data['Url'] = $CONST_URL;
			//$data['Reason'] = $reason;
             $data['SupportEmail'] = $CONST_SUPPMAIL;

		     $subject=$WUserName." has rejected your request at this moment.";

		    $tempdate=date("Y-m-d H:i:s");

           list($type,$message) = getTemplateByName("myFriendreject",$data,getDefaultLanguage($AUserID));
           send_mail ("$app->mem_email", "$CONST_MAIL", "$CONST_COMPANY Friend Request rejected.", $message,$type,"ON");

   $query="INSERT INTO messages (msg_senderid, msg_receiverid, msg_senderhandle, msg_title, msg_text, msg_dateadded, msg_read) VALUES ('".$WUserID."', '$AUserID', '".$WUserName."', '$subject', '$message', '$tempdate', 'U')";

    mysql_query($query,$link) or die(mysql_error());
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center"><strong>'.$app->mem_username.' has been rejected and currently have no connection with you!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>";
	echo "document.getElementById('accept_request_".$AUserID."').className = 'none'; document.getElementById('send_request_".$AUserID."').className = 'padding_b_10';"; 
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 

die;
}  

if((isset($new_type) || isset($folder))  && ($mode=="Approve")){
		if (isset($new_type) && ($new_type!='')) {
		$query="INSERT INTO my_friend_folder (name, user_id) VALUES ('$new_type','$WUserID')";
		$result=mysql_query($query,$link) or die(mysql_error());
		$folder[] = mysql_insert_id();
		}

$app=mysql_fetch_object(mysql_query("SELECT mem_username, mem_email from members where mem_userid='$AUserID'"));
$upd=mysql_query("UPDATE my_friends set approved='1', status='A' where uid='$AUserID' AND friend_uid='$WUserID'");

		$upd=mysql_query("INSERT into my_friends set uid='$WUserID', friend_uid='$AUserID', approved='1'");
		$id = mysql_insert_id();
		$RUser = mysql_fetch_object(mysql_query("SELECT mem_username, mem_profileid FROM members WHERE mem_userid=$AUserID"));
		if ($RUser->mem_profileid != null) { $male1_name1= $RUser->mem_profileid; }
		else { $male1_name1= $RUser->mem_username; }
		$activityText ="and <a href=\"".$SITE_MAIN_WWW_ROOT."/".$male1_name1."\">". ucwords($RUser->mem_username) ."</a> are now connected.";
		$query2="INSERT INTO activities_user (userID, activityID, activityText) values ('$WUserID', '6', '$activityText')";
		$result2=mysql_query($query2,$link) or die(mysql_error());
		$RUser2 = mysql_fetch_object(mysql_query("SELECT mem_username, mem_profileid FROM members WHERE mem_userid=$WUserID"));
		if ($RUser2->mem_profileid != null) { $male1_name2= $RUser2->mem_profileid; }
		else { $male1_name2= $RUser2->mem_username; }
		$activityText2 ="and <a href=\"".$SITE_MAIN_WWW_ROOT."/".$male1_name2."\">". ucwords($WUserName) ."</a> are now connected.";
		$query3="INSERT INTO activities_user (userID, activityID, activityText) values ('$AUserID', '6', '$activityText2')";
		$result3=mysql_query($query3,$link) or die(mysql_error());
		
if (isset($folder) && ($folder!='')) {
		$folder_list = '';

		foreach ($folder as $folder_list) {
		$query="INSERT into my_friend_folder_list set fid='$id',uid='$WUserID', friend_uid='$AUserID', folder='$folder_list'";
		$result=mysql_query($query,$link) or die(mysql_error());
			}
		}
			 $data['ReceiverName'] = $app->mem_username;
			 $data['Member'] = $WUserName;
             $data['Url'] = $CONST_URL;
			//$data['Reason'] = $reason;
             $data['SupportEmail'] = $CONST_SUPPMAIL;

		     $subject=$WUserName." has accepted your request.";

		    $tempdate=date("Y-m-d H:i:s");

            list($type,$message) = getTemplateByName("myFriend",$data,getDefaultLanguage($AUserID));
            send_mail ("$app->mem_email", "Wauhaha <$CONST_MAIL>", "$CONST_COMPANY ".PRGAUTHFREN_APR, $message,$type,"ON",'outside','UTF-8');//mail
            send_mail ("$app->mem_email", "", $subject, $message,"","","inside"); //inbox
			
	echo'<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo'<tr>';
	echo'<td align="center"><strong>'.$app->mem_username.' is connect with you now!</td>';
	echo'</tr>';
	echo'</table>';
	echo "<script type='text/javascript'>";
	echo "document.getElementById('accept_request_".$AUserID."').className = 'none'; document.getElementById('send_request_".$AUserID."').className = 'none';"; 
	echo "setTimeout(function() { jQuery.facebox.close(); }, 1000);"; 
	echo "</script>"; 

	die;
}

	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center">Ops! An Error Has Occurred! Please try again!</strong></td>';
	echo '</tr>';
	echo '</table>';

	die;

?>
