﻿/* Comment Controller */
var CommentControl = {
	checkLogin: function(myUrl) {
		var controller = this;
		$.ajax({
			type: "GET",
			url: myUrl,
			dataType: "xml",
			async: false,
			success: function(xml) {
				var id;
				$(xml).find('user').each(function() {
					id = $(this).find('ID').text();
				});
				(id !== null) ? controller.setLoginHTML(true) : controller.setLoginHTML(false);
			},
			error: function(resp) {
				controller.setLoginHTML(false);
			}
		});
	},
	setLoginHTML: function(loginStatus) {
		var controller = this;
		$('textarea#comment').val('');
		if (loginStatus) {
			$('#loggedIn').show();
			$('#loggedOut').hide();
		} else {
			$('#loggedIn').hide();
			$('#loggedOut').show();
		}
	},
	postComment: function() {
		var controller = this;
		var myText = $.trim($('textarea#comment').val());
		if (myText !== '') {
			$.ajax({
				type: "POST",
				url: "api/viaBlogPostComment.ashx",
				dataType: "xml",
				data: "comment=" + myText + "&postId=" + $('input#ctl00_content_postId').val(),
				async: false,
				success: function(resp) {
					$('#commentForm').html('<p class="success">Thanks for your comment! We&rsquo;ll post it after a quick review.</p>');
				},
				error: function(resp) {
					$('#commentForm').html('<p class="error">' + resp.statusText + '</p>');
				}
			});
		} else {
			$('textarea#comment').css("border-color", "red");
		}
	}
};
