/*function reset_user_form() {
    $('form[name="user"] input');
}*/

function validateProfile() {
    
    if( autoValidate( $('#profile-form') ) ) {
        var pass1 = $('[name="pass1"]').val()
        ,   pass2 = $('[name="pass2"]').val()
        ,   msg   = '';  
        
        if( ( pass1 == "" && pass2 != "" ) || ( pass1 != "" && pass2 == "" ) ) {
            msg = "Please enter the "+ ( pass1 == "" ? "New" : "Confirm" ) +" password";
        }else if( pass1 != pass2 ) {
            msg = "Given password's are not matched";
        }
        
        if( msg != "" ) {
            var $blck = $('[name="pass1"]').parent().closest('.blck');
            if( !$blck.find('.ctrl.opener').hasClass('opened') ) {
                $blck.find('.sub-title').trigger('click');
            }
            notify(msg);
            return false;
        }
        return true;
    }
    
    return false;
    
}

function check_all_check() {
    if ($('input.permission').length == $('input.permission:checked').length) {
        $('input.check_all').iCheck('check');
    }
}
function check_all_uncheck() {
    /*$('input.permission').each(function(index, element) {
        $(element).prop('checked', false);
        $(element).parent().attr('aria-checked', false).removeClass('checked');
    });*/
    
    if ($('input.check_all').is(':checked')) {
        $('input.check_all').prop('checked', false);
        $('input.check_all').parent().attr('aria-checked', false).removeClass('checked');
    }
}

function check_add_all_check() {
    if ($('input.add').length == $('input.add:checked').length) {
        $('input.add_all').iCheck('check');
    }
}

function check_add_all_uncheck() {
    if ($('input.add_all').is(':checked')) {
        $('input.add_all').prop('checked', false);
        $('input.add_all').parent().attr('aria-checked', false).removeClass('checked');
    }
}

function check_edit_all_check() {
    if ($('input.edit').length == $('input.edit:checked').length) {
        $('input.edit_all').iCheck('check');
    }
}

function check_edit_all_uncheck() {
    if ($('input.edit_all').is(':checked')) {
        $('input.edit_all').prop('checked', false);
        $('input.edit_all').parent().attr('aria-checked', false).removeClass('checked');
    }
}

function check_view_all_check() {
    if ($('input.view').length == $('input.view:checked').length) {
        $('input.view_all').iCheck('check');
    }
}

function check_view_all_uncheck() {
    if ($('input.view_all').is(':checked')) {
        $('input.view_all').prop('checked', false);
        $('input.view_all').parent().attr('aria-checked', false).removeClass('checked');
    }
}

function check_delete_all_check() {
    if ($('input.delete').length == $('input.delete:checked').length) {
        $('input.delete_all').iCheck('check');
    }
}

function check_delete_all_uncheck() {
    if ($('input.delete_all').is(':checked')) {
        $('input.delete_all').prop('checked', false);
        $('input.delete_all').parent().attr('aria-checked', false).removeClass('checked');
    }
}


function show_user_role_menus(_this) {
    if ($(_this).val() == '2') {
        $('.permission_container .user').slideUp();
        $('.permission_container .administrator').slideDown();
        /* check_all_uncheck();
        check_add_all_uncheck();
        check_edit_all_uncheck();
        check_view_all_uncheck();
        check_delete_all_uncheck();
        $('input.check_all').trigger('ifUnchecked'); */
    }
    else if ($(_this).val() == '1') {
        $('.permission_container .administrator').slideUp();
        $('.permission_container .user').slideDown();
        /* check_all_uncheck();
        check_add_all_uncheck();
        check_edit_all_uncheck();
        check_view_all_uncheck();
        check_delete_all_uncheck();
        $('input.check_all').trigger('ifUnchecked'); */
    }
    else {
        $('.permission_container .administrator').slideDown();
        $('.permission_container .user').slideDown();
    }
}

$(document).ready(function() {
    show_user_role_menus($('input[name="role"]:checked'));
    
    $('input[name="role"]').on('ifChecked', function(event){
        show_user_role_menus($(this));
    });
    
    
    $('input.check_all').on('ifChecked', function(event){
        $('.permission_container input[type="checkbox"]').each(function(key, element) {
            $(this).iCheck('check');
        });
    });
    
    
    $('input.check_all').on('ifUnchecked', function(event){
        $('.permission_container input[type="checkbox"]').each(function(key, element) {
            $(this).iCheck('uncheck');
        });
    });
    
    $('input.add_all').on('ifChecked', function(event){
        $('.permission_container .add').each(function(key, element) {
            $(this).iCheck('check');
        });
    });
    
    $('input.add_all').on('ifUnchecked', function(event){
        $('.permission_container .add').each(function(key, element) {
            $(this).iCheck('uncheck');
        });
    });
    
    $('input.edit_all').on('ifChecked', function(event){
        $('.permission_container .edit').each(function(key, element) {
            $(this).iCheck('check');
        });
    });
    
    $('input.edit_all').on('ifUnchecked', function(event){
        $('.permission_container .edit').each(function(key, element) {
            $(this).iCheck('uncheck');
        });
    });
    
    $('input.view_all').on('ifChecked', function(event){
        $('.permission_container .view').each(function(key, element) {
            $(this).iCheck('check');
        });
    });
    
    $('input.view_all').on('ifUnchecked', function(event){
        $('.permission_container .view').each(function(key, element) {
            $(this).iCheck('uncheck');
        });
    });
    
    $('input.delete_all').on('ifChecked', function(event){
        $('.permission_container .delete').each(function(key, element) {
            $(this).iCheck('check');
        });
    });
    
    $('input.delete_all').on('ifUnchecked', function(event){
        $('.permission_container .delete').each(function(key, element) {
            $(this).iCheck('uncheck');
        });
    });
    
    $('input.master').on('ifChecked', function(event){
        /*var masters = $(this).attr('data-master').replace(/,$/g, "").split(',');
        var masters_length = masters.length;
        for(var i = 0; i < masters_length; i ++) {
            $('input[name="permissions[' + masters[i].trim() + '][]').iCheck('check');
        }*/
        $(this).closest('.master_container').find('input[type="checkbox"]').iCheck('check');
        
        var permission_row = $(this).closest('.permission_row');
        if ($(permission_row).find('input.master:not(input.super_master)').length == $(permission_row).find('input.master:not(input.super_master):checked').length) {
            $(permission_row).find('input.super_master').iCheck('check');
        }
        //check_all_check();
    });
    
    $('input.master').on('ifUnchecked', function(event){
        /*var masters = $(this).attr('data-master').replace(/,$/g, "").split(',');
        var masters_length = masters.length;
        for(var i = 0; i < masters_length; i ++) {
            $('input[name="permissions[' + masters[i].trim() + '][]').iCheck('uncheck');
        }*/
        $(this).closest('.master_container').find('input[type="checkbox"]').iCheck('uncheck');
        
        var permission_row = $(this).closest('.permission_row');
        if ($(permission_row).find('input.super_master').prop('checked') == true) {
            $(permission_row).find('input.super_master').prop('checked', false);
            $(permission_row).find('input.super_master').parent().attr('aria-checked', false).removeClass('checked');
        }
        //check_all_uncheck();
    });
    
    $('input.permission').on('ifChecked', function(event) {
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input[type="checkbox"]:not(input.master)').length == $(master_container).find('input[type="checkbox"]:not(input.master):checked').length) {
            if ($(master_container).find('input.master').length > 0) {
                $(master_container).find('input.master').iCheck('check');
            }
            //else {
                check_all_check();
            //}
            
            if($(this).attr('data-master') == 'day_end_report') {
                $('input.daily_summary_report').iCheck('check');
            }
        }
    });
    
    $('input.permission').on('ifUnchecked', function(event){
        //var master = $(this).attr('data-master');
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input.master:checked').length > 0) {
            $(master_container).find('input.master').prop('checked', false);
            $(master_container).find('input.master').parent().attr('aria-checked', false).removeClass('checked');
            
            var permission_row = $(this).closest('.permission_row');
            if ($(permission_row).find('input.super_master').prop('checked') == true) {
                $(permission_row).find('input.super_master').prop('checked', false);
                $(permission_row).find('input.super_master').parent().attr('aria-checked', false).removeClass('checked');
            }
        }
        if($(this).attr('data-master') == 'day_end_report') {
            $('input.daily_summary_report').prop('checked', false);
            $('input.reports').parent().attr('aria-checked', false).removeClass('checked');
            $('input.daily_summary_report').prop('checked', false);
            $('input.daily_summary_report').parent().attr('aria-checked', false).removeClass('checked');
            
        }
        check_all_uncheck();
    });
    
    
    $('input.add').on('ifChecked', function(event) {
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input[type="checkbox"] input.add').length == $(master_container).find('input[type="checkbox"] input.add:checked').length) {
            if ($(master_container).find('input.add').length > 0) {
                $(master_container).find('input.add_all').iCheck('check');
            }
            //else {
                check_add_all_check();
            //}
        }
    });
    
    $('input.add').on('ifUnchecked', function(event){
        //var master = $(this).attr('data-master');
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input.add:checked').length > 0) {
            $(master_container).find('input.add_all').prop('checked', false);
            $(master_container).find('input.add_all').parent().attr('aria-checked', false).removeClass('checked');
        }
        check_add_all_uncheck();
    });
    
    $('input.edit').on('ifChecked', function(event) {
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input[type="checkbox"] input.edit').length == $(master_container).find('input[type="checkbox"] input.edit:checked').length) {
            if ($(master_container).find('input.edit').length > 0) {
                $(master_container).find('input.edit_all').iCheck('check');
            }
            //else {
                check_edit_all_check();
            //}
        }
    });
    
    $('input.edit').on('ifUnchecked', function(event){
        //var master = $(this).attr('data-master');
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input.edit:checked').length > 0) {
            $(master_container).find('input.edit_all').prop('checked', false);
            $(master_container).find('input.edit_all').parent().attr('aria-checked', false).removeClass('checked');
        }
        check_edit_all_uncheck();
    });
    
    
    $('input.view').on('ifChecked', function(event) {
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input[type="checkbox"] input.view').length == $(master_container).find('input[type="checkbox"] input.view:checked').length) {
            if ($(master_container).find('input.view').length > 0) {
                $(master_container).find('input.view_all').iCheck('check');
            }
            //else {
                check_view_all_check();
            //}
        }
    });
    
    $('input.view').on('ifUnchecked', function(event){
        //var master = $(this).attr('data-master');
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input.view:checked').length > 0) {
            $(master_container).find('input.view_all').prop('checked', false);
            $(master_container).find('input.view_all').parent().attr('aria-checked', false).removeClass('checked');
        }
        check_view_all_uncheck();
    });
    
    $('input.delete').on('ifChecked', function(event) {
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input[type="checkbox"] input.delete').length == $(master_container).find('input[type="checkbox"] input.delete:checked').length) {
            if ($(master_container).find('input.delete').length > 0) {
                $(master_container).find('input.delete_all').iCheck('check');
            }
            //else {
                check_delete_all_check();
            //}
        }
    });
    
    $('input.delete').on('ifUnchecked', function(event){
        //var master = $(this).attr('data-master');
        var master_container = $(this).closest('.form-group').parent();
        if ($(master_container).find('input.delete:checked').length > 0) {
            $(master_container).find('input.delete_all').prop('checked', false);
            $(master_container).find('input.delete_all').parent().attr('aria-checked', false).removeClass('checked');
        }
        check_delete_all_uncheck();
    });
    
    $.validator.addMethod("check_permission", function(value, element) {
        return ($(".permission_container input[type='checkbox']:checked").length > 0)
    });
    
    $.validator.addMethod("password_match", function(value, element) {
        return (value == $('input[name="new_password"]').val());
    });
     
    $.validator.addMethod("user_name_exists", function(value, element) {
        var return_val = true;
        var user_name = $('input[name="user_name"]').val();
        var user_id = $('input[name="user_id"]').val();
        if (user_name != '' ) {
            $.ajax({
                async: false,
                url: base_url + 'user/user_name_exists',
                type: 'POST',
                data: {
                    user_name: user_name,
                    user_id: user_id
                },
                dataType: 'JSON'
            })
            .done(function(data) {
                if (data.user_name_exists) {
                    return_val = false;
                }
            })
            .fail(function(jqXHR, status_code, error_thrown) {
                //alert(error_thrown);
            });
        }
        
        return return_val;
    });
    
    $('form[name="user"]').validate({
        errorElement: 'li',
        errorLabelContainer: '.error_container',
        /*errorPlacement: function(error, element) {
            error.insertAfter(element);
        },*/
        validClass: 'valid',
        focusInvalid: true,
        rules: {
            name: {
                required: true,
                alphabets: true
            },
            /* contact_no: {
                required: true
            }, */
            user_name: {
                required: true,
                //user_name_exists: true
            },
            password: {
                required: true,
                check_permission: true
            }
        },
        messages: {
            name: {
                required: 'Please enter Name!',
                alphabets: 'Name must be alphabets only!'
            },
            /* contact_no: {
                required: 'Please enter Contact No!'
            }, */
            user_name: {
                required: 'Please enter User Name!',
                user_name_exists: 'User Name already Exists!'
            },
            password: {
                required: 'Please enter Password!',
                check_permission: 'Please select atleast one permission!'
            }
        }
    });
    
    $('form[name="change_password"]').validate({
        errorElement: 'li',
        errorLabelContainer: '.error_container',
        focusInvalid: true,
        rules: {
            new_password: {
                required: true
            },
            confirm_password: {
                required: true,
                password_match: true
            }
        },
        messages: {
            new_password: {
                required: 'Please Enter New Password!'
            },
            confirm_password: {
                required: 'Please Enter Confirm Password!',
                password_match: 'New Password and Confirm Password does not match!'
            }
        }
    });
    
    $('.compress_btn').on('click', function() {
        if ($(this).hasClass('compress')) {
            $(this).closest('.panel').find('.panel-body').slideUp();
            $(this).find('.fa-compress').removeClass('fa-compress').addClass('fa-expand');
            $(this).removeClass('compress').addClass('expand');
        }
        else {
            $(this).closest('.panel').find('.panel-body').slideDown('slow');
            $(this).find('.fa-expand').removeClass('fa-expand').addClass('fa-compress');
            $(this).removeClass('expand').addClass('compress');
        }
    });
});