<?php
include("../session.php");
$user=$_SESSION['agent'];	

?>
<link href="header.css" rel="stylesheet" type="text/css"/>
<div class="headPass"><h3 class="password">Change Password</h3></div>
<form name="formOne">
<br/>
<table cellpadding=8 cellspacing=3 width="80%" align=center>
<tr >
	<td class=menu align=center>Current	password:</td>
	<td class=menu2><input type="password" name=currentP onchange="checkCurrent(this.value,user.value)" id="old"/>
	<div id="current"></div></td>
	</tr>
<tr >
	<td class=menu align=center>New	password:</td>
	<td class=menu2><input type="password" name=pass1 onchange="newPass1(this.value,pass2.value)" id="new1"/>
	<div id="pass1"></div></td>
	</tr>
<tr >
	<td class=menu align=center>Retype New password:</td>
	<td class=menu2><input type="password" name=pass2 onchange="newPass2(this.value,pass1.value)" id="new2"/>
	<div id="pass2"></div></td>
	</tr>
<tr>
	<td colspan=2><hr width=100%/>	</td>
	</tr>
<tr>
	<td colspan=2 align=center>
	<input type="hidden" name="user" value="<?php echo "$user";?>">
	<input type="button" value="Save Changes" onclick="finalCheck2(pass1.value,user.value);" />
	<input type="reset" value="Reset" onClick="empty()"></td>
	</tr>
</table>
</form>
<script type="text/javascript">
function finalCheck2(pass,user){
var data1 = document.getElementById("current")
var data2 = document.getElementById("pass1")
var data3 = document.getElementById("pass2")

if(data1.innerHTML!="OK"){
	document.formOne.currentP.value="";
	alert("Please fill in current password correctly")
	return
}
if(data2.innerHTML!="OK"){
	document.formOne.pass1.value="";
	alert("New Password can not less than 3 character")
	return
}
if(data3.innerHTML!="OK"){
	document.formOne.pass2.value="";
	alert("No match password with new password")
	return
	}

if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		//document.getElementById("current").innerHTML=xmlhttp.responseText;
		if(xmlhttp.responseText=="true"){
			alert("Change Password Successful");
			document.formOne.currentP.value="";
			document.formOne.pass1.value="";
			document.formOne.pass2.value="";
			document.getElementById("current").innerHTML = ""
			document.getElementById("pass1").innerHTML = ""
			document.getElementById("pass2").innerHTML = ""
			
		}
	}
}
xmlhttp.open('GET','editing.php?user='+user+'&pass='+pass,true);
xmlhttp.send();
	
	
}

function checkCurrent(passC,user){
	
	if(passC.length==0){
		document.getElementById("current").innerHTML = ""
	}
if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("current").innerHTML=xmlhttp.responseText;
		myObj=document.getElementById("current");
		
		if(xmlhttp.responseText=="OK"){
			myObj.style.color = "green";
		}else{
			myObj.style.color = "red";
		}
	}
}
xmlhttp.open('GET','checkCurrent.php?user='+user+'&q='+passC,true);
xmlhttp.send();
}
function newPass1(num1,num2){
	if(num1.length==0){
		document.getElementById("pass1").innerHTML=""
	}else if(num1.length<3){
		document.getElementById("pass1").innerHTML="*Password too short"
		myObj=document.getElementById("pass1");
		myObj.style.color = "red";
	}else{
		document.getElementById("pass1").innerHTML="OK"
		myObj=document.getElementById("pass1");
		myObj.style.color = "green";
		
		if(num2!=""){
		if(num1!=num2){
			document.getElementById("pass2").innerHTML="*No match password"
			myObj=document.getElementById("pass2");
			myObj.style.color = "red";
		}else{
			document.getElementById("pass2").innerHTML="OK"
			myObj=document.getElementById("pass2");
			myObj.style.color = "green";
		}
		}
	}

}
function newPass2(num2,num1){

	if(num2.length==0){
		document.getElementById("pass2").innerHTML=""
	}else{
		if(num2!=num1){
			document.getElementById("pass2").innerHTML="*No match password"
			myObj=document.getElementById("pass2");
			myObj.style.color = "red";
		}else{
			document.getElementById("pass2").innerHTML="OK"
			myObj=document.getElementById("pass2");
			myObj.style.color = "green";
		}
	}

}

</script>
