// Pagham Parish Council JavaScript
// ================================
function submit_form() {
   if (window.document.searchform.q.value=='') {
   } else {
      window.document.searchform.submit();
   }
}
function WeddingForm_setfocus() {
	if (location.search.indexOf("mysql=59") > 0) {
		window.document.WeddingForm.FirstName.focus();
		window.document.WeddingForm.FirstName.select();
		var imageNumber = Math.floor(6 * Math.random()) + 5;
		if (imageNumber < 10) {
			document.getElementById("SecurityImage").src = "/images/arundel/codes/code0" + imageNumber + ".jpg";
		} else {
			document.getElementById("SecurityImage").src = "/images/arundel/codes/code" + imageNumber + ".jpg";
		}
	}
}
function ValidEmail(emailAddress) {
	var emailIndex = 0;
	if (emailAddress == "") {
		return true;
	} else {
		if (emailAddress.length < 5) {
			return false;
		} else {
			if (emailAddress.indexOf(".") < 1) {
				return false;
			} else {
				if (emailAddress.indexOf("@") < 1) {
					return false;
				} else {
					emailIndex = emailAddress.indexOf("@");
					if (emailAddress.indexOf(".", emailIndex) < emailAddress.indexOf("@")) {
						return false;
					} else {
						return true;
					}
				}
			}
		}
	}
}
function ValidDate(checkDate) {
         // 1 = Invalid day
         // 2 = invalid month
         // 3 = invalid year
         // 4 = date entered < today's date
         errorCode = 0;
         days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
         today = new Date();
         newToday = ""
         todayDay = today.getDate();
         todayMonth = today.getMonth() + 1;
         todayYear = today.getFullYear();
         startYear = todayYear;
         endYear = todayYear + 1;
         // Build newToday as today's date in yyyymmdd format
         newToday = todayYear;
         if (todayMonth < 10) {
            newToday = newToday + "0" + todayMonth;
         } else {
            newToday = newToday + "" + todayMonth;
         }
         if (todayDay < 10) {
            newToday = newToday + "0" + todayDay;
         } else {
            newToday = newToday + "" + todayDay;
         }
         newDate = checkDate.substring(4,8) + checkDate.substring(2,4) + checkDate.substring(0,2);
         newDay = parseInt(checkDate.substring(0,2),10);
         newMonth = parseInt(checkDate.substring(2,4),10);
         newYear = parseInt(checkDate.substring(4,8),10);
         // Check the month
         if ((newMonth < 1) || (newMonth > 12) || (isNaN(newMonth))) {
            errorCode = 2
         }
         // Check the year
         if ((newYear < startYear) || (newYear > endYear) || (isNaN(newYear))) {
            errorCode = 3
         }
         // Check the day
         if (errorCode == 0) {
            // Check for leap year
            febCheck1 = parseInt((newYear / 4),10);
       	 febCheck2 = febCheck1 * 4;
       	 if (febCheck2 == newYear) {
       	    days[1] = 29;
       	 }
            checkMonth = newMonth - 1;	
            if ((newDay < 1) || (newDay > days[checkMonth]) || (isNaN(newDay))) {
               errorCode = 1;
       	 }
         }
         // Set errorCode to 4 if the date entered is not greater than today's date
         if (errorCode == 0 && newDate < newToday) {
            errorCode = 4;
         }
         // Check the error code and display a suitable message if <> 0
         if (errorCode != 0) {
            return false;
         } else {
            return true;
         }
}

function Validate_WeddingForm() {

	weddingDate = "ddmmyyyy";
	foundError = "N";
	if (document.getElementById("FirstName").value == "") {
		alert("Please enter your first name");
		document.getElementById("FirstName").select();
		document.getElementById("FirstName").focus();
		foundError = "Y";
	} else {
		if (document.getElementById("Surname").value == "") {
			alert("Please enter your surname");
			document.getElementById("Surname").select();
			document.getElementById("Surname").focus();
			foundError = "Y";
		} else {
			if (document.getElementById("Address1").value == "") {
				alert("Please enter the first line of your address");
				document.getElementById("Address1").select();
				document.getElementById("Address1").focus();
				foundError = "Y";
			} else {
				if (document.getElementById("County").value == "") {
					alert("Please enter your County");
					document.getElementById("County").select();
					document.getElementById("County").focus();
					foundError = "Y";
				} else {
					if (document.getElementById("Postcode").value == "") {
						alert("Please enter your Postcode");
						document.getElementById("Postcode").select();
						document.getElementById("Postcode").focus();
						foundError = "Y";
					} else {
						if (document.getElementById("TelNo").value == "") {
							alert("Please enter your telephone number");
							document.getElementById("TelNo").select();
							document.getElementById("TelNo").focus();
							foundError = "Y";
						} else {
							if (document.getElementById("Email").value == "") {
								alert("Please enter your email address");
								document.getElementById("Email").select();
								document.getElementById("Email").focus();
								foundError = "Y";
							} else {
								if (!ValidEmail(document.getElementById("Email").value)) {
									alert("Please enter a valid email address");
									document.getElementById("Email").select();
									document.getElementById("Email").focus();
									foundError = "Y";
								} else {
									if (document.getElementById("WeddingDay").value != "day" || document.getElementById("WeddingMonth").value != "month" || document.getElementById("WeddingYear").value != "year") {
										if (document.getElementById("WeddingDay").value == "day") {
											alert("Please enter the day of the Wedding");
											foundError = "Y";
										} else {
											if (document.getElementById("WeddingMonth").value == "month") {
												alert("Please enter the month of the wedding");
												foundError = "Y";
											} else {
												if (document.getElementById("WeddingYear").value == "year") {
													alert("Please enter the year of the wedding");
													foundError = "Y";
												} else {
													weddingDate = document.getElementById("WeddingDay").value;
													if (document.getElementById("WeddingMonth").value == "January") { weddingDate = weddingDate + "01"; }
													if (document.getElementById("WeddingMonth").value == "February") { weddingDate = weddingDate + "02"; }
													if (document.getElementById("WeddingMonth").value == "March") { weddingDate = weddingDate + "03"; }
													if (document.getElementById("WeddingMonth").value == "April") { weddingDate = weddingDate + "04"; }
													if (document.getElementById("WeddingMonth").value == "May") { weddingDate = weddingDate + "05"; }
													if (document.getElementById("WeddingMonth").value == "June") { weddingDate = weddingDate + "06"; }
													if (document.getElementById("WeddingMonth").value == "July") { weddingDate = weddingDate + "07"; }
													if (document.getElementById("WeddingMonth").value == "August") { weddingDate = weddingDate + "08"; }
													if (document.getElementById("WeddingMonth").value == "September") { weddingDate = weddingDate + "09"; }
													if (document.getElementById("WeddingMonth").value == "October") { weddingDate = weddingDate + "10"; }
													if (document.getElementById("WeddingMonth").value == "November") { weddingDate = weddingDate + "11"; }
													if (document.getElementById("WeddingMonth").value == "December") { weddingDate = weddingDate + "12"; }
													weddingDate = weddingDate + document.getElementById("WeddingYear").value;
													if (!ValidDate(weddingDate)) {
														alert("Please enter a valid future date");
														foundError = "Y";
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if (foundError == "Y") {
		return false;
	} else {
		if (document.getElementById("SecurityCode").value == "") {
			alert("Please enter the security code");
			document.getElementById("SecurityCode").select();
			document.getElementById("SecurityCode").focus();
			return false;
		} else {
			codeWord = "ARUNDEL";
			if (document.getElementById("SecurityImage").src.indexOf("code01.jpg") > 0) { codeWord = "rabbit" }
			if (document.getElementById("SecurityImage").src.indexOf("code02.jpg") > 0) { codeWord = "toffee" }
			if (document.getElementById("SecurityImage").src.indexOf("code03.jpg") > 0) { codeWord = "active" }
			if (document.getElementById("SecurityImage").src.indexOf("code04.jpg") > 0) { codeWord = "bounce" }
			if (document.getElementById("SecurityImage").src.indexOf("code05.jpg") > 0) { codeWord = "renergy" }
			if (document.getElementById("SecurityImage").src.indexOf("code06.jpg") > 0) { codeWord = "fibula" }
			if (document.getElementById("SecurityImage").src.indexOf("code07.jpg") > 0) { codeWord = "honest" }
			if (document.getElementById("SecurityImage").src.indexOf("code08.jpg") > 0) { codeWord = "margin" }
			if (document.getElementById("SecurityImage").src.indexOf("code09.jpg") > 0) { codeWord = "orange" }
			if (document.getElementById("SecurityImage").src.indexOf("code10.jpg") > 0) { codeWord = "thesis" }
			if (document.getElementById("SecurityCode").value != codeWord) {
				alert("The security code is incorrect. Please enter the code exactly as it appears on screen");
				document.getElementById("SecurityCode").select();
				document.getElementById("SecurityCode").focus();
				return false;
			} else {
				return true;
			}
		}
	}
}