	// JavaScript Document
	/*JS function for Permisions*/
			
			/*FUNCTIONS FOR MENU CONTROL
			The functions have been modified to cater for DEFAULT PERMISSIONS too*/
			
		//for super admin to control permissions for corporations					  					  												
		$("#corporation").on("change", function() {					  
		
		 ajaxRequestPermissionsGranted($(this).find("option:selected").val(),"corporation");
		 ajaxRequestAvailableModules($(this).find("option:selected").val(),"corporation");
		 
		 $("#corporation2").val( $(this).find("option:selected").text() );												
		});													  
															
	   $("#corporation").select2({
		   placeholder: "Select Corporation"
		   });
		   
	  //for admins to control permissions for users					  					  												
		$("#user").on("change", function() {					  
		
		 ajaxRequestPermissionsGranted($(this).find("option:selected").val(),"user");
		 ajaxRequestAvailableModules($(this).find("option:selected").val(),"user");
		 
		 $("#user2").val( $(this).find("option:selected").text() );												
		});													  
															
	   $("#user").select2({
		   placeholder: "Select User"
		   });
	   
	  //set the css3 blur to an element
	  function blurElement(element, size)
	  {
		  var filterVal = 'blur('+size+'px)';
		  $(element)
			.css('filter',filterVal)
			.css('webkitFilter',filterVal)
			.css('mozFilter',filterVal)
			.css('oFilter',filterVal)
			.css('msFilter',filterVal);
		  }
	  //set the css3 unblur to an element
	  function unblurElement(element)
	  {
		  var filterVal = 'none';
		  $(element)
			.css('filter',filterVal)
			.css('webkitFilter',filterVal)
			.css('mozFilter',filterVal)
			.css('oFilter',filterVal)
			.css('msFilter',filterVal);
		  }			
			  
	 function ajaxRequestPermissionsGranted(id,type) 
	 {
		blurElement("#permissions_granted", 2);				  
		var id = id;
		var type = type;
		var postForm = {'id' : id,'type' : type};
		$.ajax({ 
			type 		: 'POST', 
			url 		: url+'permissions/return_permissions_granted_per_corporation_or_user', 
			data 		: postForm, 
			//dataType 	: 'json',
			success 	: function(reponse) 
						{											 
						   document.getElementById("permissions_granted").innerHTML=reponse;	
							unblurElement("#permissions_granted");								
						}
		});
	}
	
	function ajaxRequestAvailableModules(id,type) 
	{	
		blurElement("#available_modules", 2);				  
		var id = id;
		var postForm = {'id' : id,'type' : type};
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/return_all_available_modules',  
			data 		: postForm, 
			//dataType 	: 'json',
			success 	: function(reponse) 
						{	
						   document.getElementById("available_modules").innerHTML=reponse;
						   unblurElement("#available_modules");								
						}
		});					
	}
	  
	function grantModulePermission(menu_id,type)
	{					  
		var menu_id = menu_id;
		var type = type;
		
		if(type == "corporation")
		{
		  var id = $("#corporation").find("option:selected").val();
		}
		else
		{
		  var id = $("#user").find("option:selected").val();
		}					  
		
		var form_data = {'id':id ,'menu_id':menu_id,'type':type};
		console.log(form_data);
		
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/insert_corporation_orUser_modules', 
			data 		: form_data, 
			//dataType 	: 'json',
			success 	: function(data) 
						{	
						   ajaxRequestPermissionsGranted(id,type);
						   ajaxRequestAvailableModules(id,type);								
						}
		});				
		
	}
	
	function grantModuleAlert(menu_id,type)
	{
		  var menu_id = menu_id;
		  //event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
		  $.jAlert({
			  'title': 'Grant Module Permission',
			  'content': 'Confirm you wish to grant module access to this user!',
			  'theme': 'blue',
			  'size': 'sm',
			  'showAnimation': 'fadeInUp',
			  'hideAnimation': 'fadeOutDown',
			  'btns': {
						  'text': 'Grant Access',
						  'theme': 'green',
						  'onClick': function(e,btn){e.preventDefault();grantModulePermission(menu_id,type);}
						}
			   
		 });
		
	}
	
	function submitCheckedSubMenu(module_id,type)
	{	
		if(type == "corporation")
		{
		  var id = $("#corporation").find("option:selected").val();
		}
		else if(type == "user")
		{
		  var id = $("#user").find("option:selected").val();
		}
		else if(type == "corporations_default" || type == 'corporation_staff' || type == 'corporation_member')
		{
		  var id = null;
		}		
		
		var type = type;
		var module_id = module_id;
		var parentDivId = 'checkbox'+module_id;
		
		var checkboxValues = [];
		$('#'+ parentDivId +' input[type="checkbox"]:checked').each(function(index, elem) {
			checkboxValues.push($(elem).val());
		});
		var submenu = checkboxValues.toString();
		console.log(submenu);
		console.log(id,module_id,type);
		
		var form_data = {'id': id,'module_id': module_id,'submenu': submenu,'type': type};
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/update_submenu_permission_forAjax', 
			data 		: form_data, 
			success 	: function(data) 
						{
							console.log("success");
						}
		});
				
	}
	
	function removeModulePermission(id,type)
	{
		if(type == "corporation")
		{
		   var subject_id = $("#corporation").find("option:selected").val();
		}
		else if(type == "user")
		{
		   var subject_id = $("#user").find("option:selected").val();
		}
		else if(type == "corporations_default" || type == 'corporation_staff' || type == 'corporation_member')
		{
			var subject_id = null;
		}
		
		var id = id;
		var type = type;					  					  
		var form_data = {'id': id,'type': type};					  
		
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/delete_module_permission', 
			data 		: form_data, 
			success 	: function(data) 
						{	
						   ajaxRequestPermissionsGranted(subject_id,type);
						   ajaxRequestAvailableModules(subject_id,type);								
						}
		});				
		
	}
	
	function removeModuleAlert(id,type)
	{
		  var id = id;
		  var type = type;
		  //event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
		  $.jAlert({
			  'title': 'Remove Module Permission',
			  'content': 'Are you sure you want to remove access to this module?',
			  'theme': 'red',
			  'size': 'sm',
			  'showAnimation': 'fadeInUp',
			  'hideAnimation': 'fadeOutDown',
			  'btns': {
						  'text': 'Remove Access',
						  'theme': 'green',
						  'onClick': function(e,btn){e.preventDefault();removeModulePermission(id,type);}
						}
			   
		 });
		
	}
	
	function selectAll(id,module_id,type)
	{
		//event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
		var module_id = module_id;
		var id = id;
		var type = type;
		var class_name = document.getElementById('check-all'+id).className;
		
		if(class_name == 'select-all')
		{
		   document.getElementById('check-all'+id).className = 'select-all-checked';
		   checkAll(module_id,type); 
		}
		else if(class_name == 'select-all-checked')
		{
		   document.getElementById('check-all'+id).className =  'select-all';
		   unCheckAll(module_id,type);
		}	 
		
	}	
	
	function checkAll(module_id,type)	
	{				  
		var module_id = module_id;
		var type = type;					  
		var form_data = {'module_id': module_id,'type': type};					  
		
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/return_submenu_for_module', 
			data 		: form_data, 
			success 	: function(data) 
						{	
						   //document.getElementById("array").innerHTML = data;
						   console.log(data);
						   var values = JSON.parse(data)
						   for(i in values)
						   {											
							  $("#chkbox"+values[i]).prop("checked", true);
							  console.log(values[i]);											
						   }
		   submitCheckedSubMenu(module_id,type);
						}
		});
	}	
	
	function unCheckAll(module_id,type)	
	{					  
		var module_id = module_id;
		var type = type;					  
		var form_data = {'module_id': module_id,'type': type};					  
		
		$.ajax({ 
			type 		: 'POST',
			url 		: url+'permissions/return_submenu_for_module', 
			data 		: form_data, 
			success 	: function(data) 
						{	
						   //document.getElementById("array").innerHTML = data;
						   console.log(data);
						   var values = JSON.parse(data)
						   for(i in values)
						   {
							  $("#chkbox"+values[i]).prop("checked", false);
							  console.log(values[i]);
						   }
		   submitCheckedSubMenu(module_id,type);
						}
		});	
	}
	 
	
	function unCheckAll2(module_id)	
	{					  
		var module_id = module_id;
		var parentDivId = 'checkbox'+module_id;
		
		console.log(parentDivId);
		$("#chkbox9").prop('checked', $(this).prop("checked"));
		//$("#"+ parentDivId +" input:checkbox").prop('checked', $(this).prop("checked"));
			
		
		//var checkboxValues = [];
	//					  $('#'+ parentDivId +' input[type="checkbox"]:checked').each(function(index, elem) {
	//						  checkboxValues.push($(elem).val());
	//						  
	//					  });
	//					  var submenu = checkboxValues.toString();
	//					  console.log(submenu);
	//					  console.log(corporation_id,module_id);
	}
	
	
	
	
	function ajaxUpdateSubmenu(id,submenu) 
	 {					  				  
		var id = id;
		var submenu = submenu;
		var postForm = {'id' : id,'sub_menu' : submenu};
		$.ajax({ 
			type 		: 'POST', 
			url 		: url+'permissions/update_submenu_permission_forAjax', 
			data 		: postForm, 
			//dataType 	: 'json',
			success 	: function(reponse) 
						{											 
						   //document.getElementById("permissions_granted").innerHTML=reponse;
							document.getElementById("array").innerHTML = "Updated";
							console.log(id);
							console.log(submenu);								
						}
		});
	}   
	
	/*
	Not Sure what the code was used for. Commented instead of deleting.
	</script>',
	'<script type="text/javascript">
	$("#check-all12").change(function () {
	$("input:checkbox").prop('checked', $(this).prop("checked"));
	});
	</script>'*/	
	
	
	
	/*FUNCTIONS FOR ACCOUNT ACTIVATION*/
	//function called in activate account which uses jAlert(as name suggests)				  
	function activateAccountForJAlert(type,id)
	{
		  var type = type;
		  var id = id;					  					  
		  var form_data = {'type': type,'id': id};					  
		  //console.log(form_data);
		  $.ajax({ 
			  type 		: 'POST',
			  url 		: url+'permissions/activate_account', 
			  data 		: form_data, 
			  success 	: function(data) 
						  {	
						  	if(type == 'user')
							{
							 	$('#status'+id).removeClass('red').addClass('green').text('Active');
							}
							else
								window.location.href = url+"permissions/account_activation";
						  }
		  });	
	}
	
	function activateAccount(type,id)
	  {
			var type = type;
			var id = id;
			//event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
			$.jAlert({
				'title': 'Activate User Account',
				'content': 'Are you sure you want to activate the user account?',
				'theme': 'blue',
				'size': 'sm',
				'showAnimation': 'fadeInUp',
				'hideAnimation': 'fadeOutDown',
				'btns': {
							'text': 'Activate Account',
							'theme': 'green',
							'onClick': function(e,btn){e.preventDefault();activateAccountForJAlert(type,id);}
						  }
				 
		   });					  
	  }
	
	//function called in deactivate account which uses jAlert(as name suggests)
	function deactivateAccountForJAlert(type,id)
	{
		  var type = type;
		  var id = id;					  					  
		  var form_data = {'type': type,'id': id};					  
		 // console.log(form_data);
		  $.ajax({ 
			  type 		: 'POST',
			  url 		: url+'permissions/deactivate_account', 
			  data 		: form_data, 
			  success 	: function(data) 
						  {	
							 if(type == 'user')
							{
							 	$('#status'+id).removeClass('green').addClass('red').text('Inactive');
							}
							else
								window.location.href = url+"permissions/account_activation";
							 							
						  }
		  });	
	}
	
	function deactivateAccount(type,id)
	  {
			var type = type;
			var id = id;
			//event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
			$.jAlert({
				'title': 'Deactivate User Account',
				'content': 'Are you sure you want to deactivate the user account?',
				'theme': 'red',
				'size': 'sm',
				'showAnimation': 'fadeInUp',
				'hideAnimation': 'fadeOutDown',
				'btns': {
							'text': 'Deactivate Account',
							'theme': 'green',
							'onClick': function(e,btn){e.preventDefault();deactivateAccountForJAlert(type,id);}
						  }
				 
		   });					  
	  }
	  
	  
	  
	  /*DEFAULT FUNCTIONS PERMISSIONS*/
	   //for admins to control default permissions for user levels					  					  												
		$("#userLevel").on("change", function() {					  
		
			var user_type = $(this).find("option:selected").val();
			if(user_type == 3)
			{
				ajaxRequestPermissionsGranted(user_type,"corporation_staff");
				ajaxRequestAvailableModules(user_type,"corporation_staff");
			}
			if(user_type == 4)
			{
				ajaxRequestPermissionsGranted(user_type,"corporation_member");
				ajaxRequestAvailableModules(user_type,"corporation_member");
			}
			
			//shows the selected option on the readonly textfield
		 	$("#userLevel2").val( $(this).find("option:selected").text() );												
		});													  
															
	   $("#userLevel").select2({
		   placeholder: "Select User Level"
		   });
		   
		//the suffix DP(stands for default permissions) is to distinguish Default Permissions functions from Others
		function grantModulePermissionDP(menu_id,type)
		{					  
			var menu_id = menu_id;
			var type = type;
			var form_data = {'type': type,'menu_id': menu_id};
			console.log(form_data);
			
			$.ajax({ 
				type 		: 'POST',
				url 		: url+'permissions/insert_default_permissions', 
				data 		: form_data, 
				//dataType 	: 'json',
				success 	: function(data) 
							{	
							   ajaxRequestPermissionsGranted(null,type);
							   ajaxRequestAvailableModules(null,type);						
							}
			});				
			
		}
	
		function grantModuleAlertDP(menu_id,type)
		{
			  var menu_id = menu_id;
			  var type = type;
			  //event.preventDefault();//throws error on mozilla: ReferenceError: event is not defined
			  $.jAlert({
				  'title': 'Grant Module Permission',
				  'content': 'Confirm you wish to add this module to Default Permissions!',
				  'theme': 'blue',
				  'size': 'sm',
				  'showAnimation': 'fadeInUp',
				  'hideAnimation': 'fadeOutDown',
				  'btns': {
							  'text': 'Add Module',
							  'theme': 'green',
							  'onClick': function(e,btn){e.preventDefault();grantModulePermissionDP(menu_id,type);}
							}
				   
			 });
			
		}
		