function incCommentCount()
{
	var ccount = document.getElementById('commentcount');
	commentCount++;
	text = commentCount + (commentCount == 1 ? ' Comment' : ' Comments');

	var newNode = document.createTextNode(text);
	ccount.replaceChild(newNode, ccount.childNodes[0]);
}

function decCommentCount()
{
	var ccount = document.getElementById('commentcount');
	commentCount--;
	if(commentCount > 0) {
		text = commentCount + (commentCount == 1 ? ' Comment' : ' Comments');
	} else {
		text = 'No Comments';
	}

	var newNode = document.createTextNode(text);
	ccount.replaceChild(newNode, ccount.childNodes[0]);
}

function addCommentToList(type, userName, message, commentID)
{
	var list = document.getElementById('commentlist');
	var element = document.createElement('hr');
	var attr = document.createAttribute('size');
	attr.value = "1";
	element.setAttributeNode(attr);
	attr = document.createAttribute('id');
	attr.value = "comment_hr_" + commentID;
	element.setAttributeNode(attr);
	list.appendChild(element);

	// Create the "username says..." text.
	var bElement = document.createElement('b');
	var textNode = document.createTextNode(userName);
	bElement.appendChild(textNode);
	var pElement = document.createElement('p');
	textNode = document.createTextNode(' says...');
	pElement.appendChild(bElement);
	pElement.appendChild(textNode);

	element = document.createElement('ul');
	attr = document.createAttribute('id');
	attr.value = 'comment_' + commentID;
	element.setAttributeNode(attr);
	element.appendChild(pElement);

	pElement = document.createElement('p');
	textNode = document.createTextNode(message);
	pElement.appendChild(textNode);

	var aElement = document.createElement('a');
	attr = document.createAttribute('href');
	attr.value = '#';
	aElement.setAttributeNode(attr);
	attr = document.createAttribute('onClick');
	attr.value = "delComment('" + type + "', '" + commentID + "'); return false;";
	aElement.setAttributeNode(attr);
	textNode = document.createTextNode('delete');
	aElement.appendChild(textNode);

	element.appendChild(pElement);
	element.appendChild(aElement);

	list.appendChild(element);
	incCommentCount();
}

function removeCommentFromList(type, commentID)
{
	var element = document.getElementById('comment_hr_' + commentID);
	element.parentNode.removeChild(element);
	element = document.getElementById('comment_' + commentID);
	element.parentNode.removeChild(element);
}

function addComment(type, userID, releaseID, message, userName)
{
	if(!oPostCommentValidation.validateForm()) {
		return false;
	}

	var oAjax = new clsAjax();
	oAjax.url	= "../ajax/ajax_comments.php";
	oAjax.method	= "POST";
	oAjax.async	= false;
	oAjax.addPostField('act', 'add');
	oAjax.addPostField('t', type);
	oAjax.addPostField('uid', userID);
	oAjax.addPostField('rid', releaseID);
	oAjax.addPostField('m', message);
	oAjax.addPostField('un', userName);
	oAjax.fetchData();
	var commentId = oAjax.responseText;

	document.getElementById('comment_message').value = '';
	addCommentToList(type, userName, message, commentId);
	//new Effect.Fade(document.getElementById('addcommentspan'));

	new Effect.Appear(document.getElementById('commentthankyou'));
	setTimeout("new Effect.Fade(document.getElementById('commentthankyou'))", 3000);
	return true;
}

function delComment(type, commentID)
{
	var oAjax = new clsAjax();
	oAjax.url	= "../ajax/ajax_comments.php";
	oAjax.method	= "POST";
	oAjax.async	= false;
	oAjax.addPostField('act', 'del');
	oAjax.addPostField('t', type);
	oAjax.addPostField('id', commentID);
	oAjax.fetchData();

	removeCommentFromList(type, commentID);
	decCommentCount();

	return true;
}
