jQuery(document).on("click", ".offer-admission", function(){
	$("#alert").css("display", "none");
	var parent = $(this).closest("td");
	var element = $(parent).find(".dispamount");
	var student_data = $(this).data("studentmeta");
	var dialog;
	dialog = $( "#dialog-form" ).dialog({
      autoOpen: false,
      minHeight: 400,
      width: 350,
      modal: true,
      buttons: {
              "Submit": function() {
              	var form = $(this).find("form");
              	var formid = $(form).attr("id");
              	var subBut = $(".ui-dialog-buttonset .ui-button:first");
				var text = $(subBut).html();
              	if($(form).validateForm()) {
              		var formData = new FormData(form[0]);
              		// Submit the form using AJAX.
              		$(subBut).html("Processing").attr("disabled", true);

					$.ajax({
					    type: 'POST',
					    url: 'contents/plugins/admission/ajax.php',
					    data: formData,
					    processData: false,
			    		contentType: false,
					    success: function(data) {
					    	try	{
					    		data = JSON.parse(data);
						    	$(subBut).html(text).attr("disabled", false);
						    	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) {
						    		$("#offer-admission-btn-"+student_data.id).html('<span class="green">Admitted</span>');
									
									if($('#'+formid+' #addMore').data('activate')) {
										window.location = $('#'+formid+' #addMore').data('url');
									}
						    	}
					    	}
					    	catch (err) {
					    		console.log(err);
					    		console.log(data);
					    	}
					    	finally {
					    		console.log(data);
					    		dialog.delay( 800 ).dialog( "close" );
					    	}
							
					    },
					    error: function(xhr, status, error) {
					    	var err = JSON.parse(xhr.responseText);
					    	$('#'+formid+' #alert').css('display', '');
							$('#'+formid+' #alert').removeClass("alert-danger");
							$('#'+formid+' #alert').removeClass("alert-success");
							$('#'+formid+' #alert').addClass("alert-warning");
							$('#'+formid+' #alert span').html(err.Message);
					    }
					});
				}
              },
              Cancel: function() {
                dialog.dialog( "close" );
              }
            },
      close: function() {
      	$("#alert span").html("");
      	$("#alert").css("display", "none");
      	$("div.error em").html('').css({
			            'color': '#fff',
			            'background': 'none',
			            'padding-left': '0',
			            'padding-right': '0'
			        });
      }
    });
	
	$("#dialog-form #studentName").html(student_data.surname+" "+student_data.other_names);
	$("#dialog-form #student_id").val(student_data.id);
	$("#dialog-form #gender").val(student_data.gender);
	dialog.dialog("open");
});