// JavaScript Document
jQuery.fn.forcepositiveNum = function () {

    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.which || e.keyCode;
            //alert(key);
            if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                    // numbers
                    key >= 48 && key <= 57 ||
                    // Numeric keypad
                    key >= 96 && key <= 105 ||
                    // comma, period and minus, . on keypad
                    //key == 190 || key == 188 || key == 109 || key == 110 ||
                    // Backspace and Tab and Enter
                    key == 8 || key == 9 || key == 13 ||
                    // Home and End
                    key == 35 || key == 36 ||
                    // left and right arrows
                    key == 37 || key == 39 ||
                    // Del and Ins
                    key == 46 || key == 45)
                return true;

            return false;
        });
    });
}
$(document).ready(function (e) {
    /*---add--------------------------------------------------------------------------------------------------------------------------*/
    $(".positiveNo").forcepositiveNum();


    $('#accounts_tree_check').live('click', function () {
        $('span[data-value="1"]').addClass("badge badge-important");
        $('span[data-value="2"]').addClass("badge badge-warning");
        $('span[data-value="3"]').addClass("badge badge-success");
        $('span[data-value="4"]').addClass("badge badge-warning");

    });


    $('#trunk').live('click', function (e) {
        //$('#trunk').on("hidden", '.collapse', function (event) {
        e.preventDefault();
        if ($('#trunk').hasClass("collapsed"))
        {
            $('#expandAll').click();
        }
        if ($('#trunk').hasClass("expanded"))
        {
            $('#collapseAll').click();
        }
    });



    $('#expandAll').live('click', function () {
        $('#trunk').removeClass("collapsed");
        $('.maxtree li.parent_li > span').each(function (e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            children.show('fast');
            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
        });
        $('#trunk').addClass("expanded");
    });
    $('#collapseAll').live('click', function () {
        $('#trunk').removeClass("expanded");
        $('.maxtree li.parent_li > span').each(function (e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            children.hide('fast');
            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
        });
        $('#trunk').addClass("collapsed");
    });

    //$('#tree_expense').on('nodeSelected', function(event, data) {});

    /*$('#tree_expense_collapse').click(function () {
     $('.tree-toggle', $('#tree_expense > li > ul')).addClass("closed");
     $('.branch', $('#tree_expense > li > ul')).removeClass("in");
     });

     $('#tree_expense_expand').click(function () {
     $('.tree-toggle', $('#tree_expense > li > ul')).removeClass("closed");
     $('.branch', $('#tree_expense > li > ul')).addClass("in");
     });*/

    $('#resetFromProg').live('click', function () {
        var id = $(this).attr('data-id');
        //gaet parents
        $.post('accountstreeajax.php?do=resetfromprog', function (data) {
            redrawTree();
        });

    });
    $('.editLeaf').live('click', function () {
        var id = $(this).attr('data-id');
        //gaet parents
        $.post('accountstreeajax.php?do=getalltree', function (data) {
            $("#parent").html(data);
            //fill data to edit
            $.post('accountstreeajax.php?do=getitem', {id: id}, function (data) {
                var obj = JSON.parse(data);

                $('#itemIdModal').val(obj.id);
                $('#elementName').val(obj.customName);
                $("#parent").val(obj.parent).trigger("change");
                $('#itemtype').val(obj.itemtype).trigger("change");
                $('#itemtype2').val(obj.itemtype2).trigger("change");
                $('#accountNature').val(obj.accountNature).trigger("change");
                $('#reportid').val(obj.reportid).trigger("change");
                $('#listid').val(obj.listId).trigger("change");
                //$('#itemfrom').val(obj.itemfrom).trigger("change");
                $('#theValue').val(obj.theValue);

                $('#notes').val(obj.notes);

                if (obj.itemfrom == 0)
                {
                    $("#parent").attr("disabled", "disabled");
                    $('#itemtype').attr("disabled", "disabled");
                    $('#itemtype2').attr("disabled", "disabled");
                    //$('#itemfrom').attr("disabled", "disabled");
                    $('#theValue').attr("disabled", "disabled");
                    $('#notes').attr("disabled", "disabled");
                } else {
                    $("#parent").removeAttr("disabled");
                    $('#itemtype').removeAttr("disabled");
                    $('#itemtype2').removeAttr("disabled");
                    //$('#itemfrom').removeAttr("disabled");
                    $('#theValue').removeAttr("disabled");
                    $('#notes').removeAttr("disabled");
                }
                $('#accountsTreeModal').modal('show');
                //$('#theValueDiv').hide();
            });
        });

    });
    $('.delLeaf').live('click', function () {
        var id = $(this).attr('data-id');
        $('#myDelete').attr('data-id', id);
        $('#confirm').modal('show');


    });
    $('.orderLeaf').live('change', function () {
        var id = $(this).attr('data-id');
        var neworder = $(this).val();
        $.post('accountstreeajax.php?do=reorder', {id: id, neworder: neworder}, function (data) {
            redrawTree();
        });

    });
    $('#myDelete').live('click', function () {
        var id = $(this).attr('data-id');
        $.post('accountstreeajax.php?do=delthis', {id: id}, function (data) {
            if (data == "0")
            {
                redrawTree();
            } else {
                alert('عفوا لا يمكن حذف هذا العنصر لانه مرتبط بعناصر اخرى');
            }
        });
    });


    $('.infoLeaf').live('click', function () {
        var id = $(this).attr('data-id');
        //gaet parents
        $.post('accountstreeajax.php?do=getdetail', {id: id}, function (data) {
            $("#accountDetailDiv").html(data);
            $('#accountDetailModal').modal('show');
        });

    });
    $('.reShape').live('click', function () {
        var id = $(this).attr('data-id');
        var shape = $(this).attr('data-shape');
        //gaet parents
        $.post('accountstreeajax.php?do=getdetail', {id: id, shape: shape}, function (data) {
            $("#accountDetailDiv").html(data);
        });

    });
    $('.closeAccountDetailForm').live('click', function () {
        $('#accountDetailDiv').html("");
        $('#accountDetailModal').modal('hide');
    });


    $('#accounts_tree_add').live('click', function () {
        emptyaccountsTreeModal();
        $.post('accountstreeajax.php?do=getalltree', function (data) {
            $("#parent").html(data);
            $('#theValue').val("0");
            $('#accountsTreeModal').modal('show');
        });

    });


    $('#itemtype2').live('change', function () {
        var itemIdModal = $('#itemIdModal').val();
        if ($(this).val() == 0)
        {
            $('#theValueDiv').hide();
        } else if ($(this).val() == 1 && itemIdModal == "") {
            $('#theValueDiv').show();
        }

    });
    $('#parent').live('change', function () {
        var id = $('#parent').val();
        if (id != "" && id != 0)
        {
            $.post('accountstreeajax.php?do=gettypeandlist', {id: id}, function (data) {
                $("#itemtype").val(data.split(',')[0]).change();
                $("#listid").val(data.split(',')[1]).change();
                $("#accountNature").val(data.split(',')[2]).change();
                chooseReport(data);
            });
        }
    });

    $('#itemtype').live('change', function () {
        var val = $(this).val();
        chooseReport(val);
    });



    $('.insertAccountsTree').live('click', function () {
        var itemName = $('#elementName').val();
        if (itemName == "")
        {
            $('#elementName').css('background-color', "yellow");
            return false;
        } else
        {
            $('#elementName').css('background-color', "white");
            var itemIdModal = $('#itemIdModal').val();
            var parent = $('#parent').val();
            var itemtype = $('#itemtype').val();
            //var itemfrom = $('#itemfrom').val();
            var notes = $('#notes').val();
            var itemtype2 = $('#itemtype2').val();
            var theValue = $('#theValue').val();
            var reportid = $('#reportid').val();
            var listid = $('#listid').val();
            var accountNature = $('#accountNature').val();

            $.post('accountstreeajax.php?do=additem', {itemIdModal: itemIdModal, itemName: itemName, parent: parent, itemtype: itemtype, notes: notes, itemtype2: itemtype2, theValue: theValue, reportid: reportid, listid: listid, accountNature: accountNature}, function (data) {
                if (Number(data) > 0)
                {
                    closeAccountsTreeForm();
                    redrawTree();
                } else if (Number(data) == -10)
                {
                    alert('عفوا هذا الاسم مكرر.جرب اسم اخر');
                }

            });
        }

    });

    $('#redrawtree').live('click', function () {
        redrawTree();
    });

    function redrawTree()
    {
        $('.load_maxtree').show();
        $.post('accountstreeajax.php?do=redrawtree', function (data) {
            $("#myTree").html(data);
            $('.load_maxtree').hide();
            $(".positiveNo").forcepositiveNum();
            //activate collapse expand tree again
            $(function () {
                $('.maxtree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
                $('.maxtree li.parent_li > span').on('click', function (e) {
                    if ($(this).attr("id") != "trunk")
                    {
                        var children = $(this).parent('li.parent_li').find(' > ul > li');
                        if (children.is(":visible")) {
                            children.hide('fast');
                            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
                        } else {
                            children.show('fast');
                            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
                        }
                        e.stopPropagation();
                    }
                });
            });

            $('#trunk').removeClass("collapsed");
            $('#trunk').addClass("expanded");

            //$('.parent_li').collapse("hide");
            //$('.parent_li').collapse();
        });
    }







    $('.closeAccountsTreeForm').live('click', function () {
        closeAccountsTreeForm();
    });


    function closeAccountsTreeForm()
    {
        $('#accountsTreeModal').modal('hide');
        emptyaccountsTreeModal();
    }
    function emptyaccountsTreeModal()
    {
        $('#accountsTreeInfo').find(':input').each(function () {
            var type = this.type, tag = this.tagName.toLowerCase();
            if (type == 'text' || type == 'hidden' || type == 'password' || tag == 'textarea')
                this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.value = '0';
            $(this).change();
        });
    }

    function chooseReport(val) {
        $('#reportid').val($('#listid').val()).trigger('change');
        //$('#itemtype2').val(1).change();
        $('#reportidDiv').show();
//        if (val == 0 || val == 2) {
//            $('#reportidDiv').show();
//            //$('#reportid').val(1).trigger('change');
//        } else
//        {
//            $('#reportidDiv').hide();
//            //$('#reportid').val(0).trigger('change');
//        }
    }

    /*-------------------------------------------------------------------------------*/
    $('#startGoods').live('click', function () {
        $("#startgoodsvalDiv").html("");
        $.post('accountstreeajax.php?do=getstartgoodsval', function (data) {
            $("#startgoodsvalDiv").html(data);
            $('#startgoodsvalModal').modal('show');
        });
    });
    $(".closestartgoodsvalModal").live('click', function () {
        $('#startgoodsvalModal').modal('hide');
    });
    $(".approveasstartgoods").live('click', function () {
        $.post('accountstreeajax.php?do=approveasstartgoods', function (data) {
            $('#startgoodsvalModal').modal('hide');
            redrawTree();
            $("#startgoodsvalDiv").html("");
        });
    });
});

/*--tree new------------------------------------------------------------------------------------*/
$(function () {
    $('.maxtree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
    //this line to show it collapsed by default
    $('.maxtree li ul > li').hide();
    $('.maxtree li.parent_li > span').on('click', function (e) {
        //if ($(this).attr("id") != "trunk")
        {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            if (children.is(":visible")) {
                children.hide('fast');
                $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
            } else {
                children.show('fast');
                $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
            }

            e.stopPropagation();
        }
    });
});


/*.maxtree li::before {
 border-right:1px solid #999;
 bottom:50px;
 height:100%;
 top:-5px;
 width:1px
 }*/

