﻿addLoadEvent(preparePage);

function preparePage() {
    if ($("newComment")) {
	    $("newComment").getElementsByTagName("form")[0].onsubmit = function() {
		    submitFormComment(); return false;
	    }
    }
}
function submitFormComment() {
    var getPostID = parseInt($("hdnPostID").value);
    var getAuthID = $("hdnAuthID");
    var getName = $("txtName");
    var getEmail = $("txtEmail");
    var getUrl = $("txtUrl");
    var getBody = $("txtBody");
    var getRemember = $("chkRemember").checked;
    
	var str = "";

    if (getName.value.length < 2) {
        $("notifyArea").innerHTML = "You must enter your name in order to post your comment.";
        showNotification("notifyArea");
    } else if (getEmail.value.length < 6) {
        $("notifyArea").innerHTML = "You must enter your email in order to post your comment.";
        showNotification("notifyArea");
    } else if (getBody.value.length < 1) {
        $("notifyArea").innerHTML = "You must enter your message in order to post your comment.";
        showNotification("notifyArea");
    } else {
	    str += "hdnPostID=" + escape(getPostID);
	    str += "&hdnAuthID=" + escape(getAuthID.value);
	    str += "&txtName=" + escape(getName.value);
	    str += "&txtEmail=" + escape(getEmail.value);
	    str += "&txtUrl=" + escape(getUrl.value);
	    str += "&txtBody=" + escape(getBody.value);
	    str += "&chkRemember=" + escape(getRemember);

        //initiateLoading();
        $("btnSubmit").disabled = true;
        $("notifyArea").innerHTML = "Posting comment, please wait...";
        showNotification("notifyArea",true);

	    sendFormComment(str);
    }
}
function sendFormComment(str) {
	var request = getConnection();
	if (request) {
		request.onreadystatechange = function() {
			parseResult(request);
		};
		request.open("POST", "/ajax.aspx?x=PostComment"+getRandom(), true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send(str);
	}
	return false;
}
function parseResult(request) {
    var getResponse = false;

	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var data = eval('('+request.responseText+')');

			if (data.Response.Status) {
			    //stopLoading();

			    if (data.Response.Status.Error) {
			        $("btnSubmit").disabled = false;
			        $("notifyArea").innerHTML = data.Response.Status.Text;
			        showNotification("notifyArea");
			    } else {
		            $("notifyArea").innerHTML = data.Response.Status.Text;
		            showNotification("notifyArea",true);
		            addClass($("newComment").getElementsByTagName("form")[0], "hide");
		            if (data.Response.Status.Updated) {
		                addComment(data.Response.Status.Updated);
		            }
			    }
			}
		} else if (request.status == 500) {
		    $("btnSubmit").disabled = false;
		    $("notifyArea").innerHTML = "Your comment could not be posted due to an error. Please try again later.";
		    showNotification("notifyArea");
		    //stopLoading();
		}
	}
}
function addComment(updated) {
    if ($("dCommentHeader")) {
        if (updated.Count == 1) {
            $("dCommentHeader").innerHTML = "COMMENT ("+updated.Count+")";
        } else {
            $("dCommentHeader").innerHTML = "COMMENTS ("+updated.Count+")";
        }
    }
    if ($("dCommentLink"+parseInt($("hdnPostID").value))) {
        if (updated.Count == 1) {
            $("dCommentLink"+parseInt($("hdnPostID").value)).innerHTML = updated.Count+" COMMENT";
        } else {
            $("dCommentLink"+parseInt($("hdnPostID").value)).innerHTML = updated.Count+" COMMENTS";
        }
    }
    if ($("divNoComments")) {
        addClass($("divNoComments"), "hide");
    }

    if ($("commentsList")) {
	    var comList = $("commentsList");
	    comList.lastChild.className = "comment";
	    
	    var newComment = document.createElement("div");
	    newComment.className = "highlight last comment";

	    var comBody = document.createElement("div");
	    comBody.className = "body";
	    var bodyTitle = document.createElement("h5");
	    var bodyTitleNode = document.createTextNode(updated.Date+" by ");
	    bodyTitle.appendChild(bodyTitleNode);
	    
	    var nameText = document.createTextNode(updated.Name);
	    if (updated.Url != "") {
	        var bodyUrl = document.createElement("a");
	        bodyUrl.href = updated.Url;
	        bodyUrl.setAttribute("rel","nofollow");
	        bodyUrl.appendChild(nameText);
	        bodyTitle.appendChild(bodyUrl);
	    } else {
	        bodyTitle.appendChild(nameText);
	    }
	    comBody.appendChild(bodyTitle);

	    var bodyText = document.createElement("p");
	    var bodyTextNode = document.createTextNode(updated.Body);
	    bodyText.appendChild(bodyTextNode);
	    comBody.appendChild(bodyText);

	    var comAvatar = document.createElement("div");
	    comAvatar.className = "avatar";

        var avatarLink = document.createElement("a");
        avatarLink.href = "http://gravatar.com/";
        avatarLink.setAttribute("rel","nofollow");
        var avatarImg = document.createElement("img");
        avatarImg.setAttribute("src", updated.Avatar);
        avatarLink.appendChild(avatarImg);
        comAvatar.appendChild(avatarLink);

	    var comClr = document.createElement("div");
	    comClr.className = "clr";

	    newComment.appendChild(comBody);
	    newComment.appendChild(comAvatar);
	    newComment.appendChild(comClr);
	    comList.appendChild(newComment);
    }
}