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

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

	// $('#submit').click(function(event){
	// 	event.preventDefault();
	// 	// if($('#class_name').validateInput()){
	// 		$('#preloader').css('display', '');
	// 		var formData = $(form).serialize();

	// 		// Submit the form using AJAX.
	// 		$.ajax({
	// 		    type: 'POST',
	// 		    url: 'contents/plugins/eedu_portal/ajax.php',
	// 		    data: formData,
	// 		    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) {
	// 		    		$('#alert').css('display', '');
	// 					$('#alert').removeClass("alert-danger");
	// 					$('#alert').removeClass("alert-warning");
	// 					$('#alert').addClass("alert-success");
	// 					$('#alert span').html(data.msg);
	// 					if($(form).data('verify')) {
	// 						window.location = $(form).data('url') + "&comment=" + $(form).data('comment');
	// 					}
	// 		    	}						
	// 		    }
	// 		});
	// 	// }
	// });

	$('#forward').click(function(){
	    $.confirm({
	    	theme: 'supervan',
	        title: 'Are you sure you want to forward this result?',
	        content: '' +
	        '<form action="contents/plugins/eedu_portal/ajax.php" id="comment">' +
	        '<div class="form-group">' +
	        '<label>Please add a comment</label>' +
	        '<textarea type="text" placeholder="Please make a comment" class="yourComment form-control" required ></textarea>' +
	        '</div>' +
	        '</form>',
	        buttons: {
	            formSubmit: {
	                text: 'Submit',
	                btnClass: 'btn-blue',
	                action: function () {
	                    var comment = this.$content.find('.yourComment').val();
	                    if(!comment){
	                        $.alert('Please make a comment!');
	                        return false;
	                    }
	                    $(form).data('comment', comment);
	                    $(form).data('verify', true);
	                    $('#submit').submitForm();
	                }
	            },
	            cancel: function () {
	                //close
	            },
	        },
	        onContentReady: function () {
	            // bind to events
	            var jc = this;
	            this.$content.find('form').on('submit', function (e) {
	                // if the user submits the form by pressing enter in the field.
	                e.preventDefault();
	                jc.$$formSubmit.trigger('click'); // reference the button and click it
	            });
	        }
	    });
	})
})

function calculate_total(e) {
	var studid = e.dataset.studid;
	var etotal = document.getElementById('total'+studid);
	var modes = JSON.parse(etotal.dataset.modes);
	var egrades = document.getElementById('grades'+studid);
	var grades = JSON.parse(egrades.dataset.grades);
	var total = 0;
	for (var i = modes.length - 1; i >= 0; i--) {
		var val = parseInt(document.getElementById('input'+i+studid).value);
		if(isNaN(val)) {
			val = 0
		}
		var max = parseInt(modes[i].max);
		if(val > max) {
			val = max;
			document.getElementById('input'+i+studid).value = val;
		}
		total += val;
		for (var j = grades.length - 1; j >= 0; j--) {
			if(total >= grades[j].min && total <= grades[j].max) {
				egrades.value = grades[j].grade+' --- '+grades[j].description;
			}
		}
	}
	etotal.value = total;
}

function compute_scores() {
	var scoredivs = document.getElementsByClassName('scoredivs');
	for (var i = scoredivs.length - 1; i >= 0; i--) {
		for (var j = scoredivs[i].cells.length - 3; j >= 2; j--) {
			calculate_total(scoredivs[i].cells[j].children[0].children[0])
		}
	}
}
compute_scores();