<?php
include("includes/site_info.php");
include("session_handler.inc");
include('lib/lib.Functions.php');

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

$mode = $_REQUEST['mode'];
$RUserID = $_REQUEST['RUserID'];
$RUserName = $_REQUEST['RUserName'];
$flirtmsg = $_REQUEST['flirt-msg'];

$ntf_id = $_REQUEST['flirt_id'];

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

    $query="INSERT INTO notifications (ntf_senderid, ntf_receiverid, ntf_senderhandle, ntf_receiverhandle, ntf_dateadded, ntf_response, ntf_msg) VALUES ('$WUserID', '$RUserID','$WUserName', '$RUserName','$tempdate','W','$flirtmsg')";

    $result=mysql_query($query,$link) or die(mysql_error());

    $query="UPDATE notifications set ntf_response='F' where ntf_receiverid='$WUserID' and ntf_senderid='$RUserID'";

    $result=mysql_query($query,$link) or die(mysql_error());

		$RUser = mysql_fetch_object(mysql_query("SELECT mem_username, mem_profileid FROM members WHERE mem_userid=$RUserID"));
		if ($RUser->mem_profileid != null) { $male1_name1= $RUser->mem_profileid; }
		else { $male1_name1= $RUser->mem_username; }
		$activityText ="flirt <a href=\"".$SITE_MAIN_WWW_ROOT."/".$male1_name1."\">". ucwords($RUser->mem_username) ."</a>.";
		$query2="INSERT INTO activities_user (userID, activityID, activityText) values ('$WUserID', '4', '$activityText')";
		$result2=mysql_query($query2,$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'>setTimeout(function() { parent.jQuery.facebox.close(); }, 1000);</script>";
die;
}

if (isset($RUserID) && isset($WUserID) && ($mode=="req_flirt")) {
                $advQuery=mysql_query("SELECT * FROM adverts where adv_userid=$WUserID AND adv_approved=1",$link);
                $advNote=mysql_num_rows($advQuery);
                if ($CONST_FLIRT=='Y' && $advNote > 0) {
						 $NoteQuery=mysql_query("SELECT * FROM notifications where ntf_senderid='$WUserID' and ntf_receiverid='$RUserID' and ntf_response='W'",$link); 
                        $hadNote=mysql_num_rows($NoteQuery);
                        if ($hadNote > 0) {
 	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center">'.ucwords($RUserName).'</strong> has not respond your last flirt yet!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>setTimeout(function() { parent.jQuery.facebox.close(); }, 1000);</script>";
die;
                        }else {
	?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#form_flirt_me").submit(function(){
	 	jQuery('#loading1').show();

	var str = 'mode=Flirt&' + jQuery(this).serialize();

   		jQuery.ajax({
   		type: "POST",
   		url: "flirt_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>
<form action="#" name='form_flirt_me' id='form_flirt_me' method="post" style="padding:0; margin:0; border:0;">
<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">
  <tr>
  	<td align="left" valign="baseline">Message:</td>
    <td align="left"><textarea name="flirt-msg" cols="55" rows="3"></textarea></td>
  </tr>
  <tr>
  	<td align="left">&nbsp;</td>
    <td align="left">
	  <input type='hidden' name='RUserID' value='<?=$RUserID?>' />
	  <input type='hidden' name='RUserName' value='<?=$RUserName?>' />
	<input name="mode" type="submit" id="mode" value="Flirt" class="button"/>
      <input name="cancel" type="button" id="cancel" value="Cancel" class="button" onclick="javascript: jQuery.facebox.close(); return false;"/>	  </td>
  </tr>
</table>
</form>
<?php
	die;
                        }
                } elseif ($CONST_FLIRT=='Y' && $advNote < 1) {
	echo '<table width="100%" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb">';
	echo '<tr>';
	echo '<td align="center"><strong>Only profiles that are approved can send flirts.!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>setTimeout(function() { parent.jQuery.facebox.close(); }, 1000);</script>";
die;
                }
}

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

    $query="INSERT INTO notifications (ntf_senderid, ntf_receiverid, ntf_senderhandle, ntf_receiverhandle, ntf_dateadded, ntf_response, ntf_msg) VALUES ('$WUserID', '$RUserID','$WUserName', '$RUserName','$tempdate','W','$flirtmsg')";

    $result=mysql_query($query,$link) or die(mysql_error());

    $query="UPDATE notifications set ntf_response='F' where ntf_receiverid='$WUserID' and ntf_senderid='$RUserID'";

    $result=mysql_query($query,$link) or die(mysql_error());
	
		$RUser = mysql_fetch_object(mysql_query("SELECT mem_username, mem_profileid FROM members WHERE mem_userid=$RUserID"));
		if ($RUser->mem_profileid != null) { $male1_name1= $RUser->mem_profileid; }
		else { $male1_name1= $RUser->mem_username; }
		$activityText ="flirt <a href=\"".$SITE_MAIN_WWW_ROOT."/".$male1_name1."\">". ucwords($RUser->mem_username) ."</a>.";
		$query2="INSERT INTO activities_user (userID, activityID, activityText) values ('$WUserID', '4', '$activityText')";
		$result2=mysql_query($query2,$link) or die(mysql_error());

	echo 'Wow, Your request was sent!';
	echo "<script type='text/javascript'>setTimeout(function() { parent.jQuery.facebox.close(); window.location='#inbox';}, 1000);</script>";
die;
}
if($mode=="inbox_remove") {
    $query="Delete from notifications where ntf_id=$ntf_id";

    $result=mysql_query($query,$link) or die(mysql_error());

	echo 'O! Flirt has been removed!';
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>';
	echo "<script type='text/javascript'>setTimeout(function() { parent.jQuery.facebox.close(); }, 1000);</script>";

	die;

?>
