<?php 
		include('header.php');
		include('config.php');
			$query = "select * from `tbladmin` where `id`='".$_SESSION['id']."';";
			$result = mysql_query($query);
			$rows = mysql_num_rows($result);
			if ($rows >0) 
			{		
				while($row=mysql_fetch_array($result))
				{
				?>
				<div class="InnerContainer" >            
				<h2> Account Setting</h2>
				<div class="Border"></div>
				<form name="edit_form" id="edit_form" method="post">
					<table align="center" cellpadding="5" cellspacing="0" border="0" width="40%" class="ListContent MediumSize" style="text-align:center;">
						<tr>    
							<th colspan="2" align="left">Edit Account</th>                          
						</tr>
						<tr>
							<td align="right">UserName: </td>
							<td align="left"><input type="text" name="name" id="name" value="<?php echo $row['username']; ?>" /></td>
						</tr>
						<tr class="ListColor">
							<td align="right">Email :</td>
							<td align="left"><input type="text" name="email" id="email" value="<?php echo $row['email']; ?>" /></td>
						</tr>
						
						<tr class="ListColor">
							<td align="right">Address: </td>
							<td align="left"><input type="text" name="address" id="address" value="<?php echo $row['address']; ?>" /></td>
						</tr>
						<tr>
							<td align="right">Old Password: </td>
							<td align="left"><input type="password" name="oldpassword" id="oldpassword" /></td>
						</tr>
						<tr class="ListColor">
							<td align="right">New Password: </td>
							<td align="left"><input type="password" name="newpassword" id="newpassword" /></td>
						</tr>
						<tr>
							<td colspan="2"><input class="Submit" type="submit" name="submit" value="Save" />&nbsp;&nbsp;<a href="lenders.php"><input class="Cancel" type="button" name="cancel" value="cancel" /></a></td>
						</tr>
					</table>
				</form>
				<?php
				}
			}
				?>
			</div>
				<script  type="text/javascript">
					 var frmvalidator = new Validator("edit_form");
					 frmvalidator.addValidation("name","req","Please enter your Username");
					 frmvalidator.addValidation("email","req","Please enter email");
					 frmvalidator.addValidation("email","email","Please enter valid email");
					 frmvalidator.addValidation("address","req","Please enter address");
				</script>
			<?php
			//echo "<pre>";
			//print_r($_SESSION);
			//die;

			if(isset($_POST['submit']))
			{
				if(empty($_POST['oldpassword']) && empty($_POST['newpassword']))
				{
					$query="update `tbladmin` set `username`='".$_POST['name']."',`email`='".$_POST['email']."',`address`='".$_POST['address']."' where `id` = '".$_SESSION['id']."'";
					$result = mysql_query($query);
					if($result)
					{
						$_SESSION['username'] = $_POST['name'];
						echo "<script language='javascript'>alert('Updated successfully')</script>";
						echo "<script>window.location='lenders.php';</script>";
					}
				}
				elseif(empty($_POST['oldpassword']) || empty($_POST['newpassword']))
				{
					echo "<script language='javascript'>alert('Please enter both passwords')</script>";
				}
				else
				{
					$query="select * from `tbladmin` where `id`='".$_SESSION['id']."';";
				
					$result = mysql_query($query);
					if($result)
					{
						$row=mysql_fetch_array($result);
						if($row['password'] == md5($_POST['oldpassword']))
						{
								$pass = md5($_POST['newpassword']);
								$query="update `tbladmin` set `password`='".$pass."',`username`='".$_POST['name']."',`email`='".$_POST['email']."',`address`='".$_POST['address']."' where `id` = '".$_SESSION['id']."'";
								$result = mysql_query($query);
								if($result)
								{
									$_SESSION['username'] = $_POST['name'];
									echo "<script language='javascript'>alert('Updated successfully')</script>";
									echo "<script>window.location='lenders.php';</script>";
								}

						}
						else
						{
							echo "<script language='javascript'>alert('Please check your password')</script>";
						}
						
					}
					
				}
			}
			
		?>
	</body>
</html>