
function disableButtons() {
  var buttons = document.getElementsByName("b");
  for (index = 0; index < buttons.length; index++) {
    buttons[index].disabled = true;
  } 
}

function enableButtons() {
  var buttons = document.getElementsByName("b");
  for (index = 0; index < buttons.length; index++) {
    buttons[index].disabled = false;
  } 
}

function showcommentform(divid, topicid, replytoid) {
  loadFragmentInToElement(divid, '/comments.php?showcommentform='+topicid+'&replyid='+replytoid+'&elementid='+divid, true);
	disableButtons();
}

function editcomment(divid, commentid) {
  loadFragmentInToElement(divid, '/comments.php?editcomment='+commentid+'&elementid='+divid, true);
	disableButtons();
}

function submitcomment(topicid, replyid, elementid) {
	var textareaid = 'textarea_'+elementid;
	var textarea = document.getElementById(textareaid);
  
	if (textarea) {
		if (textarea.value=='') {
	    alert('The message is empty, nothing will be saved.');		
		}	
		loadFragmentInToElementPost('comments_'+topicid, '/comments.php', 'postcomment=1&topicid='+topicid+'&replyid='+replyid+'&message='+escape(textarea.value));	
	}
  else alert('bug: textarea not found: '.textareaid);	
	enableButtons();
}

function submitedit(elementid, editid) {
	var textareaid = 'textarea_'+elementid;
	var textarea = document.getElementById(textareaid);
  
	if (textarea) {
		if (textarea.value=='') {
	    alert('The message is empty, nothing will be saved.');		
		}	
		loadFragmentInToElementPost(elementid, '/comments.php', 'postcomment=1&editid='+editid+'&message='+escape(textarea.value));	
	}
  else alert('bug: textarea not found: '.textareaid);	
	enableButtons();
}

function cancelcomment(elementid) {
  var div = document.getElementById(elementid);
	if (div) {
	  div.innerHTML = '';
	}
	else alert('cancelcomment: element not found: '+elementid);	
	enableButtons();
}

function canceledit(elementid, editid) {
  loadFragmentInToElement(elementid, '/comments.php?showcomment='+editid);
}

function bbcode_ins(fieldId, tag)  // http://blog.groomi.net/JAVASCRIPT:_Add_BBCode_buttons_to_a_form-,9#post
{
  // This code handles the action from various buttons which insert BBCode
	field=document.getElementById(fieldId);
	if(tag=='b' || tag=='i' || tag=='u' || tag == 'php' || tag == 'code')
	{
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + '][/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + '][/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else if(tag == 'img')
	{
		var path = prompt('Enter image path', 'http://');
		if(!path)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + ']' + path + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + ']' + path + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else if(tag == 'url')
	{
		var url = prompt('Enter link URL', 'http://');
		var linkText = prompt('Enter link text', '');
		if(!url || !linkText)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + '='+url+']' + linkText + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + '='+url+']' + linkText + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
}