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="Post Comment" 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', "Your comment has successfully been posted!");
		fetchComments(media_id, '1');
	}
	else
	{
		if(response_code == "NOPERM") 
		{
			alert("You do not have permission to post comments!");
			dstDiv.disabled = false;
		}
		else if(response_code == "TOOSHORT") 
		{
			alert("The comment you have entered is too short. Please write a longer comment and try again");
			dstDiv.disabled = false;
			commentDiv.disabled = false;
			commentDiv.focus();
		}
		else 
		{
			dstDiv.disabled = false;
		}
	
		dstDiv.value = "Post Comment";
	}
}

function postComment(comment_form_id) 
{
	/*if(!is_logged_in)
		return alert("You must login to post a comment!");*/

	var form = document.forms[comment_form_id];
		
	if (checkComment(form, comment_form_id)) 
	{
		var add_button = form.add_comment_button;
		add_button.value = "Adding comment...";
		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() + '&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("You must enter a comment!");
		comment.disabled = false;
		comment.focus();
		return false;
	}

	if (comment.value.length > 500)
	{
		alert("Your comment must be shorter than 500 characters!");
		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});
}
