/****************************** INSERT & UPDATE ***************************************/
function userrole_cu(userroll_id,action)
{
	$('button').attr('disabled','disabled');
		var checkboxes = $('.checkbox');
		var checkboxesChecked = [];
  
  		for(var i=0; i<checkboxes.length; i++) {
     		if(checkboxes[i].checked) {
				var checkedid = checkboxes[i].id;				
		        checkboxesChecked.push($('#'+checkedid).attr('data-id'));
        	}
  		}
			
		format=$("form").serialize()+"&userroll_id="+userroll_id+"&checkboxesChecked="+checkboxesChecked+"&action="+action;
		
		jQuery.ajax({
			type: "POST",
			url: "userroll/curd.php",
			data: format,
			success: function(msg){ 
			
			         if(msg !='1')
					{
						console.log(msg);
						$("#distinct_error").text("Invalid Data");
						return false;
					}
					else
					{
						console.log(msg);
						window.location.href="index1.php?fopen=userroll/list";						
					}
			
			}});
	

}
function del(userroll_id)
{
	value=confirm("Are Sure You Want Delete?");
	if(value){
	  jQuery.ajax({
			type: "POST",
			url: "userroll/curd.php",
			data: "userroll_id="+userroll_id+"&action="+"Delete",
			success: function(msg){ 
			console.log(msg);
			location.reload();
			}});
	}

}

$(document).ready(function(){	

$('.checkbox').on('click', function(){
// 	debugger;
		/*************ROOT-CHILD-SUBchild************************/
		/***Childs and parent be selected*******/
		var name = $(this).attr('name');
		var data_name = $(this).attr('data-name');
		if(!this.checked){
			/***Sub Child be removed check child ****/
			$('.'+name).prop('checked', false);
			var checkname = $(this).attr('data-name');
			var checkedcount = $('.'+checkname).filter(':checked').length;
			if(checkedcount == '1'){
				/*****Make child unchecked*******/
				$('.'+checkname).prop('checked', false);
			}
			
			/****check Child count - parent remove******/
			//var data = ($(this).parents('li').children('input[type=checkbox]'));
			var strArray = data_name.split("-");			
			var data = $('.'+strArray[1]);	
			var datacount = data.length;
			var data_count = parseInt(datacount)-1;	
			//console.log(data);		
			var checkedval = data[data_count].value;		
			var valcount = '';//$('.'+data).filter(':checked').length;
			var ckeckcount = 0;
			for(var i = 0; i < data.length; i++){
				valcount = $(data[i]).filter(':checked').length;
				ckeckcount = parseInt(ckeckcount)+parseInt(valcount);
			}
			//console.log(ckeckcount);
			
			if(ckeckcount == '1'){
				/*****Make root unchecked*******/
				$(data[0]).prop('checked', false);
			}
			
		}else{
			$('.'+name).prop('checked', true);
			var strArray = data_name.split("-");
			$('#'+data_name).prop('checked', true);
			$('#'+strArray[1]).prop('checked', true);			
			$('.'+name).parents('li').children('input[type=checkbox]').prop('checked',true);			
			
			/*var array = $.map(data, function(value, index) {
				return [value];
			});
			alert(datacount+array[2].value);
			console.log(array);*/
		}	
});

});

