<?php
	$break = explode('/', $_SERVER["SCRIPT_NAME"]);
	if($break[count($break) - 1] != "index.php") { header("Location: ../index.php"); exit(); } 
?>
			<div class="componentheading">Private Messages</div>
<?php
	if(isset($_SESSION['errormsg']))
	{
?>
			<div class="module_menu">
				<div>
					<div>
						<div>
							<h3>Notification</h3>
								<b><?php echo $_SESSION['errormsg']; ?></b>
						</div>
					</div>
				</div>
			</div>
<?php
		unset($_SESSION['errormsg']);
	}
?>
			<a name="content"></a>
<?php
	if(!isset($_GET['archive']) || (isset($_GET['archive']) && $_GET['archive'] != "true"))
	{
?>
			<div class="module_menu">
				<div>
					<div>
						<div>
							<h3>Send A Private Message</h3>
							<form action="process.php" method="POST">
								<select name="pmto" size="8" STYLE="width: 100%; overflow: scroll">
								<?php $database->printActiveBuddiesPMList($session->userDBid,$_SESSION['lastBuddyPMed']); ?>
								</select>
								<br>Message:<br>
								<input type="text" name="pmmessage" STYLE="width: 99%;"><br>
								<input type="hidden" name="sendpm" value="1">
								<input type="submit" value="Send Message">
							</form>
						</div>
					</div>
				</div>
			</div>
<?php
	}
?>
			<div class="module_menu">
				<div>
					<div>
						<div>
							<h3>Recent Private Messages</h3>
							<?php
								if (isset($_GET['pageno'])) {
								   $pageno = $_GET['pageno'];
								} else {
								   $pageno = 1;
								}
								if(isset($_GET['archive']) && $_GET['archive'] == "true")
								{
									$database->printCurrentPMHistory($session->userDBid,false,$pageno);
								}
								else
								{
							?>
							The messages below are sorted by newest first, and will update automatically every 5 seconds.<br><br>
							<span id="scriptoutput">Loading AJAX...<br><br><br><br><br><br><br><br><br><br><br><br><br>
								<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></span>
							<script type='text/javascript'>
							window.onload = function (){
							ajax(page,'scriptoutput');					
							<?php if(isset($_SESSION['lastBuddyPMed'])) { echo "location.replace('index.php?p=messages#content');"; unset($_SESSION['lastBuddyPMed']); } ?>
							
							}
							</script>
							<?php
								}
							?>
						</div>
					</div>
				</div>
			</div>