// JavaScript Document

$(document).ready(function (e) {
    $("#clientid").change(function (e) {
        $.ajax({
            url: "clientDeficitControllerAjax.php?id=" + $(this).val(),
            type: "get",
            success: function (data) {
                // alert(data);
                if (data != null)
                {
                    $("#clientdebtchangebefore").val(data);
                }
            },
            error: function () {
                alert("لقد حدث خطأ أثناء إجراء العملية");
            }
        });
    });


    ///////////////////////////////////////////////

    $("#clientdebtchangetype").change(function (e) {
        $("#clientdebtchangeamount").val(0);
    });

    $('#spbutton').live('click', function (e) {
        var myForm = document.getElementById("spform");
        var SS = Spry.Widget.Form.validate(myForm);
        if (SS == true) {
            if (!$('#spbutton').hasClass("processing"))
            {
                $('#spbutton').addClass("processing");
                myForm.submit();
                $('#spbutton').attr("disabled", true);

            }
        }

    });



});


function populateDebt()
{
    var type = $("#clientdebtchangetype").val();
    var payedDept = Number($("#clientdebtchangeamount").val());
    var debtBefore = Number($("#clientdebtchangebefore").val());
    if (type != '' && type != '-1')
    {
        if (type == '1') //بالخصم
        {
            var clientdebtchangeamountafter = (debtBefore - payedDept).toFixed(3);
            $("#clientdebtchangeamountafter").val(clientdebtchangeamountafter);
        } else {
            var clientdebtchangeamountafter = (debtBefore + payedDept).toFixed(3);
            $("#clientdebtchangeamountafter").val(clientdebtchangeamountafter);
        }
    }

}
