<?php 
require_once("header.php");
require_once("session_check.php");
?>

<?php
if(isset($_POST['send']))
{	
	$sqlCheck = "SELECT * FROM `sc_users` WHERE `password` = '".md5($_POST['old_password'])."' AND `user_id` = '".$_SESSION['USER_ID']."'";
	$sqlCheck = mysqli_query($connection,$sqlCheck);
	$numCheck = mysqli_num_rows($sqlCheck);
	if($numCheck > 0)
	{		
		$sqlUpdate = "UPDATE `sc_users` SET `password` = '".md5($_POST['new_password'])."',`original_password` = '".$_POST['new_password']."' WHERE `user_id` = '".$_SESSION['USER_ID']."'";
		$exeUpdate = mysqli_query($connection,$sqlUpdate);
		
		$_SESSION['msg_succ'] = 'Password changed successfully.';
		header("location: change-password.php");
		exit;
	}
	else
	{
		$_SESSION['msg_fail'] = 'Old password not found. Please try again!';
		header("location: change-password.php");
		exit;
	}
}
?>

			<script language="javascript" type="text/javascript">
			function form_validation()
			{
				if(document.getElementById('old_password').value == "")
				{
					alert("Please enter old password");
					document.getElementById('old_password').focus();
					return false;
				}

				if(document.getElementById('new_password').value == "")
				{
					alert("Please enter new password");
					document.getElementById('new_password').focus();
					return false;
				}
				
				if(document.getElementById('confirm_password').value == "")
				{
					alert("Please enter confirm password");
					document.getElementById('confirm_password').focus();
					return false;
				}
				
				if(document.getElementById('confirm_password').value != document.getElementById('new_password').value)
				{
					alert("New password and confirm password both should be same.");
					document.getElementById('confirm_password').focus();
					return false;
				}
			}
			</script>

			<!-- content area start -->
		<section id="about">
			<div class="container mt-0 pb-0 pt-5">	
				<div class="row">
					<div class="col-lg-12">
				        <h1 class="title mt-0 mb-10" style="text-align:center;">Change Password</h1>
						<?php if(isset($_SESSION['msg_succ'])) { ?>
						<p style="text-align:center; margin-bottom:10px;"><img src="images/tick.png" alt="" /> <?php echo $_SESSION['msg_succ'];?></p>
						<?php 
						unset($_SESSION['msg_succ']);
						} 
						?>	
						<?php if(isset($_SESSION['msg_fail'])) { ?>
						<p style="text-align:center; margin-bottom:10px;"><img src="images/cross.png" alt="" /> <?php echo $_SESSION['msg_fail'];?></p>
						<?php 
						unset($_SESSION['msg_fail']);
						} 
						?>
						<form id="" action="" method="post" enctype="multipart/form-data" class="contact-form text-right" onsubmit="return form_validation();">
						<div class="row">
							<div class="col-lg-12 form-group">
								<input name="old_password" id="old_password" placeholder="Enter Old Password *" class="common-input form-control" type="password">
							</div>
							<div class="col-lg-12 form-group">
								<input name="new_password" id="new_password" placeholder="Enter New Password *" class="common-input form-control" type="password">
							</div>
							<div class="col-lg-12 form-group">
								<input name="confirm_password" id="confirm_password" placeholder="Enter Confirm Password *" class="common-input form-control" type="password">
							</div>
							<div class="col-lg-12 form-group">
								<button class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px" type="submit" style="float: left;">Save</button>
							</div>
						</div>
						<input type="hidden" name="send" id="send" value="1" />
						</form>	
					</div>
				</div>
			</div>	
		</section>
		<!-- content area end -->
  
<?php require_once("footer.php");?>