// 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);

            }
        }

    });


    $("#clientid").select2({
        allowClear: true,
        placeholder: "كل العملاء",
        minimumInputLength: document.getElementById("clientsearchvalue").value,
        multiple: false,
        quietMillis: 100,
        ajax: {
            url: "clientControllerAjax.php?do=select2ClientsAjax",
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term, //search term
                    page_limit: 7, // page size
                    //withAddOption: 1,
                    //withNakdi: 1,
                    //jsonRowRet: 1,
                };
            },
            results: function (data) {
                return {results: data};
            }
        },
        formatResult: function (data) {
            if (Number(data.id) == -2) {
                return  '<span class="newoption">' + data.text + '</span >';
            } else {
                return  '<span>' + data.text + '</span >';
            }
        },
        initSelection: function (element, callback) {
            var item = element.val().split(':');
            //  var data = {id: item[0], text: item[1]};
            var data = {id: item[0], text: item[1], productquantity: item[2], price: item[3]};
            callback(data);
        },
        //dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    });

});


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);
        }
    }

}
