// validate.js (form validator)

function validateForm() {
		
		var message = "The following fields are required:";
		var missing = new Array();
		var errorsFound = false;	
	
		missing[0] = "";
		missing[1] = "";
		missing[2] = "";
		missing[3] = "";
		missing[4] = "";
		missing[5] = "";
		missing[6] = "";
		missing[7] = "";
	
		//If Name field is empty:
		if(document.myForm.Name.value == "") {
		
			missing[0] = "    " + missing[0] + "Name" + "\n";
			errorsFound = true;			
		
		}
		
		//If E-mail field is empty:
		if(document.myForm.email.value == "") {
		
			missing[1] = "    " + missing[1] + "E-mail Address" + "\n";
			errorsFound = true;
			
		}
		
		//If College/Department field is empty:
		if(document.myForm.CollegeDepartment.value == "") {
		
			missing[2] = "    " + missing[2] + "College/Department" + "\n";
			errorsFound = true;
		
		}
		
		//If Office Extension field is empty:
		if(document.myForm.OfficeExtension.value == "") {
		
			missing[3] = "    " + missing[3] + "Office Extension" + "\n";
			errorsFound = true;
		
		}
		
		//If Workshop Title field is empty:
		if(document.myForm.Title.value == "") {
		
			missing[4] = "    " + missing[4] + "Workshop Title" + "\n";
			errorsFound = true;
		
		}
		
		//If Workshop Number of Participants field is empty:
		if(document.myForm.NumberOfParticipants.value == "") {
		
			missing[5] = "    " + missing[5] + "Number of Participants" + "\n";
			errorsFound = true;
		
		}
		
		//If Workshop Date field is empty:
		if(document.myForm.myDate.value == "" || document.myForm.myDate.value == "mm/dd/yyyy") {
		
			missing[6] = "    " + missing[6] + "Date" + "\n";
			errorsFound = true;
		
		}		
		
		//If Workshop Time field is empty:
		if(document.myForm.myTime.value == "") {
		
			missing[7] = "    " + missing[7] + "Time" + "\n";
			errorsFound = true;
		
		}			
		
		//If any of the required fields are not filled, return false so the form won't get submitted.
		if(errorsFound == true) {
		
			window.alert(message + "\n" + "\n" + missing[0] + missing[1] + missing[2] + missing[3] + 
										  		 missing[4] + missing[5] + missing[6] + missing[7]);
			
			return false;
		
		}
		
		//If none of the required fields are left blank, check for formatting errors.
		
		//If e-mail format is wrong:		
		var atSymbolPos = document.myForm.email.value.indexOf("@");
		var periodPos = document.myForm.email.value.indexOf(".");
		var spacePos = document.myForm.email.value.indexOf(" ");
			
		//If the value inputted for e-mail doesn't contain an '@' or a '.' (or starts with either of them) or contains a space:
		if( (atSymbolPos == -1) || (atSymbolPos == 0) || (periodPos == -1) || (periodPos == 0) || (spacePos > -1) ) {
			
			window.alert("Invalid e-mail format.");
			return false;
			   
		}			
			
		//If the value of "number of participants" is not numerical:
		if(isNaN(document.myForm.NumberOfParticipants.value) ||
		   (document.myForm.NumberOfParticipants.value.indexOf(" ") > -1) ) {
				
			window.alert("Input for the number of participants must be a number.");
			return false;
					
		}					
			
		//If date format is wrong:
		var slash1Pos = 2;
		var slash2Pos = 5;
		
		var now = new Date();
		
		var input = document.myForm.myDate.value;
		var inputMonth = input.substring(0,2);
		var inputDay = input.substring(3,5);
		var inputYear = input.substring(6,10);
						
		var dateInput = new Date();
		dateInput.setFullYear(inputYear, (inputMonth - 1), inputDay);	
					
		//If the value inputted for date is not in proper formatting (wrong slash positions), is too long, or is invalid:								
		if( document.myForm.myDate.value.charAt(slash1Pos) != "/" || 
		    document.myForm.myDate.value.charAt(slash2Pos) != "/" || 
		    document.myForm.myDate.value.length > 10 || document.myForm.myDate.value.length < 10 ||
			isNaN(inputMonth) || isNaN(inputDay) || isNaN(inputYear) || (dateInput <= now)  ) {
					
			window.alert("Invalid workshop date or format.\nMust follow \"mm/dd/yyyy\" format.");
			return false;
				
		}
		
		//If no errors were found in the form, return true so that the form may submit.
		if(errorsFound == false) {
		
			return true;
		
		}		
		
}