//-------------------------------------------------------------------------------
//                               C O M M E N T S
//-------------------------------------------------------------------------------

function validateCommentForm(f)
{
	if (commentDisabledSubmit) {
		alert(MsgCommentPostWait);
		return false;
	}
	
	if (f.comment_body.value == null || f.comment_body.value.length == 0 || isEmpty(f.comment_body.value)) {
		alert(MsgCommentPostEmpty);
		return false;
	}
	
	disableButtons(f);
	commentDisabledSubmit = true;
	return true;
}

function doCommentFormSubmit(f)
{
	if (!validateCommentForm(f)) return false;
	f.submit();
}

function commentToggleOn(id)
{
	var label = "linkblock_" + id;
	var block = document.getElementById(label);
	if (typeof (block) == 'undefined') return;
	if (block.style.display != 'block') {
		block.style.display = 'block';
	}
}

function commentToggleOff(id)
{
	var label = "linkblock_" + id;
	var block = document.getElementById(label);
	if (typeof (block) == 'undefined') return;
	if (block.style.display == 'block') {
		block.style.display = 'none';
	}
}

function doReplyToComment(id)
{
	fComments = document.getElementById('formComment');
	
	// First commentToggle form header
	commentToggleOn('comment_reply');
	
	// Save commentID as parent
	fComments.comment_parent.value = id;
	
	// Jump to anchor and set focus in body field
	document.location='#commentForm';
	fComments.comment_body.focus();
	
	// Set reply fields
	getComment(id);
}

function doCommentRate(id,points) {
	// determine language
	var lang = document.location.pathname.substring(0,4);

	// do rating through php script
	var img = new Image();
	img.src = lang + "_common/comments_nested/rate.php?id=" + id + "&points=" + points + "&x=" + new String (Math.random());

	// disable buttons for id
	var posbutton = document.getElementById('rategood_' + id);
	if (posbutton) {
		posbutton.className = "poscommentdisabled";
	}
	var negbutton = document.getElementById('ratebad_' + id);
	if (negbutton) {
		negbutton.className = "negcommentdisabled";
	}
}

function doCommentRateBad(id) {
	doCommentRate(id, -1);
}

function doCommentRateGood(id) {
	doCommentRate(id, 1);
}

function getComment(id)
{
	// Fields
	var parent = document.getElementById('comment_' + id);
	var reply = document.getElementById('comment_reply_body');
	var parent_poster = document.getElementById('comment_poster_' + id);
	var reply_poster = document.getElementById('comment_reply_poster');
	
	// Set reply field's poster
	reply_poster.innerHTML = parent_poster.innerHTML;
	// Set reply field's body
	reply.innerHTML = parent.innerHTML;
}

function doNewComment()
{
	fComments = document.getElementById('formComment');
	
	// First commentToggle form header
	commentToggleOff('comment_reply');
	
	// Empty parent field + body
	fComments.comment_parent.value = '';
	fComments.comment_body.value = '';
	
	// Jump to anchor and set focus in body field
	document.location='#commentForm';
	fComments.comment_body.focus();
}

function removeComment(id)
{
	var f = document.getElementById('formRemoveComment');

	if (!confirm(MsgRemoveComment)) {
		return;
	}

	f.cmd.value = 'delete_comment';
	f.comment_id.value = id;
	f.submit();
}

// Function used to redirect parent-window if called as popup
function parentRedirect(url)
{
	if (opener && !opener.closed){
		opener.document.location.href = url; 
		window.close(); 
		return;
	}
	document.location.href = url;
}