/* --------------------------------------------------------------------------- */			
function clear_value( element_id ) {
	$( element_id ).val("");		
} 
/* --------------------------------------------------------------------------- */
function disable_form_on_enter(element_id){
	$(element_id).on("keypress", function (event) {
		var keyPressed = event.keyCode || event.which;
		if (keyPressed === 13) {
			//alert("You pressed the Enter key!!");
			event.preventDefault();
			return false;
		}
	});
};
/* --------------------------------------------------------------------------- */
function modal_ajax_process ( title, button, step, form_id, notify = 'modal', notify_div = '' ) {
	
	$('#Modal_1 .modal_button_1').removeAttr('disabled');
	
	$('#Modal_1 .modal-title').html( title );
	
	$.ajax({
		url: step[0],
		method: 'POST',
		success: function( result ) {
			//console.log(result);
			
			$('#Modal_1 .modal-body').empty();
			$('#Modal_1 .modal-body').html(result);
			
			disable_form_on_enter('form input');
			// REMOVE PREVIOUS CLICK FUNCTION AND ADD NEW ONE
			
			$('#Modal_1 .modal_button_1').unbind('click'); 
			
			$('#Modal_1 .modal_button_1').on('click').click(function() {
				
				var form = $(form_id); 
				//alert ( form_id + ' = ' + form.serialize() );
				
				if(!$(form_id)[0].checkValidity())
				{
					$(form_id)[0].reportValidity();		
					//$(form_id).find("#submit-hidden").click();
					return false;						
				}
				
				$('#Modal_1 .modal_button_1').attr('disabled','disabled');
				
				let formToWorkOn = document.querySelector(form_id);
				var formData = new FormData(formToWorkOn);
				$.ajax({
					url: step[1],
					method: 'POST',
					//data: form.serialize(),
					data: formData,
					async: true,
					cache: false,
					contentType: false,
					processData: false,
					
					success: function( result,textStatus, jqXHR ) {
						
						
						//alert(result);
						
						if (step[2]) { eval( step[2] );	}
						
						// ***** START - BRING THIS INTO PANEL *****
						if ( notify == 'modal') {							
							
							// SHOW FINAL NOTIFICATION MODAL
							//modal_alert( result, 'ok', true);							
							if(jqXHR.status==200){
								modal_alert( result, 'ok', true);
							}else if(jqXHR.status==250){
								modal_alert( result, 'warn', true);
							}else if(jqXHR.status==260){
								modal_alert( result, 'error', true);
							}else{
								modal_alert( result, 'ok', true);
							}
						
						} else if ( notify == 'inline') {	
							
							// SHOW FINAL NOTIFICATION MESSAGE INLINE
							if(jqXHR.status==200){
								inline_alert( result, 'ok', notify_div);
							}else if(jqXHR.status==250){
								inline_alert( result, 'warn', notify_div);
							}else if(jqXHR.status==260){
								inline_alert( result, 'error', notify_div);
							}else{
								inline_alert( result, 'error', notify_div);
							}
							
							/*
							$(notify_div).html(result);
							autoCloseAlert();
							*/
							$('#Modal_1').modal('hide');
							$('#Modal_2').modal('dispose');	
							
							
						} else if ( notify == 'none') {							
							
							// SKIP FINAL NOTIFICATION MODAL
							
							$('#Modal_1').modal('hide');
							$('#Modal_2').modal('dispose');
															
						}						
						// ***** END - BRING THIS INTO PANEL *****
						
					},
					error: function( xhr, status, error ) {
						
						if (xhr.status == 404 || xhr.status == 400) {					
							modal_alert( xhr.responseText, 'error');		
						}else{
							modal_alert( xhr.responseText, 'warn');		
						}						
					},
				});										
			});				
		},			
		error: function( xhr, status, error ) {
			
			if (xhr.status == 404 || xhr.status == 400) {					
				modal_alert( xhr.responseText, 'error');		
			}else{
				modal_alert( xhr.responseText, 'warn');		
			}									
		},
	}); //END-AJAX()
	$('#Modal_1 .modal_button_1').html( button[0] );
	$('#Modal_1 .modal_button_2').html( button[1] );
	
	$('#Modal_1').modal('show');
}
/* --------------------------------------------------------------------------- */
function modal_alert( message, status, refresh=false ) {
	if ( status == "error" ) {
		color = '#FF0000';
		icon  = '';
		text  = 'Error';
		$('#Modal_2 .modal-header').css('color', '#000000');
		$('#Modal_2 .modal-header').css('background-color', '#FF0000');
		$('#Modal_2 .modal-title').html(icon +' '+ text);	
		$('#Modal_2 .modal-body').html( message );			
		$('#Modal_2 .modal_button_1').html(icon +' OK');
	}
	if ( status == "warn" ) {
		color = '#FF0000';
		icon  = '';
		text  = 'Warning';
		 
		$('#Modal_2 .modal-header').css('color', '#000000');
		$('#Modal_2 .modal-header').css('background-color', '#FFFF00');
		$('#Modal_2 .modal-title').html(icon +' '+ text);
		$('#Modal_2 .modal-body').html( message );
		
		$('#Modal_2 .modal_button_1').html(icon +' OK');
	}
	if ( status == "ok" ) {
		color = '#00FF00';
		icon  = '';
		text  = 'Success';
		
		$('#Modal_2 .modal-header').css('color', '#FFFFFF');
		$('#Modal_2 .modal-header').css('background-color', '#00FF00');
		$('#Modal_2 .modal-title').html(' Success');
		$('#Modal_2 .modal-body').html( message );
		
		$('#Modal_2 .modal_button_1').html(' OK');
		if ( refresh ) {
			$('#Modal_2').on('hidden.bs.modal', function (e) {
				//location.reload(true);
				$('#Modal_1').modal('dispose');
				$('#Modal_2').modal('dispose');
			});
		}
	}
	
	$('#Modal_1').modal('hide');	
	$('#Modal_2').modal('show');
	$('#Modal_3').modal('hide');
}
/* --------------------------------------------------------------------------- */
function inline_alert( message, status, notify_div ) {
	
		if ( status == "error" ) {
			var errorClass = 'alert-danger';
		}else if ( status == "warn" ) {
			var errorClass = 'alert-warning';
		}else if ( status == "ok" ) {
			var errorClass = 'alert-success';
		}
		
		
		var alert_message = '';
		alert_message += '
';
		alert_message += '
';
		alert_message += '
';
		alert_message += message;
		alert_message += '
';
		alert_message += '
';
					  
					});
					
					if ( notify == 'modal') 
					{	
						modal_alert( error_string, 'error' );
					}
					
				}
				else
				{
					return true;
				}
				
			},
			error: function( xhr, status, error ) {
				if (xhr.status == 404 || xhr.status == 400) {					
					modal_alert( xhr.responseText, 'error');		
				}else{
					modal_alert( xhr.responseText, 'warn');		
				}						
			},
		});	
}
/* --------------------------------------------------------------------------- */
function validate_form_field(element_id,form_id)
{
	var form = $('#'+form_id);	
	var formID = '#'+form_id;	
	var notify_type = form.attr("data-validate-display-edit");
	if(notify_type==''){
		notify_type = 'inline';
	}
	field_validation_ajax_process( element_id , formID , notify_type);
}
/* --------------------------------------------------------------------------- */
function form_validate_save(form_id, url, notify_form_type='modal', notify_div='', fileinputID='', validateFileUpload=false) {
	var form = $('#'+form_id);	
	var notify_type = form.attr("data-validate-display-save");
	
	if(notify_type==''){
		notify_type = 'inline';
	}
	 
	if(notify_type=='inline')
	{
		$('input').removeClass("is-invalid");
		
		$("[class*=validation-feedback]").removeClass("invalid-feedback");
		$("[class*=validation-feedback]").html("");
		
		//$('.validation-feedback').removeClass("invalid-feedback");
		//$('.validation-feedback').html("");
	}
	
	var validateurl = 'https://www.covemarine.com/core/ajax/validate.php';
	
	$.ajax({
			url: validateurl,
			method: 'POST',
			data: form.serialize(),
			dataType: 'JSON',
			success: function( result,textStatus, jqXHR ) {
				
				var error_count = Object.keys(result).length;
			
				if(error_count > 0)
				{
					  var error_string = '';
					  $.each(result, function(key, val){
						  
							if(notify_type=='inline')
							{
								$('input[name='+key+']').addClass('is-invalid ');
								
								$(".validation-feedback-" + key).addClass('invalid-feedback')
								$(".validation-feedback-" + key).html(val);
		
								
								//$('input[name='+key+']').next(".validation-feedback").html(val);
								//$('input[name='+key+']').next(".validation-feedback").addClass('invalid-feedback');
							}
													
						error_string += val + '
';
					  
					});
					
					if ( notify_type == 'modal') 
					{	
						modal_alert( error_string, 'error' );
					}
					
				}
				else
				{ 
					if(fileinputID != '' && validateFileUpload==true)
					{
						if($('#'+fileinputID).fileinput('getFilesCount')==0){		
							modal_alert( 'Please select file', 'error' );
							return;								
						}
					}
					
					$.ajax({
					url: url,
					method: 'POST',
					data: form.serialize(), 
					processData: false,
					dataType: 'JSON',
					//contentType: false,
					success: function( result,textStatus, jqXHR ) {
									
						$("#validate_id").val(result.record_id);
						
						if(result.status==200)
						{							
							if(fileinputID != '')
							{
								$('#'+fileinputID).fileinput('upload');								
							}
						}
						
						// ***** START - BRING THIS INTO PANEL *****
						if ( notify_form_type == 'modal') {							
							
							// SHOW FINAL NOTIFICATION MODAL
							
							if(jqXHR.status==200){
								modal_alert( result.content, 'ok', true);
							}else if(jqXHR.status==250){
								modal_alert( result.content, 'warn', true);
							}else if(jqXHR.status==260){
								modal_alert( result.content, 'error', true);
							}else{
								modal_alert( result.content, 'error', true);
							}			
						
						} else if ( notify_form_type == 'inline') {	
							
							// SHOW FINAL NOTIFICATION MESSAGE INLINE
							if(jqXHR.status==200){
								inline_alert( result.content, 'ok', notify_div);
							}else if(jqXHR.status==250){
								inline_alert( result.content, 'warn', notify_div);
							}else if(jqXHR.status==260){
								inline_alert( result.content, 'error', notify_div);
							}else{
								inline_alert( result.content, 'error', notify_div);
							}
												
							$('#Modal_1').modal('hide');
							$('#Modal_2').modal('dispose');	
							
							
						}else if ( notify_form_type == 'replace') {	
							
							$(notify_div).html(result.content);
												
							$('#Modal_1').modal('hide');
							$('#Modal_2').modal('dispose');	
							
							
						} else if ( notify_form_type == 'none') {							
							
							// SKIP FINAL NOTIFICATION MODAL
							
							$('#Modal_1').modal('hide');
							$('#Modal_2').modal('dispose');
															
						}					
						// ***** END - BRING THIS INTO PANEL *****
						
					},
					error: function( xhr, status, error ) {
						if (xhr.status == 404 || xhr.status == 400) {					
							modal_alert( xhr.responseText, 'error');		
						}else{
							modal_alert( xhr.responseText, 'warn');		
						}						
					},
					});	
				}
				
			},
			error: function( xhr, status, error ) {
				if (xhr.status == 404 || xhr.status == 400) {					
					modal_alert( xhr.responseText, 'error');		
				}else{
					modal_alert( xhr.responseText, 'warn');		
				}						
			},
		});
}
/* --------------------------------------------------------------------------- */
	
function copy_to_clipboard( element_id ) {
	var copyText = $( element_id ).val()
	navigator.clipboard.writeText(copyText);
}
/* --------------------------------------------------------------------------- */
function fun_ajax_replace(url,form_id,element_id)
{
	var form = $(form_id);
	$.ajax({
		url: url,
		method: 'POST',
		data: form.serialize() + '&request_type=ajax',
		success: function( result,textStatus, jqXHR ) {			
			$(element_id).html(result);				
		},
		error: function( xhr, status, error ) {
			if (xhr.status == 404 || xhr.status == 400) {					
				modal_alert( xhr.responseText, 'error');		
			}else{
				modal_alert( xhr.responseText, 'warn');		
			}						
		},
	});	
}
/* --------------------------------------------------------------------------- */
function fun_search(url,form_id,element_id)
{
	fun_ajax_replace(url,form_id,element_id);	
}
/* --------------------------------------------------------------------------- */
function fun_pagination(url,form_id,element_id)
{
	fun_ajax_replace(url,form_id,element_id);	
}
function ajax_process(form_id,step,notify='modal',notify_div='')
{	
	var form = $(form_id); 
	
	if(!$(form_id)[0].checkValidity())
	{
		$(form_id)[0].reportValidity();		
		return false;						
	}
	
	$.ajax({
		url: step[0],
		method: 'POST',
		data: form.serialize(),
		success: function( result,textStatus, jqXHR ) {
			
			if (step[1]) { eval( step[1] );	}
			
			
			// ***** START - BRING THIS INTO PANEL *****
			if ( notify == 'modal') {							
				
				// SHOW FINAL NOTIFICATION MODAL
				//modal_alert( result, 'ok', true);							
				if(jqXHR.status==200){
					modal_alert( result, 'ok', true);
				}else if(jqXHR.status==250){
					modal_alert( result, 'warn', true);
				}else if(jqXHR.status==260){
					modal_alert( result, 'error', true);
				}else{
					modal_alert( result, 'ok', true);
				}
			
			} else if ( notify == 'inline') {	
				
				// SHOW FINAL NOTIFICATION MESSAGE INLINE
				if(jqXHR.status==200){
					inline_alert( result, 'ok', notify_div);
				}else if(jqXHR.status==250){
					inline_alert( result, 'warn', notify_div);
				}else if(jqXHR.status==260){
					inline_alert( result, 'error', notify_div);
				}else{
					inline_alert( result, 'error', notify_div);
				}
				
				/*
				$(notify_div).html(result);
				autoCloseAlert();
				*/
				$('#Modal_1').modal('hide');
				$('#Modal_2').modal('dispose');	
				
				
			} else if ( notify == 'none') {							
				
				// SKIP FINAL NOTIFICATION MODAL
				
				$('#Modal_1').modal('hide');
				$('#Modal_2').modal('dispose');
												
			}						
			// ***** END - BRING THIS INTO PANEL *****
			
		},
		error: function( xhr, status, error ) {
			
			if (xhr.status == 404 || xhr.status == 400) {					
				modal_alert( xhr.responseText, 'error');		
			}else{
				modal_alert( xhr.responseText, 'warn');		
			}						
		},
	});	
}
function custom_modal_ajax_process ( title, buttons, url) {
	
	$('#Modal_3 .modal-title').html( title );
	
	$('#Modal_3 .modal-footer').html('');
	
	$.each(buttons, function(key, value) {
		
		$('#Modal_3 .modal-footer').append(value);
		
	});
	$.ajax({
		url: url,
		method: 'POST',
		success: function( result ) {
			$('#Modal_3 .modal-body').empty();
			$('#Modal_3 .modal-body').html(result);
			
			disable_form_on_enter('form input');			
		},			
		error: function( xhr, status, error ) {
			
			if (xhr.status == 404 || xhr.status == 400) {					
				modal_alert( xhr.responseText, 'error');		
			}else{
				modal_alert( xhr.responseText, 'warn');		
			}	
			
		},
	}); //END-AJAX()
	$('#Modal_3').modal('show');
}
/* --------------------------------------------------------------------------- */
function form_validate_submit(form_id) {
	var form = $('#'+form_id);	
	var notify_type = form.attr("data-validate-display-save");
	
	if(notify_type==''){
		notify_type = 'inline';
	}
	 
	if(notify_type=='inline')
	{
		$('input').removeClass("is-invalid");
		
		$("[class*=validation-feedback]").removeClass("invalid-feedback");
		$("[class*=validation-feedback]").html("");
		
		//$('.validation-feedback').removeClass("invalid-feedback");
		//$('.validation-feedback').html("");
	}
	
	var validateurl = 'https://www.covemarine.com/core/ajax/validate.php';
	
	$.ajax({
			url: validateurl,
			method: 'POST',
			data: form.serialize(),
			dataType: 'JSON',
			success: function( result,textStatus, jqXHR ) {
				
				var error_count = Object.keys(result).length;
			
				if(error_count > 0)
				{
					  var error_string = '';
					  $.each(result, function(key, val){
						  
							if(notify_type=='inline')
							{
								$('input[name='+key+']').addClass('is-invalid ');
								$('select[name='+key+']').addClass('is-invalid ');
								
								$(".validation-feedback-" + key).addClass('invalid-feedback')
								$(".validation-feedback-" + key).html(val);
		
								
								//$('input[name='+key+']').next(".validation-feedback").html(val);
								//$('input[name='+key+']').next(".validation-feedback").addClass('invalid-feedback');
							}
													
						error_string += val + '
';
					  
					});
					
					if ( notify_type == 'modal') 
					{	
						modal_alert( error_string, 'error' );
					}
					
				}
				else
				{ 
					form.submit();	
				}
				
			},
			error: function( xhr, status, error ) {
				if (xhr.status == 404 || xhr.status == 400) {					
					modal_alert( xhr.responseText, 'error');		
				}else{
					modal_alert( xhr.responseText, 'warn');		
				}						
			},
		});
}