function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute('maxlength')) : ''
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function recount(){
	shout_messagelength = document.getElementById("shoutInput").value.length;
	document.getElementById("shoutCounter").value = 200 - shout_messagelength;
}

function checkShout(e) {
	if(e.type == "keydown") {
		if(e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37 || e.keyCode == 35 || e.keyCode == 36 ) return true; 
		var length = tinyMCE.activeEditor.getContent( ).length;
		document.getElementById("shoutCounter").value = 200 - length;
		if(length >= 200 && e.type == "keydown") tinymce.dom.Event.cancel(e);
		return true;
	}
}
$(document).ready(function( ) {
	$("#shoutbox").submit(function( ) {
		if(typeof( tinyMCE ) != "undefined") tinyMCE.triggerSave( );
		shout_id = $("#shout_id").val( );
		$.post(basedir+"plugins/shoutbox/shoutsave.php", {shout: $("#shoutInput").val( ), shout_id: shout_id, userdigit: $("#userdigit").val( ), shoutname: $("#shoutname").val( )}, function(xml) {
			if($("error", xml).text( ) == "false") {
				if(!shout_id) $("#shoutContainer").prepend($("shout", xml).text( ));
				else $("#shout_"+shout_id).replaceWith($("shout", xml).text());
				$("#shoutInput").val("");
				$("#userdigit").val("");
				if(tinyMCE) tinyMCE.activeEditor.setContent("");
				document.getElementById("captchaImage").src = document.getElementById("captchaImage").src + '#';
			}
			else {
				alert($("message", xml).text());
				document.getElementById("captchaImage").src = document.getElementById("captchaImage").src + '#';
			}
		});
		return false;
	});
	$(".jswarning").css({display: "none"});
});
function editShout(num) {
	$("$shoutInput").val($("#shout_"+num+" .shoutMessage").text());
	if(tinyMCE) tinyMCE.activeEditor.setContent($("#shout_"+num+" .shoutMessage").text());
	$("#shout_id").val(num);
	return false;
}
function deleteShout(num) {
	$.get(basedir+"plugins/shoutbox/shoutdelete.php", {shout: num}, function(xml) {
		if($("error", xml).text( ) === "false") {
			$("#shout_"+num).empty( );
			$("#shout_"+num).append($("message", xml).text());
			$("#shout_"+num).fadeOut(3000);
		}
		else {
			alert($("message", xml).text());
		}
	});
	return false;
}
if(typeof( tinyMCE ) != "undefined") {
tinyMCE.init({
	mode: "textareas",
	theme: "advanced",
	plugins: "bbcode,emotions",
	theme_advanced_buttons1: "bold,italic,underline,"+(allowlinks ? "link,unlink," : "")+"emotions",
	theme_advanced_buttons2: "",
	theme_advanced_buttons3: "",
	handle_event_callback : "checkShout",
	relative_urls: false,
	editor_selector: "shoutEditor"
});
}