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

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

$referrer_query=mysql_query("SELECT B.mem_userid, B.mem_username FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid AND B.mem_joindate < date_sub(current_date, interval dayofmonth(current_date)-1 day)") or die(mysql_error());
$referrer_TOTAL = mysql_num_rows($referrer_query);
$referrer_group_query=mysql_query("SELECT B.mem_userid, B.mem_username FROM adverts AS A, members AS B, members AS C, members AS D WHERE D.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=D.mem_userid And B.mem_referrer = C.mem_userid AND C.mem_referrer = D.mem_userid  AND B.mem_joindate < date_sub(current_date, interval dayofmonth(current_date)-1 day)") or die(mysql_error());
$referrer_group_TOTAL = mysql_num_rows($referrer_group_query);
$new_member_query=mysql_query("SELECT B.mem_userid, B.mem_username FROM adverts AS A, members AS B, members AS C WHERE C.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=C.mem_userid And B.mem_referrer = C.mem_userid AND B.mem_joindate >= date_sub(current_date, interval dayofmonth(current_date)-1 day) AND B.mem_joindate <= date_add(date_sub(current_date, interval dayofmonth(current_date) day), interval 1 month)") or die(mysql_error());
$new_member_TOTAL = mysql_num_rows($new_member_query);
$new_member_group_query=mysql_query("SELECT B.mem_userid, B.mem_username FROM adverts AS A, members AS B, members AS C, members AS D WHERE D.mem_userid = '$WUserID' AND A.adv_approved='1' And A.adv_userid=D.mem_userid And B.mem_referrer = C.mem_userid AND C.mem_referrer = D.mem_userid AND B.mem_joindate >= date_sub(current_date, interval dayofmonth(current_date)-1 day) AND B.mem_joindate <= date_add(date_sub(current_date, interval dayofmonth(current_date) day), interval 1 month)") or die(mysql_error());
$new_member_group_TOTAL = mysql_num_rows($new_member_group_query);

$invite_query=mysql_query("SELECT *, DATE_FORMAT(date, '%M %d, %Y at %h:%i%p') as date FROM networkinvite WHERE User_id = '$WUserID'") or die(mysql_error());
$invite_TOTAL = mysql_num_rows($invite_query);

?>
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb p5">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> Structure 
			Report (<?=ucwords($WUsername)?>)</td>
		</tr>
		<tr>
			<td><table width="100%" border="0" cellpadding="0" cellspacing="0" id="structureTable">
					<tr>
						<th width="45%" align="left" valign="baseline">Client Development </th>
						<th width="15%" align="center">Total Clients </th>
						<th width="20%" align="right">Volume </th>
						<th width="20%" align="right">Current Reward </th>
					</tr>
					<tr>
						<td valign="baseline"><?php if($referrer_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','clients');" title="All Clients" class="blue_b">Client Accumulated</a><?php } else echo "Client Accumulated"; ?> </td>
						<td align="center"><?php if($referrer_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','clients');" title="All Clients" class="blue_b"><?=$referrer_TOTAL?></a><?php } else echo $referrer_TOTAL; ?> </td>
						<td align="right">&nbsp;</td>
						<td align="right">&nbsp;</td>
					</tr>
					<tr>
						<td valign="baseline"><?php if($referrer_group_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','group_clients');" title="All Direct Group Clients" class="blue_b">Direct Group Client Accumulated</a><?php } else echo "Direct Group Client Accumulated"; ?> </td>
						<td align="center"><?php if($referrer_group_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','group_clients');" title="All Direct Group Clients" class="blue_b"><?=$referrer_group_TOTAL?></a><?php } else echo $referrer_group_TOTAL; ?></td>
						<td align="right">&nbsp;</td>
						<td align="right">&nbsp;</td>
					</tr>
					<tr>
						<td valign="baseline"><?php if($new_member_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','mclients');" title="All Current Month New Clients" class="blue_b">Monthly New Client</a><?php } else echo "Monthly New Client"; ?> </td>
						<td align="center"><?php if($new_member_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','mclients');" title="All Current Month New Clients" class="blue_b"><?=$new_member_TOTAL?></a><?php } else echo $new_member_TOTAL; ?></td>
						<td align="right">&nbsp;</td>
						<td align="right">&nbsp;</td>
					</tr>
					<tr>
						<td valign="baseline"><?php if($new_member_group_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','mgroup_clients');" title="All Current Month Direct Group New Clients" class="blue_b">Monthly Direct Group New Client</a><?php } else echo "Monthly Direct Group New Client"; ?> </td>
						<td align="center"><?php if($new_member_group_TOTAL>0){?><a href="javascript:;" onclick="javascript: callboxBottom('network_action.php','mgroup_clients');" title="All Current Month Direct Group New Clients" class="blue_b"><?=$new_member_group_TOTAL?></a><?php } else echo $new_member_group_TOTAL; ?></td>
						<td align="right">&nbsp;</td>
						<td align="right">&nbsp;</td>
					</tr>
					<tr>
						<th align="right">Total</th>
						<th align="right">&nbsp;</th>
						<th align="right">&nbsp;</th>
						<th align="right">&nbsp;</th>
					</tr>
				</table></td>
		</tr>
		<tr class="p0">
			<td><div id="sub_content">&nbsp;</div></td>
		</tr>
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td><div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10" /></div> Invitations History </td>
		</tr>
		<tr>
			<td><table width="100%" border="0" cellpadding="5" cellspacing="1" id="structureTable">
					<tr>
						<th width="25%">Email</th>
						<th width="15%">Date</th>
						<th width="40%">Status</th>
						<th width="10%">Action</th>
					</tr>
					<?php if ($invite_TOTAL > 0) { 
							 while ($invite = mysql_fetch_object($invite_query)) {
							 $query = "SELECT mem_userid FROM members WHERE mem_email='$invite->Email'";
							 $updateTOTAL = mysql_num_rows(mysql_query($query));
							 if ($updateTOTAL > 0) {
							 $update = mysql_fetch_object(mysql_query($query));
							 $query2 = "SELECT approved FROM my_friends WHERE uid='$WUserID' AND friend_uid='$update->mem_userid'";
//							 echo $query2;
							 $result = mysql_num_rows(mysql_query($query2));
							 if ($result > 0) {
							 $sql_array = mysql_fetch_object(mysql_query($query2));
							 if ($sql_array->approved == 0) {
							 $upd=mysql_query("UPDATE networkinvite set stat='i' where Email='$invite->Email' and inv_id='$invite->inv_id'");
							 $invite->stat = 'i';
							 }
							 if ($sql_array->approved == 1) {
							 $upd=mysql_query("UPDATE networkinvite set stat='f' where Email='$invite->Email' and inv_id='$invite->inv_id'");
							 $invite->stat = 'f';
							 }
							 } else {
							 $upd=mysql_query("UPDATE networkinvite set stat='j' where Email='$invite->Email' and inv_id='$invite->inv_id'");
							 $invite->stat = 'j';
							 }
							 }
							 ?>
					<tr id="<?=$invite->inv_id?>">
						<td valign="baseline" nowrap="nowrap"><?=$invite->Email?></td>
						<td valign="baseline" nowrap="nowrap"><?=$invite->date?></td>
						<td valign="baseline"><?php
									switch($invite->stat){

		case 'p':
			echo "Pending";
			break;

		case 'j':
			echo "User joined the site";
			break;

		case 'i':
			echo "User is already an existing site member. Your invitation was sent.";
			break;

		case 'f':
			echo "Member is your hahaper now.";
			break;

		case 'd':
			echo "Your invitation was declined by this user.";
			break;

	}//switch							 

								?></td>
						<td valign="baseline" nowrap="nowrap"><?php
															if ($invite->stat=="p") {
															echo "<a href=\"javascript: void(null)\" onclick=\"javascript: jQuery.facebox({ajax:'$CONST_LINK_ROOT/prop-me.php?mode=resend&inv_id=$invite->inv_id'}, 'Resend Invitation'); return false;\" rel=\"facebox\">resend</a> | ";
															}
															echo "<a href=\"javascript: void(null)\" onclick=\"javascript: jQuery.facebox({ajax:'$CONST_LINK_ROOT/prop-me.php?mode=delete&inv_id=$invite->inv_id'}, 'Delete Invitation'); return false;\" rel=\"facebox\">delete</a>";
															?></td>
					</tr>
					<?php } } else {?>
					<tr>
						<td colspan="4">No invitations were sent.</td>
					</tr>
					<?php } ?>
				</table></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
	</table>
