<?php  session_start(); error_reporting(0); 
//if session
if(isset($_SESSION['j_admin']) || 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['old_password'])){ }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 
//
$old_password = RMC($_POST['old_password']); 
check_length ($old_password,"1","30","Current Password","old_password");
//
$new_password = RMC($_POST['new_password']); 
check_length ($new_password,"4","30","New Password","new_password");
//
$new_password2 = RMC($_POST['new_password2']); 
check_length ($new_password2,"4","30","Confirm New Password","new_password2");
//
if($new_password==$new_password2){
    //do nothing
}else{
    die("<div class='alert alert-danger'> Passwords do not match. Try again.</div>
    <script> $('[name=new_password]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    <script> $('[name=new_password2]').css({'border-color': 'brown', 'border-width':'1px', 'border-style':'solid'});</script>
    ");
}

//hash password
$old_password = base64_encode("$hash$old_password");  
$new_password = base64_encode("$hash$new_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>
    ");
}
$sql="UPDATE j_users SET j_password='".$new_password."' WHERE j_id='".$id."' AND j_password='".$old_password."'";
$result = runSQL($sql);
if($result){
//send mail
$u_email = $email;
$u_subject ="PASSWORD UPDATED";
$u_message ="<p>Hi $name;</p> <p>Your account password has been changed successfully.</p>";
require_once("../j_incs/mail.php");
//send-notify
send_notify("",$id,$u_subject,$u_message);
//
}else{
   die("<div class='alert alert-danger'> Error Occurred. Code 103. Try again later.</div>
    "); 
}	
session_unset();
session_destroy();
?>
<script>
    $("#form").remove();
     $("#submit").attr('disabled', '');
    //window.location.href="<?php //echo $dir; ?>login/";           
 </script>
<div class="card">
    <div class="card-body">
        <div class="container">
            <div class="row">
                 <div class="col-md-12" style="text-align:center;">
                    <div class="responseBox success">
                        <i class="anticon anticon-check"></i>
                    </div>
                     <p class="text-center"><?php echo $u_message; ?> </p>
                     <p class="text-center">
                         <a class="btn btn-primary btn-md" href="<?php echo $dir; ?>login/"> Login with new password</a>
                     </p>
                 </div>
            </div>
        </div>
    </div>
</div>