<?php
session_start();
?>
<? 
	include("user.php");
	include("login.php");
//	session_start();
	
	if ($_SESSION['user']) {
		if ($_SESSION['admin'] >= 0)
			header("Location: index.php");
	}
	else
		header("Location: index.php");
	
	$user = getUser('ID',$_GET['id']);
	
	$changinginfo = isset($_POST['email']);
	if ($changinginfo) {
		$confirmed = confirmPassword($_SESSION['user'], $_POST['enterpass']);
		
		if ($confirmed) {
			$oldemail = $user['Email'];
			if (!get_magic_quotes_gpc()) {
				$fname = addslashes($_POST['fname']);
				$lname = addslashes($_POST['lname']);
				$email = addslashes($_POST['email']);
				$worked = updateUser($user['ID'], $fname, $lname, $email, $_POST['change'], $_POST['adminlevel']);
			}
			else
				$worked = updateUser($user['ID'], $_POST['fname'], $_POST['lname'], $_POST['email'], 
								 		$_POST['change'], $_POST['adminlevel']);
			if ($worked) {
				$user = getUser('ID', $user['ID']);
				$curUser = getUserName($_SESSION['user']);
				$curName = $curUser['FirstName']." ".$curUser['LastName'];
				if ($_POST['change']) {
					$body = "You are receiving this message because ".$curName." at Syllogistek has ".
							"changed your password.  You may now log in with the password:\n\n\t".
							$_POST['change']."\n\nIf you believe your password has been changed".
							" without your consent, please consult with your class instructor.".
							"\n\nThis message has been generated automatically.  Please do not".
							" reply to this message.";
					$body = wordwrap($body, 70);
					mail($oldemail, "Your Syllogistek password has been changed", $body, 
						 "From: donotreply@syll.net");
				}
				if ($oldemail != $_POST['email']) {
					$body = "You are receiving this message because ".$curName." at Syllogistek has ".
							"changed your email.  The new email associated with your account is:\n\n".
							$_POST['email']."\n\nIf you believe your email has been changed".
							" without your consent, please consult with your class instructor.".
							"\n\nThis message has been generated automatically.  Please do not".
							" reply to this message.";
					$body = wordwrap($body, 70);
					mail($oldemail, "Syllogistek: Your email has been changed", $body, 
						 "From: donotreply@syll.net");
				}
			}
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Edit User Information</title>

<script language="javascript">
<!-- 	
	function checkForm() {
		document.getElementById('noemail').style.display = 'none';
		document.getElementById('noname').style.display = 'none';
		document.getElementById('nomatch').style.display = 'none';
		var fname = document.getElementById('fname').value;
		var lname = document.getElementById('lname').value;
		var noNames = !fname || !lname;
		var email = document.getElementById('email').value;
		var index = email.indexOf('@');
		var wrong = index <= 0 || index == (email.length-1);
		var newpass = document.getElementById('change').value;
		var newpass2 = document.getElementById('confirm').value;
		var nomatch = newpass != newpass2;
		if (nomatch || wrong || noNames) {
			if (wrong)
				document.getElementById('noemail').style.display = 'block';
			if (noNames)
				document.getElementById('noname').style.display = 'block';
			if (nomatch)
				document.getElementById('nomatch').style.display = 'block';
			return false;	
		}
		return true;
	}
	
	function goBack() {
		window.location = '<? if (isset($_GET['return'])) echo($_GET['return']); else echo("users.php"); ?>';
	}
// -->
</script>

</head>

<? include("header.php"); ?>
Please make any changes necessary to the user below.  Your login password will be required<br />
to confirm any changes made.  After accepting any changes, use the 
<?
	if (strpos($_SERVER['HTTP_REFERER'], "editclass.php") !== false)
		echo("'Class Roster' button at the<br>bottom to return to the class roster page.");
	else
		echo("'View Users' button at the<br />bottom to return to the users page.");
?> 
<br />

<form name='info' id='info' submit='edituser.php?id=
	<? 
		echo($_GET['id']);  
		if (isset($_GET['return']))
			echo("&return=".$_GET['return']);
	?>' method='POST' onsubmit='return checkForm()'>
<table>
	<tr>
		<td></td>
		<td align='left' style='color:#FF3300' colspan=2>
		<?
			if ($changinginfo) {
				if($confirmed) {
					if ($worked)
						echo("Change successful!");
					else
						echo("Error in database query.<br>Please ensure that information is correct and ".
							 " the email entered is not already in use by another user.<br>If problem ".
							 "persists, please request that your server's database manager inspect it.");
				}
				else
					echo("Validation failed, no changes were made.");
			}
		?></td>
	</tr>
	<tr>
		<td align='right'><b>First Name:</b></td>
		<td align='left'><input type='text' id='fname' name='fname' 
								value='<? echo($user['FirstName']) ?>' /></td>
		<td align='left'><div id='noname' style='display:none; color:#FF3300'>Name fields required!</div></td>
	</tr>
	<tr>
		<td align='right'><b>Last Name:</b></td>
		<td align='left'><input type='text' id='lname' name='lname' 
								value='<? echo($user['LastName']) ?>' /></td>
	</tr>
	<tr>
		<td align='right'><b>Email:</b></td>
		<td align='left'>
			<input name='email' id='email' type='text' value='<? echo($user['Email']) ?>'/></td>
		<td align='left' style='color:#FF3300'>
			<div id='noemail' style='display:none'>Invalid email address!</div></td>
	</tr>
	<tr>
		<td align='right'><b>New password:</b></td>
		<td align='left'>
			<input id='change' name='change' type='password'/></td>
		<td align='left'>
			<div id='nomatch' style='color:#FF3300; display:none'>Fields do not match!</div>
		</td>
	</tr>
	<tr>
		<td align='right'><b>Confirm new password:</b></td>
		<td align='left'>
			<input id='confirm' name='confirm' type='password'/></td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold;'>Admin:</td>
		<td align='left'>
			<? if ($_SESSION['admin'] < -1) { ?>
				<select id='adminlevel' name='adminlevel'>
				<option value='0' <? if ($user['IsAdmin']==0) echo "selected" ?>>Student</option>
				<option value='-1' <? if ($user['IsAdmin']==-1) echo "selected" ?>>Instructor</option>
				</select>
			<? } else
				echo("Student") ?>
		</td>
	</tr>
	<tr>
		<td colspan=2 align='left'>To validate these changes you are required to provide your login password.</td>
	</tr>
	<tr>
		<td align='right'><b>Enter password:</b></td>
		<td align='left'><input id='enterpass' name='enterpass' type='password' /></td>
	</tr>
	<tr>
		<td></td>
		<td align='left'><input type='submit' value='Accept' />
		<?
			if (strpos($_SERVER['HTTP_REFERER'], "editclass.php") !== false)
				echo("<input type='button' value='Class Roster' onclick='goBack()' /></td>");
			else
				echo("<input type='button' value='View Users' onclick='goBack()' /></td>");
		?>
	</tr>
</table>
</form>
<? include("footer.php"); ?>
</html>
