<?php
	session_start();
	include("connect.php");
	
	$userID=$_POST["userID"];
	$pass=$_POST["password"];

	isset($_SESSION['company']);
	isset($_SESSION['admin']);
	isset($_SESSION['agent']);
	isset($_SESSION['start']);
	isset($_SESSION['expire']);
	//isset($_SESSION['session_id']);
		
	//------------------------------IP address--------------------------------//
	$ip=$_SERVER['REMOTE_ADDR'];
	$_SESSION['start'] = time(); 
	$logindate = time();
	$_SESSION['expire'] = $_SESSION['start'] + (20 * 60) ;
	$logindate2 = $_SESSION['expire'];
	$tt = mysql_query("select * from logincontrol where id='$userID'");
	$ttt = mysql_fetch_array($tt);
	$getcurrent1=$ttt['ipaddress'];
	$getcurrent2=$ttt['starttime'];
	$getcurrent3=$ttt['status'];
	
	//-------------------------Date & Time---------------------------//
	date_default_timezone_set ("Singapore"); 
		$ldate=date("d-m-Y", time());
		$ltime=date("h:i:sa", time());
		
	//-------Auto increment ID--------------//
	$str = "SELECT Max(recordID) FROM loginreport";
	
		$result2 = mysql_query ($str,$login);
		list($result2)=mysql_fetch_array($result2);
		$number = ++$result2;
			
		if($number == "1")
		{
			$number = "L000001";
		}
	//--------------------------------------//
		
	$check=substr($userID,0,1);
	
	if(($check=="a")||($check=="A")){
		//echo "Welcome ".$_SESSION['admin'];
		$result=mysql_query("select * from admin where adminID='$userID' and password='$pass'");
		if(mysql_num_rows($result) == 1 ){
			if ($getcurrent1 != $ip){
				$_SESSION['admin']= $userID;
				mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
				mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
				VALUES ('$number', 'Admin','$ip','$ldate','$ltime')");
				echo"<script language=javascript>window.location='admin/admin.php'</script>";
			}
			elseif($getcurrent1 == $ip){
				if($getcurrent3 == "1"){
					if(($logindate-$getcurrent2) > 1){
						$_SESSION['admin']= $userID;
						mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
						mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
						VALUES ('$number', 'Admin','$ip','$ldate','$ltime')");
						echo"<script language=javascript>window.location='admin/admin.php'</script>";
					}
					else{
						echo "<script language='javascript'>alert ('You have logged in at another pc,please log out first.')</script>";
						session_destroy();
						session_unset();
						echo "<script language='javascript'>window.location='login.php'</script>";die;
					}
				}
				elseif($getcurrent3 == "0"){
					$_SESSION['admin']= $userID;
					mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
					mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
					VALUES ('$number', 'Admin','$ip','$ldate','$ltime')");
					echo"<script language=javascript>window.location='admin/admin.php'</script>";
				}
			
			}
		}
		else{
				echo "<script language=javascript>alert('failed login!!');window.location='login.php'</script>";
			}
	}
	else if($check=="C"){
		//echo "Welcome ".$_SESSION['company'];
		$resultlogin = mysql_query("select * from company where id='$userID' and password='$pass'");
		if(mysql_num_rows($resultlogin) == 1 ){
			if ($getcurrent1 != $ip){
				$_SESSION['company']= $userID;
				mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
				mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
					VALUES ('$number', '$userID','$ip','$ldate','$ltime')");					
				echo"<script language=javascript>window.location='company/company.php'</script>";
			}
			elseif($getcurrent1 == $ip){
				if($getcurrent3 == "1"){
					if(($logindate-$getcurrent2) > 1){
						$_SESSION['company']= $userID;
						mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
						mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
							VALUES ('$number', '$userID','$ip','$ldate','$ltime')");					
						echo"<script language=javascript>window.location='company/company.php'</script>";
					}
					else{
						echo "<script language='javascript'>alert ('You have logged in at another pc,please log out first.')</script>";
						session_destroy();
						session_unset();
						echo "<script language='javascript'>window.location='login.php'</script>";die;
					}
				}
				elseif($getcurrent3 == "0"){
					$_SESSION['company']= $userID;
					mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
					mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
						VALUES ('$number', '$userID','$ip','$ldate','$ltime')");					
					echo"<script language=javascript>window.location='company/company.php'</script>";
				}
			
			}
		}
		else{
			echo "<script language=javascript>alert('failed login!!');window.location='login.php'</script>";
		}
	}
	else if($check=="G"){
		//echo "Welcome ".$_SESSION['agent'];
		$result=mysql_query("select * from agent where agentID='$userID' and password='$pass'");
		if(mysql_num_rows($result) == 1 ){
			if ($getcurrent1 != $ip){
				$_SESSION['agent']= $userID;
				mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
				mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
				VALUES ('$number', '$userID','$ip','$ldate','$ltime')");				
				echo"<script language=javascript>window.location='agent/agent.php'</script>";
			}
			elseif($getcurrent1 == $ip){
				if($getcurrent3 == "1"){
					if(($logindate-$getcurrent2) > 1){
						$_SESSION['agent']= $userID;
						mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
						mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
						VALUES ('$number', '$userID','$ip','$ldate','$ltime')");				
						echo"<script language=javascript>window.location='agent/agent.php'</script>";
					}
					else{
						echo "<script language='javascript'>alert ('You have logged in at another pc,please log out first.')</script>";
						session_destroy();
						session_unset();
						echo "<script language='javascript'>window.location='login.php'</script>";die;
					}
				}
				elseif($getcurrent3 == "0"){
					$_SESSION['agent']= $userID;
					mysql_query("update logincontrol set ipaddress='$ip',starttime='$logindate',endtime='$logindate2',status='1' where id='$userID'");
					mysql_query("INSERT INTO loginreport (recordID, user, ip, date, time)
					VALUES ('$number', '$userID','$ip','$ldate','$ltime')");				
					echo"<script language=javascript>window.location='agent/agent.php'</script>";
				}
			
			}
		}
		else{
				echo "<script language=javascript>alert('failed login!!');window.location='login.php'</script>";
			}
	}
	else{
		echo"<script language=javascript>alert('Invalid ID!');window.location='login.php'</script>";
	}
	
	
	

?>
