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

