(function() {
	"use strict";
	function initPage() {
		// The required fields should not keep the form from submitting.
		// Turn off the browser's required field processing, but keep the indication that the field is required, so that it can be styled.
		var required_fields = $(".edit_generic_work [required=required]");
		required_fields.prop("required", false);
		required_fields.addClass("edit-required-field");

		// Turn off the Sufia submit prevention
		// Do this in a timeout so that the sufia code will run first.
		setTimeout(function() {
			var form = $(".edit_generic_work");
			form.off("submit");
		}, 10);

		// Override the sufia code that disables the "save" button. We always want to be able to save.
		$(".edit_generic_work").on('blur change', 'input, select, textarea', function(){
			// Do this in a timeout so that the sufia code will run first.
			setTimeout(function() {
				setRequired();
			}, 10);
		});

		// Override the "Add files" requirement so that it looks at previously uploaded files, too.
		var requiredFiles = $("#required-files");
		var requiredMetaData = $("#required-metadata");
		var fileUploadSection = $("#fileupload");
		function setRequired() {
			var save = $("#with_files_submit_exit,#with_files_submit_continue,#with_files_submit");

			// Be sure the agreement is set.
			var agreement = $("#agreement:checked");
			if (agreement.length > 0)
				save.prop("disabled", false);
			else
				save.prop("disabled", true);

			// Be sure there is at least one file uploaded.
			// If the fileUploadSection isn't there at all, then we aren't on the edit page, so don't do anything.
			if (fileUploadSection.length === 0)
				return;
			var fileList = fileUploadSection.find("tr");
			if (fileList.length > 0) {
				requiredFiles.removeClass("incomplete");
				requiredFiles.addClass("complete");
			} else {
				requiredFiles.removeClass("complete");
				requiredFiles.addClass("incomplete");
			}

			// Be sure all the metadata is filled in.
			var allFilledIn = true;
			$.each(required_fields, function(index, value) {
				var val = $(value).val();
				if (val.length === 0)
					allFilledIn = false;
			});
			if (allFilledIn) {
				requiredFiles.removeClass("incomplete");
				requiredFiles.addClass("complete");
			} else {
				requiredFiles.removeClass("complete");
				requiredFiles.addClass("incomplete");
			}

		}
		// Do this in a timeout so that the sufia code will run first.
		setTimeout(function() {
			setRequired();
		}, 10);

		// Also recheck the file requirement when the file delete button is pressed.
		fileUploadSection.on("click", ".delete", function() {
			// Do this in a timeout so that the delete action will run first. The item fades out with a css transform, so the timeout needs to be longer than the transform.
			setTimeout(function() {
				setRequired();
			}, 2000);
		});
	}

	$(window).bind('page:change', function() {
		initPage();
	});
})();

@@ -45,16 +45,57 @@
 age() %7B%0A
+%09%09var form = $(%22.edit_generic_work%22);%0A%09%09%0A
 %09%09// The
@@ -314,30 +314,19 @@
 s = 
-$(%22.edit_generic_work 
+form.find(%22
 %5Breq
@@ -390,59 +390,8 @@
 se);
-%0A%09%09required_fields.addClass(%22edit-required-field%22);
 %0A%0A%09%09
@@ -525,47 +525,8 @@
 ) %7B%0A
-%09%09%09var form = $(%22.edit_generic_work%22);%0A
 %09%09%09f
@@ -654,39 +654,20 @@
 save.%0A%09%09
-$(%22.edit_generic_work%22)
+form
 .on('blu
@@ -674,16 +674,22 @@
 r change
+ keyup
 ', 'inpu
@@ -1994,18 +1994,17 @@
 %09%09%09%09var 
-va
+e
 l = $(va
@@ -2007,16 +2007,34 @@
 $(value)
+;%0A%09%09%09%09var val = el
 .val();%0A
@@ -2054,24 +2054,26 @@
 ength === 0)
+ %7B
 %0A%09%09%09%09%09allFil
@@ -2087,16 +2087,108 @@
  false;%0A
+%09%09%09%09%09el.addClass(%22invalid-input%22);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09el.removeClass(%22invalid-input%22);%0A%09%09%09%09%7D%0A
 %09%09%09%7D);%0A%09
@@ -2212,37 +2212,40 @@
 ) %7B%0A%09%09%09%09required
-Files
+MetaData
 .removeClass(%22in
@@ -2260,37 +2260,40 @@
 %22);%0A%09%09%09%09required
-Files
+MetaData
 .addClass(%22compl
@@ -2315,37 +2315,40 @@
 e %7B%0A%09%09%09%09required
-Files
+MetaData
 .removeClass(%22co
@@ -2361,37 +2361,40 @@
 %22);%0A%09%09%09%09required
-Files
+MetaData
 .addClass(%22incom

Fix requirements checkboxes on edit page; add styling when required field is not filled in.