$(document).ready(function () {
    $("body").on("change", ".itemgrp", getscode);
    $("body").on("blur", ".brand", createBrand);
    $("body").on("blur", ".size", createSize);
    $("body").on("blur", ".quality", createQuality);
    $("body").on("change blur", ".size,.brand,.prodtype", getProduct);

    function getProduct() {
        var prodtype = $(".prodtype").val(),
                brand = $(".brand").val(),
                size = $(".size").val();
        if (prodtype != "" && brand != "" && size != "") {
            $.post('getProdName.php', {prodtype: prodtype, brand: brand, size: size}, function (data) {
                $("#prodname").html(data);
            });
        }
    }

    function createQuality() {
        var prodtype = $(".prodtype").val(),
                brand = $(".brand").val(),
                quality = $(".quality").val(),
                size = $(".size").val();
        if (prodtype != "" && brand != "" && size != "") {
            $.post('createQuality.php', {prodtype: prodtype, quality:quality, brand: brand, size: size,  check: '1'}, function (data) {
                if (data > 0) {
                    var m = confirm("Create New Quality ?");
                    if (m == true) {
                        $.post('createBrand.php', {prodtype: prodtype, quality:quality, brand: brand, size: size,  create: '1'}, function (data) {

                        });
                    }
                }
            });
        }
    }
    function createBrand() {
        var prodtype = $(".prodtype").val(),
                brand = $(".brand").val();
        if (prodtype != "" && brand != "") {
            $.post('createBrand.php', {prodtype: prodtype, brand: brand, check: '1'}, function (data) {
                if (data > 0) {
                    var m = confirm("Create New Brand?");
                    if (m == true) {
                        $.post('createBrand.php', {prodtype: prodtype, brand: brand, create: '1'}, function (data) {

                        });
                    }
                }
            });
        }
    }

    function createSize() {
        var prodtype = $(".prodtype").val(),
                brand = $(".brand").val(),
                size = $(".size").val();
        if (prodtype != "" && brand != "" && size != "") {
            $.post('createSize.php', {prodtype: prodtype, brand: brand, size: size, check: '1'}, function (data) {
                if (data > 0) {
                    var m = confirm("Create New Size?");
                    if (m == true) {
                        $.post('createSize.php', {prodtype: prodtype, brand: brand, size: size, create: '1'}, function (data) {

                        });
                    }
                }
            });
        }
    }

    function getscode() {
        var itemgrp = $('.itemgrp').val();
        $.post('gethsn_item.php', {itemgrp: itemgrp}, function (data) {
            $('.hsncode').val(data.hsncode);
            $('.gstperc').val(data.gstperc).trigger("change");
        }, 'json');
    }


});