//JavaScript file included in display_content_signup() function. Validates
//form data before it is inserted into the database.

//Prevent patrons from using the browser's back button from this page (to protect privacy
//of information for those registering on public access computers)
window.history.forward(1);

//check if value is text only
function isAlpha(passedVal) {
				 var alpha = " .-'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
				 if (passedVal == "") {
				 		return false
				 }
				 for(var i =0; i < passedVal.length; i++) {
				 temp = passedVal.substring(i,i+1)
				 if(alpha.indexOf(temp) == -1 &&
				 passedVal != "") {
				 			return false
								}
							}
							return true
					}
					
function validFirst(inFirst) {
					if (isAlpha(inFirst)) {
						 return true
					}
					return false
			}
			
function validLast(inLast) {
					if (isAlpha(inLast)) {
						 return true
					}
					return false
			}
			
//check to make sure an age has been selected from the dropdown menu
function validAge(age) {
				 if (age == "") {
					  return false
				}
				return true
			}
			
//check to make sure an area code has been selected from the dropdown menu
function validAreacode(areacode) {
				 if (areacode == "") {
					  return false
				}
				return true
			}
			
//check phone prefix for numerical characters and length
function validPhoneprefix(phone_prefix) {
				var numbers = "0123456789";
				if (phone_prefix == "") {
					  return false
				}
				if ((phone_prefix.length != "") && (phone_prefix.length !=3)) {
						return false
				}
				for(var i=0; i < phone_prefix.length; i++) {
				temp = phone_prefix.substring(i,i+1)
				if(numbers.indexOf(temp) == -1 && phone_prefix.length != "") {
						return false
							}
						}
						return true
				}
				
//check phone number for numerical characters and length
function validPhonemain(phone_main) {
				var numbers = "0123456789";
				if (phone_main == "") {
					  return false
				}
				if ((phone_main.length != "") && (phone_main.length !=4)) {
						return false
				}
				for(var i=0; i < phone_main.length; i++) {
				temp = phone_main.substring(i,i+1)
				if(numbers.indexOf(temp) == -1 && phone_main.length != "") {
						return false
							}
						}
						return true
				}
			
//check to make sure a branch has been selected from the dropdown menu
function validBranch(branch) {
				 if (branch == "") {
					  return false
				}
				return true
			}
			
//check to make sure a school has been selected from the dropdown menu
function validSchool(school) {
				 if (school == "") {
					  return false
				}
				return true
			}
			
function checkRequired(register) {
//check first name and last name text fields
				if (!validFirst(register.first.value)) {
				alert("Invalid first name")
				register.first.focus()
				register.first.select()
				return false
			}
	
	 			if (!validLast(register.last.value)) {
				alert("Invalid last name")
				register.last.focus()
				register.last.select()
				return false
			}
			
//check age
				if (!validAge(register.age.value)) {
				alert("Please Select Your Age from the List")
				register.age.focus()
				return false
			}
			
//check area code
				if (!validAreacode(register.areacode.value)) {
				alert("Please Select Your Area Code from the List")
				register.areacode.focus()
				return false
			}
			
//check phone prefix
				if(!validPhoneprefix(register.phone_prefix.value))  {
				alert("Invalid Phone Number")
				register.phone_prefix.focus()
				register.phone_prefix.select()
				return false
			}
			
//check phone number
				if(!validPhonemain(register.phone_main.value))  {
				alert("Invalid Phone Number")
				register.phone_main.focus()
				register.phone_main.select()
				return false
			}
			
//check branch
				if (!validBranch(register.branch.value)) {
				alert("Please Select Your Library from the List")
				register.branch.focus()
				return false
			}
			
//check school
				if (!validSchool(register.school.value)) {
				alert("Please Select Your School")
				register.school.focus()
				return false
			}
}
