﻿//json call for reporting comment
function ReportFanComment(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 {
				        alert("Your report was sent successfully!");
				        window.location.reload()
				    }
				}
		);
}

function ReportFanCommentParametersJson(ReportFanCommentProxyServiceParameters, FanCommentId, FanId, Reason, Comment) {
    // { "FanCommentId" : {FanCommentId}, "FanId" : {FanId}, "Reason" : "{Reason}", "Comment" : "{Comment}" }
    var parametersJson = ReportFanCommentProxyServiceParameters;

    parametersJson = parametersJson.replace("{FanCommentId}", FanCommentId);
    parametersJson = parametersJson.replace("{FanId}", FanId);
    parametersJson = parametersJson.replace("{Reason}", Reason);
    parametersJson = parametersJson.replace("{Comment}", htmlentities(Comment));
    return parametersJson;
}

function openReportComment() {
    return function() {

        // display comment info
        $("div#ReportFanComment").attr("fancommentid", $(this).attr("fancommentid"));
        $("div#ReportFanComment div.Message span#Author").html($(this).attr("author"));
        $("div#ReportFanComment div.Comment span#Comment").html($(this).attr("comment"));
        $("div#ReportFanComment [id*=FanCommentIdHiddenField]").val($(this).attr("fancommentid"));
        $("div#ReportFanComment [id*=FanIdHiddenField]").val($(this).attr("fanid"));

        $('#ReportFanComment').lightbox_me({
            centered: true,
            onLoad: function() {
                $('#ReportFanComment #ReportFanCommentReasonDropDownList').focus();
            },
            closeSelector: '.ReportFanCommentCancel'
        });
    };
};

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
$('#FanComments').ready(function() {

    // lightbox setup on click
    $('#FanComments div.CommentList div.items div.FanCommentItem div.Buttons span.ReportThisPost').click(openReportComment());

});

// Report Comment Popup
$("#ReportFanComment").ready(function() {


    // clear keyword text box on enter if default email
    $('#ReportFanComment #ReportFanCommentTextBox').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default keyword text box if empty
    $('#ReportFanComment #ReportFanCommentTextBox').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // if press enter in email click the button
    $('#ReportFanComment #ReportFanCommentTextBox').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#ReportFanComment #ReportFanCommentSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // ReportFanCommentSubmit click
    $('#ReportFanComment #ReportFanCommentSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;

        // trim any spaces
        $('#ReportFanComment #ReportFanCommentTextBox').val($.trim($('#ReportFanComment #ReportFanCommentTextBox').val()));

        if (($('#ReportFanComment #ReportFanCommentTextBox').val() == $('#ReportFanComment #ReportFanCommentTextBox').attr('default')) || ($('#ReportFanComment #ReportFanCommentTextBox').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#ReportFanComment #ReportFanCommentTextBox').attr('default') + '\n';
            setFocus = $('#ReportFanComment #ReportFanCommentTextBox');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
            var parametersJson = ReportFanCommentParametersJson($("#ReportFanComment").attr("proxyserviceparameters"), $("#ReportFanComment").attr("fancommentid"), $("div#ReportFanComment [id*=FanIdHiddenField]").val(), $('#ReportFanComment #ReportFanCommentReasonDropDownList option:selected').text(), $("#ReportFanComment #ReportFanCommentTextBox").val());
            ReportFanComment($("#ReportFanComment").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

} // function ()
);
//-->    
