<div id="left">

	      	  <?php
				include($setting['sitepath'] . '/includes/filelist.inc.php');
				if(isset($_SESSION["user"])) {
					 $user=$_SESSION["user"];
					 $id=$_SESSION["userid"];
				
				?>
				<p><h2 style="text-align:left;">Upload an Avatar</h2></p><br/>
				<form enctype="multipart/form-data" name="settings" method="post" action="<?php echo $setting['siteurl'];?>includes/avatarupload.php">
					<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
					<input type="hidden" name="userid" value="<?php echo $_SESSION['userid'];?>" />
					<input type="file" name="file" class="fileUpload" multiple>
					<button id="px-submit" type="submit">Upload</button>
					<button id="px-clear" type="reset">Clear</button>
				</form>
				<br/><p><h2>Click on any image that you want to use for your avatar</h2></p><br/>
				<ul style="list-style:none; padding:0;margin:0;">
				<div class="avatarBox">
				<?php
				$dir = $setting['sitepath'] . '/avatars';
				$files = array();
				$directory = opendir($dir);
				while($item = readdir($directory)){
				// We filter the elements that we don't want to appear ".", ".." and ".svn"
					 if(($item != ".") && ($item != "..") && ($item != ".svn") ){
						  $files[] = $item;
					 }
				}
				$count = count($files); 
				$os = array("gif", "jpg", "jpeg", "png", "GIF", "JPG", "JPEG", "PNG");
				$num = 0;
				if ($count==0) {
					echo '<p>No free avatars available.</p>';
					exit();
				}
				$avquery = yasDB_select("SELECT avatar FROM avatars WHERE userid = '$id'");
				if ($avquery->num_rows != 0) {
					while ($avfile = $avquery->fetch_array(MYSQL_ASSOC)) {
						echo '<li style="list-style:none;width:126px;float:left;padding:5px 0px 15px 0px;margin:0px;"><input type="image" class="useravatars" src="' . $setting['siteurl'] . 'avatars/' . $avfile['avatar'] . '" onclick="switchAvatar(\''.$avfile['avatar'].'\');return false">';
						echo '<input type="image" src="'.$setting['siteurl'].'templates/'.$setting['theme'].'/skins/'.$setting['skin'].'/images/close.png" height="15" width="15" name="avatar" value="Remove" onclick="deleteAvatar(\''.addslashes($avfile['avatar']).'\');return false"/></li>';
					}
				}
				$avquery->close();
				while ($num < $count) {
					$file = $files[$num];
					$num++;
					$ext = pathinfo($file, PATHINFO_EXTENSION);
					if (in_array($ext, $os)) {// && $files[$num]['type'] == 'file'  This kept on cutting off last pic??
						echo '<li style="list-style:none;width:126px;float:left;padding:5px 0px 15px 0px;margin:0px"><input type="image" src="' . $setting['siteurl'] . 'avatars/' . $file . '"  onclick="switchAvatar(\''.$file.'\');return false" width="100px" height="100px"></li>';
					}	
				}
				unset($files);
				?>
				</div>
				</ul>
				<?php
		} 
		else {	
			echo "<p><h3>Not logged in!</h3><p>";
		}  
	    ?>