var firstErrorField = '';

function checkForm(form) {
	var alertText = '';
	firstErrorField = '';
	
	if (!form.organization.value) {
		alertText += "Please enter the name of the sponsoring organization/individual.\n";
		checkFirstErrorField('organization');
	}
	if (!form.npo[0].checked && !form.npo[1].checked) {
		alertText += "Is your organization a nonprofit?\n";
		checkFirstErrorField('npo');
	}
	if (!form.title.value) {
		alertText += "Please enter the title of the event/class.\n";
		checkFirstErrorField('title');
	}
	if (!form.date.value) {
		alertText += "Please enter the day(s) and date(s) for your listing.\n";
		checkFirstErrorField('date');
	}
	if (!form.location.value) {
		alertText += "Please enter the location of your event\n";
		checkFirstErrorField('location');
	}
	if (!form.admission.value) {
		alertText += "Please enter the admission price/fee.\n";
		checkFirstErrorField('admission');
	}
	if (!form.pubContact.value) {
		alertText += "Please enter contact information for readers (phone/email).\n";
		checkFirstErrorField('pubContact');
	}
	if (!form.desc.value) {
		alertText += "Please enter a description for your listing.\n";
		checkFirstErrorField('desc');
	}
	
	if (form.photo.value) {
		var imageExtension = getExtension(form.photo.value);
		imageExtension = imageExtension.toLowerCase();
	}
	
	if (form.photo.value && imageExtension != 'jpg') {
		alertText += "You must upload a file with a .jpg extension for your photo.\n";
		checkFirstErrorField('photo');
	}
	
	if (!form.contactName.value) {
		alertText += "Please enter a contact name.\n";
		checkFirstErrorField('contactName');
	}
	if (!form.contactPhone.value) {
		alertText += "Please enter a contact phone number.\n";
		checkFirstErrorField('contactPhone');
	}
	if (!form.contactEmail.value) {
		alertText += "Please enter a contact email address.\n";
		checkFirstErrorField('contactEmail');
	}
	else {
		if (!(emailCheck(form.contactEmail.value))) {
			alertText += "Please enter a valid email address.\n";
			checkFirstErrorField('contactEmail');
		}
	}
	
	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);
		if (firstErrorField != 'npo') {
			form[firstErrorField].focus();	
			form[firstErrorField].select();
		}
		else {
			form.npo[0].focus();
		}
		return false;
	}
	else {
		/*alert("Upon submission, you should receive a confirmation email from our office notifying you that we have successfully received your information.\n\nIn the event that you do not receive this email, please call our office at 508-693-6100 ext.40\n\nClick \"OK\" to proceed.");*/
		/*if (form.photo.value) {
			progressWindow = window.open('/submissions/calSubProgress.php', 'progressWindow', 'resizable=no,scrollbars=no,width=250,height=150');
		}*/
		form.submit();
	}
}
