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

$id = $_POST['id'];
$name2 = $_POST['cName'];
$code2 = $_POST['code'];
$no2 = $_POST['cNo'];
$email2 = $_POST['email'];
$ic2 = $_POST['ic'];


date_default_timezone_set ("Singapore"); 
		$ldate=date("d-m-Y", time());
		$ltime=date("h:i:sa", time());

$query=mysql_query("select * from agent where agentID='$id' ");
		$display=mysql_fetch_array($query);
		
		$name=$display['name'];
		$code=$display['countryCode'];
		$no=$display['contactNo'];
		$ic=$display['ic'];
		$email=$display['email'];
		$pic=$display['pic'];
		$person=$display['cPerson'];
		$rDate=$display['registerDate'];
		$eDate=$display['expireDate'];
	
	//--------------------------------------Validate for edit---------------------------------------//
	if($name2!=$name){
		//-------Auto increment ID--------------//
		$str = "SELECT Max(recordID) FROM editreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "R000001";
		}
		//--------------------------------------//
		mysql_query("UPDATE agent SET name = '$name2' WHERE agentID = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Agent Name', '$name','$name2','$ldate','$ltime')");
	}
	
	if($code2!=$code){
		//-------Auto increment ID--------------//
		$str = "SELECT Max(recordID) FROM editreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "R000001";
		}
		//--------------------------------------//
		mysql_query("UPDATE agent SET countryCode = '$code2' WHERE agentID = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Agent country code', '$code','$code2','$ldate','$ltime')");
	}
	if($no2!=$no){
		//-------Auto increment ID--------------//
		$str = "SELECT Max(recordID) FROM editreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "R000001";
		}
		//--------------------------------------//
		mysql_query("UPDATE agent SET contactNo = '$no2' WHERE agentID = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Agent Contact No.', '$no','$no2','$ldate','$ltime')");
	}
	if($email2!=$email){
		//-------Auto increment ID--------------//
		$str = "SELECT Max(recordID) FROM editreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "R000001";
		}
		//--------------------------------------//
		mysql_query("UPDATE agent SET email = '$email2' WHERE agentID = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Agent Email', '$email','$email2','$ldate','$ltime')");
	}
	if($ic2!=$ic){
		//-------Auto increment ID--------------//
		$str = "SELECT Max(recordID) FROM editreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "R000001";
		}
		//--------------------------------------//
		mysql_query("UPDATE agent SET ic = '$ic2' WHERE agentID = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'I/C', '$ic','$ic2','$ldate','$ltime')");
	}
			
mysql_close($login);

echo "<script language=javascript>alert('Edit successful!!');
		window.location='editAgent.php?id=$id'</script>";
	
?>