<?php
$PageSecurity = 1;


//include('config.php');
//include('includes/session.inc');
//include('includes/ConnectDB.inc');
//include('includes/function_lib.php');
//include('includes/SQL_CommmonFunctions.inc');
//include('includes/ConnectDB_postgres.inc');
//$s=new db_query();

$username1=DB_query("select realname from www_users where id='".$_SESSION['UserID']."'",$db);
$username=DB_fetch_array($username1);


?>
<?php

if(isset($_REQUEST['impsave']))
{
   // echo CryptPass($_REQUEST['old_pass']);
    $client=DB_fetch_assoc(DB_query("SELECT password  FROM `www_users` where id='".$_SESSION['UserID']."'",$db));

  if($client['password']==CryptPass($_REQUEST['old_pass']))
  {
   
       if(CryptPass($_REQUEST['newPwd'])==CryptPass($_REQUEST['confPwd']))
       {
             $sql = "UPDATE www_users SET password='" . CryptPass($_REQUEST['newPwd']) . "' WHERE id = '" . $_SESSION['UserID'] . "'"; 
            	$result = DB_query($sql,$db);
             echo '<script>alert("Password changed")</script>';  
       }
       else
       {
           echo '<script>alert("New password and Confirm password dont match")</script>';
       }
  }   
  else
  {
      echo '<script>alert("old password dont match")</script>';
  }
      
  
/*      $sql = "UPDATE www_users
				SET password='" . CryptPass($_POST['newPwd']) . "'
				WHERE id = '" . $_SESSION['UserID'] . "'"; 

			$result = DB_query($sql,$db, $ErrMsg, $DbgMsg);
                        if($result)
                        {
                            echo '<div class="success">Password Updated Succesfully!!</div>';
                            echo "<script>parent.window.close();
parent.window.opener.location.href='index.php'</script//>";	
                        }
                        else {
                            echo '<div class="error">Password Not Updated!!</div>';
                            echo "<script>parent.window.close();
parent.window.opener.location.href='index.php'</script//>";
                        }
 */  
}

?>
<script>
    function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
}
function old_pass_match(str)
{
if(trim(str.value)=='')
    {
        document.getElementById(str.id).value='';
        document.getElementById(str.id).focus();
        alert("Please Enter Old Password");
    }
    else{
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
               // alert(xmlhttp.responseText);
                if(xmlhttp.responseText=="false"){
                document.getElementById(str.id).value='';
                 document.getElementById(str.id).focus();
                alert("old password did not match");
                // document.getElementById('impsave').style.display='none';
                }
                else if(xmlhttp.responseText=="true")
                {
                   //  document.getElementById('impsave1').style.display='block';
                }
            //document.getElementById(hiddiv).innerHTML=xmlhttp.responseText;
            }
            }
            xmlhttp.open("POST","ajax_pass_change.php?old_pass="+str.value,true);
            xmlhttp.send();
            }

    }
    function validation(form)
    {
       if(trim(document.getElementById('old_pass').value)=='')
	{
	alert('Enter Old Password');
	document.getElementById('old_pass').focus();
	return false;
	} 
       if(trim(document.getElementById('newPwd').value)=='')
	{
	alert('Enter New Password');
	document.getElementById('newPwd').focus();
	return false;
	} 
       
       
       if(trim(document.getElementById('confPwd').value)=='')
	{
	alert('Enter Confirm Password');
	document.getElementById('confPwd').focus();
	return false;
	} 
       
       var newpasswd=document.getElementById('newPwd').value;
       var confpasswd=document.getElementById('confPwd').value;
       if(newpasswd!='' && confpasswd!='')
           {
               if(newpasswd!=confpasswd)
                   {
                       alert('Password Do Not Matches');
                       document.getElementById('newPwd').value='';
                        document.getElementById('confPwd').value='';
                       document.getElementById('newPwd').focus();
                       return false;
                   }
               
           }
           return true;
    }
</script>
<style>
    .error
    {
        background-color: rosybrown;
        color: #d56b6b;
    }
    .success
    {
        background-color: greenyellow;
        color: green;
    }
</style>
<title>User Settings</title>
<link rel="stylesheet" href="css/new_mxerp.css">
<form  enctype="multipart/form-data" method="post" action="" onsubmit="return validation(this.form);">
        <div class="body_contain">
        <div class="tab_div" style="border:none;">
        <div style="width:100%; margin:auto;">
        <fieldset><legend style="font-size:13px;"><strong>Change Password</strong></legend>
        <table width="100%" border="1" cellspacing="0" cellpadding="3">
        <tr class="row1">
            <td width="30%" align="left" valign="top"><strong>User Login</strong></td>
            <td align="left" valign="top"><?php echo $username['realname']; ?></td>
          </tr>
          <tr class="row1">
            <td width="30%" align="left" valign="top"><strong>Old Password<font color="#FF0000">*</font></strong></td>
            <td align="left" valign="top"><input type="password" name="old_pass" onchange="javascript:old_pass_match(this)" id="old_pass" style="width:60%;" value=""></td>
          </tr>
          <tr class="row1">
            <td width="30%" align="left" valign="top" nowrap><strong>New Password<font color="#FF0000">*</font></strong></td>
            <td align="left" valign="top"><input type="password" name="newPwd" id="newPwd" style="width:60%;" value=""></td>
          </tr>
          <tr class="row1">
            <td align="left" valign="top" nowrap><strong>Confirm Password<font color="#FF0000">*</font></strong></td>
            <td align="left" valign="top"><input type="password" name="confPwd" id="confPwd" style="width:60%;"></td>
          </tr>
          <!--<tr class="row1">
            <td colspan="2" align="left" valign="top">(If you leave the Password boxes empty your password will not changed)</td>
            </tr>-->
          <tr class="row1">
              <td id="impsave1" align="center" valign="top" style="text-align:center; display: " colspan="2"> <input type="submit" class="search" name="impsave" id="impsave"  value="Update" /><a href="index.php"><input type="button" class="search" name="impsave" value="Cancel" /></a></td>
            </tr>
        </table>
        </fieldset>
        </div>
        </div>

      
        </div>
</form>



