var addrowUnit;

var productINfoYes = false;

//the common itration and the start // 2
$aw_k = 2;


$(document).ready(function (e) {



    $('.productCat').live('change', function () {
        var id = $(this).val();
        var level = Number($(this).attr('data-level'));
        level = level + 1;
        $('#level').val(level);
        $.post('salesreportajax.php?do=getsubcat', {id: id, level: level}, function (data) {
            $('.productCat').each(function () {
                var thisLevel = Number($(this).attr('data-level'));
                if (thisLevel >= level) {
                    $(this).parent('div').remove();
                }
            });
            $('#productIdDiv').remove();
            if (data != "-1") {
                $('#manyParents').append(data);
            }
        });

        var last_level = $('#level').val();
        if (typeof last_level !== 'undefined') {

            last_level = last_level - 1;

            var last_productCatId = $('#productCatId' + last_level + '').val();
            $('#productCatId').val(last_productCatId);
            console.log('productCatId = ' + last_productCatId);

            //# fill buypricereal ,  buytotal , buyhalf , buypart
            var productCatId_level = $('#level').val();
            //console.log('level = '+productCatId_level);
            var i, last_level;
            for (i = 1; i <= productCatId_level; i++) {
                if (i == productCatId_level) {
                    last_level = productCatId_level - 1;
                }
            }
            var productCatId = $('#productCatId' + last_level + '').val();

        }
        $.post('productControllerAjax.php?do=get_prices', {productCatId: productCatId}, function (data) {
            data = JSON.parse(data);
            $('.buypricereal_precentage1').val(data.buypricereal);
            $('.buytotal_precentage1').val(data.buytotal);
            $('.buyhafe_precentage1').val(data.buyhalf);
            $('.buypart_precentage1').val(data.buypart);
        });

    });

    //# m7md products form
    $('body').on('keyup', '.buy_price', function () {
        var ths_val = $(this).val();
        var ths_id = $(this).attr('data-id');

        var buypricereal = $('.buypricereal_precentage' + ths_id + '').val();
        var buytotal = $('.buytotal_precentage' + ths_id + '').val();
        var buyhalf = $('.buyhafe_precentage' + ths_id + '').val();
        var buypart = $('.buypart_precentage' + ths_id + '').val();


        var b_pricereal = (((Number(buypricereal) * Number(ths_val)) / 100) - Number(ths_val)) * -1;
        var b_total = ((Number(buytotal) * Number(ths_val)) / 100) + Number(ths_val);
        var b_hafe = ((Number(buyhalf) * Number(ths_val)) / 100) + Number(ths_val);
        var b_part = ((Number(buypart) * Number(ths_val)) / 100) + Number(ths_val);

        $('.buy_pricereal' + ths_id + '').val(b_pricereal);
        $('.buy_total' + ths_id + '').val(b_total);
        $('.buy_hafe' + ths_id + '').val(b_hafe);
        $('.buy_part' + ths_id + '').val(b_part);

        /*    var productCatId_level = $('#level').val();
         console.log('level = '+productCatId_level);
         var i , last_level;
         for( i = 1 ;  i <= productCatId_level ;  i++){
         if( i == productCatId_level ){
         last_level =  productCatId_level - 1;
         }
         }
         var productCatId = $('#productCatId'+last_level+'').val();
         console.log('productCatId = '+productCatId);
         //# get buytotal , buyhalf , buypart
         $.post('productControllerAjax.php?do=get_prices',{ productCatId:productCatId },function(data){
         data = JSON.parse(data);

         var b_pricereal = ( ( Number(data.buypricereal) * Number(ths_val) ) /100) + Number(ths_val) ;
         var b_total = ( ( Number(data.buytotal) * Number(ths_val) ) /100) + Number(ths_val) ;
         var b_hafe  = ( ( Number(data.buyhalf)* Number(ths_val) )  /100) + Number(ths_val) ;
         var b_part  = ( ( Number(data.buypart) * Number(ths_val) ) /100) + Number(ths_val) ;
         // console.log('b_total = '+b_total);
         $('.buy_pricereal'+ths_id+'').val(b_pricereal);
         $('.buy_total'+ths_id+'').val(b_total);
         $('.buy_hafe'+ths_id+'').val(b_hafe);
         $('.buy_part'+ths_id+'').val(b_part);
         });*/



    });


    $('.awdatepicker').datepicker();



    $("#addrawUnit").click(function () {


        addrowUnit($aw_k);


        // fill the iltration filed with common iltration
        document.getElementById("unitsItr").value = $aw_k;

        // incrementaion of row number and tab index
        $aw_k++


        //reload the dropdownlist and jquery files
        $('select').select2();


        return false;

    });







    addrowUnit = function ($aw_l)
    {
        $("#containerUnit").append('<tr>'

                + ' <td> <select name="newunitid' + $aw_l + '" id="newunitid' + $aw_l + '"  class="select2 wdt100">'
                + $('#addNewUnit').html()
                + '</select></td>'


                + ' <td><input type="text" name="newproductnumber' + $aw_l + '" id="newproductnumber' + $aw_l + '"  class="form-control txt-nonraduis numberinput" value="1" /></td>'
//                + ' <td><input type="text" name="proUnitParcode' + $aw_l + '" id="proUnitParcode' + $aw_l + '"  class="form-control txt-nonraduis numberinput positiveNo" onblur="checkUnitbarcode(this.value, ' + $aw_l + ');" value="" /></td>'
//                + ' <td><input type="text" name="proUnitSellAllPrice' + $aw_l + '" id="proUnitSellAllPrice' + $aw_l + '"  class="form-control txt-nonraduis numberinput positivefloat" value="" /></td>'
//                + ' <td><input type="text" name="proUnitSellHalfPrice' + $aw_l + '" id="proUnitSellHalfPrice' + $aw_l + '"  class="form-control txt-nonraduis numberinput positivefloat" value="" /></td>'
//                + ' <td><input type="text" name="proUnitSellUnitPrice' + $aw_l + '" id="proUnitSellUnitPrice' + $aw_l + '"  class="form-control txt-nonraduis numberinput positivefloat" value="" /></td>'



                + '<td>'
                + '<button id="removeUnit' + $aw_l + '" type="button" class="close flt-none removeicon removeUnit" aria-hidden="true">&times;</button>'
                + '</td>'

                + '</tr>');


        $(".numberinput").forceNumeric();

        $('select').select2();


    }
    /**************************/



    // the delete action that delete row of products
    /************************/
    $(document).on('click', '.removeUnit', function () {

        // delete by partents
        $(this).parent().parent().remove();


        return false;
    });
    /************************/











    $('.savemodel3').on('click', function () {


        $('#productModal').modal('hide');

        emptyProductModalForm();
    });




});


$('[id^="proUnitParcode"]').live('keypress', function (e) {
    if (e.which == 13) { //Enter
        e.preventDefault();
        //get pro itr//unitsTable1
        var proItr = $(this).parents('table').first().attr('id').substring(10);
        var val = $(this).val();
        if (val != "") {
            addUnits(proItr);
        }
        //unitsItr3

        return false
    }
})


function showProductModal($rawItr, $rawTabINdex)
{
    $('.awdatepicker').datepicker();

    //generate parcode
    generateParcode();

    $('#productModal').modal('show');



    $("#newproductItr").val($rawItr);
    $("#newproductTabIndex").val($rawTabINdex);

    $('.awdatepicker').datepicker();


}

function generateParcode()
{
    $.ajax({
        url: 'productControllerAjax.php?do=generate',
        type: "get",
        success: function (data) {
            document.getElementById("parcode").value = data;
        }
    });
}

function emptyProductModalForm()
{

    var unitsItr = document.getElementById("unitsItr").value;
    var c = 2;
    for (c = 2; c <= unitsItr; c++)
    {

        if ($("#newproductnumber" + c).length == 0)
        {

            continue;
        } else
        {

            $("#removeUnit" + c).parent().parent().remove();
        }
    }

    $('#productInfo').find(':input').each(function () {
        var type = this.type, tag = this.tagName.toLowerCase();
        if ((type == 'text' || type == 'password' || tag == 'textarea') && !this.id.startsWith("newproductnumber"))
            this.value = '';
//        else if (type == 'checkbox' || type == 'radio')
//            this.checked = false;
        else if (tag == 'select' && !this.id.startsWith("newunitid"))
            this.value = '-1';
    });


    if ($("#newproductnumber1").length == 0)
    {

    } else
    {

        $("#newunitid1").val('-1');
        $('select').select2();


    }




}



function populateSellPrice()
{
    $("#productSellHalfPrice").val($("#productSellAllPrice").val());
    $("#productSellUnitPrice").val($("#productSellAllPrice").val());
}




function insertProduct(e)
{
    var newproductItr = $("#newproductItr").val();
    var newproductTabIndex = $("#newproductTabIndex").val();

    var pricetype = document.getElementById("pricetype").value;
    var supplier = document.getElementById("supplier").value;
    //alert("pricetype: "+pricetype+"  supplier: "+supplier);

    $previousPriceStatus = $('#previousPriceStatus').val();
    if ($previousPriceStatus == 1)//don't show previous product price
    {
        supplier = "";
    }

    var billnameId = document.getElementById("billnameid").value;

    if (!productINfoYes)
    {
        e.preventDefault();

        $.ajax({
            type: 'POST',
            url: 'sellbillAjaxController.php?do=addNewproduct&billnameid=' + billnameId + '&newproductItr=' + newproductItr + '&newproductTabIndex=' + newproductTabIndex + '',
            data: $("#productInfo").serialize(),
            success: function (data) {

                if (data == '0')
                {
                    $("#productName").val("");
                    $("#productnameError").css("display", "block");
                } else
                {

                    $("#productraw" + newproductItr).html(data);

                    var productid = $("#newproductid" + newproductItr).val();

                    formatResult = function (data)
                    {
                        return  data.text;
                    }

                    //assign select2 properties
                    $(".select2product" + newproductItr).select2({
                        placeholder: "إسم المنتج",
                        minimumInputLength: document.getElementById("searchvalue").value,
                        //  minimumInputLength: 3,
                        multiple: false,
                        quietMillis: 100,
                        ajax: {
                            url: "sellbillAjaxController.php?do=getProducts",
                            dataType: 'json',
                            data: function (term, page) {
                                return {
                                    term: term, //search term
                                    page_limit: 10, // page size
                                    storeid: document.getElementById("storeid").value,
                                    hiddenpricetype: document.getElementById("pricetype").value,
                                    searchWithCatAndProductNameOnly: document.getElementById("searchWithCatAndProductNameOnly").value
                                };
                            },
                            results: function (data) {
                                //alert("data: "+data);
                                return {results: data};
                            }
                        },
                        formatResult: formatResult,
                        initSelection: function (element, callback) {
                            var item = element.val().split(':');
                            var data = {id: item[0], text: item[1]};
                            callback(data);
                        }
                    }).on('select2-open', function () {
                        $('#select2-drop').addClass('largeWidthForDropDown');
                    }).on('select2-close', function () {
                        $('#select2-drop').removeClass('largeWidthForDropDown');
                    });


                    $.ajax({
                        url: 'sellbillAjaxController.php?do=getproductNamebyOnlyId&productid=' + productid + '',
                        success: function (data) {

                            var response = $.parseJSON(data);

                            $("#product" + newproductItr).select2('val', ['' + response.id + ':' + response.text + ''], true);

                            if (productid != "" && productid != "null" && productid != null && productid != "-1")
                            {
                                //using the load method to load the price
                                //$("#productunitdiv" + newproductItr).html('<img src="../views/default/images/loader/6.gif" alt="">');

                                //////////////////////////////////////////////////
                                $.ajax({
                                    url: '' + productpricepagename + '?do=getunit&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + productid + '&pricetype=' + pricetype + '&clientid=' + supplier + '',
                                    type: "get",
                                    success: function (data) {
                                        var response = $.parseJSON(data);
                                        //populate unit
                                        $('#productunitdiv' + newproductItr).html(response.unitString);
                                        $("#addunit").empty();

                                        $("#productunitdiv" + newproductItr + " option").clone().appendTo("#addunit");

                                        //populate buy price
                                        //alert(response.priceString);
                                        $('#propricechange' + newproductItr).html(response.priceString);


                                        var productpricetype = document.getElementById("productpricetype" + newproductItr).value;

                                        if (productpricetype == 1)
                                        {
                                            $('#proprice' + newproductItr).css("background-color", "#B7DFA4");
                                            $('#propricechange' + newproductItr).css("background-color", "#B7DFA4");
                                        } else
                                        {
                                            $('#proprice' + newproductItr).css("background-color", "#FFF");
                                            $('#propricechange' + newproductItr).css("background-color", "#FFF");
                                        }


                                        $('#productparcode' + newproductItr).html(response.parcodeString);
                                        //alert(response.priceString)



                                        var parcodeType = document.getElementById("parcodeType" + newproductItr).value;


                                        if (parcodeType == "serialnumber")
                                        {
                                            $('#pronum' + newproductItr).val(1);
                                            $('#pronum' + newproductItr).attr('readonly', true);
                                        } else
                                        {
                                            $('#pronum' + newproductItr).attr('readonly', false);
                                        }
                                        //here all code that will happend with loading contants
                                        //the number of rows of products
                                        var noitr = document.getElementById("hidden_itr").value;


                                        // document.getElementById("proprice" + newproductItr).value = 0;
                                        document.getElementById("pronum" + newproductItr).value = 1;
                                        document.getElementById("prodis" + newproductItr).value = 0;


                                        //to calculate the price and amount with discount
                                        calcuproduct(newproductItr);



                                        //get the total price of sell
                                        selltotalprice(noitr);

                                        // get the total price of sell and return sell
                                        totalprice();

                                        //get the general discount
                                        thegeneraldiscount();

                                        checkthepayed();

                                        //get the general payed
                                        thegeneralpayed();

                                        // calculate the rest of debt
                                        therestofdebt();


                                        //daiable price  type
                                        //$('#pricetype').attr('disabled', true);
                                        //$("#pricetype").trigger('liszt:updated');

                                        document.getElementById("hiddenpricetype").value = document.getElementById("pricetype").value;


                                        var storeid = $("#storeid").val();


                                        $.ajax({
                                            type: 'POST',
                                            url: '' + productpricepagename + '?do=getProductQuantity&productid=' + productid + '&storeid=' + storeid,
                                            success: function (data)
                                            {


                                                if (data <= 0)
                                                {
                                                    $("#errorquantity").html(data);

                                                    $('#storemodel').modal('show');

                                                }

                                            }
                                        });

                                        $('select').select2();


                                        var settingQuantitiyFocus = document.getElementById("settingQuantitiyFocus").value;

                                        if ((Number($aw_i) - Number(newproductItr)) == 1)
                                        {

                                            $("#addunit").empty();

                                            addrow($aw_i, $tabindexnum);



                                            if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
                                            {
                                                $("#pronum" + newproductItr).focus();
                                                $("#pronum" + newproductItr).select();
                                            } else
                                            {
                                                $("#parcode" + $aw_i).focus();
                                                $("#parcode" + $aw_i).select();
                                            }


                                            // fill the iltration filed with common iltration
                                            document.getElementById("hidden_itr").value = $aw_i;

                                            // incrementaion of row number and tab index
                                            $aw_i++
                                            $tabindexnum++;

                                        } else

                                        {

                                            if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
                                            {
                                                $("#pronum" + newproductItr).focus();
                                                $("#pronum" + newproductItr).select();
                                            } else
                                            {
                                                $("#parcode" + $aw_i).focus();
                                                $("#parcode" + $aw_i).select();
                                            }
                                        }


                                        //reload the dropdownlist and jquery files
                                        $('select').select2();

                                        $(".numberinput").forceNumeric();


                                    },
                                    error: function () {
                                        alert("لقد حدث خطأ أثناء إجراء العملية");
                                    }
                                });
                                //////////////////////////////////////////////////

                                //using the load method to load the product Unit
                                //    $('#productunitdiv' + newproductItr).load('' + productpricepagename + '?do=getunit&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + productid + '', function() {

                                //using the load method to load the parcode
                                //  $('#productparcode' + newproductItr).load('' + productpricepagename + '?do=getparcode&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + productid + '', function() {//
//
//
//                                        var parcodeType = document.getElementById("parcodeType" + newproductItr).value;
//
//
//                                        if (parcodeType == "serialnumber")
//                                        {
//                                            $('#pronum' + newproductItr).val(1);
//                                            $('#pronum' + newproductItr).attr('readonly', true);
//                                        }
//                                        else
//                                        {
//                                            $('#pronum' + newproductItr).attr('readonly', false);
//                                        }
//
//
//
//
//                                        //here all code that will happend with loading contants
//                                        //the number of rows of products
//                                        var noitr = document.getElementById("hidden_itr").value;
//
//
//                                        document.getElementById("proprice" + newproductItr).value = 0;
//                                        document.getElementById("pronum" + newproductItr).value = 1;
//                                        document.getElementById("prodis" + newproductItr).value = 0;
//
//
//                                        //to calculate the price and amount with discount
//                                        calcuproduct(newproductItr);
//
//
//
//                                        //get the total price of sell
//                                        selltotalprice(noitr);
//
//                                        // get the total price of sell and return sell
//                                        totalprice();
//
//                                        //get the general discount
//                                        thegeneraldiscount();
//
//                                        checkthepayed();
//
//                                        //get the general payed
//                                        thegeneralpayed();
//
//                                        // calculate the rest of debt
//                                        therestofdebt();
//
//
//                                        //daiable price  type
//                                        //$('#pricetype').attr('disabled', true);
//                                        //$("#pricetype").trigger('liszt:updated');
//
//                                        document.getElementById("hiddenpricetype").value = document.getElementById("pricetype").value;
//
//
//
//
//
//                                        var storeid = $("#storeid").val();
//
//
//                                        $.ajax({
//                                            type: 'POST',
//                                            url: '' + productpricepagename + '?do=getProductQuantity&productid=' + productid + '&storeid=' + storeid,
//                                            success: function(data)
//                                            {
//
//
//                                                if (data <= 0)
//                                                {
//                                                    $("#errorquantity").html(data);
//
//                                                    $('#storemodel').modal('show');
//
//                                                }
//
//                                            }
//                                        });
//
//
//
//
//
//                                        $('select').select2();
//                                        //load the jquery checker for number only
//                                        // $.getScript('../views/default/js/head_scripts.js');
//
//
//                                        getunitprice(newproductItr);
//
//
//                                        var settingQuantitiyFocus = document.getElementById("settingQuantitiyFocus").value;
//
//                                        if ((Number($aw_i) - Number(newproductItr)) == 1)
//                                        {
//
//                                            $("#addunit").empty();
//
//                                            addrow($aw_i, $tabindexnum);
//
//
//
//                                            if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
//                                            {
//                                                $("#pronum" + newproductItr).focus();
//                                                $("#pronum" + newproductItr).select();
//                                            }
//                                            else
//                                            {
//                                                $("#parcode" + $aw_i).focus();
//                                                $("#parcode" + $aw_i).select();
//                                            }
//
//
//                                            // fill the iltration filed with common iltration
//                                            document.getElementById("hidden_itr").value = $aw_i;
//
//                                            // incrementaion of row number and tab index
//                                            $aw_i++
//                                            $tabindexnum++;
//
//                                        }
//
//                                        else
//
//                                        {
//
//                                            if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
//                                            {
//                                                $("#pronum" + newproductItr).focus();
//                                                $("#pronum" + newproductItr).select();
//                                            }
//                                            else
//                                            {
//                                                $("#parcode" + $aw_i).focus();
//                                                $("#parcode" + $aw_i).select();
//                                            }
//                                        }
//
//
//
//
//
//
//
//                                        //reload the dropdownlist and jquery files
//                                        $('select').select2();
//
//                                        $(".numberinput").forceNumeric();
//                                    });


                                //  });

                            }
                        }
                    });




                    //action in change of created dropdown list  to load the price
                    $("#product" + newproductItr).change(function () {



                        //here  will get the price for every product
                        //the selected value in drop down list
                        var theselectedvalue = $(this).val();


                        if (theselectedvalue != "" && theselectedvalue != "null" && theselectedvalue != null && theselectedvalue != "-1")
                        {
                            checkForRepeatedProducts(newproductItr, 0);
                            //var storeid = document.getElementById("storeid").value;

                            //using the load method to load the price
                            //$("#productunitdiv" + newproductItr).html('<img src="../views/default/images/loader/6.gif" alt="">');

                            $("#addunit").empty();
                            //////////////////////////////////////////////////
                            $.ajax({
                                url: '' + productpricepagename + '?do=getunit&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + theselectedvalue + '&pricetype=' + pricetype + '&clientid=' + supplier + '',
                                type: "get",
                                success: function (data) {
                                    var response = $.parseJSON(data);
                                    //populate unit
                                    $('#productunitdiv' + newproductItr).html(response.unitString);
                                    //  $("#addunit").empty();

                                    $("#productunitdiv" + newproductItr + " option").clone().appendTo("#addunit");

                                    //populate buy price
                                    //alert(response.priceString);
                                    $('#propricechange' + newproductItr).html(response.priceString);


                                    var productpricetype = document.getElementById("productpricetype" + newproductItr).value;

                                    if (productpricetype == 1)
                                    {
                                        $('#proprice' + newproductItr).css("background-color", "#B7DFA4");
                                        $('#propricechange' + newproductItr).css("background-color", "#B7DFA4");
                                    } else
                                    {
                                        $('#proprice' + newproductItr).css("background-color", "#FFF");
                                        $('#propricechange' + newproductItr).css("background-color", "#FFF");
                                    }


                                    $('#productparcode' + newproductItr).html(response.parcodeString);

                                    var parcodeType = document.getElementById("parcodeType" + newproductItr).value;


                                    if (parcodeType == "serialnumber")
                                    {
                                        $('#pronum' + newproductItr).val(1);
                                        $('#pronum' + newproductItr).attr('readonly', true);
                                    } else
                                    {
                                        $('#pronum' + newproductItr).attr('readonly', false);
                                    }
                                    //here all code that will happend with loading contants
                                    //the number of rows of products
                                    var noitr = document.getElementById("hidden_itr").value;


                                    // document.getElementById("proprice" + newproductItr).value = 0;
                                    document.getElementById("pronum" + newproductItr).value = 1;
                                    document.getElementById("prodis" + newproductItr).value = 0;


                                    //to calculate the price and amount with discount
                                    calcuproduct(newproductItr);



                                    //get the total price of sell
                                    selltotalprice(noitr);

                                    // get the total price of sell and return sell
                                    totalprice();

                                    //get the general discount
                                    thegeneraldiscount();

                                    checkthepayed();

                                    //get the general payed
                                    thegeneralpayed();

                                    // calculate the rest of debt
                                    therestofdebt();


                                    //daiable price  type
                                    //$('#pricetype').attr('disabled', true);
                                    //$("#pricetype").trigger('liszt:updated');

                                    document.getElementById("hiddenpricetype").value = document.getElementById("pricetype").value;


                                    var storeid = $("#storeid").val();


                                    $.ajax({
                                        type: 'POST',
                                        url: '' + productpricepagename + '?do=getProductQuantity&productid=' + productid + '&storeid=' + storeid,
                                        success: function (data)
                                        {


                                            if (data <= 0)
                                            {
                                                $("#errorquantity").html(data);

                                                $('#storemodel').modal('show');

                                            }

                                        }
                                    });

                                    $('select').select2();
                                    //load the jquery checker for number only
                                    // $.getScript('../views/default/js/head_scripts.js');


                                    var settingQuantitiyFocus = document.getElementById("settingQuantitiyFocus").value;

                                    if ((Number($aw_i) - Number(newproductItr)) == 1)
                                    {

                                        $("#addunit").empty();

                                        addrow($aw_i, $tabindexnum);



                                        if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
                                        {
                                            $("#pronum" + newproductItr).focus();
                                            $("#pronum" + newproductItr).select();
                                        } else
                                        {
                                            $("#parcode" + $aw_i).focus();
                                            $("#parcode" + $aw_i).select();
                                        }


                                        // fill the iltration filed with common iltration
                                        document.getElementById("hidden_itr").value = $aw_i;

                                        // incrementaion of row number and tab index
                                        $aw_i++
                                        $tabindexnum++;

                                    } else

                                    {

                                        if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
                                        {
                                            $("#pronum" + newproductItr).focus();
                                            $("#pronum" + newproductItr).select();
                                        } else
                                        {
                                            $("#parcode" + $aw_i).focus();
                                            $("#parcode" + $aw_i).select();
                                        }
                                    }


                                    //reload the dropdownlist and jquery files
                                    $('select').select2();

                                    $(".numberinput").forceNumeric();


                                },
                                error: function () {
                                    alert("لقد حدث خطأ أثناء إجراء العملية");
                                }
                            });
                            //////////////////////////////////////////////////

                            //using the load method to load the product Unit
                            // $('#productunitdiv' + newproductItr).load('' + productpricepagename + '?do=getunit&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + theselectedvalue + '', function() {//
//
//                                //using the load method to load the parcode
//                                $('#productparcode' + newproductItr).load('' + productpricepagename + '?do=getparcode&itrs=' + newproductItr + '&tabindex=' + newproductTabIndex + '&productid=' + theselectedvalue + '', function() {
//
//
//                                    var parcodeType = document.getElementById("parcodeType" + newproductItr).value;
//
//                                    if (parcodeType == "serialnumber")
//                                    {
//                                        $('#pronum' + newproductItr).val(1);
//                                        $('#pronum' + newproductItr).attr('readonly', true);
//                                    }
//                                    else
//                                    {
//                                        $('#pronum' + newproductItr).attr('readonly', false);
//                                    }
//
//
//                                    //here all code that will happend with loading contants
//                                    //the number of rows of products
//                                    var noitr = document.getElementById("hidden_itr").value;
//
//
//                                    document.getElementById("proprice" + newproductItr).value = 0;
//                                    document.getElementById("pronum" + newproductItr).value = 1;
//                                    document.getElementById("prodis" + newproductItr).value = 0;
//
//
//                                    //to calculate the price and amount with discount
//                                    calcuproduct(newproductItr);
//
//                                    //get the total price of sell
//                                    selltotalprice(noitr);
//
//                                    // get the total price of sell and return sell
//                                    totalprice();
//
//                                    //get the general discount
//                                    thegeneraldiscount();
//
//                                    checkthepayed();
//
//                                    //get the general payed
//                                    thegeneralpayed();
//
//                                    // calculate the rest of debt
//                                    therestofdebt();
//
//
//                                    //daiable price  type
//                                    //$('#pricetype').attr('disabled', true);
//                                    //$("#pricetype").trigger('liszt:updated');
//
//                                    document.getElementById("hiddenpricetype").value = document.getElementById("pricetype").value;
//
//
//
//                                    getunitprice(newproductItr);
//
//
//                                    var settingQuantitiyFocus = document.getElementById("settingQuantitiyFocus").value;
//
//                                    if ((Number($aw_i) - Number(newproductItr)) == 1)
//                                    {
//
//                                        $("#addunit").empty();
//
//                                        addrow($aw_i, $tabindexnum);
//
//
//
//
//                                        if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
//                                        {
//                                            $("#pronum" + newproductItr).focus();
//                                            $("#pronum" + newproductItr).select();
//                                        }
//                                        else
//                                        {
//                                            $("#parcode" + $aw_i).focus();
//                                            $("#parcode" + $aw_i).select();
//                                        }
//
//
//                                        // fill the iltration filed with common iltration
//                                        document.getElementById("hidden_itr").value = $aw_i;
//
//
//                                        // incrementaion of row number and tab index
//                                        $aw_i++
//                                        $tabindexnum++;
//
//                                    }
//
//                                    else
//                                    {
//
//                                        if (settingQuantitiyFocus == 0 && parcodeType != "serialnumber")
//                                        {
//                                            $("#pronum" + newproductItr).focus();
//                                            $("#pronum" + newproductItr).select();
//
//                                        }
//                                        else
//                                        {
//                                            $("#parcode" + $aw_i).focus();
//                                            $("#parcode" + $aw_i).select();
//                                        }
//                                    }
//
//
//
//                                    var storeid = $("#storeid").val();
//
//
//                                    $.ajax({
//                                        type: 'POST',
//                                        url: '' + productpricepagename + '?do=getProductQuantity&productid=' + theselectedvalue + '&storeid=' + storeid,
//                                        success: function(data)
//                                        {
//
//
//                                            if (data <= 0)
//                                            {
//                                                $("#errorquantity").html(data);
//
//                                                $('#storemodel').modal('show');
//
//                                            }
//
//                                        }
//                                    });
//
//
//
//
//                                    $('select').select2();
//
//
//
//                                    //load the jquery checker for number only
//                                    // $.getScript('../views/default/js/head_scripts.js');
//
//
//
//
//                                });
//                            });

                        }
                    });




                    $('#productModal').modal('hide');

                    //empty modal box's inputs' value
                    emptyProductModalForm();






                }
            }
        });





    } else
    {





    }



}
