function useful(id)
{
    $.get(
        '/survey/useful/' + id,
        function(data) {
            $('#useful-' + data.id).html('(' + data.count + ')');
        }
    );
}

function not_useful(id)
{
    $.get(
        '/survey/not-useful/' + id,
        function(data) {
            $('#useful-total-' + data.id).html('(' + data.count + ')');
        }
    );
}

function view_detail(id)
{
    window.open('/survey/detail/' + id, '', "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=500,height=650");
}

function flagged_detail(seoname, id)
{
    window.open('/merchant/dashboard/flagged_detail/'+seoname+'/detail/' + id, '', "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=500,height=650");
}

function report(id)
{
    $.get(
        '/survey/report/' + id,
        function(data) {
            $('#report-' + data.id).html(data.message);
        }
    );
}

function expand_survey_comments(id, page)
{
    $('#survey-comments-' + id).html('<span class="blue">Loading user comments...</span>');
    limit = 10;
    if (!page) page = 1;
    $.get(
        '/survey/comments/' + id + '/page/' + page + '/limit/' + limit,
        function (data) {
            id = data.id;
            container = $('#survey-comments-' + id);
            container.addClass('sub-comment-box');
            if (data.message) {
                container.html(data.message);
            } else {
                content = ""
                for (var ele in data.comments) {
                    sub_content = data.comments[ele]
                    content = content + sub_content;
                }
                if (data.count >= limit) {
                    content = content + "<div style='float:right;' class='blue' onclick='expand_survey_comments(" + id + ", " + (page + 1) +");'>next &gt;</div>";
                    if (page > 1) {
                        content = content + "<div style='float:right;' class='blue' onclick='expand_survey_comments(" + id + ", " + (page - 1) +");'>&lt; prev</div>";
                    }
                }
                content = content + "<div onclick='collapse_survey_comments(" + id + ", " + page + ")' class='blue sub-comment-footer pointer'>Close comments</div>";
                container.html(content);
            }
        }
    );
}

function collapse_survey_comments(id, page)
{
    $('#survey-comments-' + id)
        .html('<span class="blue link" onclick="expand_survey_comments(' + id + ', ' + page + ')">+ Read the user comments for this survey</span>')
        .removeClass('sub-comment-box');
}

inNew = false;
inEdit = false;
function new_survey_comment(id)
{
    if (!inNew && !inEdit) {
        inNew = true;
        $('#new-comment-response-' + id).show()
            .html('<div><textarea name="comment-content" style="width:100%;" rows="3"></textarea></div><div style="float:right"><span name="save-comment" class="blue pointer" onclick="save_survey_comment(' + id + ')">Save</span> or <span name="save-comment" class="blue pointer" onclick="cancel_new_survey_comment(' + id + ')">Cancel</span></div><br style="clear:both;" />');
    }
}

function cancel_new_survey_comment(id)
{
    $('textarea[name="comment-content"]').val('');
    $('#new-comment-response' + id).hide().html('');
    inNew = false;
}

function save_survey_comment(id)
{
    user_comment = $('textarea[name="comment-content"]').val();
    if (inEdit) {
        $.post(
            '/survey/edit-comment/' + id,
            {comment: user_comment},
            function (data) {
                if (data.success) {
                    id = data.id;
                    $('#survey-comment-' + id + ' > .sub-comment-content').html(data.comment);
                }
            }
        );
        inEdit = false;
    } else {
        $.post(
            '/survey/comment/' + id,
            {comment: user_comment},
            function (data) {
                if (data.success) {
                    $('#new-comment-response-' + id).hide().html('');
                    $('#new-comment-' + id).html('Thank you!');

                    expand_survey_comments(id, 1);
                }
            }
        );
        inNew = false;
    }
}

function cancel_edit_survey_comment(id)
{
    $('textarea[name="comment-content"]').val('');
    comment = $('#survey-comment-'+id+' > .sub-comment-content');
    comment.html($('#survey-comment-'+id+'-old').html());
    inEdit = false;
}

function edit_survey_comment(id)
{
    if (!inEdit && !inNew) {
        inEdit = true;
        comment = $('#survey-comment-'+id+' > .sub-comment-content');
        value = comment.html();
        comment.html('<div><textarea name="comment-content" style="width:100%;" rows="3">' + value + '</textarea></div><div style="float:right"><span name="save-comment" class="blue pointer" onclick="save_survey_comment(' + id + ')">Save</span> or <span name="save-comment" class="blue pointer" onclick="cancel_edit_survey_comment(' + id + ')">Cancel</span></div><br style="clear:both;" /><div style="display:none;" id="survey-comment-' + id + '-old">' + value + '</div>');
    }
}

function delete_survey_comment(id)
{
    $.get('/survey/delete-comment/' + id, function(data) { if (data.success) { $('#survey-comment-' + id).remove(); }});
}

function report_survey_comment(id)
{
    $.get('/survey/report-comment/' + id);
}
