// JavaScript Document
$('.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);
            });
        }
    });

});

$('#addthis').live('click', function () {
    var itr = $('#itr').val();
    var proIsOptic = $('#proIsOptic').val();

    var existids = "";
    $('.proid').each(function () {
        existids += $(this).val() + ",";
    });

    if ($('#productId').length > 0 && $('#productId').val() != "-1")
    {
        var id = $('#productId').val();
        //add this product
        $.post('salesreportajax.php?do=getproductformail', {id: id, itr: itr, proIsOptic: proIsOptic, existids: existids}, function (data) {
            $('#productstable').append(data);
            setTimeout(function () {
                var biggestitr = $(".proname").last().attr('id').substr(11);
                biggestitr++;
                $("#itr").val(biggestitr);
            }, 500);
        });
    }
    else if ($('#productCatId0').val() > 0)
    {
        //add cat products
        var level = Number($('.productCat').last().attr('data-level'));
        var id = $('#productCatId' + level).val();
        if ($('#productCatId' + level).val() == "" || $('#productCatId' + level).val() == "-1")
        {
            level = level - 1;
            id = $('#productCatId' + level).val();
        }
        var datatype = 0;
        if ($('#datatype1').attr('checked') == 'checked') {
            datatype = 1;
        }
        $.post('salesreportajax.php?do=getsubcatproductsformail', {id: id, datatype: datatype, itr: itr, existids: existids}, function (data) {
            $('#productstable').append(data);
            setTimeout(function () {
                var biggestitr = $(".proname").last().attr('id').substr(11);
                biggestitr++;
                $("#itr").val(biggestitr);
            }, 500);
        });
    }
    else {
        alert('اختر تصنيف اولا');
    }
});

$('.removethis').live('click', function () {
    $(this).parent().parent().remove();
});


