var regAnyNumbers = /[0-9]+$/ //Checks for one or more numbers only
var regThreeNumbers = /([0-9][0-9][0-9])$/ //Checks for 3 numbers
var regTwoNumbers = /([0-9][0-9])$/ //Checks for 2 numbers
var regOneNumber = /([0-9])$/ //Checks for 1 number
var regJustText = /^([a-zA-Z\s.,']+)$/ //Checks for words only on one line of text 
var regTextAndCommonSymbols = /^([a-zA-Z0-9\s#,./$!'?]+)$/ //Checks for words and Numbers only on one line of text
var regZip = /^\d{5}([\-]\d{4})?$/ //Checks for zip code
var regEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/ //Checks for correct email format
var regMmddyyyy = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d/

function oc(a) {
  var o = {};
  for(var i=0;i<a.length;i++) {
    o[a[i]]='';
  }
  return o;
}

function validateRadioButton(button) {
    var count = -1;
    for (var i=button.length-1; i > -1; i--) {
        if (button[i].checked) {count = i; i = -1;}
    }
    if (count > -1){
    	return button[count].value;
    }else{
    	return null;
    }
}
function validateForm(formName){
	var passed = true;
	for(i=0; i< document.forms[formName].elements.length; i++){
		var thisElementValue = document.forms[formName].elements[i].value;
		var thisElementName = document.forms[formName].elements[i].name;

		// pass on unrequired blanks
		if (thisElementValue==""){
			//if (!(thisElementName == "grade"||thisElementName == "studentFirstName"||thisElementName == "studentMiddleName"||thisElementName == "studentLastName"||thisElementName == "birthCity"||thisElementName == "studentHomeAddress"||thisElementName == "studentHomeCity"||thisElementName == "studentHomeZipcode"||thisElementName == "studentMailingAddress"||thisElementName == "studentMailingCity"||thisElementName == "studentMailingZipcode"||thisElementName == "motherLastName"||thisElementName == "motherFirstName"||thisElementName == "motherOccupation"||thisElementName == "motherMiddleName"||thisElementName == "motherHomeCity"||thisElementName == "motherHomeAddress"||thisElementName == "motherHomeZipcode"||thisElementName == "motherMailingAddress"||thisElementName == "motherMailingCity"||thisElementName == "motherMailingZipcode"||thisElementName == "motherHomePhoneArea"||thisElementName == "motherCellArea"||thisElementName == "motherDayPhoneArea"||thisElementName == "motherEmail"||thisElementName == "motherBusiness"||thisElementName == "motherBusinessAddress"||thisElementName == "motherBusinessCity"||thisElementName == "motherBusinessZipcode"||thisElementName == "motherBusinessArea"||thisElementName == "motherBusinessEmail"||thisElementName == "fatherLastName"||thisElementName == "fatherFirstName"||thisElementName == "fatherOccupation"||thisElementName == "fatherMiddleName"||thisElementName == "fatherHomeCity"||thisElementName == "fatherHomeAddress"||thisElementName == "fatherHomeZipcode"||thisElementName == "fatherMailingAddress"||thisElementName == "fatherMailingCity"||thisElementName == "fatherMailingZipcode"||thisElementName == "fatherHomePhoneArea"||thisElementName == "fatherCellArea"||thisElementName == "fatherDayPhoneArea"||thisElementName == "fatherEmail"||thisElementName == "fatherBusiness"||thisElementName == "fatherBusinessAddress"||thisElementName == "fatherBusinessCity"||thisElementName == "fatherBusinessZipcode"||thisElementName == "fatherBusinessArea"||thisElementName == "fatherBusinessEmail"||thisElementName == "guardianLastName"||thisElementName == "guardianFirstName"||thisElementName == "guardianOccupation"||thisElementName == "guardianMiddleName"||thisElementName == "guardianHomeCity"||thisElementName == "guardianHomeAddress"||thisElementName == "guardianHomeZipcode"||thisElementName == "guardianMailingAddress"||thisElementName == "guardianMailingCity"||thisElementName == "guardianMailingZipcode"||thisElementName == "guardianHomePhoneArea"||thisElementName == "guardianCellArea"||thisElementName == "guardianDayPhoneArea"||thisElementName == "guardianEmail"||thisElementName == "guardianBusiness"||thisElementName == "guardianBusinessAddress"||thisElementName == "guardianBusinessCity"||thisElementName == "guardianBusinessZipcode"||thisElementName == "guardianBusinessArea"||thisElementName == "guardianBusinessEmail"||thisElementName == "parentSignature"||thisElementName == "guardianHomePhone3"||thisElementName == "guardianHomePhone4"||thisElementName == "guardianCell3"||thisElementName == "guardianCell4"||thisElementName == "guardianDayPhone3"||thisElementName == "guardianDayPhone4"||thisElementName == "guardianBusiness3"||thisElementName == "guardianBusiness4"||thisElementName == "motherHomePhone3"||thisElementName == "motherHomePhone4"||thisElementName == "motherCell3"||thisElementName == "motherCell4"||thisElementName == "motherDayPhone3"||thisElementName == "motherDayPhone4"||thisElementName == "motherBusiness3"||thisElementName == "motherBusiness4"||thisElementName == "fatherHomePhone3"||thisElementName == "fatherHomePhone4"||thisElementName == "fatherCell3"||thisElementName == "fatherCell4"||thisElementName == "fatherDayPhone3"||thisElementName == "fatherDayPhone4"||thisElementName == "fatherBusiness3"||thisElementName == "fatherBusiness4")){
			//alert("value is null");
			if( 
				thisElementName in oc([
							'grade', 
							'studentFirstName',
							'studentMiddleName',
							'studentLastName',
							'birthCity',
							'studentHomeAddress',
							'studentHomeCity',
							'studentHomeZipcode',
							'studentMailingAddress',
							'studentMailingCity',
							'studentMailingZipcode',
							'bestContactNumberArea',
							'bestContactNumber3',
							'bestContactNumber4',
							'firstLanguage',
							'mostLanguage',
							'languageRead',
							'languageWrite',
							'releaseFormSignature',
							'localSchool',
							'attendingSchool',
							'studentRace',
							'immunization'
						])
			) {
				alert("Please complete all required fields.--"+thisElementName+"--");
				document.forms[formName].elements[thisElementName].focus();
				document.forms[formName].elements[thisElementName].select();
				passed = false;
				return passed;
			}
		}else{
			//if the form element is not an unrequired blank, then see if it passes muster
			
			// validate mmddyyyy
			if (thisElementName in oc([
										'lastTetanus',
										'medicalExemptionDate',
										'religiousExemptionDate'
									])
			) {
				if (regMmddyyyy.test(thisElementValue)==false){
					alert("Please enter a valid date for the child's last Tetanus shot in mm/dd/yyyy format.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			
			// validate grade
			if (thisElementName  in oc([
										'grade',
										'sibling1Grade',
										'sibling2Grade',
										'sibling3Grade',
										'previousGrade'
									])
			) {
											
				if (regAnyNumbers.test(thisElementValue)==false || thisElementValue > 12){
					alert("Please enter a valid grade for the student in this field (a number less than 13).")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					//alert(thisElementName + " tests true!");
					passed = false;
					return passed;
				}
			}
			// validate fields that are text only
			if (thisElementName in oc([
										'studentFirstName',
										'studentMiddleName',
										'studentLastName',
										'birthCity',
										'studentHomeCity',
										'studentMailingCity',
										'motherLastName',
										'motherFirstName',
										'motherMiddleName',
										'motherOccupation',
										'motherHomeCity',
										'motherBusinessCity',
										'motherMailingCity',
										'fatherLastName',
										'fatherFirstName',
										'fatherMiddleName',
										'fatherOccupation',
										'fatherHomeCity',
										'fatherBusinessCity',
										'fatherMailingCity',
										'guardianLastName',
										'guardianFirstName',
										'guardianMiddleName',
										'guardianOccupation',
										'guardianHomeCity',
										'guardianBusinessCity',
										'guardianMailingCity',
										'parentSignature',
										'emergency1LastName',
										'emergency1FirstName',
										'emergency1MiddleName',
										'emergency1Relationship',
										'emergency2LastName',
										'emergency2FirstName',
										'emergency2MiddleName',
										'emergency2Relationship',
										'emergency3LastName',
										'emergency3FirstName',
										'emergency3MiddleName',
										'emergency3Relationship',
										'physician',
										'physicianCity',
										'release1LastName',
										'release1FirstName',
										'release1MiddleName',
										'release2LastName',
										'release2FirstName',
										'release2MiddleName',
										'release3LastName',
										'release3FirstName',
										'release3MiddleName',
										'release4LastName',
										'release4FirstName',
										'release4MiddleName',
										'sibling1LastName',
										'sibling1FirstName',
										'sibling1MiddleName',
										'sibling2LastName',
										'sibling2FirstName',
										'sibling2MiddleName',
										'sibling3LastName',
										'sibling3FirstName',
										'sibling3MiddleName',
										'recentSchoolCity',
										'firstLanguage',
										'mostLanguage',
										'languageRead',
										'languageWrite',
										'releaseFormSignature',
										'immunizationSignature',
										'medicalExemptionSignature',
										'religiousExemptionSignature'
									])
			) {
											
				if (regJustText.test(thisElementValue)==false){
					alert("Please enter only letters, spaces, and periods in this field.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			// validate fields that are various characters
			if (thisElementName in oc([	
										'studentHomeAddress',
										'studentMailingAddress',
										'motherHomeAddress',
										'motherMailingAddress',
										'motherBusinessAddress',
										'fatherHomeAddress',
										'fatherMailingAddress',
										'fatherBusiness',
										'motherBusiness',
										'fatherBusinessAddress',
										'guardianBusiness',
										'guardianHomeAddress',
										'guardianMailingAddress',
										'guardianBusinessAddress',
										'physicianAddress',
										'preferredHospital',
										'insurance',
										'policyNumber',
										'specialServices',
										'medicalCondition',
										'medication',
										'allergies',
										'medicaid',
										'emergencyFormSignature',
										'disabilitiesInfo',
										'recentSchool',
										'equipmentInfo',
										'religiousExemptionReason',
										'attendingSchool',
										'localSchool'
									])
			) {
				if (regTextAndCommonSymbols.test(thisElementValue)==false){
					alert("Please enter only letters, numbers, spaces, and common symbols in this field.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			// validate zip codes
			if (thisElementName in oc([	
										'studentHomeZipcode',
										'studentMailingZipcode',
										'motherHomeZipcode',
										'motherMailingZipcode',
										'motherBusinessZipcode',
										'fatherHomeZipcode',
										'fatherMailingZipcode',
										'fatherBusinessZipcode',
										'guardianHomeZipcode',
										'guardianMailingZipcode',
										'guardianBusinessZipcode',
										'physicianZipcode'
									])
			) {
				if (regZip.test(thisElementValue)==false){
					alert("Please enter a valid zip code in this field.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			// validate numbers only
			if (thisElementName in oc([
										'studentHomePhoneArea',
										'studentHomePhone3',
										'studentHomePhone4',
										'motherHomePhoneArea',
										'motherHomePhone3',
										'motherHomePhone4',
										'motherCellArea',
										'motherCell3',
										'motherCell4',
										'motherDayPhoneArea',
										'motherDayPhone3',
										'motherDayPhone4',
										'motherBusinessArea',
										'motherBusiness3',
										'motherBusiness4',
										'fatherHomePhoneArea',
										'fatherHomePhone3',
										'fatherHomePhone4',
										'fatherCellArea',
										'fatherCell3',
										'fatherCell4',
										'fatherDayPhoneArea',
										'fatherDayPhone3',
										'fatherDayPhone4',
										'fatherBusinessArea',
										'fatherBusiness3',
										'fatherBusiness4',
										'guardianHomePhoneArea',
										'guardianHomePhone3',
										'guardianHomePhone4',
										'guardianCellArea',
										'guardianCell3',
										'guardianCell4',
										'guardianDayPhoneArea',
										'guardianDayPhone3',
										'guardianDayPhone4',
										'guardianBusinessArea',
										'guardianBusiness3',
										'guardianBusiness4',
										'bestContactNumberArea',
										'bestContactNumber3',
										'bestContactNumber4',
										'emergency1Area',
										'emergency13',
										'emergency14',
										'emergency2Area',
										'emergency23',
										'emergency24',
										'emergency3Area',
										'emergency33',
										'emergency34',
										'emergency1OtherArea',
										'emergency1Other3',
										'emergency1Other4',
										'emergency2OtherArea',
										'emergency2Other3',
										'emergency2Other4',
										'emergency3OtherArea',
										'emergency3Other3',
										'emergency3Other4',
										'physicianPhoneArea',
										'physicianPhone3',
										'physicianPhone4',
										'afterHourArea',
										'afterHour3',
										'afterHour4'
									])
			) {											
				if (regAnyNumbers.test(thisElementValue)==false){
					alert("Please enter only numbers in this field.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			// validate emails
			if (thisElementName in oc([
										'motherEmail',
										'motherBusinessEmail',
										'fatherEmail',
										'fatherBusinessEmail',
										'guardianEmail',
										'guardianBusinessEmail'
									])
			) {
				if (regEmail.test(thisElementValue)==false){
					alert("Please enter a valid email address in this field.")
					document.forms[formName].elements[thisElementName].focus();
					document.forms[formName].elements[thisElementName].select();
					passed = false;
					return passed;
				}
			}
			//if (thisElementName == "hasSibling" && validateRadioButton(document.forms[formName].elements["hasSibling"]) == null){
			//	alert('Please select a whether the child has siblings.');
			//	passed = false;
			//	return passed;
			//}
			//if (thisElementName == "gender" && validateRadioButton(document.forms[formName].elements["gender"]) == null){
			//	alert('Please select a gender.');
			//	passed = false;
			//	return passed;
			//}
			//if (thisElementName == "contactPerson" && validateRadioButton(document.forms[formName].elements["contactPerson"]) == null){
			//	alert('Please select a contact person.');
			//	passed = false;
			//	return passed;
			//}
			if (thisElementName == "personLiveWith" && validateRadioButton(document.forms[formName].elements["personLiveWith"]) == null){
				alert('Please select the person with whom the child lives.');
				passed = false;
				return passed;
			}
			//if (thisElementName == "courtOrder" && validateRadioButton(document.forms[formName].elements["courtOrder"]) == null){
			//	alert("Please select the child's court order status.");
			//	passed = false;
			//	return passed;
			//}
			//if (thisElementName == "nonCustodialParent1" && validateRadioButton(document.forms[formName].elements["nonCustodialParent1"]) == null){
			//	alert("Please select the status of the Child's non-custodial parent.");
			//	passed = false;
			//	return passed;
			//}
			//if (thisElementName == "medicaidEligible" && validateRadioButton(document.forms[formName].elements["medicaidEligible"]) == null){
			//	alert("Please select whether or not the child is medicaid eligible.");
			//	passed = false;
			//	return passed;
			//}
			if (thisElementName == "dailyMedication" && validateRadioButton(document.forms[formName].elements["dailyMedication"]) == null){
				alert("Please select whether the child requires daily medication or not.");
				passed = false;
				return passed;
			}
			//I did not do these because they didn't seem to be required fields.  I'll add them if they are deemed required.
			//||thisElementName == "giftTest"
			//||thisElementName == "talentProgram"
			//||thisElementName == "disabilities"
			//||thisElementName == "specialEducation"
			//||thisElementName == "iep504Plan"
			//||thisElementName == "specializedEquipment"
			//||thisElementName == "esl"
			//||thisElementName == "expelled"
			//||thisElementName == "toBeExpelled"
			//Language test required
			if (thisElementName == "otherLanguage" && validateRadioButton(document.forms[formName].elements["otherLanguage"]) == null){
				alert("Please complete the Home Language Survey.");
				passed = false;
				return passed;
			}
			if (thisElementName == "otherSpokenLanguage" && validateRadioButton(document.forms[formName].elements["otherSpokenLanguage"]) == null){
				alert("Please complete the Home Language Survey.");
				passed = false;
				return passed;
			}
			if (thisElementName == "otherHomeLanguage" && validateRadioButton(document.forms[formName].elements["otherHomeLanguage"]) == null){
				alert("Please complete the Home Language Survey.");
				passed = false;
				return passed;
			}
		}
	}
	return passed;
	alert(passed);
}
function enrollFormSubmitStudent() {
	if (validateForm('enrollFormStudent') == true){
		window.location.href='?p=enroll_online&f=mother';
	}else{
		return false;
	}
}
function enrollFormSubmitStudent2(resp) {

}
function enrollFormSubmitMother() {
	if (validateForm('enrollFormMother') == true){
		window.location.href='?p=enroll_online&f=father';
	}else{
		return false;
	}
}
function enrollFormSubmitMother2(resp) {
	//what is this function for?
}
function enrollFormSubmitFather() {
	if (validateForm('enrollFormFather') == true){
		window.location.href='?p=enroll_online&f=guardian';
	}else{
		return false;
	}
}
function enrollFormSubmitFather2(resp) {
	//what is this function for?

}
function enrollFormSubmitGuardian() {
	if (validateForm('enrollFormGuardian') == true){
		window.location.href='?p=enroll_online&f=parentGuardian';
	}else{
		return false;
	}	
}
function enrollFormSubmitGuardian2(resp) {
	//what is this function for?

}
function enrollFormSubmitParentGuardian() {
	if (validateForm('enrollFormGuardian') == true){
		window.location.href='?p=enroll_online&f=emergency';
	}else{
		return false;
	}	
}
function enrollFormSubmitParentGuardian2(resp) {
	//what is this function for?

}
function enrollFormSubmitEmergency() {
	if (validateForm('enrollFormEmergency') == true){
		window.location.href='?p=enroll_online&f=profile';
	}else{
		return false;
	}	
}
function enrollFormSubmitEmergency2(resp) {
	//what is this function for?

}
function enrollFormSubmitProfile() {
	if (validateForm('enrollFormProfile') == true){
		window.location.href='?p=enroll_online&f=records';
	}else{
		return false;
	}	
}
function enrollFormSubmitProfile2(resp) {
	//what is this function for?

}
function enrollFormSubmitRecords() {
	if (validateForm('enrollFormRecords') == true){
		window.location.href='?p=enroll_online&f=complete';
	}else{
		return false;
	}	
}
function enrollFormSubmitRecords2(resp) {
	//what is this function for?

}
