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

<?php
if(isset($_POST['send']))
{	
	$sql_check = "SELECT * FROM `pd_brokers` WHERE `password` = '".md5($_POST['old_password'])."' AND `user_id` = '".$_SESSION['USER_ID']."'";
	$exe_check = mysqli_query($connection,$sql_check);
	$num_check = mysqli_num_rows($exe_check);
	if($num_check>0)
	{
		$sql_update = "UPDATE `pd_brokers` SET `password` = '".md5($_POST['confirm_password'])."',`original_password` = '".$_POST['confirm_password']."' WHERE `user_id` = '".$_SESSION['USER_ID']."'";
		$exe_update = mysqli_query($connection,$sql_update);
		
		$_SESSION['cp_succ_msg'] = 'Password changed successfully.';
		header("location: change-password.php");
		exit;
	}
	else
	{
		$_SESSION['cp_fail_msg'] = 'Old password not found in database.';
		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 should be same");
				document.getElementById('confirm_password').focus();
				return false;
			}
		}
		</script>

		<!-- content area start -->
		<section id="about">
			<div class="container mt-10 pb-10 pt-0">		
				<div class="row">
					<div class="col-lg-4">
						<?php require_once("user_left_panel.php");?>
					</div>
					<div class="col-lg-8">
					    <h1 class="title mt-5 mb-15">Change Password</h1>
						<?php if(isset($_SESSION['cp_succ_msg'])) { ?>
						<p><img src="images/tick.png" alt="" /> <?php echo $_SESSION['cp_succ_msg'];?></p>
						<?php 
						unset($_SESSION['cp_succ_msg']);
						} 
						?>	
						<?php if(isset($_SESSION['cp_fail_msg'])) { ?>
						<p><img src="images/cross.png" alt="" /> <?php echo $_SESSION['cp_fail_msg'];?></p>
						<?php 
						unset($_SESSION['cp_fail_msg']);
						} 
						?>
						<form id="" action="" method="post" 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");?>