﻿//<!--

$("#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 #txtUsername");
            this.emailAddress = $("div#addComment #txtEmailAddress");
            this.comment = $("div#addComment #articleCommentTextBox");
            this.recaptchapublickey = $("div#addComment").attr("recaptchapublickey");
            this.challenge = Recaptcha.get_challenge();
            this.response = Recaptcha.get_response();
            this.submitCtl = $("div#addComment div.commentBox div.buttons input.submit");
            this.addCommentCtl = $("div#addComment");

            // create new reCaptcha object
            Recaptcha.create(Instance.recaptchapublickey, "dynamic_recaptcha_1", {
                theme: "red",
                tabindex: 4
                //callback: Recaptcha.focus_response_field
            });

            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.SumbitReCaptchaOnSuccess = function(results) {

                if (results.Response) {
                    // ReCaptcha was successful so submit the Comment
                    Instance.Service.JsonGet("SubmitArticleComment", "ArticleCommentJSONString=" + Instance.GetJSONString(), Instance.SubmitResponseOnSuccess, Instance.AjaxError);
                } else {
                    alert("Please re-try entering the words displayed in the ReCaptcha box");
                    // create new reCaptcha object
                    Recaptcha.create(Instance.recaptchapublickey, "dynamic_recaptcha_1", {
                        theme: "red",
                        tabindex: 4,
                        callback: Recaptcha.focus_response_field
                    });

                }
            };

            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}", "EmailAddress":"{4}"}', this.articleId, this.swnUserId, this.swnUserName.val(), this.comment.val(), this.emailAddress.val());
            };

            this.ReCaptchaJSONString = function() {
                //return Salem.String.Format('{"Challenge":"{0}", "Response":"{1}"}', this.challenge.val(), this.response.val());
                return Salem.String.Format('{"Challenge":"{0}", "Response":"{1}"}', Recaptcha.get_challenge(), Recaptcha.get_response());
            };

            this.SubmitResponse = function() {
                var submit = false;
                var message = "Please fix the following errors:\n\n";
                var emailAddress = jQuery.trim(Instance.emailAddress.val());

                // check if name is supplied
                if (Instance.swnUserName.val() == '') {
                    message = message + "Please enter your name.\n";
                    submit = false;
                } else {
                    submit = true;
                };

                // check if email address supplied
                if (emailAddress == '') {
                    message = message + "Please enter your e-mail address.\n";
                    submit = false;
                } else {
                    // check if valid email address
                    if (!isValidEmail(emailAddress, $("#NLSignUpQuick input#NLSignUpQuickEmailValidationRegExp").val())) {
                        message = message + "Please enter a valid email address.\n";
                        submit = false;
                    } else {
                        submit = true;
                    };
                };

                // check if comment is supplied
                if (Instance.comment.val() == '') {
                    message = message + "Please enter your comment.\n";
                    submit = false;
                } else {
                    submit = true;
                };

                if (submit) {
                    // check ReCaptcha field and verify that it is correct
                    Instance.Service.JsonGet("SubmitReCaptchaChallenge", "ReCaptchaJSONString=" + Instance.ReCaptchaJSONString(), Instance.SumbitReCaptchaOnSuccess, Instance.AjaxError);
                } else {
                    alert(message);
                };
            };

            // 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 ()
);
//-->    
