var buttonactionsjs = function () {

    //global hosturl
    var hosturl = $('#hosturl').val();

    var deleteSelected = function () {

        // Handle click on "Select all" control
        $('#checkAll').on('click', function () {
            var tableq = $(this).closest('table').dataTable();
            // var tableq = $('#controllprop').dataTable();
            var rows = tableq.api().rows().nodes();

            // Check/uncheck checkboxes for all rows in the table
            if ($(this).is(':checked')) {
                $('input[type="checkbox"]', rows).attr('checked', 'checked');
                $('input[type="checkbox"]', rows).parent('span').addClass('checked');
            }
            else {
                $('input[type="checkbox"]', rows).removeAttr('checked');
                $('input[type="checkbox"]', rows).parent('span').removeClass('checked');
            }
            $('input[type="checkbox"]').uniform();
        });

        $('#showForm').on('change', ':checkbox', function () {
            if (this.checked) {
                $('#deleteBtn').removeClass('disabled');
                // $('#printBtn').removeClass('disabled');
                $('#activateBtn').removeClass('disabled');
                $('#deactivateBtn').removeClass('disabled');
                $('#newsletterfatma').removeClass('disabled');

            } else {
                if ($('#showForm input[class=checkItem]:checked').length) { //if there is any checked checkbox, do nothing
                } else {
                    $('#deleteBtn').addClass('disabled');
                    // $('#printBtn').addClass('disabled');
                    $('#activateBtn').addClass('disabled');
                    $('#deactivateBtn').addClass('disabled');
                    $('#newsletterfatma').addClass('disabled');
                }
            }
        });
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //to open model for accept del
        $('.delSelected').click(function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;
            if (action == undefined) {
                golink = 'del';
            } else {
                golink = action;
            }
            bootbox.dialog({
                message: "هل تريد حذف هذا العنصر؟",
                title: "رسالة تأكيد الحذف",
                buttons: {
                    danger: {
                        label: "إلغاء",
                        className: "btn-danger"
                    },
                    main: {
                        label: "حذف",
                        className: "btn-primary",
                        callback: function () {

                            //delete selected items
                            //check if there are any related data before deleting
                            if (urlgo && (urlgo == "videocategories" || urlgo == "slider" || urlgo == "managementCenter" || urlgo == "news" || urlgo == "teams" || urlgo == "members" || "registerInfo")) {
                                //iterate through checked checkboxes to get id
                                var arrayValues = [];
                                $('.checkItem:checked').each(function () {
                                    var items = {};
                                    items.itemid = $(this).val();
                                    items.nameOrTitle = $(this).data('extra');
                                    items.relatedFlag = 0;
                                    arrayValues.push(items);
                                });

                                //use promise to be able to perform other things after .each() inishes completely.
                                var promises = [];
                                $.each(arrayValues, function (k, v) {

                                    var dfd = $.Deferred();

                                    $.post(hosturl + '/' + urlgo + '/checkRelatedData', {id: v.itemid}, function (data) {
                                        if (data.replace(/\s/g, "") == 'yes') {
                                            v.relatedFlag = 1;
                                            dfd.resolve();
                                        } else {
                                            //delete item
                                            $.ajax({
                                                method: "GET",
                                                url: hosturl + "/" + urlgo + "/" + golink + "/" + v.itemid
                                            });
                                            dfd.resolve();
                                        }

                                    }); //post

                                    promises.push(dfd);
                                });
                                //after .each finished completely
                                $.when.apply($, promises).done(function () {
                                    // console.log("All was done");
                                    if (arrayValues.length > 0) {
                                        var nameString = "";
                                        $.each(arrayValues, function (index, val) {
                                            /* iterate through array */
                                            if (val.relatedFlag == 1) {
                                                nameString += "<br/>" + val.nameOrTitle;
                                            }
                                        });
                                        if (nameString != "") {
                                            //there are items with related data
                                            var alertText3 = "";
                                            if (urlgo == "_role" || urlgo == "teachers" || urlgo == "subjects" || urlgo == "students" || urlgo == "teacher_schedule") {
                                                if (urlgo == "_role") {
                                                    alertText3 = "لا يمكنك حذف هذه المجموعة يوجد مستخدمين مضافة لها ";
                                                } else if (urlgo == "teachers") {
                                                    alertText3 = "لا يمكنك حذف هذا المدرس لأنه مرتبط بعمليات أخرى";
                                                } else if (urlgo == "subjects") {
                                                    alertText3 = "لا يمكنك حذف هذه المادة لأنها مرتبطة بعمليات أخرى";
                                                } else if (urlgo == "students") {
                                                    alertText3 = "لا يمكنك حذف هذا الطالب لأنه مرتبط بعمليات أخرى";
                                                } else if (urlgo == "teacher_schedule") {
                                                    alertText3 = "لا يمكنك حذف هذه المجموعة لأن بها طلاب مسجلين";
                                                }
                                                //so show alert
                                                bootbox.dialog({
                                                    message: alertText3 + nameString,
                                                    title: "رسالة تأكيد الحذف",
                                                    buttons: {
                                                        danger: {
                                                            label: "إغلاق",
                                                            className: "btn-danger",
                                                            callback: function () {
                                                                if (urlgo == "registerInfo") {
                                                                    window.location.href = hosturl + "/" + urlgo;

                                                                } else
                                                                {
                                                                    window.location.href = hosturl + "/" + urlgo + "/show";
                                                                }
                                                            }
                                                        }
                                                    }
                                                }).find('.modal-content').addClass('modal-sm');
                                            } else {
                                                alertText3 = " هذا العنصر مرتبط بعمليات أخرى هل أنت متأكد من أنك تريد حذفها ؟: ";

                                                //so show alert
                                                bootbox.dialog({
                                                    message: alertText3 + nameString,
                                                    title: "رسالة تأكيد الحذف",
                                                    buttons: {
                                                        main: {
                                                            label: "نعم",
                                                            className: "btn-success",
                                                            callback: function () {
                                                                $('#showForm').submit();
                                                            }
                                                        },
                                                        danger: {
                                                            label: "إلغاء",
                                                            className: "btn-danger",
                                                            callback: function () {
                                                                if (urlgo == "registerInfo") {
                                                                    window.location.href = hosturl + "/" + urlgo;

                                                                } else {
                                                                    window.location.href = hosturl + "/" + urlgo + "/show";
                                                                }
                                                            }
                                                        }
                                                    }
                                                }).find('.modal-content').addClass('modal-sm');
                                            }
                                        } else {
                                            //there are items without related data
                                            if (urlgo == "registerInfo") {
                                                window.location.href = hosturl + "/" + urlgo;

                                            } else
                                            {
                                                window.location.href = hosturl + "/" + urlgo + "/show";
                                            }
                                        }
                                    }
                                });

                            } //if
                            else {
                                //if this is another controller & it isn't required to check for related data
                                $('#showForm').attr('action', hosturl + "/" + urlgo + "/del");
                                $('#showForm').submit();
                            }

                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');
        });

        $('.deleteJobSelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else if (data == -1)
                                    {
                                        var registerInfoFunction = 'warning';
                                        var msg = 'بعض العناصر لم يتم حذفها لان بها متقدمين';
                                        var title = 'ملاحظة';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    window.location.href = hosturl + "/" + urlgo + "/show";

                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });

        $('.deleteCitySelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else if (data == -1)
                                    {
                                        var registerInfoFunction = 'warning';
                                        var msg = 'بعض العناصر لم يتم حذفها لان بها فروع';
                                        var title = 'ملاحظة';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    if (data == -1)
                                        setTimeout(function () {
                                            window.location.href = hosturl + "/" + urlgo + "/show";
                                        }, 3000);
                                    else
                                        window.location.href = hosturl + "/" + urlgo + "/show";
                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });


        $('.deleteProCatSelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else if (data == -1)
                                    {
                                        var registerInfoFunction = 'warning';
                                        var msg = 'بعض التصنيفات لم يتم حذفها لأن بها منتجات';
                                        var title = 'ملاحظة';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    if (data == -1)
                                        setTimeout(function () {
                                            window.location.href = hosturl + "/" + urlgo + "/show";
                                        }, 3000);
                                    else
                                        window.location.href = hosturl + "/" + urlgo + "/show";
                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });

        $('.deleteCatSelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else if (data == -1)
                                    {
                                        var registerInfoFunction = 'warning';
                                        var msg = 'بعض العناصر لم يتم حذفها لان بها صور أو فيديوهات';
                                        var title = 'ملاحظة';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    if (data == -1)
                                        setTimeout(function () {
                                            window.location.href = hosturl + "/" + urlgo + "/show";
                                        }, 3000);
                                    else
                                        window.location.href = hosturl + "/" + urlgo + "/show";
                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });

        $('.deleteTeamSelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else if (data == -1)
                                    {
                                        var registerInfoFunction = 'warning';
                                        var msg = 'بعض العناصر لم يتم حذفها لان بها فرق أو أعضاء';
                                        var title = 'ملاحظة';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    if (data == -1)
                                        setTimeout(function () {
                                            window.location.href = hosturl + "/" + urlgo + "/show";
                                        }, 3000);
                                    else
                                        window.location.href = hosturl + "/" + urlgo + "/show";
                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });

        $('.deleteAppSelected').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = 'deleteselected';
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkItem:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                bootbox.dialog({
                    message: "هل أنت متأكد؟",
                    title: "رسالة تأكيد الحذف",
                    buttons: {
                        main: {
                            label: "حذف",
                            className: "btn-primary",
                            callback: function () {
                                //do something else
                                $.post(hosturl + "/" + urlgo + "/deleteselected", {deleteids: val}, function (data)
                                {
                                    if (data == 1)
                                    {
                                        //Message Success
                                        var registerInfoFunction = 'success';
                                        var msg = 'تم الحذف بنجاح';
                                        var title = 'نجاح';
                                    } else {
                                        var registerInfoFunction = 'error';
                                        var msg = 'حدث خطأ أثناء الحذف';
                                        var title = 'خطأ';
                                    }
                                    toastr.options = {
                                        closeButton: true,
                                        debug: $('#debugInfo').prop('checked'),
                                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                                        onclick: null
                                    };
                                    var $toast = toastr[registerInfoFunction](msg, title);
                                    window.location.href = hosturl + "/" + urlgo + "/show";

                                });
                            }
                        },
                        danger: {
                            label: "إلغاء",
                            className: "btn-danger"
                        },
                    }
                }).find('.modal-content').addClass('modal-sm');
            }
        });
    }
    //load prop
    var addnewmember = function () {

        $('.addnewmember').click(function () {

            var memberitr = Number($('#memberitr').val()) + 1;
            $.post(hosturl + "/members/insertNewMember", {memberitr: memberitr}, function (data) {

                $("#addmember").append(data);
            })

            //memberitr++;
            $('#memberitr').val(memberitr);

        })

        //remove the selected
        $('.delmember').live('click', function () {

            $(this).parent().parent().remove();
        })
    }
    var print = function () {
        $(".printit").click(function () {
            $('.no-print').hide();
            $(".portlet").print();
            $('.no-print').show();
        })

    }

    var hide = function () {
        $('body').on('click', '.approveItem', function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');
            var ishide = $(this).attr('data-ishide');

            var thisitem = $(this);

            $.post(hosturl + "/" + urlgo + "/approveItem", {itemid: itemid, ishide: ishide}, function (rst) {

                if (Number(rst) == 1) {
                    if (ishide == 0)
                    {
                        thisitem.attr('data-ishide', 1);
                        thisitem.html('<i class="fa fa-history"></i> ' + "ظاهر");
                        thisitem.removeClass('red');
                        thisitem.addClass('purple-intense');
                    } else
                    {
                        thisitem.attr('data-ishide', 0);
                        thisitem.html('<i class="fa fa-history"></i> ' + "مخفي");
                        thisitem.removeClass('purple-intense');
                        thisitem.addClass('red');
                    }

                    var shortCutFunction = 'success';
                    var msg = '.. تم التعديل بنجاح ';
                    var title = 'تم بنجاح';
                } else {
                    var shortCutFunction = 'error';
                    var msg = 'جرب مره اخرى ....  لقد حدث خطا';
                    var title = 'حذث خطا';
                }

                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-right',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        });
    }

    var dateSearch = function () {
        $('body').on('change', '.searchDate', function () {
            var from = $("#fromDate").val();
            var to = $("#toDate").val();

            $.post(hosturl + '/applicant/searchDate', {from: from, to: to}, function (res) {
                $("#showForm tbody").html(res);
            });
        });
    }
    var jobForm = function () {
        $("#file").on("change", function () {
            var ValidFileExtension = ["pdf", "doc", "docx"];

            if ($.inArray($(this).val().split('.').pop().toLowerCase(), ValidFileExtension) == -1) {
                $("#fileError").css('display', 'inline');
                alert('من فضلك اختر ملف بامتداد pdf, doc, docx فقط');
                $(this).val('');
            } else
            {
                $("#fileError").css('display', 'none');
            }
        });
        $("#phone").on('change', function () {
            var phone = $(this).val();
            if (phone != '' && /^[+0-9]+$/.test(phone) == false) {
                $(this).val('');
                $("#phoneError").css('display', 'inline');
            } else
                $("#phoneError").css('display', 'none');
        });

        $('.editform input').on('change', function () {
            if ($(this).val()[0] == " ") {
                $(this).val('');
                $("#errorDiv").css('display', 'inline');
            } else
                $("#errorDiv").css('display', 'none');
        });
        $("#email").on('change', function () {
            var email = $(this).val();
            var appid = $("#appid").val();
            $.ajax({
                url: hosturl + '/applicant/checkMail',
                type: "post",
                data: {email: email, appid: appid},
                async: false,
                success: function (data) {
                    if (data == 1)
                    {
                        $("#email").val('');
                        $("#mailError").css('display', 'inline');
                    } else
                        $("#mailError").css('display', 'none');
                }
            });
        })
    }

    var team = function () {
        $("#teamType").on('click', function () {
            $("#activityDiv").css('display', 'block');
        });

        $("#actType").on('click', function () {
            $("#activityDiv").css('display', 'none');
        });

    }

    var comments = function () {
        var newsId = $("#newsId").val();

        $('#checkAllComment').on('click', function () {
            var tableq = $(this).closest('table').dataTable();
            // var tableq = $('#controllprop').dataTable();
            var rows = tableq.api().rows().nodes();

            // Check/uncheck checkboxes for all rows in the table
            $('input[type="checkbox"]', rows).attr('checked', this.checked);
            $('input[type="checkbox"]', rows).parent('span').addClass('checked');

            $('input[type="checkbox"]').uniform();
        });

        $('#CommentDiv').on('change', ':checkbox', function () {
            if (this.checked) {
                $('#ApproveChecked').removeClass('disabled');
                $('#refuseChecked').removeClass('disabled');
            } else {
                if ($('#CommentDiv input[class=checkComment]:checked').length) { //if there is any checked checkbox, do nothing
                } else {
                    $('#ApproveChecked').addClass('disabled');
                    $('#refuseChecked').removeClass('disabled');
                }
            }
        });

        $('.ApproveChecked').on('click', function () {
            var urlgo = $(this).attr('data-link');//controller name
            var action = $(this).attr('data-action');
            var golink;

            if (action == undefined) {
                golink = "/showComments/" + newsId;
            } else {
                golink = action;
            }

            var val = '';
            var counter = 0;
            $('.checkComment:checked').each(function () {
                if (val == "")
                {
                    val = $(this).val();
                } else {
                    val += ',' + $(this).val();
                }
                counter++;
            });
            if (val == '')
            {
                var registerInfoFunction = 'warning';
                var msg = 'عفوا لم تختر شيئا';
                var title = 'إخطار';
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
                alert('عفوا لم تختر شيئا');
            } else
            {
                $.post(hosturl + "/" + urlgo + "/" + golink, {approveids: val}, function (data)
                {
                    if (data == 1)
                    {
                        //Message Success
                        var registerInfoFunction = 'success';
                        var msg = 'تم الحذف بنجاح';
                        var title = 'نجاح';
                    } else {
                        var registerInfoFunction = 'error';
                        var msg = 'حدث خطأ أثناء الحذف';
                        var title = 'خطأ';
                    }
                    toastr.options = {
                        closeButton: true,
                        debug: $('#debugInfo').prop('checked'),
                        positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                        onclick: null
                    };
                    var $toast = toastr[registerInfoFunction](msg, title);

                    window.location.href = hosturl + "/" + urlgo + "/showComments/" + newsId;
                });
            }
        });

        $('body').on('click', '.approveComment', function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');
            var ishide = $(this).attr('data-ishide');

            var thisitem = $(this);

            $.post(hosturl + "/" + urlgo + "/approveComment", {itemid: itemid, ishide: ishide}, function (rst) {

                if (Number(rst) == 1) {
                    if (ishide == 0)
                    {
                        thisitem.attr('data-ishide', 1);
                        thisitem.html('<i class="fa fa-check"></i> ' + "موافق");
                        thisitem.removeClass('red');
                        thisitem.addClass('green');
                    } else
                    {
                        thisitem.attr('data-ishide', 0);
                        thisitem.html('<i class="fa fa-ban"></i> ' + "مرفوض");
                        thisitem.removeClass('green');
                        thisitem.addClass('red');
                    }

                    var shortCutFunction = 'success';
                    var msg = '.. تم التعديل بنجاح ';
                    var title = 'تم بنجاح';
                } else {
                    var shortCutFunction = 'error';
                    var msg = 'جرب مره اخرى ....  لقد حدث خطا';
                    var title = 'حذث خطا';
                }

                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-right',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        });

        $('body').on('click', '.delComment', function () {
            var itemid = $(this).attr('data-id');
            var urlgo = $(this).attr('data-link');
            var action = $(this).attr('data-action');

            var golink;
            if (action == undefined) {
                golink = 'del';
            } else {
                golink = action;
            }
            bootbox.dialog({
                message: "هل تريد حذف هذا العنصر؟",
                title: "رساله تاكيد الحذف",
                buttons: {
                    danger: {
                        label: "الغاء",
                        className: "btn-danger"
                    },
                    main: {
                        label: "حذف",
                        className: "btn-primary",
                        callback: function () {
                            //do something else
                            $.post(hosturl + "/" + urlgo + "/" + golink, {itemid: itemid}, function (data) {
                                if (data == 1)
                                {
                                    window.location.href = hosturl + "/" + urlgo + "/showComments/" + newsId;
                                } else
                                {
                                    var shortCutFunction = 'error';
                                    var msg = 'جرب مره اخرى ....  لقد حدث خطا';
                                    var title = 'حذث خطا';

                                }
                            });
                        }
                    }
                }
            }).find('.modal-content').addClass('modal-sm');


            toastr.options = {
                closeButton: $('#closeButton').prop('checked'),
                debug: $('#debugInfo').prop('checked'),
                positionClass: $('#positionGroup input:checked').val() || 'toast-top-right',
                onclick: null
            };
            var $toast = toastr[shortCutFunction](msg, title);
        });
    }

    var role = function () {
        $("#showRole").on('click', function () {
            $("#roleDiv").css('display', 'block');
            $("#role").attr('required', 'required');
        });

        $("#hideRole").on('click', function () {
            $("#roleDiv").css('display', 'none');
        });

    }

    var siteFn = function () {
        $('#regionid').on('change', function () {
            var id = $(this).val();

            $.post(hosturl + '/branch/getCity', {id: id}, function (data) {
                $('#cityid').html(data);
                $("#cityid").select2("val", '');
            });
        });
    }

    var menuFn = function () {
        $('.updateSort').on('change', function () {
            var val = $(this).val();
            var id = $(this).attr('data-id');

            $.post(hosturl + '/_menu/sortMenu', {id: id, value: val}, function (data) {
                if (data == 1)
                {
                    //Message Success
                    var registerInfoFunction = 'success';
                    var msg = 'تم الترتيب بنجاح';
                    var title = 'نجاح';
                } else {
                    var registerInfoFunction = 'error';
                    var msg = 'حدث خطأ أثناء الترتيب';
                    var title = 'خطأ';
                }
                toastr.options = {
                    closeButton: true,
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-center',
                    onclick: null
                };
                var $toast = toastr[registerInfoFunction](msg, title);
            });
        });
    }
    return {
        //main function to initiate the module
        init: function () {
            $('form').validate();
            deleteSelected();
            addnewmember();
            print();
            hide();
            dateSearch();
            jobForm();
            team();
            comments();
            role();
            siteFn();
            menuFn();
        }

    };

}();