<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

	if (stristr(htmlentities($_SERVER['PHP_SELF']), "block_messages.php")) {
		die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
	}
	
	echo "<div id='std_mb_info'></div>\n";
	echo "<div id='std_mb_messages' style='padding:2px; overflow:auto; height:250px;'></div>\n";
	
	global $dblink,$siteConfig,$userid,$userInfo;
	
	if (isUser($userid) || ($siteConfig['guest_msgbox']==1)) {
		echo "<table width='100%' border='0' cellspacing='1' cellpadding='0'>\n";
		if (isUser($userid)) {
			echo "<input type='hidden' id='std_mb_name' value='".$userInfo['user']."'>\n";
		} else {
			echo "<tr><td colspan='2'><div class='info'>"._NAME_."</div><input id='std_mb_name' type='text' style='width:97%; margin:2px 0;' maxlength='255'></td></tr>\n";
		}
		echo "<tr><td colspan='2'>";
		echo "<div class='info'>"._MESSAGE_."</div><input id='std_mb_message' type='text' style='width:97%; margin:2px 0;' maxlength='255'></td></tr>\n";
		echo "<tr><td style='padding-top:2px;'><input type='submit' value='"._ADD_."' id='std_mb_addmessage'></td>\n";
		echo "<td align='right'><img src='images/smiles/smile_smile.gif' border='0' alt='Smile' title='"._SMILES_."' id='std_mb_showsmiles'></td></tr>\n";
		echo "</table>\n";
		
		echo "<div id='std_mb_smiles' style='margin-top:4px; display:none;' align='center'>\n";
			$result = $dblink->get_list("SELECT * FROM memht_smiles LIMIT 16");
			foreach ($result as $row) {
				$sbtitle = outCode($row['title']);
				$sbimg = outCode($row['img']);
				$sbcode = outCode($row['code']);
				
				echo "<img src='images/smiles/$sbimg' border='0' title='$sbtitle' alt='$sbtitle' style='margin: 0 1px;' onclick=\"addItemToMb('$sbcode')\">";
			}
		echo "</div>\n";
	} else {
		echo "<div class='box' align='center'>"._DOTHE_." <a href='index.php?page=users' title='"._LOGIN_."'>"._LOGIN_."</a> "._OR_." <a href='index.php?page=users&op=register' title='"._REGISTER_."'>"._REGISTER_."</a> "._FORFREETOSENDMES_."</div>";
	}
	?>
	
	<script type="text/javascript">
		$(document).ready(function(){
			/* Load messages */
			mdreload();
			
			/* Add messages */
			$('input#std_mb_addmessage').click(function() {
				var name = escape($.trim($('input#std_mb_name').val()));
				var text = escape($.trim($('input#std_mb_message').val()));
				
				/* Reset */
				$('div#std_mb_info').html('');
				
				/* Validate */
				var save = true;
				if (name=="") {
					save = false;
					$('div#std_mb_info').html('<div style="margin-bottom:4px; padding:3px; border:1px solid #B36462; color:#B36462; background-color:#EEDBDB;" align="center"><b><?php echo _NAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); ?></b></div>');
				}
				if (text=="") {
					save = false;
					$('div#std_mb_info').html('<div style="margin-bottom:4px; padding:3px; border:1px solid #B36462; color:#B36462; background-color:#EEDBDB;" align="center"><b><?php echo _MESSAGE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); ?></b></div>');
				}
				if (save) {
					var glue = "&";
					$.ajax({
						type: "POST",
						url: "inc/ajax/ajax_messagebox.php",
						cache: false,
						data: "op=add"+glue+"user="+name+glue+"message="+text,
						success: function(data,textStatus) {
							$('div#std_mb_messages').html(data);
							$('input#std_mb_message').val('');
						}
					});
				}
			});
			
			/* Show smiles */
			$("img#std_mb_showsmiles").click(function () {
				$("div#std_mb_smiles").slideToggle("slow");
			});
		});
		function addItemToMb(myitem) {
			var text = window.document.getElementById('std_mb_message').value + myitem;
			window.document.getElementById('std_mb_message').value = text;
		}
		function mdreload() {
			$('div#std_mb_messages').load('inc/ajax/ajax_messagebox.php?'+new Date().getTime());
			setTimeout("mdreload()",30*1000);
		}
	</script>
	<?php
?>