//Form validation script
//By: Aaron LaMontagne
//Date: 03-30-05
//File Name: validation.js

function agreeToTerms(){
	var form = document.form;
	
	if (form.agree.checked == false){
		alert("You must agree to the above Terms.");
		form.agree.focus()
		return false;
	}
		return checkForm();
}//ends Function agreeToTerms()



function checkForm(){
	var form = document.form;
	
	//check contact fields
	if(form.first_name.value == ""){
		alert("Please enter your 'First Name'");
		form.first_name.focus()
		return false;
	}//ends if
	
	if(form.mi.value == ""){
		alert("Please enter your 'Middle Initial'");
		form.mi.focus()
		return false;
	}//ends if
	
	if(form.last_name.value == ""){
		alert("Please enter your 'Last Name'");
		form.last_name.focus()
		return false;
	}//ends if
	
	if(form.address.value == ""){
		alert("Please enter an 'Address'");
		form.address.focus()
		return false;
	}//ends if
	
	if(form.city.value == ""){
		alert("Please enter an 'City'");
		form.city.focus()
		return false;
	}//ends if
		
	//if(form.state.value == "NONE"){
		//alert("Please select an 'State'.")
		//form.state.focus();
		//return false;
	//}//ends if
	
	if(form.zip.value == ""){
		alert("Please enter a 'Zip'");
		form.zip.focus()
		return false;
	}//ends if
	
	if(form.phone.value == ""){
		alert("Please enter a 'Phone Number'");
		form.phone.focus()
		return false;
	}//ends if
	
	if(form.cell.value == ""){
		alert("Please enter a 'Cell Number'");
		form.cell.focus()
		return false;
	}//ends if
	
	if(form.email.value == ""){
		alert("Please enter an 'Email Address'");
		form.email.focus()
		return false;
	}//ends if
	if ((form.email.value==null)||(form.email.value=="")){
		alert("Please enter an 'Email Address'")
		form.email.focus()
		return false
	}
	if (echeck(form.email.value)==false){
		form.email.value=""
		form.email.focus()
		return false
	}
	
	if(form.birth_date.value == ""){
		alert("Please enter your 'Birth Date'");
		form.birth_date.focus()
		return false;
	}//ends if
	
	if(form.ssn.value == ""){
		alert("Please enter your 'Social Security Number'");
		form.ssn.focus()
		return false;
	}//ends if
	
	//Start Validation for emergency contact information
	
	if(form.emergency_contact_1_name.value == ""){
		alert("Please enter an 'Emergency Contact Name' for your first contact");
		form.emergency_contact_1_name.focus()
		return false;
	}//ends if
	
	if(form.emergency_contact_1_day_phone.value == ""){
		alert("Please enter an 'Daytime Phone Number' for your first contact");
		form.emergency_contact_1_day_phone.focus()
		return false;
	}//ends if
	
	if(form.emergency_contact_1_evening_phone.value == ""){
		alert("Please enter an 'Evening Phone Number' for your first contact");
		form.emergency_contact_1_evening_phone.focus()
		return false;
	}//ends if
/*	
	if(form.emergency_contact_2_name.value == ""){
		alert("Please enter an 'Emergency Contact Name' for your second contact");
		form.emergency_contact_2_name.focus()
		return false;
	}//ends if
	
	if(form.emergency_contact_2_day_phone.value == ""){
		alert("Please enter an 'Daytime Phone Number' for your second contact");
		form.emergency_contact_2_day_phone.focus()
		return false;
	}//ends if
	
	if(form.emergency_contact_2_evening_phone.value == ""){
		alert("Please enter an 'Evening Phone Number' for your second contact");
		form.emergency_contact_2_evening_phone.focus()
		return false;
	}//ends if
	//Ends Validation of emergency contacts
	*/

	//Check the check boxes for Student Type
	var myckbox = false;
	for(var i = 0; i<form.student_type.length; i++){
		if(form.student_type[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please check if your a 'New or Returning Student'.");
		return false;
	}

	
	//Check the check boxes for type of application
	var myckbox = false;
	for(var i = 0; i<form.applying_for.length; i++){
		if(form.applying_for[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please check your 'Academic Term'.");
		return false;
	}
	
	//Check the check boxes for type of apartment type
	var myckbox = false;
	for(var i = 0; i<form.apartment_type.length; i++){
		if(form.apartment_type[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please check your 'Apartment Type Requested'.");
		return false;
	}
	
	//Check the check boxes for type of gender
	var myckbox = false;
	for(var i = 0; i<form.gender.length; i++){
		if(form.gender[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please tell us 'Your Gender'.");
		return false;
	}


	//Check the check boxes for parking space needed
	var myckbox = false;
	for(var i = 0; i<form.parking_space.length; i++){
		if(form.parking_space[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please tell us 'If you need a Parking Space'.");
		return false;
	}
	
	//Check the check boxes Disabled Service
	var myckbox = false;
	for(var i = 0; i<form.disable.length; i++){
		if(form.disable[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please tell us if you need 'Disabled/Medical Accessibility'.");
		return false;
	}
	
	//Check the check boxes Dismissed
	var myckbox = false;
	for(var i = 0; i<form.dismissed.length; i++){
		if(form.dismissed[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please tell us if you have ever been 'Dismissed from another college'.");
		return false;
	}

	//Check the check boxes Convicted
	var myckbox = false;
	for(var i = 0; i<form.convicted.length; i++){
		if(form.convicted[i].checked == true){
			myckbox = true;
		}
	}
	if (myckbox == false) {
		alert("Please tell us if you have ever been 'Convicted of a felony'.");
		return false;
	}

}//ends checkform

function otherSchool(){
		//Check to see if the select Other as the school
	if(form.school.options[form.school.selectedIndex].value == "Other School"){
		form.other_school.disabled = false;
	} else {
		form.other_school.disabled = true;
	}//ends if
}//ends otherSchool()

function checkYears(){
	//Check to see years requirement is met
	if(form.year_of_study.options[form.year_of_study.selectedIndex].value == 1 && form.school.options[form.school.selectedIndex].value == "Albany College of Pharmacy"){
		form.year_of_study.options.value = 2
		alert("Albany College of Pharmacy Students must be in at least 3rd year");
		form.year_of_study.focus()
	}//ends if
}//ends otherSchool()

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
 		 return true					
	}//ends echeck
