﻿$(function() {
    PrepKCTootsieRollForm();
});

function PrepKCTootsieRollForm() {

    $(".kctootsierollinput").hide();
    $(".EditKCTootsierollEntrySave").hide();
    $(".EditKCTootsierollEntryCancel").hide();

    $(".EditKCTootsieRollEntryEdit").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $("#labelname_" + id).hide();
        $("#labelcomments_" + id).hide();
        $("#edit_" + id).hide();

        $("#name_" + id).fadeIn();
        $("#comments_" + id).fadeIn();
        $("#save_" + id).fadeIn();
        $("#cancel_" + id).fadeIn();

        return false;
    });

    $(".EditKCTootsieRollEntryCancel").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $("#name_" + id).hide();
        $("#comments_" + id).hide();
        $("#save_" + id).hide();
        $("#cancel_" + id).hide();

        $("#labelname_" + id).fadeIn();
        $("#labelcomments_" + id).fadeIn();
        $("#edit_" + id).fadeIn();

        $("#name_" + id).val($("#labelname_" + id).text());
        $("#comments_" + id).val($("#labelcomments_" + id).text());

        return false;
    });

    $(".EditKCTootsieRollEntrySave").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $.ajax({
            type: "POST",
            url: "/KCTootsieRoll/SaveEntry",
            data: "id=" + id + "&name=" + $("#name_" + id).val() + "&comments=" + $("#comments_" + id).val(),
            success: function(msg) {
                //alert("Data Saved: " + msg);
            }
        });
        
        $("#name_" + id).hide();
        $("#comments_" + id).hide();
        $("#save_" + id).hide();
        $("#cancel_" + id).hide();

        $("#labelname_" + id).text($("#name_" + id).val());
        $("#labelcomments_" + id).text($("#comments_" + id).val());

        $("#labelname_" + id).fadeIn();
        $("#labelcomments_" + id).fadeIn();
        $("#edit_" + id).fadeIn();

        return false;
    });
}
