var formatResult;
$(document).ready(function (e) {






//alert(document.getElementById("searchvalue").value);
    formatResult = function (data) {
        return  data.text;
    }





//assign select2 properties
    $(".getFinalProductsWithOutProductionEquation").select2({
        placeholder: "إسم المنتج",
        minimumInputLength: document.getElementById("searchvalue").value,
        multiple: false,
        quietMillis: 100,
        ajax: {
            url: "taskOrderAjaxController.php?do=getProductionEquationProducts", //getFinalProducts
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            results: function (data) {
                return {results: data};
                alert(1);
            }
        },
        formatResult: formatResult,
        initSelection: function (element, callback) {
            var item = element.val().split(':');
            var data = {id: item[0], text: item[1]};
            callback(data);
        }
    });
    $('#finalProductId').on('change', function () {
        var proId = $(this).val();
        var proNumber = $('#proNumber').val();
        $.post("taskOrderAjaxController.php?do=getEquationProducts", {proId: proId, proNumber: proNumber}, function (data) {
            $('#table tbody').html(data);
        });
    });
    $('#proNumber').on('change', function () {
        var proNumber = $('#proNumber').val();
        $i = 1;
        $('.quantity').each(function () {
            var quantity = $(this).attr('data-val');
            $(this).parent().html(proNumber * quantity +'<input type="hidden" class="quantity" name="quantity'+$i+'" data-val="'+quantity+'" value="'+proNumber * quantity+'"/>');
            $i++;
        });
    });
    $(".select2RawMaterials").select2({
        placeholder: "إسم المنتج",
        minimumInputLength: document.getElementById("searchvalue").value,
        //minimumInputLength: 3,
        multiple: false,
        quietMillis: 100,
        ajax: {
            url: "sellbillAjaxController.php?do=GetRawMaterials", //getFinalProducts
            dataType: 'json',
            data: function (term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            results: function (data) {
                return {results: data};
                alert(1);
            }
        },
        formatResult: formatResult,
        initSelection: function (element, callback) {
            var item = element.val().split(':');
            var data = {id: item[0], text: item[1]};
            callback(data);
        }
    });
    $("#product1").change(function () {


//alert(222);

        var theselectedvalue = $(this).val();
        //alert('damagedProductAjaxController.php?do=units&productid='+theselectedvalue);
        $('#unitDiv1').load('damagedProductAjaxController.php?do=unitsWithItration&productid=' + theselectedvalue + '&itr=1', function () {
//reload the dropdownlist and jquery files
//$('select').chosen();
        });
    });
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//check if iput value is number and calculate new percentage
    $(document).on('blur', '.check', function () {

        var numericExpression = /^(?:[1-9]\d*|0)?(?:\.\d+)?$/;
        if (this.value.match(numericExpression))
        {
//here means +ve float number
            this.style.backgroundColor = "white";
            if (this.value > 100)
            {
                this.value = 100;
                //alert("لابد ان يكون الرقم من 0 الى 100");
                this.style.backgroundColor = "yellow";
            }

            $rows = Number(document.getElementById("rowitr").value);
            //alert($rows);
            $totalpercentage = 0;
            for ($i = 1; $i < $rows + 1; $i++)
            {
//alert($("#input"+$i+"").length);
                if ($("#input" + $i + "").length != 0)
                {
                    $x = Number(document.getElementById("input" + $i + "").value);
                    //alert($x);
                    $totalpercentage += $x;
                }

            }

            document.getElementById('percentage').innerHTML = $totalpercentage;
        }
        else
        {
//here it is not +ve float
            this.value = 0;
            this.style.backgroundColor = "yellow";
        }



//var id=this.id.substr(5);
//calculate sum

    });
//check if input is number(float)and +ve
    $(document).on('blur', '.positive', function () {


//this check for +ve int
        /*var numericExpression = /^[0-9]+$/;

         if(this.value.match(numericExpression)) {
         */
        /*alert ("true");*/
        /* this.style.backgroundColor = "white";

         } else {
         this.value = 0;
         //alert("لابد  ان يكون رقم صحيح موجب");
         this.style.backgroundColor = "yellow";

         }*/


//this check for +ve float
        var numericExpression = /^(?:[1-9]\d*|0)?(?:\.\d+)?$/;
        if (this.value.match(numericExpression))
        {
            this.style.backgroundColor = "white";
            if (this.value == 0)
            {
                this.value = 0;
                this.style.backgroundColor = "yellow";
            }

        }
        else
        {
            this.value = 0;
            //alert("لابد  ان يكون رقم صحيح موجب");
            this.style.backgroundColor = "yellow";
        }





    });
//stop default action of form if {no equation name} or (total percentage !=100) or {one of products list with choice = -1}
    $("#save").click(function (event) {

//check if there is an equation name
        $name1 = document.getElementById('name').value;
        if ($name1.length > 0)
        {
//true contine
            document.getElementById('name').style.backgroundColor = "white";
        }
        else
        {
            document.getElementById('name').style.backgroundColor = "yellow";
            event.preventDefault();
            return false;
        }

        $from = document.getElementById('from').value;
        if ($from.length >= 0)
        {
//true contine
            document.getElementById('from').style.backgroundColor = "white";
        }
        else
        {
            document.getElementById('from').style.backgroundColor = "yellow";
            event.preventDefault();
            return false;
        }

        $to = document.getElementById('to').value;
        if ($to.length >= 0)
        {
//true contine
            document.getElementById('to').style.backgroundColor = "white";
        }
        else
        {
            document.getElementById('to').style.backgroundColor = "yellow";
            event.preventDefault();
            return false;
        }

        $num = document.getElementById('proNumber').value;
        if ($num.length > 0)
        {
//true contine
            document.getElementById('proNumber').style.backgroundColor = "white";
        }
        else
        {
            document.getElementById('proNumber').style.backgroundColor = "yellow";
            event.preventDefault();
            return false;
        }

//check if final product name is choosen
        $finalProduct = document.getElementById('finalProductId').value;
        if ($finalProduct.length > 0)
        {
//true contine
        }
        else
        {
            event.preventDefault();
            alert(" من فضلك اختر المنتج ");
            return false;
        }


    });
});



