<style>

    .dropdown-toggle {
        background-color:#3B4752;
        border-color : #3B4752;
    }

    .dropdown-toggle:hover{
        background-color:#3B4752;
        border-color : #3B4752;
    }

    .table thead tr >th{
        background-color:#3B4752;
        color:#fff;
        width: 16% !important;
    }
    .table thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }

    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:red!important;
        color:#fff;
        border: none;
    }
    .table-responsive {

        overflow-x: visible; 
    }
    .dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus{
        background-color:#fff!important ;
        color:#fff;

    }
    .TreeItem{
        display:inline-block;
        cursor:default();
    }
    .TreeItem > .AddButton{
        position:relative;
        top:1px;
        cursor:pointer;
        margin:0 5px;
         font-size:18px;
         -webkit-transition:opacity 0.2s linear;
        -moz-transition:opacity 0.2s linear;
        transition:opacity 0.2s linear;
    }
    .TreeItem:hover > .AddButton{
        opacity:1;
        visibility:visible;
        color: #ed9421;

    }
    .actionButton{
        background-color:#4c0870 ;
    }
    #AddNewItemDiv,#RemoveAddedItemDiv,#EditItemDiv{
        border: 1px dashed rgba(0, 0, 0, 0.25);
        padding: 10px;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        border-radius: 2px;
    }
    .errorInputText{
        border:1px solid rgba(255,0,0,0.3);
    }
    .input-group > input,.input-group > span{
        border-color:#d5d5d5 !important;
    }
    .input-group-addon i{
        color:#000 !important;
    }
</style>
<script src=" <?php echo THEME_PATH; ?>/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="<?php echo THEME_PATH; ?>/plugins/jquery-dynatree/jquery.dynatree.min.js" type="text/javascript"></script>
<link href="<?php echo THEME_PATH; ?>/plugins/jquery-dynatree/skin/ui.dynatree.css" rel="stylesheet" type="text/css" media="screen"/>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('view_department_details'); ?></div>
            <div id="alertnotify" ></div>
            <div class="pull-right">
                <div class="col-xs-12">


  <!--<div class="floatingContainer bg-info ADDCAT" ><a  a href="#" class="mainPageLinks" data-toggle="modal" data-target="#create-pos-cat-modal" type="button"  ><?php print CommonLanguage::getMessage('add_department') ?></a></div>-->
                    <?php if ($permissions->hasPermission('addDepartment')) { ?>
                        <div class="floatingContainer ADDCAT pull-right">
                            <a herf="#" data-toggle="modal" data-target="#create-pos-cat-modal"  type="button" ><div class="actionButton" >
                                    <p class="floatingText"><span class="floatingTextBG"><?php print CommonLanguage::getMessage('add_department') ?></span></p>
                                </div></a>
                        </div>
                    <?php } ?>


                </div>
            </div>



            <div id="tree"></div>

        </div>



    </div>
</div>
<div class="modal modal-lg fade" tabindex="-1" role="dialog" aria-labelledby="role-mode" id="create-pos-cat-modal" aria-hidden="true" style="display: none;
     margin: 0 auto;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="myModalLabel"><?php print CommonLanguage::getMessage('add_department'); ?></h4>
                <br>
            </div>
            <div class="modal-body">
                <div class="form-group col-md-12">
                    <div  class=" col-md-6">
                        <label><?php print CommonLanguage::getMessage('department_name'); ?></label><span class="red-star">*</span>
                        <input type="text" name="Department_Name" placeholder="<?php print CommonLanguage::getMessage('department_name'); ?>"   id="Department_Name" class="form-control input-rounded" required>
                    </div>
<!--                    <div  class=" col-md-6 form-group hidden">
                        <label><?php print CommonLanguage::getMessage('department_prefix'); ?></label><span class="red-star">*</span>
                        <input type="text" name="Department_Prefix"  id="Department_Prefix" placeholder="<?php print CommonLanguage::getMessage('department_prefix'); ?>" class="form-control input-rounded">
                    </div>-->
                </div>
                <div class="form-group col-md-12">
                    <div  class=" col-md-6">
                        <label><?php print CommonLanguage::getMessage('phone_extension'); ?></label>
                        <input type="text" name="department_phone" placeholder="<?php print CommonLanguage::getMessage('phone_extension'); ?>"   id="department_phone" class="form-control input-rounded" >
                    </div>
                    <div  class=" col-md-6">
                        <label><?php print CommonLanguage::getMessage('location'); ?></label>
                        <input type="text" name="department_location" placeholder="<?php print CommonLanguage::getMessage('location'); ?>"   id="department_location" class="form-control input-rounded" >
                    </div>

                </div>
            </div>

            <style>
.hover_close:hover{background-color:#4c0870 !important;}


            </style>
            <div class="modal-footer">
                <button type="button" class="btn btn-default hover_close" id="CloseAdd_Cat" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="background-color:#4c0870 !important;opacity:1;" type="submit" class="btn btn-info disabled" id="Add_Cat"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <!--< ?php echo Forms::ModalLoader('CreatePOSValidationAlert')?>-->
        </div>
    </div>
</div>
<!-- End of Modal -->
<!-- Edit CAT modal -->


<div id="ModalDisabler" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:999;display:none"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>



<script type="text/javascript">
    var sure_to_delete = <?php echo json_encode(CommonLanguage::getMessage('sure_to_delete')); ?>,
            removeLang = <?php echo json_encode(CommonLanguage::getMessage('remove')); ?>,
            AddLang = <?php echo json_encode(CommonLanguage::getMessage('add')); ?>,
            EditLang = <?php echo json_encode(CommonLanguage::getMessage('Edit')); ?>,
            CancelLang = <?php echo json_encode(CommonLanguage::getMessage('Close')); ?>,
            DeleteLang = <?php echo json_encode(CommonLanguage::getMessage('delete')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            duplicate = <?php echo json_encode(CommonLanguage::getMessage('duplicate')); ?>,
            ExpandAfterReloading = "";






    $(function () {
        // Attach the dynatree widget to an existing <div id="tree"> element
        // and pass the tree options as an argument to the dynatree() function:
        $("#tree").dynatree({
            title: "",
            checkbox: false,
            selectMode: 3,
            keyboard: false,
            fx: {height: "toggle", duration: 200},
            autoFocus: false,
            initAjax: {
                url: "HRMAjax/fetchBranch?cmd=fetch&id=0",
            },
            onPostInit: function (isReloading, isError) {
                this.$tree.dynatree('getRoot').visit(function (node) {
                    node.expand(true);
                });
            },
            onActivate: function (node) {

            },
            onCreate: function (dtnode, nodeSpan) {
                if (dtnode.data.DontShow == "yes")
                    dtnode.li.style.display = "none";

            },
            onCustomRender: function (node) {
                var TreePermissions = [<?php echo json_encode($permissions->hasPermission('addDepartment')); ?>,
<?php echo json_encode($permissions->hasPermission('EditDepartment')); ?>,
<?php echo json_encode($permissions->hasPermission('DeleteDepartment')); ?>
                ];
                var RemoveButton = TreePermissions[2] ? "<span onclick='RemoveNewItem(this)' class='AddButton DeleteItem fa fa-minus-circle'  style='margin:0'></span>" : '',
                        AddButton = TreePermissions[0] ? "<span onclick='AddNewItem(this)' class='AddButton fa fa-plus-circle'></span>" : '',
                        EditButton = TreePermissions[1] ? "<span onclick='EditItem(this)' class='AddButton fa fa-pencil-square-o'></span>" : '';
                return "<div class='TreeItem'    Department_Name='" + node.data.department_name + "' department_phone='" + node.data.department_phone + "' department_location='" + node.data.department_location + "' MyId='" + node.data.department_id + "' MyParent='" + node.data.parent_id + "'><strong>" + node.data.department_name /*+ " - " + node.data.department_prefix */ + "</strong> " + AddButton + EditButton + RemoveButton + "</div>";

            },
            onRender: function (node, nodeSpan) {

            },
            onLazyRead: function (node) {


                node.appendAjax({
                    url: "HRMAjax/fetchBranch?cmd=fetch&id=" + node.data.department_id,
                    debugLazyDelay: 750,
                    success: function (node) {


                        // Called after nodes have been created and the waiting icon was removed.
                        // now expand all children
                        node.visit(function (n) {
                            n.expand(true);
                        });
                    }

                });




            },
            dnd: {
                onDragStart: function (node) {
                    /** This function MUST be defined to enable dragging for the tree.
                     *  Return false to cancel dragging of node.
                     */
                    logMsg("tree.onDragStart(%o)", node);
                    return true;
                },
                onDragStop: function (node) {
                    // This function is optional.
                    logMsg("tree.onDragStop(%o)", node);
                },
                autoExpandMS: 1000,
                preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
                onDragEnter: function (node, sourceNode) {
                    /** sourceNode may be null for non-dynatree droppables.
                     *  Return false to disallow dropping on node. In this case
                     *  onDragOver and onDragLeave are not called.
                     *  Return 'over', 'before, or 'after' to force a hitMode.
                     *  Return ['before', 'after'] to restrict available hitModes.
                     *  Any other return value will calc the hitMode from the cursor position.
                     */

                    var NodeType = parseInt(node.data.department_id),
                            SourceNodeType = parseInt(sourceNode.data.department_id)
                    if ((node.data.department_id != SourceNodeType))
                        return true;
                    else if (sourceNode.data.parent_id == node.data.department_id)
                        return false;
                },
                onDrop: function (node, sourceNode, hitMode, ui, draggable) {

                    sourceNode.move(node, hitMode);
                    updateParentForDepartment(sourceNode.data.department_id, sourceNode.parent.data.department_id)


                }
            }

        });


        window.PageTreeObject = $("#tree").dynatree("getTree");

        $("#Add_Cat").on("click", function () {
            $("#CreatePOSValidationAlert").remove();



            var AlertBox = $('<div id="CreatePOSValidationAlert" class="WizardValidationAlert alert"></div>'), IsValidated = true;
            AlertBox.insertAfter($("#create-pos-cat-modal").find(".modal-dialog .modal-header").find("h4"))
            $("#CreateIsBusy").remove();
            $("#create-pos-cat-modal").append('<div id="CreateIsBusy" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:9999;opacity:0"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>');
            $("#CreateIsBusy").animate({opacity: 1});
            var Data = {
                Department_Name: $("#Department_Name").val(),
//                Department_Prefix: $("#Department_Prefix").val(),
                department_location: $("#department_location").val(),
                department_phone: $("#department_phone").val(),
                Parent_ID: 0
            }
            if ($("#Department_Name").val() == '' /*|| $("#Department_Prefix").val() == ''*/) {
                $("#CreatePOSValidationAlert").addClass("alert-danger").empty().html(Required_Fields);
                $("#CreateIsBusy").remove();
                return;
            }
            $.ajax({
                url: "HRMAjax/ViewDepartmentRequestList",
                method: "POST",
                dataType: 'json',
                data: Data,
                complete: function (v1, v2) {

                    if (v2 == 'success' && v1.responseJSON[0] == 'ok') {
                        $("#CreateIsBusy").remove();
                        $("#CreatePOSValidationAlert").addClass("alert-success").empty().html(Submitted_Successfully);
                        setTimeout(function () {
                            $("#create-pos-cat-modal").modal('hide')
                            $("#CreatePOSValidationAlert").hide()
                            $("#create-pos-cat-modal input").val('')
                        }, 1500)
                        window.PageTreeObject = $("#tree").dynatree("getTree");

                        location.reload()

                    } else {
                        $("#CreatePOSValidationAlert").addClass("alert-danger").empty().html(duplicate);
                    }
                    $("#CreateIsBusy").remove();

                }
            })
        });
    });
    $("#Department_Name").on("keyup", function () {
        UpdateStatusButton()
    })
    function UpdateStatusButton() {
        var IsValidated = true;
        if ($("#Department_Name").val() == "") {
            IsValidated = false;
        }
        if (!IsValidated) {
            $("#Add_Cat").addClass("disabled")
        } else {
            $("#Add_Cat").removeClass("disabled")
        }
    }
    $("#CloseAdd_Cat").on("click", function () {
        $("#Department_Name").val('');
    })

    function RemoveNewItem(who) {
        var Item = $(who).parents(".dynatree-node"), MyID = $(who).parent().attr("MyID"), Parent_ID = $(who).parent().attr("myparent")

        var ADDDIV = '<div id="RemoveAddedItemDiv">' +
                '    <div class="row">' +
                '        <div class="col-sm-6">' + sure_to_delete +
                '        </div>' +
                '    </div>' +
                '    <div class="row" style="padding:5px 0 0 0;">' +
                '        <div class="col-sm-12" style="text-align:right">' +
                '            <button class="btn btn-xs btn-complete ButtonRemove" MyParent="' + $(who).parent().attr("myparent") + '" MyID="' + MyID + '">' + removeLang + '</button>' +
                '            <button class="btn btn-xs btn-default ButtonCancel">' + CancelLang + '</button>' +
                '        </div>' +
                '    </div>' +
                '</div>'

        if (Item.length > 0) {
            $("#RemoveAddedItemDiv").remove();
            ADDDIV = $(ADDDIV);
            Item.append(ADDDIV)
            ADDDIV.find(".ButtonRemove").on("click", function () {
                MyID = $(this).attr("myid");
                $("#ModalDisabler").show();
                $("#RemoveAddedItemDiv").remove();
                $('#alertnotify').addClass('hidden');
                $.ajax({
                    url: "HRMAjax/fetchBranch?cmd=removeaddeditem",
                    method: "POST",
                    data: {MyID: MyID},
                    dataType: 'html',
//                    complete: function (v1, v2) {
//                        var row = v1.responseJSON[1];
//                        console.log(row)
//                        if (v2 == 'success' && v1.responseJSON[0] == 'ok') {
//
//                            if (row != 0) {
//                                $('div[myid="'+row+'"]').each(function () {
//                                    $(this).find('.AddButton.DeleteItem').addClass('hidden')
//                                });
//
//
//                                $('#alertnotify').attr('class', 'alert alert-danger').empty().html(Failed_Submit)
//                            } else {
//                                $("#RemoveAddedItemDiv").remove();
//                                $("#ModalDisabler").show();
//                                var myActiveNode = $("#tree").dynatree("getActiveNode");
//                                $.ajax({
//                                    url: "HRMAjax/fetchBranch?cmd=removeaddeditem",
//                                    method: "POST",
//                                    data: {MyID: MyID},
                    complete: function (v1, v2) {
                        if (v2 == "success") {
                            if (v1.responseText == 'OK') {
                                var myActiveNode = $("#tree").dynatree("getActiveNode");
                                myActiveNode.remove();
                                $("#alertnotify").removeClass('hidden');
                                $("#alertnotify").addClass("alert alert-success").empty().html(Submitted_Successfully);
                                setTimeout(function() {
                                        $("#alertnotify").addClass("hidden")
                                }, 3000)
                                } else {
                                $('#alertnotify').attr('class', 'alert alert-danger').empty().html(v1.responseText)
                                setTimeout(function() {
                                        $("#alertnotify").addClass("hidden")
                                }, 3000)

                                $('#alertnotify').removeClass('hidden');
                            }
                            $("#ModalDisabler").hide();
                        }
                    }
                });
            });

            ADDDIV.find(".ButtonCancel").on("click", function () {
                $("#RemoveAddedItemDiv").remove()
            });
        }
    }




    function AddNewItem(who) {
        var Item = $(who).parents(".dynatree-node");
        var ADDDIV = '<div id="AddNewItemDiv" class="form-group">' +
                '    <div class="row">' +
                '        <div class="col-sm-6">' +
                 '        <label> <?php print CommonLanguage::getMessage('Name'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '                <input id="AddNewItemEnglishTitle" type="text" placeholder="<?php print CommonLanguage::getMessage('title'); ?>" class="form-control">' +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '    <div class="row">' +
                '        <div class="col-sm-6">' +
                 '        <label><?php print CommonLanguage::getMessage('Phone'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '                <input id="AddNewItemPhone" type="text" placeholder="<?php print CommonLanguage::getMessage('Phone'); ?>" class="form-control">' +
                '            </div>' +
                '        </div>' +
                '        <div class="col-sm-6 ">' +
                 '        <label><?php print CommonLanguage::getMessage('location'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '               <input id="AddNewItemLocation" type="text" placeholder="<?php print CommonLanguage::getMessage('location'); ?>" class="form-control">' +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '    <div class="row" style="padding:5px 0 0 0;">' +
                '        <div class="col-sm-12" style="text-align:right">' +
                '            <button class="btn btn-md btn-info ButtonAdd" MyID="' + $(who).parent().attr("MyID") + '">' + AddLang + '</button>' +
                '            <button class="btn btn-md btn-default ButtonCancel">' + CancelLang + '</button>' +
                '        </div>' +
                '    </div>' +
                '</div>'


        if (Item.length > 0) {
            $("#AddNewItemDiv").remove();
            ADDDIV = $(ADDDIV);
            Item.append(ADDDIV)

            ADDDIV.find(".ButtonAdd").on("click", function () {
                $("#ModalDisabler").hide();
                var V1 = $("#AddNewItemEnglishTitle").removeClass("errorInputText"),
                        V2 = $("#AddNewItemPrefix").removeClass("errorInputText"),
                        MyID = $(this).attr("MyID");
                if ($.trim(V1.val()) == "" /*|| $.trim(V2.val()) == ""*/) {
                    $("#AddNewItemEnglishTitle").addClass("errorInputText");
                    $("#alertnotify").addClass("alert-danger").empty().html(Required_Fields);
                    return;
                }

                $("#ModalDisabler").show();
                $.ajax({
                    url: "HRMAjax/fetchBranch?cmd=addnewitem",
                    method: "POST",
                    encoding: "UTF-8",
                    data: {Department_Name: $.trim(V1.val()),department_phone:$("#AddNewItemPhone").val(),department_location:$("#AddNewItemLocation").val(), Parent_ID: MyID},
                    complete: function (v, v2) {


                        $("#AddNewItemDiv").remove();
                        ExpandAfterReloading = MyID;
                        var Completed = function () {
                            if (ExpandAfterReloading != "") {
                                PageTreeObject.getNodeByKey(ExpandAfterReloading).expand()
                                ExpandAfterReloading = "";
                                $("#ModalDisabler").hide();
                            }
                        }

                        PageTreeObject.getNodeByKey(ExpandAfterReloading).appendAjax({
                            url: "HRMAjax/fetchBranch?cmd=fetch&id=" + MyID,
                            debugLazyDelay: 750,
                            success: function () {
                                Completed();
                            },
                            error: function () {
                                Completed();
                            }
                        });
                        $("#alertnotify").removeClass('hidden');
                        $("#alertnotify").addClass("alert alert-success").empty().html(Submitted_Successfully);

                        setTimeout(function() {
                                $("#alertnotify").addClass("hidden")
                            }, 3000)
                    }
                })

            })
            ADDDIV.find(".ButtonCancel").on("click", function () {
                $("#AddNewItemDiv").remove()
            })
        }
    }

    function EditItem(who) {
        var Item = $(who).parents(".dynatree-node"), MyID = $(who).parent().attr("MyID"), MyCode = $(who).parent().attr("mycode"),
                Department_Name = $(who).parent().attr('department_name'),
                department_phone = $(who).parent().attr('department_phone'), 
                department_location = $(who).parent().attr('department_location'), /*Department_Prefix = $(who).parent().attr('department_prefix'),*/ MyParent = $(who).parent().attr("myparent");
        var ADDDIV = '<div id="EditItemDiv" class="form-group">' +
                '    <div class="row">' +
                '        <div class="col-sm-6">' +
                '        <label><?php print CommonLanguage::getMessage('Name'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '                <input id="EditItemEnglishTitle" type="text" placeholder="Title" value="' + Department_Name + '" class="form-control">' +
                '            </div>' +
                '        </div>' +
       
                '    </div>' +
                '    <div class="row">' +
                '        <div class="col-sm-6">' +
                '        <label><?php print CommonLanguage::getMessage('Phone'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '                <input id="EditItemPhone" type="text" placeholder="Phone" value="' + department_phone + '" class="form-control">' +
                '            </div>' +
                '        </div>' +
                '        <div class="col-sm-6">' +
                '        <label><?php print CommonLanguage::getMessage('Location'); ?></label>' +
                '            <div class="input-group transparent">' +
                '                <span class="input-group-addon">' +
                '                    <i class="fa fa-align-left"></i>' +
                '                </span>' +
                '                <input id="EditItemLocation" type="text" placeholder="Location" value="' + department_location + '" class="form-control">' +
                '            </div>' +
                '        </div>' +
       
                '    </div>' +
                '    <div class="row" style="padding:5px 0 0 0;">' +
                '        <div class="col-sm-12" style="text-align:right">' +
                '            <button class="btn btn-md btn-info ButtonEdit" MyParent="' + $(who).parent().attr("myparent") + '" MyID="' + MyID + '">' + EditLang + '</button>' +
                '            <button class="btn btn-md btn-default ButtonCancel">' + CancelLang + '</button>' +
                '        </div>' +
                '    </div>' +
                '</div>'

        if (Item.length > 0) {
            $("#EditItemDiv").remove();

            ADDDIV = $(ADDDIV);

            Item.append(ADDDIV)

            ADDDIV.find(".ButtonEdit").on("click", function () {
                $("#ModalDisabler").hide();
                var Data = {
                    Department_Name: $("#EditItemEnglishTitle").val(),
                    department_phone: $("#EditItemPhone").val(),
                    department_location: $("#EditItemLocation").val(),
//                    Department_Prefix: $("#EditItemPrefixTitle").val(),
                    Department_ID: MyID,
                }
                if ($("#EditItemEnglishTitle").val() == "" || $("#EditItemPrefixTitle").val() == '') {
                    $("#alertnotify").addClass("alert-danger").empty().html(Required_Fields);
                    return
                }
                $("#ModalDisabler").show();
                var thisActiveNode = $("#tree").dynatree("getActiveNode").getParent()
                //                console.log(thisActiveNode)
                //                return
                $.ajax({
                    url: "HRMAjax/fetchBranch?cmd=edititem",
                    method: "POST",
                    encoding: "UTF-8",
                    data: Data,
                    complete: function (v, v2) {
                        if (v2 == "success" && v.responseText == "OK") {
                            $("#EditItemDiv").remove();
                            ExpandAfterReloading = MyID;
                            var Completed = function () {
                                if (ExpandAfterReloading !== "") {
                                    PageTreeObject.getNodeByKey(thisActiveNode.data.key).expand()
                                    ExpandAfterReloading = "";
                                    $("#ModalDisabler").hide();
                                }
                                $("#alertnotify").removeClass('hidden');
                                $("#alertnotify").addClass("alert alert-success").empty().html(Submitted_Successfully);
                                setTimeout(function() {
                                        $("#alertnotify").addClass("hidden")
                                    }, 3000)
                                                            }
                            PageTreeObject.getNodeByKey(thisActiveNode.data.key).appendAjax({
                                url: "HRMAjax/fetchBranch?cmd=fetch&id=" + MyParent,
                                debugLazyDelay: 750,
                                success: function () {
                                    Completed();
                                },
                                error: function () {
                                    Completed();
                                }
                            });
                            
                        } else {
                            $("#ModalDisabler").hide();
                            $("#alertnotify").removeClass('hidden');

                            $("#alertnotify").addClass("alert-danger").empty().html(duplicate);
                            setTimeout(function() {
                                        $("#alertnotify").addClass("hidden")
                                    }, 3000)   
                        }
                    }
                })
            })
            ADDDIV.find(".ButtonCancel").on("click", function () {
                $("#EditItemDiv").remove()
            })
        }
    }


    function updateParentForDepartment(department_id, parent_id) {
        var Data = {
            parent_id: parent_id,
            department_id: department_id
        }
        $("#ModalDisabler").show();

        $.ajax({
            url: "HRMAjax/fetchBranch?cmd=update",
            data: Data,
            method: "POST",
            complete: function (jqXHR, status) {
                if (status == 'success') {
                    if (jqXHR.responseText == "OK") {


                        $('#alertnotify').attr('class', 'alert alert-success').empty().html(Submitted_Successfully)

                    } else {
                        $('#alertnotify').attr('class', 'alert alert-danger').empty().html(Failed_Submit)
                    }
                }

                $("#ModalDisabler").hide();
            }
        })


    }

</script>