
////// Adding new product with modal box /////////////

function showSupplierModalBox(newLinkId)
{
    $('#supplierModal').modal('show');
    emptySupplierModalForm();
}


function insertSupplier()
{
    $.ajax({
        type: 'POST',
        url: 'supplierAjaxController.php?do=add',
        data: $("#supplierInfo").serialize(),
        success: function (data) {
            if (data == '2')
            {
                alert("هذا الإسم موجود من قبل.");
                $("#sup_txtName").val(" ");
            } else
            {
                if (data != "1") //not error
                {
                    //$("#supplierIdDiv").html(data);

                    $('#supplierModal').modal('hide');
                    //empty modal box's inputs' values
                    emptySupplierModalForm();
                    //$('select').select2();

                    var supplierData = JSON.parse(data);
                    $("#supplier").select2("data", {id: supplierData.supplierid, text: supplierData.suppliername});
                    //$addSuppCurrentDept = document.getElementById('addSuppCurrentDept').value;
                    document.getElementById('hidden_debt').value = supplierData.suppliercurrentDebt;
                    document.getElementById('debt').innerHTML = supplierData.suppliercurrentDebt;

                } else
                {
                    alert("يرجى إدخال البيانات بشكل صحيح");
                }
            }
        }
    });
}

function emptySupplierModalForm()
{

    $('#supplierInfo').find(':input').each(function () {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.value = '-1';
    });
}


function closeSupplierForm()
{
    $('#supplierModal').modal('hide');
    emptySupplierModalForm();

}
////////////////// End adding new supplier /////////////////////////////////
