$(document).ready(function () {
//    $("#branch").on('change keyup blur', GetPlace);
    $("tbody").on('change', '.finyear', getYearDates);
    $("body").on('change blur', '.user', checkuser);
    $("tbody").on('focus', '.yearname', getYearDates);
    $("tbody").on('click', '.remove', remove);
    $('#addnew').click(function () {
        AddRows();
    });
    function remove() {
        $(this).closest("tr").remove();
    }

    function getYearDates() {
        var tr = $(this).closest("tr"),
                finyear = tr.find(".finyear").val(),
                data = {finyear: finyear};
        $.ajax({
            type: "GET",
            dataType: 'json',
            url: 'getDates.php',
            data: data,
            success: function (response) {
                var m = response;
                tr.find(".startdate").val(m.startdate);
                tr.find(".lastdate").val(m.lastdate);
                tr.find(".yearname").val(m.yearname);
            },
            error: function (request, textStatus, errorThrown) {
            }
        });
    }

    function AddRows() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('user_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }

      function checkuser() {
        var user = $(".user").val() ;
        $.ajax({
            type: "POST",
            datatype: "json",
            data: {user: user},
            url: "checkuser.php",
            success: function (response) {
                var d = response ;
                if (d.s > 0) {
                    alert("This User Name Already Exist");
                    $(".user").val('');
                    $(".user").focus();
                }
            },
            error: function (request, errorThrown, textStatus) {
            }
        });
    } 


//    function GetPlace() {
//        var brch = $('#branch').val();
//        $.post('getplace.php', {branch: brch}, function (data) {
//            $("#place").val(data);
//        });
//    }

    $("#Smasters").click(function () {
        $(".masters").prop('checked', $(this).prop('checked'));
    });
    $("#Stransactions").click(function () {
        $(".transactions").prop('checked', $(this).prop('checked'));
    });
    $("#Sreports").click(function () {
        $(".reports").prop('checked', $(this).prop('checked'));
    });
    $("#Sregisters").click(function () {
        $(".registers").prop('checked', $(this).prop('checked'));
    });
    $("#Sfinalization").click(function () {
        $(".finalization").prop('checked', $(this).prop('checked'));
    });
    $("#Sdashboard").click(function () {
        $(".dashboard").prop('checked', $(this).prop('checked'));
    });
    $("#confpass").blur(function () {
        var pass = $("#password").val();
        var cpass = $("#confpass").val();
        if (pass !== cpass) {
            $("#alert").html("<b style='color:red;'>Password Doesn't Match</b>");
        } else {
            $("#alert").html("<b style='color:green;'>Password Matched</b>");
        }
    });
});