function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

function submitCommentWithAjax(form) {
	
	// validação
	if(Trim(form.author.value) == "") {
		alert("Para enviar um comentário, você precisa preencher seu nome.");
		form.author.focus();
		return false;
	}
	// validação
	if(Trim(form.commentText.value) == "") {
		alert("Para enviar um comentário, você precisa preencher o comentário.");
		form.commentText.focus();
		return false;
	}

	// muda o botão para dar a idéia de carregando
	var commentBts = document.getElementById("commentBts").innerHTML;
	document.getElementById("commentBts").innerHTML = '<img src="images/ajax-loading.gif"/>';

	var comment = form.commentText.value;
	
	comment = comment.replace(/\r\n/g," ");
	comment = comment.replace(/\n/g," ");
	comment = comment.replace(/"/g,"\\\"");

	var params = "({";
	params += '"comment":"' + form.comment.value + '"';
	params += ',"entry_id":"' + form.entry_id.value + '"';
	params += ',"permalink":"' + form.permalink.value + '"';
	params += ',"redirect_to":"' + "" + '"';
	params += ',"author":"' + form.author.value + '"';
	params += ',"authorEmail":"' + form.authorEmail.value + '"';
	params += ',"authorURL":"' + form.authorURL.value + '"';
	params += ',"commentText":"' + comment + '"';
	params += ',"remember":"' + form.remember.value + '"';
	params += '})';	

	var jsonParams = eval(params);
	
	var action = form.action;

	if(!form.remember.checked) {
		form.reset();
	}

	$.post(action,jsonParams,function(data,textStatus) {			
			if(textStatus == "success") {
				$(".message_modal").modal({
					dataCss:{text:"center"},
					onClose: function(){window.location.reload();},
					containerCss:{
						color:"#bc9f91", 
						background:"url('images/background.gif')", 
						width: "408px", 
						height:"107px", 
						border:"2px solid #afa198"},
					closeHTML: "<a href='#'>Fechar</a>",
					closeClass: "modal_close"
					});
			}
			else {
				document.getElementById("commentBts").innerHTML = "Erro ao comentar, tente novamente";
				setTimeout(function() {
					document.getElementById("commentBts").innerHTML = commentBts;
					},4000);
			}
			}, "text");
}
