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

