﻿$(function() {
    PrepKCTreesForm();
});

function PrepKCTreesForm() {

    $(".kctreesinput").hide();
    $(".EditKCTreesEntrySave").hide();
    $(".EditKCTreesEntryCancel").hide();

    $(".EditKCTreesEntryEdit").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $("#labelname_" + id).hide();
        $("#labelphone_" + id).hide();
        $("#labelcomments_" + id).hide();
        $("#edit_" + id).hide();

        $("#name_" + id).fadeIn();
        $("#phone_" + id).fadeIn();
        $("#comments_" + id).fadeIn();
        $("#save_" + id).fadeIn();
        $("#cancel_" + id).fadeIn();

        return false;
    });

    $(".EditKCTreesEntryCancel").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $("#name_" + id).hide();
        $("#phone_" + id).hide();
        $("#comments_" + id).hide();
        $("#save_" + id).hide();
        $("#cancel_" + id).hide();

        $("#labelname_" + id).fadeIn();
        $("#labelphone_" + id).fadeIn();
        $("#labelcomments_" + id).fadeIn();
        $("#edit_" + id).fadeIn();

        $("#name_" + id).val($("#labelname_" + id).text());
        $("#phone_" + id).val($("#labelphone_" + id).text());
        $("#comments_" + id).val($("#labelcomments_" + id).text());

        return false;
    });

    $(".EditKCTreesEntrySave").click(function() {
        var id = $(this).attr("id").split("_")[1];

        $.ajax({
            type: "POST",
            url: "/KCTrees/SaveEntry",
            data: "id=" + id + "&phone=" + $("#phone_" + id).val() + "&name=" 
                    + $("#name_" + id).val() + "&comments=" + $("#comments_" + id).val(),
            success: function(msg) {
                //alert("Data Saved: " + msg);
            }
        });
        
        $("#name_" + id).hide();
        $("#phone_" + id).hide();
        $("#comments_" + id).hide();
        $("#save_" + id).hide();
        $("#cancel_" + id).hide();

        $("#labelname_" + id).text($("#name_" + id).val());
        $("#labelphone_" + id).text($("#phone_" + id).val());
        $("#labelcomments_" + id).text($("#comments_" + id).val());

        $("#labelname_" + id).fadeIn();
        $("#labelphone_" + id).fadeIn();
        $("#labelcomments_" + id).fadeIn();
        $("#edit_" + id).fadeIn();

        return false;
    });
}
