// // JavaScript Document
// forceNumeric() plug-in implementation
var formatResult;
jQuery.fn.forcepositiveNum = function () {

    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.which || e.keyCode;
            //alert(key);
            if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                    // numbers
                    key >= 48 && key <= 57 ||
                    // Numeric keypad
                    key >= 96 && key <= 105 ||
                    // comma, period and minus, . on keypad
                    //key == 190 || key == 188 || key == 109 || key == 110 ||
                    // Backspace and Tab and Enter
                    key == 8 || key == 9 || key == 13 ||
                    // Home and End
                    key == 35 || key == 36 ||
                    // left and right arrows
                    key == 37 || key == 39 ||
                    // Del and Ins
                    key == 46 || key == 45)
                return true;

            return false;
        });
    });
}
$(document).keypress(function (e) {
    if (e.which == 13) {
        $('#DE_add').click();
    }
});

//dropdown-toggle
$(document).ready(function (e) {
    //alert(document.getElementById("searchvalue").value);
    formatResult = function (data) {
        return  data.text;
    }

    /*---add--------------------------------------------------------------------------------------------------------------------------*/
    $(".positiveNo").forcepositiveNum();
    $('.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);
            }
            else
            {
                $('#level').val((level - 1));
                $.post('salesreportajax.php?do=getproducts', {id: id}, function (data) {
                    $('#manyParents').append(data);
                });
            }
        });

    });
    
    
    
    $('.governmentId').live('change', function () {
        var id = $(this).val();
        if( id != 0 ){
            $.post('salesreportajax.php?do=getareas', {id: id}, function (data) {
           
                $('#manyareas').html('');
                $('#manyareas').html(data);
                $('#areaid').select2();

            });
        }
        

    });
    
    
    $('.productCattt2').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") {
                $('#manyParents2').append(data);
            }
            else
            {
//                $('#level').val((level - 1));
//                $.post('salesreportajax.php?do=getproducts', {id: id}, function (data) {
//                    $('#manyParents2').append(data);
//                });
            }
        });

    });
    
    /*$(".getlayingOrder").select2({
     placeholder: "اختر",
     minimumInputLength: document.getElementById("searchvalue").value,
     //minimumInputLength: 3,
     multiple: false,
     quietMillis: 100,
     ajax: {
     url: "dailyentryajax.php?do=getlayingorder",
     dataType: 'json',
     data: function (term, page) {
     return {
     term: term, //search term
     page_limit: 10 // page size
     };
     },
     results: function (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);
     }
     });*/


    $('.showdetails').live('click', function () {
        var proid = $(this).attr('data-id');
        var dfrom = $('#olddatefrom').val();
        var dto = $('#olddateto').val();
        window.location.href = "freesales.php?do=detail&id=" + proid + "&datefrom=" + dfrom + "&dateto=" + dto;
    });

    $('.excel_btn').live('click', function () {
            $("#table2excel").table2excel({
              // exclude CSS class
              exclude: ".noExl",
              name: "Worksheet Name",
              filename: "تقرير عملاء بالمنتجات" //do not include extension
            }); 
    });



    $('.dataTableClass22').dataTable(
            {   "lengthMenu"
                            : [[10 , 25 , 50 , 100 , -1], [10 , 25 , 50 , 100, "All"]],
                            
                 
                 "order"
                    : [
                        [4, "desc"]       // order by drug cat
                    ]
                } 
);


});

