﻿//<!--

$("#addComment").ready(

    function() {

        Salem.RegisterNamespace("ArticleComment");
        Salem.ArticleComment = function(serviceUrl) {

            var Instance = this;
            this.Service = new Salem.Ajax.Service(serviceUrl);
            this.articleId = $("div#addComment").attr("articleid");
            this.swnUserId = $("div#addComment").attr("swnuserid");
            this.swnUserName = $("div#addComment").attr("swnusername");
            this.comment = $("div#addComment #articleCommentTextBox");
            this.submitCtl = $("div#addComment div.commentBox div.buttons input.submit");
            this.addCommentCtl = $("div#addComment");

            this.SubmitResponseOnSuccess = function(results) {

                if (results.Error) {
                    alert('Sorry an error occurred submitting your comment. Please try again.');
                } else {
                    window.location = window.location.href;
                } // if (results.Error)
            }; // this.SubmitResponseOnSuccess

            this.AjaxError = function(err) {
                alert("AJAX ERROR: Sorry an error occurred submitting your comment. Please try again.");
            };

            this.GetJSONString = function() {
                return Salem.String.Format('{"ArticleId":{0}, "Comment":"{3}","SWNUserId":{1}, "SWNUserName":"{2}"}', this.articleId, this.swnUserId, this.swnUserName, this.comment.val());
            };

            this.SubmitResponse = function() {
                if (Instance.comment == null) {
                    alert("Please enter your comment before submitting.");
                } else {
                    Instance.Service.JsonGet("SubmitArticleComment", "ArticleCommentJSONString=" + Instance.GetJSONString(), Instance.SubmitResponseOnSuccess, Instance.AjaxError);
                }
            };

            // bind events to functions declared above
            this.submitCtl.click(this.SubmitResponse);

        } // Salem.ArticleComment = function(serviceUrl)

        var ArticleCommentInstance = new Salem.ArticleComment($("div#addComment").attr("ProxyServiceUrl"));

    } // function ()
);
//-->    