// JavaScript Document

function getdeptbefor()
{

    var employeeId = document.getElementById("employeeId").value;
    var type = document.getElementById("type").value;
    //alert(type);
    if (type == '6' || type == '8')
    {
        //alert('employeePersonalajex.php?do=getdeptbefor&id='+employeeId);
        //get store value
        $.ajax({
            url: 'employeePersonalajex.php?do=getdeptbefor&id=' + employeeId,
            success: function (x) {


                //alert(x);
                //block
                //document.getElementById("productbefore").value=x;
                //document.getElementById("productafter").value=x;

                //checkproductquantity();
                document.getElementById("mydiv").style.display = "block";
                document.getElementById("mydiv2").style.display = "none";
                document.getElementById("deptbefor").value = x;
                if (x == 0)
                {
                    //document.getElementById("submitbtn").style.display = "none";
//                    $('.submitbtn').hide();

                } else
                {
                    //document.getElementById("submitbtn").style.display = "block";
                    $('.submitbtn').show();
                }


            }
        });
        document.getElementById("costdiv").style.display = "none";
        $("#costdiv").val(-1);
    } else if (type == '2' || type == '3' || type == '5' || type == '7' || type == '9' || type == '10' || type == '11'|| type == '14')
    {

//        document.getElementById("mydiv").style.display = "none";
//        document.getElementById("deptbefor").value = 0;
        //document.getElementById("submitbtn").style.display = "block";

//        document.getElementById("costdiv").style.display = "block";

        $.ajax({
            url: 'employeePersonalajex.php?do=getNetSalary&id=' + employeeId,
            async: false,
            success: function (data) {
                data = data.split("*");
                document.getElementById("mydiv2").style.display = "block";
                document.getElementById("netSalary").value = data[0];

                document.getElementById("mydiv").style.display = "block";
                document.getElementById("deptbefor").value = data[1];
                $('.submitbtn').show();
            }
        });


    } else if (type == '4')
    {
        document.getElementById("mydiv").style.display = "none";
        document.getElementById("mydiv2").style.display = "none";
        document.getElementById("deptbefor").value = 0;
        //document.getElementById("submitbtn").style.display = "block";
        $('.submitbtn').show();
        document.getElementById("costdiv").style.display = "block";



    } else
    {

        document.getElementById("mydiv").style.display = "none";
        document.getElementById("mydiv2").style.display = "none";
        document.getElementById("deptbefor").value = 0;
        //document.getElementById("submitbtn").style.display = "block"
        $('.submitbtn').show();
        document.getElementById("costdiv").style.display = "none";
        $("#costdiv").val(-1);

    }


    //###############################
    if (type == '10') {
        $("#empValueDiv").hide();
    } else {
        $("#empValueDiv").show();
    }
}

$('.submitbtn').on('click', function (e) {
    if (!$(this).hasClass('processing')) {
        var thisis = $(this);
        thisis.addClass('processing');
        var myForm = document.getElementById("myEmpPersonalForm");
        var SS = Spry.Widget.Form.validate(myForm);
        if (SS == true) {
            //
            if ($('#type').val() == '2' || $('#type').val() == '3' || $('#type').val() == '7' || $('#type').val() == '9' || $('#type').val() == '11'|| $('#type').val() == '14') {//|| $('#type').val() == '6'
                getdeptbefor();//to get new data of salary and debt of employee
                var netSalary = Number($('#netSalary').val());
                var empValue = Number($('[name="empValue"]').first().val());
                if (netSalary < empValue) {
                    if (netSalary > 0) {
                        var msg = "سيتم خصم " + netSalary + " من مرتب هذا الشهر ";
                        msg += "و " + (empValue - netSalary) + " من مرتب الشهر التالى";
                    } else {
                        var msg = "سيتم خصم " + empValue + " من مرتب الشهر التالى ";
                    }
                    msg += "\n متابعة؟";
                    var r = confirm(msg);
                    if (r == true) {//ok
                        //continue
                    } else {//cancel
                        thisis.removeClass('processing');
                        return false;
                    }
                }
            }

            $('#myEmpPersonalForm').append('<input type="hidden" name="print" value="' + thisis.attr('data-print') + '"/>')
            myForm.submit();

            $('.submitbtn').unbind('click');
        } else {
            thisis.removeClass('processing');
        }
    }
});

