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

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

$RUserID=clearInput($_REQUEST['RUserID']);
$RUserName = clearInput($_REQUEST['RUserName']);
$mode = $_REQUEST['mode'];

		if (isset($_REQUEST['WAuthEmail']) && ($_REQUEST['WAuthEmail']!='')) {
		$mail_list = implode($_REQUEST['WAuthEmail']);
		}

if($mode=="resend") {
$inv_id = $_REQUEST["inv_id"];
$invite_query=mysql_query("SELECT * FROM networkinvite WHERE inv_id = '$inv_id' and User_id='$WUserID'") or die(mysql_error());
$invite_TOTAL = mysql_num_rows($invite_query);
if ($invite_TOTAL > 0) {
	$user_array = mysql_fetch_object($invite_query);

	$subject=MESSAGE_FROM." ".ucwords($WUserName);
	
	send_mail ("$user_array->Email", "noreply@wauhaha.com", "$subject", "$user_array->message","text/html","ON");
	
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	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>";
	exit;
} else {
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	echo '<tr>';
	echo '<td align="center" class="red_10">Ops! An Error Has Occurred! Please refresh the page and try again!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>setTimeout(function() {  parent.jQuery.facebox.close(); }, 1000 );</script>";
exit;
}
}

if($mode=="delete") {
$inv_id = $_REQUEST["inv_id"];
$invite_query=mysql_query("SELECT * FROM networkinvite WHERE inv_id = '$inv_id' and User_id='$WUserID'") or die(mysql_error());
$invite_TOTAL = mysql_num_rows($invite_query);
if ($invite_TOTAL > 0) {
$query="DELETE FROM networkinvite WHERE inv_id = '$inv_id' and User_id='$WUserID'";
$result=mysql_query($query,$link) or die(mysql_error());
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	echo '<tr>';
	echo '<td align="center" class="red_10">The invitation was successfully deleted!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>setTimeout(function() {  parent.jQuery.facebox.close(); document.getElementById('$inv_id').className = 'none'; }, 1000 );</script>";
exit;
} else {
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	echo '<tr>';
	echo '<td align="center" class="red_10">Ops! An Error Has Occurred! Please refresh the page and try again!</td>';
	echo '</tr>';
	echo '</table>';
	echo "<script type='text/javascript'>setTimeout(function() {  parent.jQuery.facebox.close(); }, 1000 );</script>";
exit;
}
}

if($mode=="Up-sharing" && $mail_list=='') {
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	echo '<tr>';
	echo '<td align="center" class="red_10">Ops! Empty Value!</td>';
	echo '</tr>';
	echo '</table>';
exit;
}

if($mode=="Up-sharing" &&  $mail_list<>'' && $_REQUEST['propid']<>'') {
	$query="SELECT mem_fullname, mem_email FROM members WHERE mem_userid=$WUserID";
	$result=mysql_query($query,$link) or die(mysql_error());
	$sql_array = mysql_fetch_object($result);

	if($sql_array->mem_fullname!=''){ $subject=MESSAGE_FROM." ".ucwords($sql_array->mem_fullname); }
	else{ $subject=MESSAGE_FROM." ".ucwords($WUserName); }

	$query="SELECT mem_userid, mem_username, mem_fullname FROM members WHERE mem_userid='".$_REQUEST['propid']."'";
	$result=mysql_query($query,$link) or die(mysql_error());
	$user_array = mysql_fetch_object($result);

	foreach ( $_REQUEST['WAuthEmail'] as $key=>$value) {
	if (isset($value) && !empty($value)) {

 	$inv_id = md5($WUserID.$value.time());

    $data['Message'] = $_REQUEST['txtMessage'];
    $data['Username'] = $user_array->mem_username;
    $data['CompanyName'] = $CONST_COMPANY;
    $data['SiteUrl'] = "$CONST_LINK_ROOT/index.php?i=$inv_id";
    $data['ProfileUrl'] = "$CONST_LINK_ROOT/$user_array->mem_username&i=$inv_id";
    $mail_template = "Tip_A_Friend";

    list($type,$message) = getTemplateByName($mail_template,$data,getDefaultLanguage($user_array->mem_userid));
       
	$query="INSERT INTO networkinvite (User_id, Email, inv_id, date, stat, message, inv_userid) VALUES ('$WUserID', '$value', '$inv_id', NOW(), 'P', '$message', '$user_array->mem_userid')";
	$result=mysql_query($query,$link) or die(mysql_error());

	send_mail ("$value", "noreply@wauhaha.com", "$subject", "$message","text/html","ON");
        }
	}
		
	echo '<table width="500px" border=0 cellpadding="5" cellspacing="0" class="white_back_t_xb p5">';
	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;
}
?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#form_prop_mail").submit(function(){
	 	jQuery('#loading1').show();

	var str = 'mode=Up-sharing&' + jQuery(this).serialize();

   		jQuery.ajax({
   		type: "POST",
   		url: "prop-me.php",
   		data: str,
   		success: function(result){
		jQuery('#loading1').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 method="post" action="#" name='form_prop_mail' id='form_prop_mail' style="padding:0; margin:0; border:0;"><table width="500px" border="0" align="left" cellpadding="5" cellspacing="0" class="white_back_t_xb p5">
     
	<tr>
			  <td valign="baseline"><?=SENDMAIL_TO?>:</td>
                <td>
				<input type='text' name='WAuthEmail[]' class="button" style="width:200px" onchange="validate_username('CheckMail', this.value, 'prop_validate.php', 'prop_email1')"/><div id="prop_email1" class="red_10"> </div>
				<input type='text' name='WAuthEmail[]' class="button" style="width:200px" onchange="validate_username('CheckMail', this.value, 'prop_validate.php', 'prop_email2')"/><div id="prop_email2" class="red_10"> </div>
				<input type='text' name='WAuthEmail[]' class="button" style="width:200px" onchange="validate_username('CheckMail', this.value, 'prop_validate.php', 'prop_email3')"/><div id="prop_email3" class="red_10"> </div>
				<input type='text' name='WAuthEmail[]' class="button" style="width:200px" onchange="validate_username('CheckMail', this.value, 'prop_validate.php', 'prop_email4')"/><div id="prop_email4" class="red_10"> </div>
				<input type='text' name='WAuthEmail[]' class="button" style="width:200px" onchange="validate_username('CheckMail', this.value, 'prop_validate.php', 'prop_email5')"/><div id="prop_email5" class="red_10"> </div>
				
             <input type='hidden' name='propid' value='<?php print("$RUserID"); ?>' /></td>
    </tr>
	<tr>
	  <td valign="top"><?=SENDMAIL_MESSAGE?>:</td>
	  <td><textarea name="txtMessage" cols="" rows="10" style="width:400px"></textarea></td>
    </tr>
	
	<tr class="pink_row_back">
	  <td>&nbsp;</td>
	  <td><input name="mode" type="submit" class="button" value="Up-sharing">
	        <input name="cancel" type="button" id="cancel" value="Cancel" class="button" onclick="javascript: parent.jQuery.facebox.close(); return false;"/>
</td>
    </tr>
				  </table>
</form>
