<?php session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin'])){ }else{ 
    die(
    "
    <div class='alert alert-danger'> Error 100. Try again.</div>
    "
    );
}
require_once("../j_conn/url.php");
require_once("../j_conn/dbc.php");
require_once("../j_incs/functions.php");
if(isset($_POST['wallet_type'])){ }else{ 
die(
"
<div class='alert alert-danger'> Error 101. Try again.</div>
"
);
}
?>
<script>
$('.rMe').each(function() {
	 if ($(this).val() == "") {
 	   $(this).css({"border-color": "brown", 
		 "border-width":"1px", 
		 "border-style":"solid"});
	 }else{
		$(this).css({"border-color": "#efefef", 
		 "border-width":"1px", 
		 "border-style":"solid"});	 
	 }
});
</script>
<?php 
//
$wallet_type = RMC($_POST['wallet_type']); 
check_length ($wallet_type,"2","40","Wallet Type","wallet_type");
//$old_password = RMC($_POST['old_password']); 
//check_length ($old_password,"4","40","Password","old_password");
//

$old_password = base64_encode("$hash$old_password"); 
//check password
$sql4="SELECT count(*) as sum from j_users WHERE j_id='".$id."' AND j_password='".$old_password."' AND  j_blocked < 1 AND j_deleted < 1"; //check 
$result4 = runSQL($sql4);
$data4 = mysqli_fetch_array($result4);
$sum4 = $data4['sum'];
if($sum4 > 0){
}else{
   // die("<div class='alert alert-danger'> Invalid Current Password. Try again.</div>
    //<script> $('[name=old_password]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    //");
}
//
$amount = RMC($_POST['amount']); $amount = TWO_DEC_CRYPTO($amount,$wallet_type);
$re_username = RMC($_POST['re_username']); 


if(empty($re_username)){ 
    die("<div class='alert alert-danger'> Please provide Recipient's Username. Try again.</div>
    <script> $('[name=re_username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

if($amount > 0){ }else{
    die("<div class='alert alert-danger'> Amount must be greater than zero. Try again.</div>
    <script> $('[name=amount]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

$sql25="SELECT * from j_users WHERE 
j_username='".$re_username."' AND j_blocked < 1 AND j_deleted < 1  AND j_status < 1
|| j_email='".$re_username."' AND j_blocked < 1 AND j_deleted < 1  AND j_status < 1
|| j_phone='".$re_username."' AND j_blocked < 1 AND j_deleted < 1  AND j_status < 1
"; //  
$result25 = runSQL($sql25);
$data25 = mysqli_fetch_array($result25);
$re_id = $data25['j_id'];
$re_currency = $data25['j_currency'];
$re_wallet = $data25['j_wallet'];
$re_wallet_usd = $data25['j_wallet_usd'];
$re_wallet_btc = $data25['j_wallet_btc'];
$re_wallet_eth = $data25['j_wallet_eth'];
$re_wallet_ltc = $data25['j_wallet_ltc'];
$re_username = $data25['j_username'];
$re_passport = $data25['j_passport'];
$re_email = $data25['j_email'];
$re_phone = $data25['j_phone'];
$re_firstname = $data25['j_firstname'];
$re_lastname = $data25['j_lastname'];
$re_name = "$re_firstname $re_lastname"; $re_name = trim($re_name);
//
$cd = get_cd($re_currency);
$currency_in_use = $cd[0];
$currency_code_in_use = $cd[1];
$currency_sort_in_use = $cd[2];


if($re_id==$id){ 
    die("<div class='alert alert-danger'> You can't transfer to yourself. Please provide Recipient's Username. Try again.</div>
    <script> $('[name=re_username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}


if(empty($re_firstname)){ 
    die("<div class='alert alert-danger'> Invalid  Recipient's Username. Try another.</div>
    <script> $('[name=re_username]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}
//
$_SESSION['re_username'] = $re_username;
$_SESSION['amount'] = $amount;
$_SESSION['wallet_type'] = $wallet_type;
?>
<script>
    $("#form").remove();
    $("#submit").attr('disabled', '');
 </script>

<p class="form_hide">
    <?php 
    echo "You are about to credit <b>".amount_w_c($amount,$wallet_type)."</b> to <b>$re_name</b> (<b>$re_username</b>)'s $wallet_type wallet"; 
    ?>
    <br/><br/>
    Recipient gets: <?php echo "<b>".amount_w_c($amount,$wallet_type)."</b>"; ?>
    <br/><br/>
    Re-enter your <b>account password</b> to complete transaction.
</p>
<form method="POST" action="" id="form2">
    <div class="form-group">
        <label class="font-weight-semibold">Account Password</label>
        <input type="password" class="form-control rMe" name="old_password" placeholder="Password"/>
    </div>
    
    <div class="form-group">
        <button id="submit2"  name="submit2" class="btn btn-primary btn-block"> Credit Wallet Now</button>
    </div>
</form>
<div id="formresult2"></div>
<br/>
<div class="form_hide cancel">
    <a href="" class="btn btn-danger m-b-20 m-r-20" href=""> Cancel Transaction  </a>
</div>

<script>
var form = "#form2";
var button = "#submit2";
var button_val = $(button).html();
var result = "#formresult2";
$(form).on('submit',(function(e) {
e.preventDefault();
$(button).attr('disabled', '');
$(button).html("<i class='anticon anticon-loading'></i> Processing");
$(result).html("<div class='alert alert-warning'> Taking too long? <a href=''>Reload </a></div>");
 $.ajax({
url: "process.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
if(data){
 $(button).removeAttr('disabled');
$(button).html(button_val);
$(result).html(data);
}else{
$(result).html("Error occurred.");
$(button).removeAttr('disabled');
$(button).html(button_val);
}
}
});
}));
</script>