$(document).ready(function () {
//    $("#branch").on('change keyup blur', GetPlace);
    $("tbody").on('change', '.finyear', getYearDates);
    $("tbody").on('focus', '.yearname', getYearDates);
    $("body").on('change blur', '.user', checkuser);
    $("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(),
                cono = tr.find(".cono").val(),
                data = {finyear: finyear, cono: cono};
        $.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, textStatus, errorThrown){}
        });
    }

    $("#Sentries").click(function () {
        $(".entries").prop('checked', $(this).prop('checked'));
    });
    $("#Sreports").click(function () {
        $(".reports").prop('checked', $(this).prop('checked'));
    });
    $("#Ssearchvisa").click(function () {
        $(".search").prop('checked', $(this).prop('checked'));
    });

    $("#confpass").blur(function () {
        var pass = $("#pass").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>");
        }
    });
});