$(document).ready(function(e){
	if (gblTotalRegistros == '1')
	{
		ListarComentarios();
	}
	CarregarEventoBlog();
})

function CarregarEventoBlog() 
{
	$('input[name="cmdEnviar"]').unbind();
	$('input[name="cmdEnviar"]').click(function(e){
		e.preventDefault();
		IncluirComentario($('#formComentario'));
	});
	
	$('.btComentario').click(function(e){
		e.preventDefault();
		$('.formComentario').show()
		$('#respostaIncluirComentario').empty();
		return false;
	})
}

function ListarComentarios()
{
	$.ajax({
		async: true,
		url: "/remote/rmt_padrao.asp?funcao=blog_listarcomentario",
		data: { id:gblConteudo },
		type: 'POST',
		dataType:'text',
		success: function(resposta)
			{
				$('#Comentarios').html(resposta);
			},
		error : function(xhtr)
			{
				$('#Comentarios').html('Ocorreu um erro inesperado:<br/>' + xhtr.responseText);
			}
	});
}

function CriticaComentario(frm)
{
	var texto, msgaux, erro;
	 
	msgaux = 'Os campos listados abaixo são de preenchimento obrigatório.\n' 
	erro=false;
	
	//Campo Nome
	
	texto = frm.txtNome.value; 
 	if (texto.length == 0)  
 	{ 
        msgaux+='\n. Nome';      
        erro=true;
 	} 

	//Campo E-mail

	texto = frm.txtEmail.value; 
 	if (texto.length == 0)  
 	{ 
        msgaux+='\n. e-mail';      
        erro=true;
 	} 

	//Campo Assunto

	texto = frm.txtComentario.value; 
 	if (texto.length == 0)  
 	{ 
        msgaux+='\n. Comentário';      
        erro=true;
 	} 
	
	if (erro) alert(msgaux);
	
    return !(erro);

}


//Insere o comentario no banco e relista os comentarios com o novo incluido
function IncluirComentario(oForm)
{
	if (CriticaComentario(oForm[0]))
		$.ajax({
			async: true,
			
			url: "/remote/rmt_padrao.asp?funcao=blog_incluircomentario",
			data: oForm.serialize(),
			type:'POST',
			dataType:'json',
			success:function(oJSON)
			{
				if (oJSON.sucesso)
				{
					$('#respostaIncluirComentario').html(oJSON.mensagem);
					oForm[0].reset();
					$('.formComentario').hide()
					ListarComentarios();
				}
				else
				{
					$('#respostaIncluirComentario').html(oJSON.mensagem);
				}
			},
			error: function(xhtr)
			{
				$('#respostaIncluirComentario').html('Ocorreu um erro inesperado:<br/>' + xhtr.responseText);
			}	
		});	
}
