<?php

/**

** author:  aslapai
**
** date created: 26/06/2015
**
** this page serves ajax call to update password sent from function.js
*/




session_start();

include_once('../../../db_connect/db.php');
//include_once('../functions.php');
if(isset($_SESSION['admin_user_id']) AND isset($_SESSION['admin_group_id'])){
$admin_user_id=$_SESSION['admin_user_id'];
//collec the password
$old_passwords = $_POST['old_password'];
 $old_password = md5($old_passwords);


 $new_password = $_POST['new_password'];
$confirm_new_password = $_POST['confirm_new_password'];
if($new_password !=$confirm_new_password){
	echo $error='<div class="alert alert-success alert-dismissible" role="alert">
		<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		<strong>Alert!</strong> Password and confirm password mismatch, Thank you!.
	 </div>';

}else {
    $q1 = "SELECT password FROM admin_users  WHERE admin_user_id = '$admin_user_id'";
    $r1 = mysqli_query($con, $q1);
    $row1 = mysqli_fetch_assoc($r1);

     $old_password_db = $row1['password']; //coollect old pssword from db

     if($old_password == $old_password_db)
     {
     	//query
     	$q = "UPDATE admin_users SET password  = md5('$new_password') WHERE admin_user_id = '$admin_user_id'";
     	$r = mysqli_query($con, $q);

     	if(mysqli_affected_rows($con))
     	{
     		echo  '1';


     	}
     	else
     	{
				echo $error='<div class="alert alert-success alert-dismissible" role="alert">
					<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<strong>Alert!</strong> Your Passsword has NOT been changed, Thank you!.
				 </div>';

     	}

     }
     else
     {

			 echo $error='<div class="alert alert-success alert-dismissible" role="alert">
				 <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				 <strong>Alert!</strong> Please ensure the old password is a valid old password, Thank you!.
				</div>';
     		
     }
}









}



?>
