﻿//Posts comments via AJAX. Updates all review UI afterwards.
function PostComments() {
    $(".IsAjax").val("1");
    var serializedForm = $("#commentForm").serialize();
    $.post(SubmitAction, serializedForm, function(responseMsg) {
        try {
            var response = eval("(" + responseMsg + ")");
            if (response.RedirectTo)
                document.location = response.RedirectTo;
            else if (response.IsUpdated) {
                //GetRatings(serializedForm);
                try { UploadImage(); }
                catch (e) { }
                $("#commentForm").remove();
            }
            if (response.Message)
                SetReviewMessage(response.Message);
        }
        catch (ex) { }
    });
    $(".IsAjax").val("0");
    return false;
}

//Post a rating via AJAX. Updates all review UI afterwards.
function PostRating(ratingCode) {
    var f = $("#commentForm");
    $(".IsAjax").val("1");
    var serializedForm = f.serialize();

    $.post(RateProductAction, serializedForm, function(responseMsg) {
        try {
            var response = eval("(" + responseMsg + ")");
            if (response.RedirectTo)
                document.location = response.RedirectTo;
            else if (response.IsUpdated)
                GetRatings(serializedForm);
            if (response.Message)
                SetReviewMessage(response.Message);
        }
        catch (ex) { }
    });
    $(".IsAjax").val("0");

    return false;
}

//Post review helpfulness choice via AJAX. Updates comments list afterwards.
function PostReviewHelpfulness(btn, val) {
    var form = $(btn).parents('form');
    $(".IsAjax").val("1");
    $(form).children('#helpfulness').val(val);
    $.post(RateReviewAction, $(form).serialize(), function(data) {
        var response = eval("(" + data + ")");
        if (response.RedirectTo)
            document.location = response.RedirectTo;
        else if (response.IsUpdated) {
            //GetComments($('#toolbarForm').serialize());
            SetReviewMessage(response.Message);
        }
    });
    $(".IsAjax").val("0");
    return false;
}

//Gets entire ratings UI via AJAX.
function GetRatings(serializedForm) {
    $.post(ReviewProductAction, serializedForm, function(data) {
        $('dd.reviews').html(data);
        InitRating();
        SetupRatings();
        NicerButtons();
    });
}

//Shows a message to the user after an AJAX call.
function SetReviewMessage(msg) {
    if (msg) {
        $('.reviewMessage').html(msg);
        $('.reviewMessage').show();
    }
    else {
        $('.reviewMessage').html('');
        $('.reviewMessage').hide();
    }
}

//Gets a certain page of comments list via AJAX
function GetCommentsPage(page) {
    $('#SelectedPage').val(page);
    GetComments($('#toolbarForm').serialize());
}

//Gets comments list via AJAX
function GetComments(serializedForm) {
    AjaxWait();
    $.post(ProductCommentListingAction, serializedForm, function(data) {
        $('#comments').html(data);
        $('#comments #SelectedSort').change(function() {
            var serializedForm = $('#toolbarForm').serialize();
            GetComments(serializedForm);
        });
        AjaxStop();
    });
}
//called when page first loads to setup ratings UI stuff.
function SetupRatings() {
    $('.ratingStar').rating();//{
        //callback: function(value, link) { PostRating(value); },
        //required: true
    //});
    $('.hideForAjax').css("visibility", "hidden").css("display", "none");

    $(".sort").show();
    $('#comments #SelectedSort').change(function() {
        var serializedForm = $('#toolbarForm').serialize();
        GetComments(serializedForm);
    });
}

function AjaxWait() {
    $('div#comments').block({
        centerY: false,
        message: AjaxMessage,
        css: { border: '1px solid Silver', top: '100px', padding: '5px 5px 5px 5px' },
        overlayCSS: { backgroundColor: '#FFFFFF', opacity: '.8' }
    });
}
function AjaxStop() {

    $('div#comments').unblock();
}
function UploadImage() {
    $.ajaxFileUpload
    (
        {
            url: FileUploadAction,
            secureuri: false,
            fileElementId: 'ReviewImage',
            dataType: 'json',
            success: function(data, status) { },
            error: function(data, status, e) { }
        }
    )
    return false;
}
