(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 && $('#'+formid).attr("action") != "") {
			var action = $('#'+formid).attr("action");
		} else if($('#'+formid).attr("action") == "") {
			var action = "contents/plugins/eedu_portal/ajax.php";
		} else var action = "contents/plugins/eedu_portal/ajax.php";
		if($(form).validateForm({
			password: 'password'
		})) {
			// var formData = new FormData(form[0]);
			// $('#preloader').css('display', 'flex');
			$(this).html("Submitting").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');                
				formData.append('imagebase64', base64ImageContent);
			}			

			// 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);
				    	} 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);
							if($('#'+formid).data("clearform") != null) {
								$('#'+formid).clearForm();
							}
							if($('#'+formid).data("callback") != null && $('#'+formid).data("callbackid") != null) {
								eval('$("'+ $('#'+formid).data("callbackid") +'").' +$('#'+formid).data("callback")+ "()");
								return;
							}
							if($('#'+formid).data("callback") != null && $('#'+formid).data("callbackid") == null) {
								eval('$.' +$('#'+formid).data("callback")+ "()");
								return;
							}
							if($('#'+formid+' #addMore').data('activate')) {
								window.location = $('#'+formid+' #addMore').data('url');
							}
				    	}
					}
					catch(err) {
					    
					}
					finally {
					    $(subBut).html(text).attr("disabled", false);
					}
					
			    },
			    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);
			    }
			});
		}
	}
})( 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();
	})

	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});
	}
})