function checkSubmitForm()
{
	//console.log("1");
	//alert("comentariul are " + $('comment_content').value.length + "caractere");
	if ($('id_user').value == '0')
	{
		//console.log("2");
		if ($('title').value.length < 2)
		{
			//console.log("3");
			alert("Numele este prea scurt.");
			$('title').focus();
			return false;
		}
		//console.log("4");
		if (!mail_ok($('email').value))
		{
			//console.log("5");
			alert("Email-ul nu este corect.");
			$('email').focus();
			return false;
		}
	}
	//console.log("6");
	if ($('subject').value.length < 2)
	{
		//console.log("7");
		alert("Subiectul este prea scurt.");
		$('subject').focus();
		return false;
	}
	else if ($('comment_content').value.length < 2)
	{
		//console.log("8");
		alert("Comentariul este prea scurt.");
		$('comment_content').focus();
		return false;
	}
	else if ($('comment_content').value.length > 1000)
	{
		//console.log("8");
		alert("Comentariul este prea lung.\nComentariul nu trebuie sa depaseasca 1000 de caractere.\nComentariul tau are " + $('comment_content').value.length + " caractere.");
		$('comment_content').focus();
		return false;
	}
	else
	{
		//console.log("9");
		return true;
	}
}

function postComment()
{
	if (checkSubmitForm())
	{
		if($('title')) {
			var title = convertSpecialChars($('title').value);
		} else {
			var title = "";
		}
		if($('email')) {
			var email = convertSpecialChars($('email').value);
		} else {
			var email = "";
		}
		//alert($('comment_content').value.replace(/[^>]\n/g, '<br>'));
		var subject = convertSpecialChars($('subject').value);
		var content = nl2br(convertSpecialChars($('comment_content').value));
		content = content.replace(/[^>]\n/g, '<br>');
		//alert(content);

		var url = settings['URL']+"/library/ajax/handleComments.php";
		var what = "action=post_new_comment&title="+title+"&email="+email+"&subject="+subject+"&content="+content+"&id_user="+$('id_user').value + "&id_article=" + $('id_article').value + "&id_parent=" + $('id_parent').value;
		//alert(title+"|"+email+"|"+subject+"|"+content);
		
		new Ajax(url, {
			method: 'post',
			encoding:'utf-8',
			onComplete: function(text, xml) 
			{
				if (messages_values.contains(text))
				{
					alert(errorMessages[text]);
				}
				else
				{
					$('commentsForm').innerHTML = text + '<p><a class="input11xSubmit" id="TB_closeWindowAButton" href="#" onClick="TB_remove(); return false;" title="Inchide fereasta" style="color: #CC0000;">Inchide fereastra</a></p>';
				}
			}
		}).request(what);
	}
}


function toggleComment(id_comment, use_ajax, set_count, value_count)
{
	if ($('comment_'+id_comment).style.display == 'block')
	{
		$('comment_'+id_comment).style.display = 'none';
	}
	else
	{
		if (use_ajax == true)
		{
			createComment(id_comment);
		}
		else
		{
			$('comment_'+id_comment).style.display = 'block';
		}
		
		if (set_count == true)
		{
			updateCommentViews(id_comment);
		}
	}
}

function updateCommentViews(id_comment)
{
	var url = settings['URL']+"/library/ajax/handleComments.php?action=update_views&id_comment=" + id_comment;
		
		new Ajax(url, {
			method: 'get'
		}).request();
}

function createComment(id_comment)
{
	var url = settings['URL']+"/library/ajax/handleComments.php?action=get_comment&id_comment=" + id_comment;
	
		$('comment_'+id_comment).addClass('ajax-loading');
		new Ajax(url, {
			method: 'get',
			onComplete: function(text, xml) 
			{
				$('comment_'+id_comment).innerHTML = text;
				$('comment_'+id_comment).removeClass('ajax-loading');
				TB_init();
			}
		}).request();
		
	$('comment_'+id_comment).style.display = 'block';
}

function nl2br(str)
{
    return str.replace(/([^>])\n/g, '$1<br/>');
}


function truncateComments()
{
	$('comments-list').getElements('ol.comments-list li p').each(function(item, index) {
	
		var words = $(item).getText().split(' ');
		if (words.length >= 50) {
			var truncated = new Array(words.length);
			var more = new Array(words.length);
			for(i=0;i<words.length;i++) {
				truncated[i] = words[i];
				more[i] = words[i];
			}

			howMany = words.length - 40;
			truncated.splice(40, howMany);
			more.splice(0, 40);
			$(item).setHTML('<span>' + truncated.join(" ") + '</span> <a href="javascript: return false" class="readMore">[Citeste]</a><span class="more"> ' + more.join(" ") + '</span>');
			
			$(item).getElement('a.readMore').addEvent('click', function(){
				$(item).getElement('span.more').setStyle('display', 'inline');
				$(item).getElement('a.readMore').setStyle('display', 'none');
				return false
			});
		}
		
	});
}