function postComment(form, container) {
	var params = $('#'+form.id).serializeArray();
  	jQuery.post(
  		"/comments.html",
  		params,
  		function (data) {
  			if (data.success == "true") {
  				reloadComments(container, data.itemId, data.itemTypeId, true);
	  		} else if (data.fail == "true") {
	  			$("#commentErrMsg").html(data.message);
	  		}
	  	},
  		"json"	
	);
	
	return false;
}

function postUnregComment(form, container) {
	var frm = $('#'+form.id);
	frm.validate({
		   debug: true
	});
	if (frm.valid()) {
		var params = frm.serializeArray();
	  	jQuery.post(
	  		"/comments.html",
	  		params,
	  		function (data) {
	  			if (data.success == "true") {
	  				window.location.href=window.location.href;
		  		}else if (data.fail == "true") {
		  			$('#' + container).prepend('<div class="pending-message">User with such email already exists.<br/> Please <a href="/auth.html">SIGN IN</a> to be able to comment mix.</div>');
		  		}
		  	},
	  		"json"	
		);

	}
	return false;
}

function postBoxyComment(form, container) {
	
	var params = $('#'+form.id).serializeArray();
  	jQuery.post(
  		"/comments.html",
  		params,
  		function (data) {
  			if (data.success == "true") {
  				$('#newComment').html('<div class="pending-message">Your comment is now in pending state.<br/> Moderator will review and publish your comment soon.</div>');
	  		}
	  	},
  		"json"	
	);
	
	return false;
}

function postUnregBoxyComment(form, container) {
	
	var frm = $('#'+form.id);
	frm.validate({
		   debug: true
	});
	if (frm.valid()) {
		var params = frm.serializeArray();
	  	jQuery.post(
	  		"/comments.html",
	  		params,
	  		function (data) {
	  			if (data.success == "true") {
	  				$('#newComment').html('<div class="pending-message new-user">Your comment is now in pending state.<br/> Moderator will review and publish your comment soon.</div>');
		  		} else if (data.fail == "true") {
		  			$('#newComment').html('<div class="pending-message">User with such email already exists.<br/> Please <a href="/auth.html">sign in</a> to be able to comment mix.</div>');
		  		}
		  	},
	  		"json"	
		);
	}
	
	return false;
}

function removeComment(id, itemId, itemTypeId, container) {
	var params = {'form[id]' : id, 'form[itemId]' : itemId, 'form[itemTypeId]' : itemTypeId, 'act' : 'delete', 'responseType' : 'json'};
  	jQuery.post(
  		"/comments.html",
  		params,
  		function (data) {
  			if (data.success == "true") {
  				reloadComments(container, data.itemId, data.itemTypeId, false);
	  		}
	  	},
  		"json"	
	);
	
	return false;
}

function alertReplyComment() {
	showBoxy();
}

function approveComment(id, itemId, itemTypeId, container) {
	var params = {'form[id]' : id, 'act' : 'approve', 'responseType' : 'json'};
  	jQuery.post(
  		"/comments.html",
  		params,
  		function (data) {
  			if (data.success == "true") {
  				reloadComments(container, itemId, itemTypeId, false);
	  		}
	  	},
  		"json"	
	);
	
	return false;
}

function reloadComments(div, itemId, itemTypeId, pendingMsg) {
	$(document).ready(function(){
		$('#' + div).load("/widget/comments.html?form[itemTypeId]="+itemTypeId+"&form[itemId]="+itemId+"&container=" + div, function() {
			  if (pendingMsg) {
				  $('#' + div).prepend('<div class="pending-message">Your comment is now in pending state. Moderator will review and publish your comment soon.</div>');
			  }
		});
	});
}

function replyComment (commentId) {
	$(".reply-comment").css('display', 'none');
	$("#reply-" + commentId).css('display', 'block');
	return false;
}

function hideReplyComment (commentId) {
	$(".reply-comment").css('display', 'block');
	$("#reply-" + commentId).css('display', 'none');

	var form = document.getElementById("form-" + commentId);
	if (form != null) {
		for (var i=0;i<form.childNodes.length; i++) {
			if (form.childNodes[i].name == 'form[text]') {
				form.childNodes[i].value = '';
				break
			}
		}
	}
	
	return false;
}
