/*global $, jQuery, alert*/
$('.progress-number').each(function () {
    $(this).animate({
        left: $(this).data('percentage')
    }, {
        duration: $(this).data('time'),
        step: function (now, fx) {
            var data = Math.round(now);
            $(this).text(data + ' ');
        }
    });
});
//edit by eslamzaki
//17-2-2017.

//by Em An
// 14-12-2016
$(document).ready(function () {
    $('form').submit(function() {
        $(this).find("input[type='submit']").prop('disabled',true);
    });
    //////////////////////////////////////////////////
    $("#type_company_receiver").change(function () {
        var type_company_receiver = $("#type_company_receiver").val();
        if (type_company_receiver == 1) {
            $("#name_id_or_reg").html("رقم التسجيل الضريبى *إلزامي").show();
        } else if (type_company_receiver == 2) {
            $("#name_id_or_reg").html("رقم البطاقة *إلزامى عند 15000 جنيه").show();
        } else if (type_company_receiver == 3) {
            $("#name_id_or_reg").html("رقم جواز السفر *إلزامي").show();
        }
        return false;
    }).keyup();
    //////////////////////////////////////////////////
    $("#currencySold").change(function () {
        var currencySold = $("#currencySold").val();
        if (currencySold != 46) {
            $("#currencyExchange").css("display", "block");
        } else{
            $("#currencyExchange").css("display", "none");
        }
        return false;
    }).keyup();
    //////////////////////////////////////////////////
    $("#unit_amount").keyup(function () {
        var discount_rate = Number($("#discount_rate").val());
        var quantity = Number($("#quantity").val());
        var unit_amount = Number($("#unit_amount").val());
        var totalsale = quantity * unit_amount;
        var net_amount = totalsale ;
        if (discount_rate > 0 && discount_rate <= 100) {
            var discount_amount = (totalsale * discount_rate) / 100;
            var net_amount = totalsale - discount_amount;
            $("#discount_amount").val(discount_amount);
            $("#net_amount").val(net_amount);
        } else{
            $("#discount_amount").val("");
            $("#net_amount").val(net_amount);
        }
        $("#salestotla").val(totalsale);
        return false;
    });
    //////////////////////////////////////////////////
    $("#quantity").keyup(function () {
        var discount_rate = Number($("#discount_rate").val());
        var quantity = Number($("#quantity").val());
            var unit_amount = Number($("#unit_amount").val());
            var totalsale = quantity * unit_amount;
            var net_amount = totalsale ;
        if (discount_rate > 0 && discount_rate <= 100) {
            var discount_amount = (totalsale * discount_rate) / 100;
            var net_amount = totalsale - discount_amount;
            $("#discount_amount").val(discount_amount);
            $("#net_amount").val(net_amount);
        } else{
            $("#discount_amount").val("");
            $("#net_amount").val(net_amount);
        }
        $("#salestotla").val(totalsale);
        return false;
    });
    //////////////////////////////////////////////////
    $("#discount_rate").keyup(function () {
        var discount_rate = Number($("#discount_rate").val());
        var quantity = Number($("#quantity").val());
            var unit_amount = Number($("#unit_amount").val());
            var totalsale = quantity * unit_amount;
            var net_amount = totalsale ;
        if (discount_rate > 0 && discount_rate <= 100) {
            var discount_amount = (totalsale * discount_rate) / 100;
            var net_amount = totalsale - discount_amount;
            $("#discount_amount").val(discount_amount);
            $("#net_amount").val(net_amount);
        } else{
            $("#discount_amount").val("");
            $("#net_amount").val(net_amount);
        }
        return false;
    });
    //////////////////////////////////////////////////
    $("#discount_amount").keyup(function () {
        var discount_amount = Number($("#discount_amount").val());
        var quantity = Number($("#quantity").val());
        var unit_amount = Number($("#unit_amount").val());
        var totalsale = quantity * unit_amount;
        var net_amount = totalsale ;
        if (discount_amount > 0 && discount_amount <= totalsale) {
            var discount_rate = (discount_amount / totalsale) * 100;
            var net_amount = totalsale - discount_amount;
            $("#discount_rate").val(discount_rate);
            $("#net_amount").val(net_amount);
        } else{
            $("#discount_rate").val("");
            $("#net_amount").val(net_amount);
        }
        return false;
    });
    //////////////////////////////////////////////////
    $("#import_id").change(function () {
        var import_name = $("#import_id").val();
        var s = {
            "import_name": import_name
        };
        $.post("search_live.php", s, function (data) {
            $("#account_import").html(data).show('fast');
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#issuer").change(function () {
        var accountup = $("#issuer").val();
        var s = {
            "acc_ch": 1,
            "accountup": accountup
        };
        $.post("search_live.php", s, function (data) {
            $("#data_issuer").html(data).show();
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#receiver").change(function () {
        var accountup = $("#receiver").val();
        var s = {
            "acc_ch": 1,
            "accountup": accountup
        };
        $.post("search_live.php", s, function (data) {
            $("#data_receiver").html(data).show();
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    // $("#lv2").change(function () {
    //     var accountup = $("#lv2").val();
    //     var s = {
    //         "acc_ch": 2,
    //         "accountup": accountup
    //     };
    //     $.post("search_live.php", s, function (data) {
    
    //         $("#lv3").html(data).show('fast');
    //     });
    //     return false;
    // }).keyup();
    /////////////////////////////////////////////////////////////
    /* $("#group_types").change(function () {
        var group_types = $("#group_types").val();
        var s = {
            "acc_ch": 3,
            "group_types": group_types
        };
        $.post("search_live.php", s, function (data) {
    
            $("#types").html(data).show('fast');
        });
        return false;
    }).keyup(); */
    /////////////////////////////////////////////////////////////
    $("#group_type").change(function () {
        var group_types = $("#group_type").val();
        var s = {
            "acc_ch": 6,
            "group_types": group_types
        };
        $.post("search_live.php", s, function (data) {
    
            $("#types").html(data).show('fast');
        });
        return false;
    }).keyup();
    //////////////////////////////////////////////////
    $("#cust").change(function () {
        var cust = $("#cust").val();
        var s = {
            "acc_ch": 4,
            "cust": cust
        };
        $.post("search_live.php", s, function (data) {
            $("#project").html(data).show('fast');
        });
        return false;
    }).keyup();
    //////////////////////////////////////////////////
    $("#project").change(function () {
        var project = $("#project").val();
        var s = {
            "acc_ch": 5,
            "project": project
        };
        $.post("search_live.php", s, function (data) {
            $("#cust").html(data).show('fast');
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#lv3f").change(function () {
        var accountup = $(this).val();
        var s = {
            "acc_ch": 7,
            "accountup": accountup
        };
        $.post("search_live.php", s, function (data) {
    
            $("#lv4f").html(data).show('fast');
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#lv3t").change(function () {
        var accountup = $(this).val();
        var s = {
            "acc_ch": 7,
            "accountup": accountup
        };
        $.post("search_live.php", s, function (data) {
            
            $("#lv4t").html(data).show('fast');
        });
        return false;
    }).keyup();
    ////////////////////////////////////////////
    function loadlink(){
        var s = {
            "acc_ch": 8
        };
        $.post("search_live.php", s, function (data) {
            $("#lastidyomya").val(data);
        });
    }
    loadlink(); // This will run on page load
    setInterval(function(){
        loadlink() // this will run after every 5 seconds
    }, 2000);
    ////////////////////////////////////////////
    function checktoken(){
        var s = {
            "acc_ch": 9
        };
        $.post("search_live.php", s, function (data) {
            if (data == 2) {
                $("#ch_token_ok").show();
                $("#ch_token_no").hide();
            } else if (data != 2) {
                $("#ch_token_no").show();
                $("#ch_token_ok").hide();
            }
        });
    }
    checktoken(); // This will run on page load
    setInterval(function(){
        checktoken() // this will run after every 5 seconds
    }, 2000);
    /////////////////////////////////////////////////////////////
    $("#account_import").change(function () {
        var account_id = $("#account_import").val();

        var s = {
            "account_id": account_id
        };

        $.post("search-far3-import.php", s, function (data) {
            $("#far3_import").html(data).show('slow').delay(8000);
        });
        return false;
    }).keyup();

    /////////////////////////////////////////////////////////////
    $("#id_bank").change(function () {
        var id_bank = $("#id_bank").val();

        var s = {
            "id_bank": id_bank
        };

        $.post("search_far3_bank.php", s, function (data) {
            $("#far3_bank").html(data).show('slow').delay(8000);
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#id_bank_accounts").change(function () {
        var id_bank_accounts = $("#id_bank_accounts").val();

        var s = {
            "id_bank_accounts": id_bank_accounts
        };

        $.post("search_far3_bank.php", s, function (data) {
            $("#far3_bank").html(data).show('slow').delay(8000);
        });
        return false;
    }).keyup();
    /////////////////////////////////////////////////////////////
    $("#bankorcash").change(function () {
        var bankorcash = $("#bankorcash").val();

        if (bankorcash == 2) {
            $("#showbank").show('slow').delay(200);
        } else if (bankorcash != 2) {
            $("#showbank").hide('slow').delay(200);
        }


        return false;
    }).keyup();
    // Convert Password Field To Text Field On Hover
    var passField = $('.password');

    $('.show-pass').hover(function () {

        passField.attr('type', 'text');

    }, function () {

        passField.attr('type', 'password');

    });

    // Confirmation Message On Button

    $('.confirm').click(function () {

        return confirm('هل أنت متأكد؟');

    });
    // start all total buy and sale

    // start all total buy
    $("#kgf").keyup(function () {
        var pricebuy = Number($("#pricebuy").val());
        var kgf = Number($(this).val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy 

        $("#totalbuy").val(alltotalbuy);
        return false;
    });

    $("#kgsale").keyup(function () {
        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($("#tipsbuy").val());
        var kgf = Number($("#kgf").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($("#tipssale").val());
        var kgsale = Number($(this).val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });

    $("#pricebuy").keyup(function () {
        var pricebuy = Number($(this).val());
        var kgf = Number($("#kgf").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy ;

        $("#totalbuy").val(alltotalbuy);
        return false;
    });

    $("#nolonbuy").keyup(function () {
        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($(this).val());
        var tipsbuy = Number($("#tipsbuy").val());
        var kgf = Number($("#kgf").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($("#tipssale").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });

    $("#tipsbuy").keyup(function () {
        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($(this).val());
        var kgf = Number($("#kgf").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($("#tipssale").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });
    // end all total buy

    // start all total sale
    $("#pricesale").keyup(function () {
        var pricesale = Number($(this).val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($("#tipssale").val());
        var kgf = Number($("#kgf").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($("#tipsbuy").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });

    $("#nolonsale").keyup(function () {
        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($(this).val());
        var tipssale = Number($("#tipssale").val());
        var kgf = Number($("#kgf").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($("#tipsbuy").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });

    $("#tipssale").keyup(function () {
        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($(this).val());
        var kgf = Number($("#kgf").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($("#tipsbuy").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricecar = Number($("#pricecar").val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });
    // end all total sale
    $("#pricecar").keyup(function () {
        var pricebuy = Number($("#pricebuy").val());
        var nolonbuy = Number($("#nolonbuy").val());
        var tipsbuy = Number($("#tipsbuy").val());
        var kgf = Number($("#kgf").val());

        var totalbuy = pricebuy * kgf;
        var alltotalbuy = totalbuy + nolonbuy + tipsbuy;

        var pricesale = Number($("#pricesale").val());
        var nolonsale = Number($("#nolonsale").val());
        var tipssale = Number($("#tipssale").val());
        var kgsale = Number($("#kgsale").val());

        var totalsale = pricesale * kgsale;
        var alltotalsale = totalsale + nolonsale + tipssale;

        var pricecar = Number($(this).val());

        var totalcar = pricecar * kgsale;

        var safy = alltotalsale - (alltotalbuy + totalcar);

        $("#totalbuy").val(alltotalbuy);
        $("#totalcar").val(totalcar);
        $("#totalsale").val(alltotalsale);
        $("#safy").val(safy);
        return false;
    });
    // end all total buy and sale

    ///////////////////////////////////////////////////////
    $('li.category').addClass('plusimageapply');
    $('li.category').children().addClass('selectedimage');
    $('li.category').children().hide();
    $("ul.firstLevel li a").css("display", "block");

    $('li.category > a').click(function(e){
        e.preventDefault();
        e.stopPropagation();
        if($(this).hasClass('minusimageapply')) { 
        //collapse other category and expand its children. 
        $(this).next('ul').hide();
        $(this).removeClass('minusimageapply').addClass('plusimageapply');

        } else { 
            $(this).next('ul').show();
            $(this).removeClass('plusimageapply').addClass('minusimageapply');
        }
    });
    ////////////////////////////////////////////////////////////////
    $("#edit_payment").click(function () {
        $("#contract_payment").css("display", "block");
        $("#edit_payment_hid").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#edit_tax_dqm").click(function () {
        $("#tax_dqm").css("display", "block");
        $("#edit_tax_dqm_hid").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#edit_business_warranty").click(function () {
        $("#business_warranty").css("display", "block");
        $("#edit_business_warranty_hid").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#edit_tax_datws").click(function () {
        $("#tax_datws").css("display", "block");
        $("#edit_tax_datws_hid").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#go_edit_state").click(function () {
        $("#analytical_state").css("display", "block");
        $("#edit_analytical_state").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#go_edit_dateforon").click(function () {
        $("#dateforon").css("display", "block");
        $("#edit_dateforon").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#go_edit_dateforend").click(function () {
        $("#dateforend").css("display", "block");
        $("#edit_dateforend").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#go_edit_dateforoff").click(function () {
        $("#dateforoff").css("display", "block");
        $("#edit_dateforoff").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#go_edit_dateforend_fe3ly").click(function () {
        $("#dateforend_fe3ly").css("display", "block");
        $("#edit_dateforend_fe3ly").css("display", "none");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#add_offer_terms").click(function () {
        $("#offer_terms").css("display", "block");
        return false;
    }).keyup();
    ////////////////////////////////////////////////////////////////
    $("#msgdata").delay(500).show('fast');
    $("#msgdata").delay(8000).hide('slow');
    ////////////////////////////////////////////////////////////////
    // كود عدم ارسال النموذج أكثر من مرة
    $('form').submit(function() {
        $(this).find("input[type='submit']").prop('disabled',true);
    });
    ///////////////////////////////////////////////////////////////////
    //$('.js-example-basic-single').select2();
    $('select').select2();
    
    ///////////////////////////////////////////////////////////
    $("#myInput").on("keyup", function() {
        var value = $(this).val().toLowerCase();
        $("#myTable tr").filter(function() {
        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
    });
    ///////////////////////////////////////////////////////////
        // start Date
        $("#dateprint").change(function () {
            var dateprint = $(this).val();

            $("#dateprinted").html(dateprint).show('fast');

            return false;
        }).keyup();
        // End Date
    ////////////////////////////////////////////////////////////////
    // $(function () {
    //     $('[data-toggle="popover"]').popover()
    // })  
    

});