﻿//json call for comment
function AddCommentArticle(proxyserviceurl, parametersJson) {
    $.getJSON(proxyserviceurl
				, { ParametersJson: parametersJson, CacheBustDateTimeStamp: new Date().toString() }
				, function(json) {
				    json = JSON.parse(json.d);
				    if (json.Error) {
				        alert(json.Message);
				        if (json.RecaptchaError) RecaptchaReload();
				    } else {
				        window.location.reload()
				    }
				}
		);
}

function AddCommentArticleParametersJson(AddCommentArticleProxyServiceParameters, RecaptchaChallenge, RecaptchaResponse, ArticleId, Comment) {
    // { "RecaptchaChallenge":"{RecaptchaChallenge}", "RecaptchaResponse":"{RecaptchaResponse}", "ArticleId" : {ArticleId}, "Comment" : "{Comment}" }
    var parametersJson = AddCommentArticleProxyServiceParameters;
    parametersJson = parametersJson.replace("{RecaptchaChallenge}", RecaptchaChallenge);
    parametersJson = parametersJson.replace("{RecaptchaResponse}", RecaptchaResponse);
    parametersJson = parametersJson.replace("{ArticleId}", ArticleId);
    parametersJson = parametersJson.replace("{Comment}", htmlentities(Comment));
    return parametersJson;
}

function openAddCommentArticle(allowGuest) {
    return function() {
        var authenticated = ($("#AddCommentArticle").attr("isauthenticated") == 'true');

        if (!authenticated && !allowGuest) {
            alert('Login first to comment as a member.');
            return false;
        }

        $('#AddCommentArticle').lightbox_me({
            centered: true,
            onLoad: function() {
                $('#AddCommentArticle #AddCommentArticleText').focus();
            },
            closeSelector: '.AddCommentArticleCancel'
        });

        if (!authenticated) {
            RecaptchaShow($("#AddCommentArticle").attr("recaptchapublickey"), 'AddCommentArticleRecaptcha', 'white');
        }
        return false;
    };
};

function htmlentities(str) {
    var i, output = '', len, chr = '';
    len = str.length;
    for (i = 0; i < len; i++) {
        char = str[i].charCodeAt(0);
        if ((char > 47 && char < 58) || (char > 62 && char < 127)) {
            output += str[i];
        } else {
            output += "&#" + str[i].charCodeAt(0) + ";";
        }
    }
    return output;
}

// Fan Talk control
$('#FanTalk').ready(function() {

    // lightbox setup on login click
    $('#FanTalk #FanTalkAddCommentArticleGuest').click(openAddCommentArticle(true));
    $('#FanTalk #FanTalkAddCommentArticle').click(openAddCommentArticle(false));

});

// Add Comment Popup
$('#AddCommentArticle').ready(function() {

    // clear keyword text box on enter if default email
    $('#AddCommentArticle #AddCommentArticleText').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default keyword text box if empty
    $('#AddCommentArticle #AddCommentArticleText').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });


    // if press enter in email click the button
    $('#AddCommentArticle #AddCommentArticleText').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#AddCommentArticle #AddCommentArticleSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // AddCommentArticleSubmit Click
    $('#AddCommentArticle #AddCommentArticleSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;

        // trim any spaces
        $('#AddCommentArticle #AddCommentArticleText').val($.trim($('#AddCommentArticle #AddCommentArticleText').val()));

        if (($('#AddCommentArticle #AddCommentArticleText').val() == $('#AddCommentArticle #AddCommentArticleText').attr('default')) || ($('#AddCommentArticle #AddCommentArticleText').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddCommentArticle #AddCommentArticleText').attr('default') + '\n';
            setFocus = $('#AddCommentArticle #AddCommentArticleText');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
        var parametersJson = AddCommentArticleParametersJson($("#AddCommentArticle").attr("proxyserviceparameters"), RecaptchaChallenge(), RecaptchaResponse, $("#AddCommentArticle").attr("articleid"), $("#AddCommentArticle #AddCommentArticleText").val());
        AddCommentArticle($("#AddCommentArticle").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

});

