function addrow() {
    var stores_json = JSON.parse($('#stores_json').val());
    var storesCount = Number($('#storesCount').val());
    var itr = Number($('#hidden_itr').val());
    itr++;
    var row = '<tr class="product_tr" data-itr="' + itr + '" data-product="">' +
            '                  <td>' +
            '                      <input type="text" name="parcode' + itr + '" id="parcode' + itr + '"  class="form-control" onkeypress="getproductbyParcode(event, this.value, ' + itr + ');" placeholder="الباركود" />' +
            '                  </td>' +
            '                  <td>' +
            '                      <input type="text" readonly name="product' + itr + '" id="product' + itr + '" data-itr="' + itr + '" data-product=""  class="form-control"/>' +
            '                      <input type="hidden" name="productid' + itr + '" id="productid' + itr + '"/>' +
            '                  </td>' +
            '                  <td>' +
            '                      <input type="text" readonly name="pronum' + itr + '" id="pronum' + itr + '"  class="form-control"/>' +
            '                  </td>';
    for (var i = 0; i < stores_json.length; i++) {
        row += '                  <td>' +
                '                      <input type="text" readonly name="storeQty' + itr + '_' + stores_json[i].storeId + '" id="storeQty' + itr + '_' + stores_json[i].storeId + '" class="form-control storeQty' + itr + '" data-store="' + stores_json[i].storeId + '"/>' +
                '                      <input type="hidden" name="detailid' + itr + '_' + stores_json[i].storeId + '" id="detailid' + itr + '_' + stores_json[i].storeId + '6"/>' +
                '                  </td>';
    }
    if (storesCount > 1) {
        row += '                     <td>' +
                '                           <input type="text" readonly name="allStoreQty' + itr + '" id="allStoreQty' + itr + '"  class="form-control allStoreQty"/>' +
                '                       </td>';

        row += '                  <td>' +
                '                           <input type="text" readonly name="diff' + itr + '" id="diff' + itr + '" data-itr="' + itr + '"  class="form-control diff"/>' +
                '                  </td>';
    }
    row += '              </tr>';
    $('#hidden_itr').val(itr);
    $("#inventoryTbody").append(row);
    $("#parcode" + itr).focus();
}
//n is itration
function getproductbyParcode(e, value, n) {
    e.preventDefault();
    e.stopPropagation();

    var code = (e.keyCode ? e.keyCode : e.which);
    //to stop the submit of the form
    if (e.which == 13 && value != "")
    {
        $.ajax({
            async: false,
            url: 'inventoryControllerAjax.php?do=getproductNamebyOnlyParcode&parcode=' + value + '&ids=' + $('#ids').val(), //+ '&billnameid=' + billnameid + '',
            success: function (data) {
                var response = $.parseJSON(data);
                $("#product" + n).val(response.text);
                $("#product" + n).attr("data-product", response.id);
                $("#productid" + n).val(response.id);
                $("tr[data-itr='" + n + "']").first().attr('data-product', response.id);
                //var pronum = Number($("#pronum" + n).val()) + 1;
                $("#pronum" + n).val(1);
                var allStoreQty = 0;
                $('.storeQty' + n).each(function () {
                    var store = $(this).attr('data-store');
                    var storeqtydata = response.quantityInStores[store].split('*');
                    var qty = storeqtydata[0];
                    if (qty == null) {//this catch null and undefined as null=undefined
                        qty = 0;
                    }
                    $('#storeQty' + n + '_' + store).val(qty);
                    $('#detailid' + n + '_' + store).val(storeqtydata[1]);
                    allStoreQty += Number(qty);
                });
                $('#allStoreQty' + n).val(allStoreQty);
                //sum product quantity if repeated
                $(".product_tr").each(function () {
                    var proid = Number($(this).attr('data-product'));
                    if (proid == null) {//this catch null and undefined as null=undefined
                        //do nothing
                    } else {
                        sumProduct(proid, $(this).attr('data-itr'));
                    }
                });
                //clac diff
                var storesCount = Number($('#storesCount').val());
                if (storesCount > 1) {
                    var totoalDiff = 0;
                    $(".diff").each(function () {
                        var itr = $(this).attr('data-itr');
                        var diff = Number($('#allStoreQty' + itr).val()) - Number($('#pronum' + itr).val());
                        totoalDiff += diff;
                        $('#diff' + itr).val(diff);
                        if (diff == 0) {
                            $('#diff' + itr).css('background-color', '#eee');
                        } else if (diff > 0) {
                            $('#diff' + itr).css('background-color', '#ff7f50');
                        } else if (diff < 0) {
                            $('#diff' + itr).css('background-color', '#50b0ff');
                        }
                    });
                    $('#totoalDiff').html(totoalDiff);
                }
                $('.product_tr').css('background-color', 'white');
                $('.product_tr[data-product="' + response.id + '"]').css('background-color', '#ffeb3b');
                addrow();
                //---fatma
                /*var useParcodeBalance = response.useParcodeBalance;
                 var WeightPricePart = response.WeightPricePart;
                 var posType = response.posType;
                 var parcodeBalance_data = useParcodeBalance + "*" + WeightPricePart + "*" + posType;
                 if ($('#parcodeBalance_data_hidden' + n).length > 0)
                 {
                 $('#parcodeBalance_data_hidden' + n).val(parcodeBalance_data);
                 }
                 else {
                 $('#pronum' + n).closest('td').append('<input type="hidden" name="parcodeBalance_data_hidden' + n + '" id="parcodeBalance_data_hidden' + n + '" value="' + parcodeBalance_data + '" />');
                 }*/
                //----
            }
        });
    }
}
function sumProduct(proid, itr) {
    var pronum = 0;
    $("input[id^='product']").each(function () {
        var loopProId = Number($(this).attr('data-product'));
        var loopItr = $(this).attr('data-itr');
        //sum products in same store  or  store == -10 means one store for all bill
        if (loopProId == proid) {
            pronum += Number($('#pronum' + loopItr).val());
            //remove the row
            if (loopItr > itr) {
                $('#product' + loopItr).parent().parent().remove();
            }
        }
    });
    $('#pronum' + itr).val(pronum);//.keyup();
}