<?php
global $dbc;
if (isset($_POST["editUser"]))
{
	$fName = sanitize($_POST['fName']);
	$lName = sanitize($_POST['lName']);
	// $uName = sanitize($_POST['uName']);
	$pswd1 = sanitize($_POST['pswd1']);
	$pswd2 = sanitize($_POST['pswd2']);
	$security = $_POST['security'];
	$emailA = sanitize($_POST['emailA']);
	// echo 'b '. $pswd1 . ' c '. $pswd2.'<br />';
	// echo $fName . ' ' .$lName . ' ' . $uName . ' '.$pswd1 . ' ' . $pswd2 . ' '. $security;

	if ($fName == "")
	{
		$boolValidate = 0;
		$strValidation .= "A first name is required.<br />";
	}
	if ($lName == "")
	{
		$boolValidate = 0;
		$strValidation .= "A last name is required.<br />";
	}
	// if ($uName == "")
	// {
	// 	$boolValidate = 0;
	// 	$strValidation .= "A username is required.<br />";
	// }
	// echo "$pswd1 . $pswd2";
	if ($pswd1 != "")
	{
		
		if ($pswd1 == $pswd2) 
		{
			$p = SHA1(mysqli_real_escape_string ($dbc, $pswd1));
		} 
		else 
		{
			$strValidation .= 'Your passwords did not match.<br />';
			$boolValidate = 0;
		}
		
		
	}
	else
	{
		$p = $password;
	}
	// echo 'new '.$p .' old '. $password. '<br />';
	if ($security == 0)
	{
		$strValidation .= 'Please select a security level<br />';
	}
	if ($emailA == "")
	{
		$boolValidate = 0;
		$strValidation .= "An email address is required.<br />";
	}
	else
	{
		//verify email is in right format
		if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $emailA)) 
		{
			$emailA = mysqli_real_escape_string ($dbc, strtolower($emailA));
		} 
		else 
		{
			$strValidation .= 'Please enter a valid email address<br />';
			$boolValidate = 0;
		}	
	}
	
	if ($boolValidate == 1)
	{
		// echo 'all good';
		// $sql = "UPDATE tblUsers set strFirstName = '$fName', strLastName = '$lName', strUsername = '$uName', strPassword = '$p', intSecurityLevel = '$security', strEmailAddress = '$emailA', dateModified = NOW(), intModifiedBy = '$user_id' where intUserID = '$id'";

		$sql = "UPDATE tblUsers set strFirstName = '$fName', strLastName = '$lName', strUsername = '$emailA', strPassword = '$p', intSecurityLevel = '$security', dateModified = NOW(), intModifiedBy = '$user_id' where intUserID = '$id'";


		// echo "$sql";
		$result = getFromDatabase($sql);
		
		// mysqli_query($dbc, $sql) or die(mysqli_error($dbc));

		header("location:user-listing.php");

	}
}

?>