﻿//<!--

$("#articleCommentReport").ready(

    function() {

        // hide report control initially
        $("#articleCommentReport").hide();
        $("#articleCommentReportResponse").hide();

        var reportThisPostSelect = function() {
            // hide comments
            $("#articleComments").hide();
            // show report control
            $("#articleCommentReport").show();
            $("div#articleCommentReport").attr("articlecommentid", $(this).attr("articlecommentid"));
            $("div#articleCommentReport div.message span#author").html($(this).attr("author"));
            $("div#articleCommentReport div.comment span#comment").html($(this).attr("comment"));
        }

        var $submitBtnImgTags = $("#articleComments div.comments div.buttons span.reportthispost");
        $submitBtnImgTags.click(reportThisPostSelect);

        var cancelSelect = function() {
            $("#articleCommentReport").hide();
            $("#articleComments").show();
        }

        var $cancelBtnImgTags = $("#articleCommentReport div.comments div.buttons input.cancel");
        $cancelBtnImgTags.click(cancelSelect);

        Salem.RegisterNamespace("ArticleCommentReport");
        Salem.ArticleCommentReport = function(serviceUrl) {

            var Instance = this;
            this.Service = new Salem.Ajax.Service(serviceUrl);
            this.swnUserId = $("div#articleCommentReport").attr("swnuserid");
            this.swnUserName = $("div#articleCommentReport").attr("swnusername");
            this.reason = $("div#articleCommentReport #ArticleCommentReportReasonDropDownList");
            this.priority = $("div#articleCommentReport #ArticleCommentReportPriorityDropDownList");
            this.report = $("div#articleCommentReport #ArticleCommentReportTextBox");
            this.submitCtl = $("div#articleCommentReport div.comments div.buttons input.submit");
            this.articleCommentReportCtl = $("div#articleCommentReport");
            this.articleCommentReportResponseCtl = $("div#articleCommentReportResponse");
            this.duplicateReportExists = false;

            this.SubmitResponseOnSuccess = function(results) {

                if (results.Error) {
                    alert('Sorry an error occurred submitting your comment. Please try again.');
                } else {
                    alert('Thank you. Your report has been sent.');
                    Instance.articleCommentReportCtl.hide();
                    $("#articleComments").show();

                } // if (results.Error)
            }; // this.SubmitResponseOnSuccess

            this.DuplicateReportExistsOnSuccess = function(results) {

                if (results.Error) {
                    alert('Sorry an error occurred submitting your report. Please try again.');
                } else {
                    Instance.duplicateReportExists = results.DuplicateReportExists;
                    if (!Instance.duplicateReportExists) {
                        Instance.Service.JsonGet("SubmitArticleCommentReport", "ArticleCommentReportJSONString=" + Instance.GetJSONString(), Instance.SubmitResponseOnSuccess, Instance.AjaxError);
                    } else {
                        Instance.swnUserId = results.SWNUserIdMax;
                        Instance.swnUserName = results.SWNUserIdMax;
                        Instance.Service.JsonGet("SubmitArticleCommentReport", "ArticleCommentReportJSONString=" + Instance.GetJSONString(), Instance.SubmitResponseOnSuccess, Instance.AjaxError);
                    }
                }
            };

            this.AjaxError = function(err) {
                alert("AJAX ERROR: Sorry an error occurred in the submission process. Please try again.");
            };

            this.GetJSONString = function() {
                return Salem.String.Format('{"ArticleCommentId":{0}, "SWNUserId":{1}, "SWNUserName":"{2}", "Reason":"{3}", "Priority":{4}, "Report":"{5}"}', $("div#articleCommentReport").attr("articlecommentid"), this.swnUserId, this.swnUserName, this.reason.val(), this.priority.val(), this.report.val());
            };

            this.GetJSONStringForDuplicateCheck = function() {
                return Salem.String.Format('{"ArticleCommentId":{0}}', $("div#articleCommentReport").attr("articlecommentid"));
            };

            this.SubmitResponse = function() {
                if (Instance.report.val() == '') {
                    alert("Please enter your report before submitting.");
                } else {
                    Instance.Service.JsonGet("CheckForDuplicateReport", "DuplicateReportJSONString=" + Instance.GetJSONStringForDuplicateCheck(), Instance.DuplicateReportExistsOnSuccess, Instance.AjaxError);
                }
            };

            // bind events to functions declared above
            this.submitCtl.click(this.SubmitResponse);

        } // Salem.ArticleCommentReport = function(serviceUrl)

        var ArticleCommentReportInstance = new Salem.ArticleCommentReport($("div#articleCommentReport").attr("ProxyServiceUrl"));

    } // function ()
);
//-->    
