//check if user choosed any item, otherwise do nothing and show an alert
function countChoosenProducts(controllerName)
{

    if ($('.choosenItem:checked').length > 0) {  // the "> 0" part is unnecessary, actually
        if (controllerName == 'buyBillController')
        {
            $("#form1").attr("action", "buyBillController.php?do=delete");
        }
        else if (controllerName == 'buyAndReturnBillController')
        {
            $("#form1").attr("action", "buyAndReturnBillController.php?do=delete");
        }
        else if (controllerName == 'returnBuyBillController')
        {
            $("#form1").attr("action", "returnBuyBillController.php?do=delete");
        }
        else
        {
            $("#form1").attr("action", controllerName + ".php?do=executeOperation");
        }
        return true;
    }
    else
    {
        $("#form1").attr("action", "");
        //alert("يجب اختيار العناصر أولا");
        $("#noElementsError").css("display", "block");
        setTimeout(function () {
            $("#noElementsError").css("display", "none");
        }, 4000);
        return false;
    }
}


$(document).ready(function (e) {


    //# change on  client dropdown.
    $('#clientid').on('change', function (e) {
        var thsVal = $(this).val();
        if (thsVal != '-1') {
            //$('#supplierId').prop('disabled', true);
            //# check .
            $('#rep1').prop('checked', true);
            $('#rep2').prop('checked', true);
            $('#rep3').prop('checked', true);
            //# disable .
            //$('#rep4').prop('disabled', true);
            //$('#rep5').prop('disabled', true);
            //$('#rep6').prop('disabled', true);

        } else {
            //$('#supplierId').prop('disabled', false);
            //# uncheck .
            $('#rep1').prop('checked', false);
            $('#rep2').prop('checked', false);
            $('#rep3').prop('checked', false);
            //# enable .
            //$('#rep4').prop('disabled', false);
            //$('#rep5').prop('disabled', false);
            //$('#rep6').prop('disabled', false);
        }
    });

    //# change on  supplier dropdown.
    $('#supplierId').on('change', function (e) {
        var thsVal = $(this).val();
        if (thsVal != '-1') {
            //$('#clientid').prop('disabled', true);
            //# uncheck .
            $('#rep4').prop('checked', true);
            $('#rep5').prop('checked', true);
            $('#rep6').prop('checked', true);
            //# diable  .
            //$('#rep1').prop('disabled', true);
            //$('#rep2').prop('disabled', true);
            //$('#rep3').prop('disabled', true);

        } else {
            //$('#clientid').prop('disabled', false);
            //# uncheck .
            $('#rep4').prop('checked', false);
            $('#rep5').prop('checked', false);
            $('#rep6').prop('checked', false);
            //# enable .
            //$('#rep1').prop('disabled', false);
            //$('#rep2').prop('disabled', false);
            //$('#rep3').prop('disabled', false);
        }
    });

    var outputStringValue = $("#outputStringValue").html();
    if (outputStringValue == "")
    {

        $("#outputString").css("display", "none");
    }
    else
    {

        $("#outputString").css("display", "block");
        setTimeout(function () {
            $("#outputString").css("display", "none");
        }, 4000);
    }

    //fadeout outputString
    //$("#outputString").fadeOut(4000);
    /*----------------------------------------*/
    //check or uncheck all
    // Listen for click on toggle checkbox
    $('#checkAll').click(function (event) {
        if (this.checked) {
            // Iterate each checkbox
            $('.choosenItem').each(function () {
                this.checked = true;
            });
        }
        else {
            // Iterate each checkbox
            $(".choosenItem").each(function () {
                this.checked = false;
            });
        }
    });

});
