$(document).ready(function () {
    $("body").on('blur', '.pincode', showpinavail);
    $("body").on('focus', '.pincode', clearPin);
    $("body").on('click', '#promobtn', ApplyPromo);

    function showpinavail() {
        var pincode = $('.pincode').val();
        var fpincode = $('.frompincode').val();
        var data = {pincode: pincode, fpincode: fpincode};

        if (pincode !== '' && fpincode !== '') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: "getavail_pincode.php",
                data: data,
                success: function (response) {
                    if (response.err === null) {
                        if (response.has_cod !== true) {
                            document.getElementById('chkpin').innerHTML = "COD NOT AVAILABLE FOR THIS PINCODE";
                            $('.pincode').val("");
                        }
                    } else {
                        alert("Something Went Wrong. Please Try Again Later  " + response.err);
                        $('.pincode').val("");

                    }
                },
                error: function (request, textStatus, errorThrown) {
                    //   alert(errorThrown);
                }
            });

        }
    }

    function clearPin() {
        document.getElementById('chkpin').innerHTML = "";
    }

    function ApplyPromo() {
        var cartamt = parseInt($(".cartamt").text()),
//                subtotal = parseInt($("#subtotal").text()),
                netamt = parseInt($("#netamount").text()),
                promocode = $(".promocode").val(),
                data = {promocode: promocode};
        if (promocode !== '') {
            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: 'get_promo.php',
                data: data,
                success: function (response) {
                    if (response !== 'No Such Promo Code Available') {
                        var discperc = response;
                        var discamt = (cartamt * discperc) / 100;
                        var totamt = Math.round(cartamt - discamt);
//                        document.getElementById('subtotal').innerHTML = totamt;
                        document.getElementById('netamount').innerHTML = totamt;
                        document.getElementById('promoresult').innerHTML = "Congratulations!! Your Promo Code Is Applied ";
                        document.getElementById('promoresult2').innerHTML = " " + discamt + "  Rupees is Deducted @" + discperc + "%.";
                        $(".netamt").val(totamt);
                        $(".discamt").val(discamt);
                        $(".promocode").hide();
                        $("#promobtn").hide();
                    } else {
                        document.getElementById('promoresult').innerHTML = "No Such Promo Code Available";
                    }
                },
                error: function (request, textStatus, errorThrown) {
                }
            });
        }
    }


});
