var firstErrorField = '';

function checkForm(form) {
	var alertText = '';
	firstErrorField = '';
	
	if (!form.name.value) {
		alertText += "Please enter the name of the deceased.\n";
		checkFirstErrorField('name');
	}
	if (!form.ageAtDeath.value) {
		alertText += "Please enter the person's age at the time of their death.\n";
		checkFirstErrorField('ageAtDeath');
	}
	if (!form.causeOfDeath.value) {
		alertText += "Please enter the cause of death.\n";
		checkFirstErrorField('causeOfDeath');
	}
	if (!form.address.value) {
		alertText += "Please enter the person's address.\n";
		checkFirstErrorField('address');
	}
	if (!form.dob.value) {
		alertText += "Please enter the person's date of birth.\n";
		checkFirstErrorField('dob');
	}
	if (!form.dod.value) {
		alertText += "Please enter the person's date of death.\n";
		checkFirstErrorField('dod');
	}
	if (!form.mvConnection.value) {
		alertText += "Please tell us how the person was connected to Martha's Vineyard.\n";
		checkFirstErrorField('mvConnection');
	}
	if (form.photo.value) {
		var imageExtension = getExtension(form.photo.value);
		imageExtension = imageExtension.toLowerCase();
	}
	if (form.photo.value && imageExtension != 'jpg') {
		alertText += "Please provide a file with a .jpg extension for your photo.\n";
		checkFirstErrorField('photo');
	}
	if (!form.contactName.value) {
		alertText += "Please enter your name.\n";
		checkFirstErrorField('contactName');
	}
	if (!form.contactPhone.value) {
		alertText += "Please enter a phone number where we can contact you.\n";
		checkFirstErrorField('contactPhone');
	}
	if (!form.contactEmail.value) {
		alertText += "Please enter your email address.\n";
		checkFirstErrorField('contactEmail');
	}
	else {
		if (!(emailCheck(form.contactEmail.value))) {
			alertText += "Please enter a valid email address.\n";
			checkFirstErrorField('contactEmail');
		}
	}
	if (!form.funeralHome.value) {
		alertText += "Please enter contact information for the funeral home.\n";
		checkFirstErrorField('funeralHome');
	}
	
	if (!form.captchaCode.value) {
		alertText += "To help prevent spam posts, please type the characters you see in the graphic.";
		checkFirstErrorField('captchaCode');
	}
	else if (form.captchaCode.value != captcha) {
		alertText += "The security code you typed in is incorrect.";
		checkFirstErrorField('captchaCode');
	}
	
	if (alertText != '') {
		alert(alertText);
		form[firstErrorField].focus();
		form[firstErrorField].select();
		return false;
	}
	else {
		form.submit();
	}
}
