(function($){
	$.fn.submitForm = function(options) {
		var form = $(this).parents("form");
		var formid = $(form).attr("id");
		var text = $(this).html();
		var subBut = $(this);
		if($('#'+formid).attr("action") != null) {
			var action = $('#'+formid).attr("action");
		} else var action = "includes/ajax.php";
		if($(form).validateForm({
			password: 'password'
		})) {
			$("#res-cont").css({
				"display": "none"
			})
			var tbody = $("#res-table tbody");
			$(tbody).html("");
			var submittingMsg = "Submitting";
			if($("#"+formid).data("submittingmsg") != null) {
				submittingMsg = $("#"+formid).data("submittingmsg");
			}
			$(this).html(submittingMsg).attr("disabled", true);
			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'); 
				var blob = $.base64ToBlob({'base64': base64ImageContent, 'mime': 'image/png'})               
				formData.append('imagebase64', base64ImageContent);
			}			

			// Submit the form using AJAX.
			$.ajax({
			    type: 'POST',
			    url: action,
			    data: formData,
			    processData: false,
	    		contentType: false,
			    success: function(data) {
			    	data = JSON.parse(data);
			    	$(subBut).html(text).attr("disabled", false);
			    	if(data.error == 1) {
			    		$("#result-heading").html("Oops! Nothing Found.");
			    		$("#res-cont").css({
							"display": "block"
						});
			    	} else if(data.error == 0) {
			    		$("#result-heading").html("Found!");
			    		$("#res-cont").css({
							"display": "block"
						});
						for (var key in data.res) {
							var tr = $("<tr></tr>");
							$(tr).addElement({
								"element": "td",
								"colspan": 3,
								"content": "<strong>"+data.res[key].candidate.surname+" "+data.res[key].candidate.other_names+"<strong>",
								"class": "pt-4 pb-0 text-center uppercase"
							});
							$(tbody).append(tr);
							var i = 1;
							var total = 0;
							for(var subject in data.res[key].score) {
								var tr = $("<tr></tr>");
								$(tr).addElement({
									"element": "td",
									"content": i,
									"class": "pt-0 pb-0"
								}).addElement({
									"element": "td",
									"content": subject,
									"class": "pt-0 pb-0"
								}).addElement({
									"element": "td",
									"content": data.res[key].score[subject],
									"class": "pt-0 pb-0"
								});
								total += parseInt(data.res[key].score[subject]);
								i++;
								$(tbody).append(tr);
							}
							var tr = $("<tr></tr>");
							$(tr).addElement({
								"element": "td",
								"colspan": 2,
								"content": "<strong>Total<strong>",
								"class": "pt-4 pb-0 text-center uppercase"
							}).addElement({
								"element": "td",
								"content": "<strong>"+total+"<strong>",
								"class": "pt-4 pb-0 uppercase"
							});
							$(tbody).append(tr);
						}
						
			    	}
			    }
			});
		}
	}
})( jQuery );

jQuery(document).ready(function($){
	// var form = $("#form");
	$('.alert').css('display', 'none');
	// $('#preloader').css('display', 'none');
	$("#submit").addClass("submit").attr("disabled", false);

	$.each($("form"), function(i, e){
		if($(e).data("callback") != null && $(e).data("callbackid") != null) {
			eval('$("'+ $(e).data("callbackid") +'").' +$(e).data("callback")+ "()");
		}
	})

	$('.submit').click(function(event){
		$('#alert').css('display', 'none');
		event.preventDefault();
		$(this).submitForm();
	});

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

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

	$.extend({
        loginCallback: function(options){
            window.location = "dashboard";
        },

        base64ToBlob: function(options) {
			mime = options.mime || '';
		    var sliceSize = 1024;
		    var byteChars = window.atob(options.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});
		}
    })

})