var userId;
var numConsecutiveFailedRequests = 0;
var newMsgUpdateIntervalId;
var idleTimer = new Date().getTime();

$(document).ready(function() {
	// support for "cut text" [jj 09Jun22]
	$("div.cut").each(function() {
		var text = $(this).attr("text");
		if (text == null || text == "") text = "Click here to view the cut text...";
		$(this).before("<span class='cutlink'>"+ text +"</span>");
	});
	
	// display a block of cut text
	$("span.cutlink").click(function() {
		// remove unwanted BRs
		var cutDisplay = $(this).next().html().trim();
		if (cutDisplay.startsWith("<br>")) cutDisplay = cutDisplay.substring(4);
		if (cutDisplay.endsWith("<br>")) cutDisplay = cutDisplay.substring(0, cutDisplay.length-4);
		$(this).next().html(cutDisplay);
		$(this).next().next().hide();
		// toggle display
		$(this).hide().next().show();
	});
	
	// add cut text html to the post box
	$("a#addcuttext").click(function() { 
		$("textarea#msg").val($("textarea#msg").val() + "\r\n\r\n<div class=\"cut\" text=\"Click here to view the cut text...\">\r\nThis is the concealed text.\r\n</div>");
		$("textarea#msg").val($("textarea#msg").val().trim());
		return false;
	});
	
	// add spoiler code to the post box
	$("a#spoiler").click(function() { 
		$("textarea#msg").val($("textarea#msg").val() + "\r\n\r\n<span class=\"spoiler\"> Spoiler Text </span>");
		$("textarea#msg").val($("textarea#msg").val().trim());
		return false;
	});
	
	userId = $("head").attr("userId");
	newMsgUpdateIntervalId = setInterval (UpdateNumNewMsgs, 5000);
});

// asynchronously update the number of new messages [jj 09Nov19]
function UpdateNumNewMsgs() {
	$.ajax({
		type: "POST",
		url: "webadelSvc.asmx/GetNumNewMessages",
		data: "userId="+ userId,
		dataType: "xml",
		cache: false,
		timeout: 400,
		success: function(html, textStatus) {
			$("span#newMsg").text(html.documentElement.firstChild.nodeValue);
			$("span#newMsg").css("background-color", "");
			numConsecutiveFailedRequests = 0;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("span#newMsg").css("background-color", "yellow");
			numConsecutiveFailedRequests++;
			if (numConsecutiveFailedRequests > 3) $("span#newMsg").css("background-color", "red");
		}
	});
	
	
	if (new Date().getTime() - idleTimer > 60000) {	// after 1 min idle time, slow down the new msg counter
		clearInterval(newMsgUpdateIntervalId);
		newMsgUpdateIntervalId = setInterval (UpdateNumNewMsgs, 30000); // every 30 seconds
	}
	if (new Date().getTime() - idleTimer > 900000) { // after 15 min, stop entirely
		clearInterval(newMsgUpdateIntervalId);
	}
}

function ChooseRecipient(authorId) {
	var recipSelectList = document.getElementById('chooseRecipient');
	for (var i=0; i < recipSelectList.options.length; i++) {
		if (recipSelectList.options[i].value == authorId)
			recipSelectList.options[i].selected = true;
	}	
}

function DCMSelect() {
	var dcmCommand = document.getElementById('dcmCommand').value;
	if (dcmCommand == "delete" || dcmCommand == "neuter") {
		document.getElementById('dcmDelete').style.display = 'inline'; 
		document.getElementById('dcmCopyMove').style.display = 'none';
	} else {
		document.getElementById('dcmDelete').style.display = 'none'; 
		document.getElementById('dcmCopyMove').style.display = 'inline';
	}
}

function ToggleDisplay (elementId) {
	var block = document.getElementById(elementId);
	if (block.style.display == 'none') block.style.display = 'block';
	else block.style.display = 'none';
	
	Set_Cookie(elementId, block.style.display, cookie_expire_date, "/", false); 
}

// Check for cookie and set the specified block display to that value.
function RestoreBlockState (elementId) {
	var val = Get_Cookie(elementId);
	if (val != "") document.getElementById(elementId).style.display = val;
}

var oldUsernameValue;
function ToggleAnony () {
	var anon = document.getElementById('postAnonymously');
	var senderName = document.getElementById('senderName');
	
	if (anon.value == "False") {
		anon.value = "True";
		oldUsernameValue = senderName.innerHTML;
		senderName.innerHTML = "Anonymous";
	} else {
		anon.value = "False";
		senderName.innerHTML = oldUsernameValue;
	}	
}

function QuoteMessage(msgId) {
	var lines = document.getElementById(msgId).innerHTML.split("<br>\n");
	var postbox = document.getElementById('msg');
	
	for (i = 0; i < lines.length; i++) {
		if (lines[i] != '') postbox.value += '> ' + lines[i] + '\r\n';
		else postbox.value += '\r\n';
	}
	postbox.value += '\r\n';
	postbox.focus();
}

String.prototype.startsWith = function(str) { return (this.match("^"+str) == str); }
String.prototype.endsWith = function(str) { return (this.match(str+"$") == str); }
String.prototype.trim = function() { return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")); }