
function loadComments(queryString){
	var query = queryString;
	
	$("#commentsLoading").show();
	$("#commentsContent").css({"visibility": "hidden"});
	
	$.get("/wjc09/common_gl/inc_comments.php", query, function(data){
		$("#commentsContent").html(data);
		$("#commentsContent").css({"visibility": "visible"});
		$("#commentsLoading").hide();
	});
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
	else 
	$('#'+countfield).html(""+(maxlimit - field.value.length)+"");
}

$().ready(function(){

	$('#postComment .warning').remove();
	
	$.get("/wjc09/common_gl/inc_comments_form_token.php",function(txt){
		$("#postComment").append('<input type="hidden" id="postTS" name="ts" value="'+txt+'" />');
	});
	
	$('#btnAddComment').click(
			function(){
				$("#postComment").submit();
			}
		);
	
	$("#postComment").submit(function() {
	
		if ($("#btnAddComment").attr("disabled") != true) {
			
			var errorMessage = "";
			
			var name = $('#postName').attr('value');
			var email = $('#postEmail').attr('value');
			var notify = $('#postNotify').is(':checked') ? '1' : '0';
			var comment = $('#postCmt').attr('value');
			var ts = $('#postTS').attr('value');
			var id = $('#postId').attr('value');
			var wt = $('#postWT').attr('value');
			
			if (name == "") errorMessage += "Please enter your name. \n";
			if (!validateEmail("postComment","postEmail") && notify == '1') errorMessage += "Please enter a valid email address. \n";
			if (comment.length == 0) errorMessage += "Please enter a comment. \n";
			
			if (errorMessage == "") {
			 
				$("#postComment input, #postComment textarea").attr("disabled", "disabled");
				$("#btnAddComment").attr("disabled", "disabled");
				$("#btnAddComment").hide();
				$("#btnPosting").show();
				$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
				$.ajax({
					type: "POST",
					url: "/wjc09/common_gl/inc_comment_post.php",
					data: "name="+ name + "&email=" + email + "&comment=" + basicURLEncode(comment) + "&notify=" + notify + "&ts=" + ts + "&id=" + id + "&wt=" + wt,
					success: function(data){
						if (data != "success") {
							$("#commentAddContent").html(data);
						} else {
							$("#btnPosting").hide();
							$("#btnPosted").show();
						}
					}
				});
				
			} else {
				alert("The following error(s) occurred: \n"+errorMessage);
			}
		
		}
		
		return false;
	});
});