<?php 

/***
 * Change password Layout
 * Author: Ritu
 * Date: 04-04-2024
 * This file contains all the code for Change password.
 * (If a user change his password then he can recover it by clicking on forgot password and all the code for the same is given in this file.)
 * 
 **/
include("config/web_mysqlconnect.php");// for database connection

$userid     =  $_GET['userid'];
$value      =  $_GET['value'];
if(!empty($_GET['comp'])){ $comp = $_GET['comp'];}
if(!empty($_SESSION['companyid'])){ $comp = $_SESSION['companyid'];}
$c=$_GET['c'];
$oldpassword=$_POST['oldpassword'];
//print $oldpassword."encrypted password"."<br>";
$compnayid=$_SESSION['companyid'];
 ?>
 <html>
   <head>
      <title>Change Password</title>
      <link rel="stylesheet" type="text/css" href="<?=$SiteURL?>public/css/styles.css"/>
      <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      <script language = "javascript" type="text/javascript">
         function callothers(){  
            $('.alert_text').text(''); 
            document.passwordfrm.npassword.value=hex_md5(sha1(hex_md5(document.passwordfrm.newpassword.value)));
            if(event.keyCode==13){
               frmsubmit(document.passwordfrm.newpassword.value,document.passwordfrm.confirmpassword.value,document.passwordfrm.oldpassword.value);
            } 
         }
         function frmsubmitNew(){
            $('.alert_text').text(''); 
            var pswlen = document.passwordfrm.newpassword.value;
            var confirmp = document.passwordfrm.confirmpassword.value;
            var oldp = document.passwordfrm.password.value;
            var opassword = document.passwordfrm.opassword.value;
            if(opassword){
               var opassword  = hex_md5(sha1(hex_md5(passwordfrm.opassword.value)));
            }                     
            var userid = document.passwordfrm.userid.value;                
            $.ajax({
               type: 'post',
               url: 'CRM/web_function.php',
               data: {'userid':userid,'password':pswlen,'confirmpassword':confirmp,'oldpassword':opassword,'oldpassdb':oldp,'action':'PasswordValidation'},
               success: function(response) {
                  response = JSON.parse(response);
                  console.log(response.status);
                  console.log(response.message);
                  if (response.status == false) {
                     $('.alert_text').text(response.message);
                  }else{
                     $('.alert_text').text(''); 
                     // document.passwordfrm.action="web_newpassword.php";
                     // document.passwordfrm.submit(); 
                     $.ajax({
                       url: 'function/web_function_change_password.php',
                       type: 'POST',
                       data: $('#passwordfrm').serialize(),
                       dataType: 'json',
                       success: function (response) {
                        console.log(response);
                        if(response.status == false){
                           $('.errormsglogin').text(response.message);
                        }else{
                           console.log($('.setting_password').val());
                           if($('.setting_password').val() == '1'){
                              $('.change_password_div').html(response.html);
                              setInterval ( "refreshParent()", 2000 );
                           }else{
                              $('.errormsglogin').text(response.message);
                              window.location.href = response.location;
                           }
                        }
                       },
                       error: function (error) {
                           console.error('Error inserting data:', error);
                       }
                   });

                  }
               }
           })              
         }
         function refreshParent(){
           window.parent.location.reload();
         }
      </script>
      <script language="javascript" src='<?=$SiteURL?>public/js/md5.js' type="text/javascript"></script>
      <script language="javascript" src='<?=$SiteURL?>public/js/sha1.js' type="text/javascript"></script>
      <style>
         label{width: 188px!important;}
         .addinteraction-popup ul li em{ font-style:normal; font-weight:bold; float: none!important; padding:0; width: auto!important; text-align: left!important; }
          .alert_text {
            font-size: 12px;
            color: red;
            text-align: center;
            margin: 28px;
          }
      </style>
</head>
<body onload="document.passwordfrm.opassword.focus();">
<div class="change_password_div">
      <form method="post" name="passwordfrm" id="passwordfrm" >
         <div class="addinteraction-popup">
         <span class="breadcrumb_head" style="height:17px;padding:9px 16px">Change Password</span>
         <div id="msgview">
            <?php
                     $res=get_password($userid);
               while($row=mysqli_fetch_array($res))
               {
                	$password=$row['V_Password'];
               }	
               ?>
            <input type="hidden" name="action" value="change_password_flow" />
            <input type="hidden" name="uname" value="<?=$name?>" />
            <input type="hidden" name="password" value="<?=$password?>" />
            <input type="hidden" name="oldpassword" />
            <span class="alert_text"></span>		
            <ul style="padding: 14px;color: crimson;font-style: italic;">
               <li><em>*</em> Passsword should contain minimum of 8 characters and maximum of 16 characters.</li>
               <li><em>*</em> Password should contain al least one upper case, one special character.</li>
               <li><em>*</em> Password should be in alphanumeric.</li>
            </ul>
            <ul style="padding:14px;">
               <li>
                  <label>Please Enter Your Old Password <em>*</em></label>
                  <input type="password" name="opassword" value="" autocomplete="off" size="30" onblur='passwordfrm.oldpassword.value=hex_md5(sha1(hex_md5(passwordfrm.opassword.value)));'  class="input-style1"/>
               </li>
               <li>
                  <label>Enter New Password<em> *</em></label>
                  <input type="password" name="newpassword" id="newpassword" autocomplete="off" class="input-style1" size="30" >
               </li>
               <li>
                  <label>Confirm Password <em>*</em></label>
                  <input type="password" name="confirmpassword" value="" autocomplete="off" class="input-style1" size="30" onblur='passwordfrm.npassword.value=hex_md5(sha1(hex_md5(passwordfrm.newpassword.value)));' onkeypress="callothers();" />
                  <input type="hidden" name="npassword" id="npassword" />
               </li>
            </ul>
         </div>
         <?php session_start(); ?>
         <?php if(($_SESSION['user_group']=='080000') || ($_SESSION['user_group']=='0000')) { ?>
         <div class="button-all" style="padding:11px 0px 0px 211px" >
            <?php }else{ ?>
            <div class="button-all">
      		<center>
      			<?php } ?> 
      			<input name="abhipass" type="button" value="Update Password" class="button-orange1" onclick='frmsubmitNew();'>
      			<?php if(($_SESSION['user_group']=='080000') || ($_SESSION['user_group']=='0000')) { ?>
      			<?php } ?>
               <input type="hidden" name="action" value="change_password_flow" />
      			<input type="hidden" name="userid" value='<?=$userid?>' />
      			<input type="hidden" name="comp" value='<?=$comp?>' />
      			<input type="hidden" name="value" value='<?=$value?>' class="setting_password"/>
      		</center>
            </div>
         </div>
      </form>
   </div>
</body>
<?php
   function get_password($userid){
   global $db,$link;
   $query = mysqli_query($link, "select V_Password from $db.tbl_mst_user_company where I_UserID='$userid'");
   return $query;
}?>
