	$(document).ready(function(){

		$("#submitBtn").click(function(){
			var first = trim($("#first").val());
			var last = trim($("#last").val());
			var title1 = $("#title1").attr("checked");
			var title2 = $("#title2").attr("checked");
			var title3 = $("#title3").attr("checked");
			var title4 = $("#title4").attr("checked");
			var title5 = $("#title5").attr("checked");
			var titleo = trim($("#titleo").val());
			var church = trim($("#church").val());
			var address = trim($("#address").val());
			var city = trim($("#city").val());
			var province = trim($("#province").val());
			var postal = trim($("#postal").val());
			var country = trim($("#country").val());
			var phone = trim($("#phone").val());
			var fax = trim($("#fax").val());
			var email = trim($("#email").val());
			
			var i1 = $("#interest1").attr("checked");
			var i2 = $("#interest2").attr("checked");
			var i3 = $("#interest3").attr("checked");
			var i4 = $("#interest4").attr("checked");
			var i5 = $("#interest5").attr("checked");
			var i6 = $("#interest6").attr("checked");
			var i7 = $("#interest7").attr("checked");
			
			var comments = trim($("#comments").val());
			
			$("#formErrors").html("");
			var e = 0;
			if(!first){ e++; a("Please enter your first name."); }
			if(!last){ e++; a("Please enter your last name."); }
			if(!title1 && !title2 && !title3 && !title4 && !title5){ e++; a("Please select your title."); }
			if(title5 && !titleo){ e++; a("You must enter a title as you selected \"Other\"."); }
			if(!church){ e++; a("You must enter the name of your Parish."); }
			if(!address){ e++; a("You must enter your address."); }
			if(!city){ e++; a("You must enter your city."); }
			if(!province){ e++; a("You must enter your province/state."); }
			if(postal.length <= 4){ e++; a("You must enter your postal/zip code."); }
			if(!country){ e++; a("You must enter your country."); }
			if(!validPhone(phone)){ e++; a("You must enter a valid phone number."); }
			if(!validEmail(email)){ e++; a("You must enter a valid email address."); }
			if(!i1 && !i2 && !i3 && !i4 && !i5 && !i6 && !i7){ e++; a("You must select at least one interest."); }
			if(i7 && !comments){ e++; a("You must enter your other area(s) of interest."); }
			
			if(e){ $("#formErrors").prepend("You have " + e + " error(s) to fix.<br /><br />"); return false; }
			
			$("#theform").submit();
		});			
		
		function a(What){
			$("#formErrors").append(What + "<br />");
		}
		
		function validEmail(email) {
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   var address = email;
		   if(reg.test(address) == false) {
			  
			  return false;
		   } else {
			  return true;
		   }
		}


		var digits = "0123456789";
		var phoneNumberDelimiters = "()- ";
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		var minDigitsInIPhoneNumber = 10;

		function isInteger(s)
		{   var i;
			for (i = 0; i < s.length; i++)
			{   
				
				var c = s.charAt(i);
				if (((c < "0") || (c > "9"))) return false;
			}

			return true;
		}
		function trim(s)
		{   var i;
			var returnString = "";
		  
			for (i = 0; i < s.length; i++)
			{   
				var c = s.charAt(i);
				if (c != " ") returnString += c;
			}
			return returnString;
		}
		function stripCharsInBag(s, bag)
		{   var i;
			var returnString = "";
			for (i = 0; i < s.length; i++)
			{   
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) returnString += c;
			}
			return returnString;
		}

		function validPhone(strPhone){
		var bracket=3
		strPhone=trim(strPhone)
		if(strPhone.indexOf("+")>1) return false
		if(strPhone.indexOf("-")!=-1)bracket=bracket+1
		if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
		var brchr=strPhone.indexOf("(")
		if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
		if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
		s=stripCharsInBag(strPhone,validWorldPhoneChars);
		return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
		}			
		
	});