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

$id = $_POST['id'];
$name2 = $_POST['cName'];
$address2 = $_POST['address'];
$no2 = $_POST['cNo'];
$email2 = $_POST['email'];
$person2 = $_POST['person'];
$post2 = $_POST['post'];
$country2 = $_POST['country'];
$code2 = $_POST['code'];

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

$query=mysql_query("select * from company where id='$id' ");
		$display=mysql_fetch_array($query);
		
		$name=$display['cName'];
		$address=$display['address'];
		$post=$display['postcode'];
		$country=$display['country'];
		$code=$display['countryCode'];
		$no=$display['telNo'];
		$email=$display['email'];
		$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 company SET cName = '$name2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Company Name', '$name','$name2','$ldate','$ltime')");
	}
	if($address2!=$address){
		//-------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 company SET address = '$address2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Company Address', '$address','$address2','$ldate','$ltime')");
	}
	if($post2!=$post){
		//-------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 company SET postcode = '$post2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Company Address Postcode', '$post','$post2','$ldate','$ltime')");
	}
	if($country2!=$country){
		//-------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 company SET country = '$country2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Company country', '$country','$country2','$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 company SET countryCode = '$code2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Company 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 company SET telNo = '$no2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', '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 company SET email = '$email2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'Email', '$email','$email2','$ldate','$ltime')");
	}
	if($person2!=$person){
		//-------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 company SET cPerson = '$person2' WHERE id = '$id'");
		mysql_query("INSERT INTO editreport (recordID, editField, old, new, date, time)
		VALUES ('$number', 'In Charge Person', '$person','$person2','$ldate','$ltime')");
	}
			
mysql_close($login);

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