// JavaScript Document
var userClickedYes = false;
$(document).ready(function () {
    $(".numberinput").forceNumeric();
});


// forceNumeric() plug-in implementation
jQuery.fn.forceNumeric = function () {

    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.which || e.keyCode;

            if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                    // numbers
                    key >= 48 && key <= 57 ||
                    // Numeric keypad
                    key >= 96 && key <= 105 ||
                    // comma, period and minus, . on keypad
                    key == 190 || key == 188 || key == 109 || key == 110 ||
                    // Backspace and Tab and Enter
                    key == 8 || key == 9 || key == 13 ||
                    // Home and End
                    key == 35 || key == 36 ||
                    // left and right arrows
                    key == 37 || key == 39 ||
                    // Del and Ins
                    key == 46 || key == 45)
                return true;

            // return false;
        });
    });
}



function checkName(e, tableName, inputName)
{


    if (!userClickedYes)
    {


        e.preventDefault();
    }


    var name = document.getElementById("" + inputName).value;
    if (name != null && name != "")
    {
        $.ajax({
            url: 'checkRedundantNames.php?name=' + name + '&tablename=' + tableName,
            success: function (data) {
                //
                //alert(data);
                if (data == '0')
                {

                    userClickedYes = true;

                    //$(".nameToBeChecked").submit();
                    $("#nameError").css("display", "none");
                    //alert(555);
                    return true;

                }
                else if (data == '1')
                {
                    userClickedYes = false;
                    $("#" + inputName).val("");
                    $("#" + inputName).focus();

                    $("#nameError").css("display", "block");
                    return false;
                }
            }

        });
    }

    //return 1;
}



function checkNameWithId(e, tableName, inputName, inputId)
{
    if (!userClickedYes)
    {
        e.preventDefault();
    }


    var name = document.getElementById("" + inputName).value;
    var id = document.getElementById("" + inputId).value;
    //alert('inputName'+ document.getElementById(""+inputName).value);
    //alert('inputId'+ document.getElementById(""+inputId).value);
    //document.write('checkRedundantNames.php?do=checkNameWithId&name='+name+'&id='+id+'&tablename='+tableName);
    //alert('checkRedundantNames.php?do=checkNameWithId&name='+name+'&id='+id+'&tablename='+tableName);
    $.ajax({
        url: 'checkRedundantNames.php?do=checkNameWithId&name=' + name + '&id=' + id + '&tablename=' + tableName,
        success: function (data) {

            if (data == '0')
            {

                userClickedYes = true;

                $("#nameError").css("display", "none");
                //  alert('data'+data);
                // $(".nameToBeChecked").submit();
                //alert('data11'+data);
                //// break;
                return true;

            }
            else if (data == '1')
            {

                userClickedYes = false;
                $("#" + inputName).val("");

                $("#nameError").css("display", "block");
                //alert("هذا الإسم موجود من قبل");
                // return false;
            }


        }

    });

}


//customized function which used only for adding product categories in add product page (productview/add.html), and for adding expenses type in add expense page(expenseview/add.html)

function checkCatName(e, tableName, inputName)
{
    if (!userClickedYes)
    {
        e.preventDefault();
    }

    var name = document.getElementById("" + inputName).value;
    if (name != null && name != "")
    {
        $.ajax({
            url: 'checkRedundantNames.php?name=' + name + '&tablename=' + tableName,
            success: function (data) {
                //
                //	alert(data);
                if (data == '0')
                {
                    userClickedYes = true;
                    $("#nameError").css("display", "none");
                    $(".nameToBeChecked2").submit();
                }
                else if (data == '1')
                {
                    userClickedYes = false;
                    $("#" + inputName).val("");
                    $("#" + inputName).focus();

                    $("#nameError").css("display", "block");
                }
            }
        });
    }
    return data;
}


//# tarek .
$('body').on('change', '.orderRows', function (e) {
    e.preventDefault();
    var thsRows = $(this).val();
    var thsRowsId = $(this).attr('id');
    var menuurlid = $(this).attr('data-id');

    console.log('thsRows = ' + thsRows);

    var rowsLength = $('.orderRows').length;
    console.log('rowsLength = ' + rowsLength);

    if (thsRows == 0) {
        $.post('productControllerAjax.php?do=updatemenuorder', {menuurlid: menuurlid, orderno: thsRows}, function (data) {
            //$('#tableWrap').html(data);
        });
    } else {
        $('.orderRows').each(function () {
            var thsVal = $(this).val();
            var thsValId = $(this).attr('id');
            console.log('thsRowsId = ' + thsRowsId);
            console.log('thsValId = ' + thsValId);

            if (thsRowsId != thsValId) {
                if (thsRows == thsVal) {
                    console.log('Error = ' + thsValId);
                    $('#' + thsRowsId + '').val(0);
                    alert('عفوا .. اختر رقم اخر.. تم اختيار هذا الرقم فى الترتيب من قبل .');
                    return false;
                }

            }

            if ($('#order_' + rowsLength + '').attr('id') == thsValId) {
                console.log('last row = ' + thsValId);
                var menuurlid = $('#' + thsRowsId + '').attr('data-id');
                console.log('menuurlid = ' + menuurlid);
                console.log('orderno = ' + thsRows);

                $.post('productControllerAjax.php?do=updatemenuorder', {menuurlid: menuurlid, orderno: thsRows}, function (data) {
                    //$('#tableWrap').html(data);
                });
            }
        });

    }



//    console.log('menuurlid = ' + menuurlid);
//    console.log('thsRows = ' + thsRows);
//
//    $.post('productControllerAjax.php?do=updatemenuorder', {menuurlid: menuurlid, orderno: thsRows}, function (data) {
//        //$('#tableWrap').html(data);
//    });
});

$('body').on('change', '.saveMenuGroups', function (e) {
    e.preventDefault();
    var groups = $(this).val();
    // var thsRowsId = $(this).attr('id');
    var menuurlid = $(this).attr('data-id');
    $.ajax({
        type: "POST",
        url: 'productControllerAjax.php?do=updatemenugroups',
        data: {groups: groups, menuurlid: menuurlid},
        async: false,
        success: function (data) {
            data = Number(data);
            if (data == 1) {
                $('#sucess' + menuurlid).show();
                setTimeout(function () {
                    $('#sucess' + menuurlid).hide();
                }, 1100);
            } else if (data == -1) {
                $('#error' + menuurlid).show();
                setTimeout(function () {
                    $('#error' + menuurlid).hide();
                }, 1100);
            }

        }
    });

});
$('.dataTableClass22').dataTable(

        );

