<script type="text/javascript">
	function getAssignPermission(event,element)
	{
		event.preventDefault();

		element = $(element);
        showLoader();
		$.get(element.attr('href'),function(data)
		{
    		$('.show_assign_permission').html(data);

    		$('#md_assign_permission').modal('show');

            removeLoader();

  		});
	}

	function assignPermission(element,user,permission,permission_name)
	{
		event.preventDefault();
          
		var route = 'permissionassign/'+user+'/'+permission;

		element = $(element);
        showLoader();
		$.get(route,function(data)
		{

    		if(data == "checked")
    		{
    			messageToaster('success',permission_name+' permison has granted.','Success');

    			element.prop('checked',true);
    		}
    		else if(data == "unchecked")
    		{
    			messageToaster('success',permission_name+' grant has  cancelled.','Success');
    			element.prop('checked',false);

    		}
    		else
    		{
    			messageToaster('error','Try Again!','Failed');

    			if(element.checked)
    			{
    				element.prop('checked',false);
    			}
    			else
    			{
    				element.prop('checked',true);
    			}

    		}

            var module_name = element.attr('class').split("_")[0];

            $('.'+module_name+'_module').prop('checked',verifySetOfPermission($('.'+element.attr('class'))));

            removeLoader();


  		},'json');
	}

    function assignModulePermission(element,user,specfic_module,module_name)
    {
        event.preventDefault();

        element = $(element);

        permission_list = $('.'+module_name+'_permission');

        var flag = false;

        if(element.prop('checked'))
        {
            flag = true;
        }

        var route = 'modulepermission/'+user+'/'+specfic_module+'/'+flag;

        showLoader();

        $.get(route,function(data)
        {
            if(data == true && flag == true)
            {
                messageToaster('success',module_name+' permisson has granted.','Success');

                permission_list.prop('checked',true);
            }
            else if(data == true && flag == false)
            {
                messageToaster('success',module_name+' grant has cancelled.','Success');

                permission_list.prop('checked',false);

            }
            else
            {
                messageToaster('error','Try Again!','Failed');

                if(element.checked)
                {
                    element.prop('checked',false);
                    permission_list.prop('checked',false);
                }
                else
                {
                    element.prop('checked',true);
                    permission_list.prop('checked',true);
                }

            }

        removeLoader();
            
        },'json');
    }

    function verifySetOfPermission(element)
    {
        var flag         = false;
        var actual_size  = 0;
        var counted_size = 0;

        element.each(function(event){


            if(this.checked)
            {
                counted_size++;
            }

            actual_size++;

        });

        if(counted_size == actual_size)
        {
            flag = true;
        }

        return flag;
    }

</script>