(function($){
	$.fn.submitForm = function(options) {
		var form = $(this).parents("form");
		var formid = $(form).attr("id");
		var text = $(this).html();
		var attempts = 1;
		var subBut = $(this);
		if($('#'+formid).attr("action") != null) {
			var action = $('#'+formid).attr("action");
		} else var action = "contents/plugins/eedu_portal/ajax.php";
		var submittingMsg = "Submitting";
		if($("#"+formid).data("submittingmsg") != null) {
			submittingMsg = $("#"+formid).data("submittingmsg");
		}
		$(this).html(submittingMsg).attr("disabled", true);
		var formData = new FormData(form[0]);			

		// Submit the form using AJAX.
		$.ajax({
		    type: 'POST',
		    url: action,
		    data: formData,
		    processData: false,
    		contentType: false,
		    success: function(data) {
		    	try { 
				    data = JSON.parse(data);
				    if(data.error == 1) {
			    		$('#'+formid+' #alert').css('display', '');
						$('#'+formid+' #alert').removeClass("alert-success");
						$('#'+formid+' #alert').removeClass("alert-warning");
						$('#'+formid+' #alert').addClass("alert-danger");
						$('#'+formid+' #alert span').html(data.msg);
						$(subBut).html(text).attr("disabled", false);
			    	} else if(data.error == 0) {
			    		$('#'+formid+' #item-img-output').data('updated', false);
			    		$('#'+formid+' #alert').css('display', '');
						$('#'+formid+' #alert').removeClass("alert-danger");
						$('#'+formid+' #alert').removeClass("alert-warning");
						$('#'+formid+' #alert').addClass("alert-success");
						$('#'+formid+' #alert span').html(data.msg);
						$.compileResult(data.students[0], data, 0, data.students.length, formid, subBut, text, attempts);
						$(subBut).attr("disabled", true).html("0% Complete");
			    	}
				}
				catch(err) {
				    console.log(err);
				    console.log(data);
				    $(subBut).html(text).attr("disabled", false);
				}
				finally {
					
				}		
		    },
		    error: function(e) {
		    	$("#preloader").css('display', 'none');
		    	$('#alert').css('display', '');
				$('#alert').removeClass("alert-danger");
				$('#alert').removeClass("alert-success");
				$('#alert').addClass("alert-warning");
				$('#alert span').html("Error in network connection");
				$(subBut).html(text).attr("disabled", false);
		    }
		});

	}
})( jQuery );

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

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

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

})

$.extend({
	compileResult: function(student, data, i, len, formid, subBut, text, attempts) {
		$('#'+formid+' #alert span').html(student.surname+" "+student.other_names+"'s Result is Compiling now. Please wait...");
		var student_id = student.id;
		$.ajax({
		    type: 'POST',
		    url: 'contents/plugins/eedu_portal/ajax.php',
		    data: {
		    		action: 'compile_student_result', 
		    		student: student_id, 
		    		use_pin: data.use_pin, 
		    		std_count: data.std_count, 
		    		term: data.args.term, 
		    		session: data.args.session, 
		    		"class": data.args.class, 
		    		category: data.args.category, 
		    	},
		}).done(function(){
			i = parseInt(i) + 1;
			len = parseInt(len);
			var percentage = $.getPercentage(len, i);
			$(subBut).attr("disabled", true).html(percentage+"%"+" Complete");
			if(i < len) {
				$.compileResult(data.students[i], data, i, len, formid, subBut, text, attempts);
			} else {
				$(subBut).html(text).attr("disabled", false);
				$('#'+formid+' #alert span').html("You have completed the result compilation process.");
			}
		}).fail(function(){
			if(attempts <= 3) {
	    		attempts++;
	    		$.compileResult(data.students[i], data, i, len, formid, subBut, text, attempts);
	    	} else {
	    		$(subBut).html(text).attr("disabled", false);
	    	}
		});
	},

	getPercentage: function(size, i) {
		size = parseInt(size) - 1;
		i = parseInt(i);
		var x = (i * 100)/size;
		var percentage = x;
		return Math.ceil(percentage);
	}
})