﻿
function validate(theForm) {
		
		if (theForm.FirstName.value == "")
			{
			alert("Please provide your first name.");
			theForm.FirstName.focus();
			return (false);
			}
		
		if (theForm.LastName.value == "")
			{
			alert("Please provide your last name.");
			theForm.LastName.focus();
			return (false);
			}
			
			if (theForm.Birthdate.value == "")
			{
				alert("Please provide your birth date.");
				theForm.Birthdate.focus();
				return (false);
			} else {
							
				// Checks for the following valid date formats:
				// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
				// Also separates date into month, day, and year variables
				
				var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
				
				// To require a 4 digit year entry, use this line instead:
				// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
				
				var matchArray = theForm.Birthdate.value.match(datePat); // is the format ok?
				if (matchArray == null) {
				alert("Date must be entered in format: mm/dd/yyyy")
				return false;
				}
				month = matchArray[1]; // parse date into variables
				day = matchArray[3];
				year = matchArray[4];
				if (month < 1 || month > 12) { // check month range
				alert("Month must be between 1 and 12.");
				return false;
				}
				if (day < 1 || day > 31) {
				alert("Day must be between 1 and 31.");
				return false;
				}
				if ((month==4 || month==6 || month==9 || month==11) && day==31) {
				alert("Month "+month+" doesn't have 31 days!")
				return false
				}
				if (month == 2) { // check for february 29th
				var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
				if (day>29 || (day==29 && !isleap)) {
				alert("February " + year + " doesn't have " + day + " days!");
				return false;
				   }
				}
			
				var today = new Date;
				var min_age = 13;
	
				var year2 = parseInt(year);
				var month2 = parseInt(month) - 1;
				var day2 = parseInt(day);
	
				var theirDate = new Date((year2 + min_age), month2, day2);
				
				if ( (today.getTime() - theirDate.getTime()) < 0) {
					//alert("too young");
					window.location.href="Sorry.html"
					return false;
				}
			}
			
			if (theForm.EmailAddress.value == ""){
				alert("Please provide your address or e-mail address.");
				theForm.EmailAddress.focus();
				return(false);
			}

			var checkEmailAddress = "@.";
			var checkStr = theForm.EmailAddress.value;
			var EmailAddressValid = false;
			var EmailAddressAt = false;
			var EmailAddressPeriod = false;
			for (i = 0;  i < checkStr.length;  i++)	{
				ch = checkStr.charAt(i);
					for (j = 0;  j < checkEmailAddress.length;  j++) {
						if (ch == checkEmailAddress.charAt(j) && ch == "@")
							EmailAddressAt = true;
						if (ch == checkEmailAddress.charAt(j) && ch == ".")
							EmailAddressPeriod = true;
						if (EmailAddressAt && EmailAddressPeriod)
						break;
						if (j == checkEmailAddress.length)
						break;
					}
				if (EmailAddressAt && EmailAddressPeriod) {
					EmailAddressValid = true
					break;
				}
			}
			
			if (!EmailAddressValid) {
				alert("The email you entered is invalid. It must contain an \"@\" and a \".\"");
				theForm.EmailAddress.focus();
				return (false);
			}
			
			
			if (theForm.Address1.value == "")
			{
				alert("Please provide your address.");
				theForm.Address1.focus();
				return (false);
			}
			
			if (theForm.City.value == "")
			{
				alert("Please provide your city.");
				theForm.City.focus();
				return (false);
			}
			
			if (theForm.StateProvince.value == "")
			{
				alert("Please provide your state.");
				theForm.StateProvince.focus();
				return (false);
			}
			
			if (theForm.ZIP.value == "")
			{
				alert("Please provide your Zip/Postal code.");
				theForm.ZIP.focus();
				return (false);
			}
			
			if (theForm.OptIn.checked == false)
			{
				alert("Choosing to receive e-mails from Majestic Athletic is required to sign up.");
				theForm.OptIn.focus();
				return (false);
			}			
	}
