jQuery(document).ready(function($){
	var form = $("#form");
	$('#alert').css('display', 'none');
	$('#preloader').css('display', 'none');

	$('#submit').click(function(event){
		event.preventDefault();
		if($(form).validateForm({
			password: 'password'
		})) {
			// var formData = new FormData(form[0]);
			$('#preloader').css('display', 'flex');
			
			var formData = new FormData(form[0]);
			if($('#item-img-output').data('updated')) {
				var image = $('#item-img-output').attr('src');
				var base64ImageContent = image.replace(/^data:image\/(png|jpg);base64,/, "");
				var blob = base64ToBlob(base64ImageContent, 'image/png');                
				formData.append('imagebase64', base64ImageContent);
			}			

			// Submit the form using AJAX.
			$.ajax({
			    type: 'POST',
			    url: 'contents/plugins/external_examination/ajax.php',
			    data: formData,
			    processData: false,
	    		contentType: false,
			    success: function(data) {
			    	console.log(data);
			    	data = JSON.parse(data);
			    	console.log(data);
			    	$("#preloader").css('display', 'none');
			    	if(data.error == 1) {
			    		$('#alert').css('display', '');
						$('#alert').removeClass("alert-success");
						$('#alert').removeClass("alert-warning");
						$('#alert').addClass("alert-danger");
						$('#alert span').html(data.msg);
			    	} else if(data.error == 0) {
			    		$('#item-img-output').data('updated', false);
			    		$('#alert').css('display', '');
						$('#alert').removeClass("alert-danger");
						$('#alert').removeClass("alert-warning");
						$('#alert').addClass("alert-success");
						$('#alert span').html(data.msg);
						if($('#addMore').data('activate')) {
							window.location = $('#addMore').data('url');
						}
			    	}
					
			    },
			    error: function(xhr, status, error) {
			    	var err = JSON.parse(xhr.responseText);
			    	$("#preloader").css('display', 'none');
			    	$('#alert').css('display', '');
					$('#alert').removeClass("alert-danger");
					$('#alert').removeClass("alert-success");
					$('#alert').addClass("alert-warning");
					$('#alert span').html(err.Message);
			    }
			});
		}
	});

	$(window).on('keydown', function(e) {
	  if (e.which == 13) {
	    $("#submit").click();
	    return false;
	  }
	});

	$('#addMore').click(function(){
		$('#addMore').data('activate', true);
		$('#submit').click();
	})

	function base64ToBlob(base64, mime) 
	{
	    mime = mime || '';
	    var sliceSize = 1024;
	    var byteChars = window.atob(base64);
	    var byteArrays = [];

	    for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
	        var slice = byteChars.slice(offset, offset + sliceSize);

	        var byteNumbers = new Array(slice.length);
	        for (var i = 0; i < slice.length; i++) {
	            byteNumbers[i] = slice.charCodeAt(i);
	        }

	        var byteArray = new Uint8Array(byteNumbers);

	        byteArrays.push(byteArray);
	    }

	    return new Blob(byteArrays, {type: mime});
	}
})