﻿//json call for comment
function AddCommentReview(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 AddCommentReviewParametersJson(AddCommentReviewProxyServiceParameters, RecaptchaChallenge, RecaptchaResponse, AlbumId, Comment) {
    // { "RecaptchaChallenge":"{RecaptchaChallenge}", "RecaptchaResponse":"{RecaptchaResponse}", "AlbumId" : {AlbumId}, "Comment" : "{Comment}" }
    var parametersJson = AddCommentReviewProxyServiceParameters;
    parametersJson = parametersJson.replace("{RecaptchaChallenge}", RecaptchaChallenge);
    parametersJson = parametersJson.replace("{RecaptchaResponse}", RecaptchaResponse);
    parametersJson = parametersJson.replace("{AlbumId}", AlbumId);
    parametersJson = parametersJson.replace("{Comment}", htmlentities(Comment));
    return parametersJson;
}

function openAddCommentReview(allowGuest) {
    return function() {
        var authenticated = ($("#AddCommentReview").attr("isauthenticated") == 'true');

        if (!authenticated && !allowGuest) {
            alert('Login first to comment as a member.');
            return false;
        }

        $('#AddCommentReview').lightbox_me({    
            centered: true,
            onLoad: function() {
                $('#AddCommentReview #AddCommentReviewText').focus();
            },
            closeSelector: '.AddCommentReviewCancel'
        });
        if (!authenticated) {
            RecaptchaShow($("#AddCommentReview").attr("recaptchapublickey"), 'AddCommentReviewRecaptcha', '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 #FanTalkAddCommentReviewGuest').click(openAddCommentReview(true));
$('#FanTalk #FanTalkAddCommentReview').click(openAddCommentReview(false));

});

// Add Comment Popup
$('#AddCommentReview').ready(function() {

    // clear keyword text box on enter if default email
    $('#AddCommentReview #AddCommentReviewText').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default keyword text box if empty
    $('#AddCommentReview #AddCommentReviewText').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });


    // if press enter in email click the button
    $('#AddCommentReview #AddCommentReviewText').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#AddCommentReview #AddCommentReviewSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // AddCommentArticleSubmit Click
    $('#AddCommentReview #AddCommentReviewSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;

        // trim any spaces
        $('#AddCommentReview #AddCommentReviewText').val($.trim($('#AddCommentReview #AddCommentReviewText').val()));

        if (($('#AddCommentReview #AddCommentReviewText').val() == $('#AddCommentReview #AddCommentReviewText').attr('default')) || ($('#AddCommentReview #AddCommentReviewText').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddCommentReview #AddCommentReviewText').attr('default') + '\n';
            setFocus = $('#AddCommentReview #AddCommentReviewText');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
        var parametersJson = AddCommentReviewParametersJson($("#AddCommentReview").attr("proxyserviceparameters"), RecaptchaChallenge(), RecaptchaResponse, $("#AddCommentReview").attr("albumid"), $("#AddCommentReview #AddCommentReviewText").val());
        AddCommentReview($("#AddCommentReview").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

});

