function hideCommentReplyForm(form_id)
{
	var div_id = "div_" + form_id;
	toggleVisibility(div_id, false);
}

function printCommentReplyForm(form_id, comment_parent_id)
{
	var div_id = "div_" + form_id;
	var reply_comment_form = "comment_form" + form_id;

	var innerHTMLContent = '\
			<form name="' + reply_comment_form + '" id="' + reply_comment_form + '" onSubmit="return false;" >\
			<input type="hidden" name="media_id" value="' + media_id + '">\
			<input type="hidden" name="add_comment" value="">\
			<input type="hidden" name="form_id" value="' + reply_comment_form + '">\
			<input type="hidden" name="reply_parent_id" value="' + comment_parent_id + '">\
			<textarea name="comment" cols="45" rows="5"></textarea>\
			<br/>\
			<div style="float:left;clear:left">\
				<input align="left" type="button" class="button" name="add_comment_button" value="Escribir Comentario" onclick="postComment(\'' + reply_comment_form + '\');">\
			</div>\
			</form><br style="clear:both"><br>';

	setInnerHTML(div_id, innerHTMLContent);
}

var postCommentSuccess = function(t)
{
	response_str = t.responseText;
	response_code = response_str.substr(0, response_str.indexOf(" "));
	form_id = response_str.substr(response_str.indexOf(" ")+1);

	var form = document.forms[form_id];
	var dstDiv = form.add_comment_button;
	var commentDiv = form.comment;

	if (response_code == "OK")
	{
		setInnerHTML('div_main_comment', "Su comentario ha sido enviado correctamente!");
		fetchComments(media_id, '1');
	}
	else
	{
		if(response_code == "NOPERM")
		{
			alert("No tiene permisos para comentar! Pruebe a registrarse.");
			dstDiv.disabled = false;
		}
		else if(response_code == "TOOSHORT")
		{
			alert("El comentario es muy corto. Por favor escriba algo más largo");
			dstDiv.disabled = false;
			commentDiv.disabled = false;
			commentDiv.focus();
		}
		else
		{
			dstDiv.disabled = false;
		}

		dstDiv.value = "Escribir Comentario";
	}
}

function postComment(comment_form_id)
{
	/*if(!is_logged_in)
		return alert("Debe estar registrado para escribir un comentario!");*/

	var form = document.forms[comment_form_id];

	if (checkComment(form, comment_form_id))
	{
		var add_button = form.add_comment_button;
		add_button.value = "Escribiendo comentario...";
		form.comment.disabled = true;
		add_button.disabled = true;

		comment_escaped = encodeURIComponent(document.forms[comment_form_id].comment.value);

		var url = base_url+'common/ajax/media_add_comment.php';
		//var pars = Form.serialize($(comment_form_id)) + '&comment=' + comment_escaped;
		var pars = $(comment_form_id).serialize() + '&pass_bot=1&comment=' + comment_escaped;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onSuccess: postCommentSuccess });
	}
}

function checkComment(comment_form, comment_form_id)
{
	var comment = comment_form.comment;
	var comment_button = comment_form.comment_button;

	if (comment.value.length == 0 || comment.value == null)
	{
		alert("Debe escribir un comentario!");
		comment.disabled = false;
		comment.focus();
		return false;
	}

	if (comment.value.length > 500)
	{
		alert("Su comentario debe ser de menos de 500 letras!");
		comment.disabled = false;
		comment.focus();
		return false;
	}

	return true;
}

function fetchComments(file_id, page)
{
	showLoading('div_display_comments');

	var url = base_url+'common/ajax/media_load_comments.php';
	var pars = 'file_id='+escape(file_id)+'&p='+escape(page);
	var target = 'div_display_comments';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

