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

$option_manager = new OptionManager();
$currencysym = $option_manager->GetValue('currency_symbol');
$currency = $option_manager->GetValue('currency');
$currencypoint = $option_manager->GetValue('currency_point');

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

$now = getdate();

$mode = $_REQUEST['mode'];
$amount = $_REQUEST['amount'];
$paymnt_method = $_REQUEST['paymnt_method'];
$email = $_REQUEST['email'];
$username = $_REQUEST['username'];
$paymentRef = $_REQUEST['paymentRef'];
$paymentRefNo = strtoupper(substr(str_replace($replace,'',md5($WUsername.date('Y-m-d H:i:s'))), 0, 12));

if($mode=="AddFund" && $paymnt_method=="cash") {
$memberS_query=mysql_query("SELECT mem_email FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) WHERE adv_userid = '$WUserID' AND adv_approved='1'") or die(mysql_error());
$sqlS_array = mysql_fetch_object($memberS_query);

$q1_tra="select sum(amount) as balance from transactions where mem_userid=$WUserID and pstatus='approved'";
$row1_tra=mysql_fetch_array(mysql_query($q1_tra));
$balance=0;
if($row1_tra)
{
	$balance=(is_numeric($row1_tra["balance"]))?$row1_tra["balance"]:0;
}

$replace = array('O','0');
?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#addFund2").submit(function(){
	 	jQuery('#AddFundDIV').hide();
	 	jQuery('#sub_loading').show();

	var str = jQuery(this).serialize();
/*	alert(str);
*/
   		jQuery.ajax({
   		type: "POST",
   		url: "addFunds.php",
   		data: str,
   		success: function(result){
		jQuery('#sub_loading').hide();	
		jQuery('#AddFundDIV').html(result);
 		jQuery('#AddFundDIV').fadeIn('slow');
		}
 });

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

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb">
	<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>
			Add Cash to your Wauhaha Bank</td>
	</tr>
	<tr>
		<td align="center"><table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td align="left"><table border="0" cellspacing="0" cellpadding="0" class="p0">
							<tr>
								<td>Amount to be Added: </td>
								<td><strong><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=$amount?>
									for
									<?=$currency.number_format($amount*$currencypoint,2)?>
									</strong></td>
							</tr>
							<tr>
								<td class="p0">Current Balance: </td>
								<td><strong><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=$balance?>
									</strong></td>
							</tr>
							<tr>
								<td class="p0">Balance After Addition: </td>
								<td><strong class="blue_b"><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=($balance+$amount)?>
									</strong></td>
							</tr>
						</table></td>
				</tr>
				<tr>
					<td align="left">&nbsp;</td>
				</tr>
				<tr>
					<td align="left"><span class="red_12">Don't forget to take the following information with you:</span></td>
				</tr>
				<tr>
					<td align="left"><strong>Your Email:
						<?=$sqlS_array->mem_email?>
						</strong></td>
				</tr>
				<tr>
					<td align="left"><strong>Your Username:
						<?=$WUsername?>
						</strong></td>
				</tr>
				<tr>
					<td align="left"><strong>Payment Reference Number:
						<?=$paymentRefNo?>
						</strong></td>
				</tr>
				<tr>
					<td align="left">&nbsp;</td>
				</tr>
				<tr>
					<td align="left">Bank Transfer Information:</td>
				</tr>
				<tr>
					<td align="left"><strong>Bank: Public Bank Berhad </strong></td>
				</tr>
				<tr>
					<td align="left"><strong>Swift: PBBEMYKL</strong></td>
				</tr>
				<tr>
					<td align="left"><strong>Branch: Taman Munshi Ibrahim Johor Bahru</strong> </td>
				</tr>
				<tr>
					<td align="left"><strong>Account Name: Wauhaha Sdn. Bhd. </strong></td>
				</tr>
				<tr>
					<td align="left"><strong>Account Number: 3153513626 </strong></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td align="center"><form method="post" name="addFund2" id="addFund2">
							<input name="mode" type="hidden" value="AddCash" />
							<input name="amount" type="hidden" value="<?=$amount?>" />
							<input name="paymnt_method" type="hidden" value="cash" />
							<input name="email" type="hidden" id="email" value="<?=$sqlS_array->mem_email?>" />
							<input name="username" type="hidden" id="username" value="<?=$WUsername?>" />
							<input name="paymentRef" type="hidden" id="paymentRef" value="<?=$paymentRefNo?>" />
							<input type="submit" name="Submit" value="Confirm" class="submit" />
							<input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: jQuery.facebox.close(); return false;" />
						</form></td>
				</tr>
			</table></td>
	</tr>
</table>
<?php
	die;
}

if($mode=="AddCash" && $paymnt_method=="cash") {
$camount = $amount*$currencypoint;
$query="insert into transactions (mem_userid,amount,description,date_submitted,ptype,currency,currency_value,paymentRef,currency_rate) values ($WUserID,$amount,'Offline Payment (Cash or Cheque)','".date("YmdHis",time())."', 'PA','$currency', '$camount','$paymentRef','$currencypoint')";
//echo $query;
$result=mysql_query($query,$link) or die(mysql_error());

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb">
	<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>
			Add Cash to your Wauhaha Bank</td>
	</tr>
	<tr>
		<td align="center"><table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td><img src="images/iconPA_o.gif" border="0" align="absmiddle" /> <strong>
						<?=$amount?>
						</strong> has been added to your Wauhaha Bank with status <strong>Pending</strong>. Please keep track of your email and <strong>My Bank</strong> for the latest payment status. </td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td><span class="red_12">* Reminder: </span></td>
				</tr>
				<tr>
					<td>Wauhaha requires your payment to clear before service is granted. You will receive an email confirmation when this payment clears.</td>
				</tr>
				<tr>
					<td>If your information is missing, your payment will be delayed or rejected. </td>
				</tr>
				<tr>
					<td>Please submit only one payment per Payment Reference Number.</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td align="center"><form action="printCashInfo.php" method="post" name="printInfo" target="myWindow" id="printInfo" onsubmit="window.open('', 'myWindow', 'width=500,height=500,status=yes,resizable=no,scrollbars=no')">
							<input name="mode" type="hidden" value="PrintCash" />
							<input name="amount" type="hidden" value="<?=$amount?>" />
							<input name="paymnt_method" type="hidden" value="cash" />
							<input name="email" type="hidden" id="email" value="<?=$email?>" />
							<input name="username" type="hidden" id="username" value="<?=$username?>" />
							<input name="paymentRef" type="hidden" id="paymentRef" value="<?=$paymentRef?>" />
							<input type="submit" name="Submit" value="Print Out Your Information" class="submit" onclick="javascript: jQuery.facebox.close();" />
							<input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: jQuery.facebox.close(); return false;" />
						</form></td>
				</tr>
			</table></td>
	</tr>
</table>
<?php
	die;
}

if($mode=="AddFund" && $paymnt_method=="paypal") {
$memberS_query=mysql_query("SELECT mem_email FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) WHERE adv_userid = '$WUserID' AND adv_approved='1'") or die(mysql_error());
$sqlS_array = mysql_fetch_object($memberS_query);

$q1_tra="select sum(amount) as balance from transactions where mem_userid=$WUserID and pstatus='approved'";
$row1_tra=mysql_fetch_array(mysql_query($q1_tra));
$balance=0;
if($row1_tra)
{
	$balance=(is_numeric($row1_tra["balance"]))?$row1_tra["balance"]:0;
}

$replace = array('O','0');
$paymentRefNo = strtoupper(substr(str_replace($replace,'',md5($WUsername.date('Y-m-d H:i:s'))), 0, 12));
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb">
	<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>
			Add Funds Through PayPal</td>
	</tr>
	<tr>
		<td align="center"><table border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td align="left"><table border="0" cellspacing="0" cellpadding="0" class="p0">
							<tr>
								<td>Amount to be Added: </td>
								<td><strong><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=$amount?>
									for
									<?=$currency.number_format($amount*$currencypoint,2)?>
									</strong></td>
							</tr>
							<tr>
								<td class="p0">Current Balance: </td>
								<td><strong><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=$balance?>
									</strong></td>
							</tr>
							<tr>
								<td class="p0">Balance After Addition: </td>
								<td><strong class="blue_b"><img src="images/iconPA_o.gif" border="0" align="absmiddle" />
									<?=($balance+$amount)?>
									</strong></td>
							</tr>
						</table></td>
				</tr>
				<tr>
					<td align="left">&nbsp;</td>
				</tr>
				<tr>
					<td align="center">
					<form action="printCashInfo.php" method="post" name="paypal" target="myWindow" id="paypal" onsubmit="myWindow = window.open('', 'myWindow', 'width=500,height=500,status=yes,resizable=no,scrollbars=no'); myWindow.focus(); return false;">
						<input type="submit" name="Submit" value="Continue to Paypal Payment" class="submit" /><input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: jQuery.facebox.close(); return false;" />
					</form>
					
					<?php /*?><form method="post" action= "https://www.sandbox.paypal.com/cgi-bin/webscr" name="addFund3" id="addFund3" >
							<!--					<form method="post" name="addFund3" id="addFund3" action="https://www.paypal.com/cgi-bin/webscr">
-->
							<input type="hidden" name="cmd" value="_xclick">
							<input type="hidden" name="business" value="<?php echo $rs_a["_paypal_id"]; ?>">
							<input type="hidden" name="item_name" value="Add Funds to Wauhaha Bank">
							<input type="hidden" name="item_number" value="<?=$paymentRefNo?>">
							<input name="amount" type="hidden" id="amount" value="<?=$amount*$currencypoint?>" />
							<input type="hidden" name="currency_code" value="<?php echo $currency;?>">
							<input type="hidden" name="return" value="<?php echo $rs_a["site_root"]; ?>/thanks.php">
							<input type="hidden" name="cancel_return" value="<?php echo $rs_a["site_root"]; ?>/cancelpurchase.php">
							<input type="hidden" name="custom" value="<?=$WUsername?>">
							<input type="hidden" name="rm" value="2">
							<input type="hidden" name="notify_url" value="<?php echo $rs_a["site_root"]; ?>/ipn/ipn.php">
							<input type="hidden" name="no_note" value="1">
							<input type="hidden" name="description" value="Added Funds through PayPal">
							
							
						</form><?php */?></td>
				</tr>
			</table></td>
	</tr>
</table>
<?php
	die;
}


?>
<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#addFund").submit(function(){
/*	if(isNaN(jQuery("input[name='amount']:checked").val()) || (jQuery("input[name='amount']:checked").val()== "") || jQuery("input[name='amount']:checked").val()<=0)
	{
		alert ('Please choose a point package!');
		return false;
	}
*/
	if(isNaN(jQuery("input[name='amount']").val()) || (jQuery("input[name='amount']").val()== "") || jQuery("input[name='amount']").val()<=0)
	{
		alert ('Please specify a non-zero positive numeric value!');
		jQuery("input[name='amount']").focus();
		jQuery("input[name='amount']").select();
		return false;
	}
	if(jQuery("input[name='paymnt_method']:checked").val()== "")
	{
		alert ('Please choose a payment method!');
		return false;
	}

	 	jQuery('#AddFundDIV').hide();
	 	jQuery('#sub_loading').show();

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

   		jQuery.ajax({
   		type: "POST",
   		url: "addFunds.php",
   		data: str,
   		success: function(result){
		jQuery('#sub_loading').hide();	
		jQuery('#AddFundDIV').html(result);
 		jQuery('#AddFundDIV').fadeIn('slow');
		}

 });
return false;
    });
});

function roundNumber(oform, rnum) { 
  rnum = Math.ceil(rnum);
  var newnumber = Math.round(rnum);
  var newfloat =Math.round(rnum*<?=$currencypoint?>*Math.pow(10,2))/Math.pow(10,2);
  if (isNaN(newnumber)) newnumber=0;
  if (isNaN(newfloat)) newfloat=0;
  oform["amount"].value = parseInt(newnumber);
  jQuery('#ActualAmount').html('<?=$currency?>'+parseFloat(newfloat).toFixed(2));
 }
   </script>
<div class="p5 width_500">
	<div id="sub_loading" style="display:none;" align="center">&nbsp;</div>
	<div id="AddFundDIV">
		<table width="100%" border="0" cellpadding="0" cellspacing="0" class="white_back_t_xb">
			<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>
					Add Funds to my Wauhaha Bank</td>
			</tr>
			<tr class="p0">
				<td><form method="post" name="addFund" id="addFund">
						<table border="0" cellpadding="0" cellspacing="0" class="p5">
							<tr>
								<td align="left"><strong>How many  Point Available would 
								you like to add to your Wauhaha Bank?</strong></td>
							</tr>
							
							<tr>
								<td align="center" class="grey_20"><strong class="blue_b"><img src="images/iconPA_o.gif" border="0" align="absmiddle" /></strong>
									<input name="amount" type="text" style="height:30px; line-height:30px; font-size:20px; vertical-align:middle; text-align:center" onblur="roundNumber(this.form, this.value)" onclick="this.select(); roundNumber(this.form, this.value)" onchange="this.select(); roundNumber(this.form, this.value)" value="100" size="10" maxlength="7" />									<span id="ActualAmount"><?=$currency.number_format(100*$currencypoint,2)?></span></td>
							</tr>
							<tr>
								<td align="left" valign="baseline">&nbsp;</td>
							</tr>
							<tr>
								<td align="left" valign="baseline"><strong>How would you like to pay?</strong></td>
							</tr>
							<tr>
								<td align="center" valign="baseline"><!--<label>
									<input type="radio" name="paymnt_method" value="paypal" />
PayPal</label>
									&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->
									<label>
									<input name="paymnt_method" type="radio" value="cash" checked />
Cash/Cheque</label></td>
							</tr>
							<tr>
								<td align="center" valign="baseline">&nbsp;</td>
							</tr>
							
							<tr>
								<td align="center"><input type="submit" name="Submit" value="Submit" class="submit" />
									<input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: jQuery.facebox.close(); return false;" />
									<!--<input type="button" name="Button" value="Button" onclick="window.open('printCashInfo.php', 'myWindow', 'width=500,height=300,status=yes,resizable=no,scrollbars=no')" />--></td>
							</tr>
						</table>
					</form></td>
			</tr>
		</table>
	</div>
</div>
