	function validSSN(inp, fld){
		var len = inp.value.length;
		if(len == 0){
			alert("Please input " + fld);
	   		inp.focus();
			return false;
		} 
		else if(len !== 9){
			alert("Social Security Number must be a 9 digit number.");
	   		inp.focus();
			return false;
		} 
		else {
			for (var j = 0; j < len; j++) {
				var ch2 = inp.value.substring(j, j + 1);
				if ((ch2 < "0") ||  (ch2 > "9")) {
					alert("Input a valid " + fld);
					inp.select()
					inp.focus();
					return false;
				}
			}
			return true;
		}
	}

	function validString(inp, fld, spaceOK, numOK){
		var len = inp.value.length;
		if(len == 0){
			alert("Please input " + fld);
	   		inp.focus();
			return false;
		}
		else {
			for (var j = 0; j < len; j++) {
				var ch2 = inp.value.substring(j, j + 1);
				if (
					(ch2 == " " && !spaceOK) ||
					(isDigit(ch2) && !numOK) || 
					!isLetterOrDigitOrSpace(ch2)
					) {
					alert("Please Input a valid " + fld);
					inp.select();
					inp.focus();
					return false;
				}
			}
			return true;
		}
	}

	function validPassword(inp, fld){
		var msg = "";
		var dc = 0; // digit counter
		
		var len = inp.value.length;
		for (var j = 0; j < len; j++) {
			var ch2 = inp.value.substring(j, j + 1);
			if (ch2 == " ") { msg = "Password cannot contain spaces." }
			if (!isLetterOrDigit(ch2)) { msg = "Password can only contain digits and leters." }
			if (isDigit(ch2)) {dc+=1}
		}
		if (dc == 0) { msg = "Password must contain at least one digit." }
		if (msg != "") {
			alert(msg + " Please Input a valid " + fld);
			inp.select();
			inp.focus();
			return false;
			}
		return true;
	}


	function validPhone(inp, fld){
		var msg = "";
		var dc = 0; // digit counter
		
		var len = inp.value.length;
		for (var j = 0; j < len; j++) {
			var ch2 = inp.value.substring(j, j + 1);
			if (isDigit(ch2)) {dc+=1}
		}
		if (dc < 10 ) { msg = "Phone Number must contain at least 10 digits." }
		if (msg != "") {
			alert(msg + " Please Input a valid " + fld);
			inp.select();
			inp.focus();
			return false;
			}
		return true;
	}


	function isLetter (c)
	{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
	}

	function isDigit (c)
	{   return ((c >= "0") && (c <= "9"))
	}

	function isLetterOrDigit (c)
	{   return (isLetter(c) || isDigit(c))
	}
	
	function isLetterOrDigitOrSpace (c)
	{   return (isLetter(c) || isDigit(c) || " ")
	}

	function isaPosNum(s) {
		return (parseInt(s) > 0)
	}

	function validNumber(inp, fld, len) {
		var returnVal = false;
  		var msg = ""
		
		if (inp.value.length == 0)
			msg = "Please enter " + fld
		else if (inp.value.length !== len && len !== 0)
			msg = fld + " must be a " + len + " digit number."
		else if (!isaPosNum(inp.value) || isNaN(inp.value))
			msg = "Please enter a positive number"
		else 
			returnVal = true
			
		if (msg !== "") {
			alert(msg)
			inp.focus()  
		}
		return returnVal
	}
	
	function inpLen(inp, fld, lenMin, lenMax) {
		var returnVal = false;
  		var msg = ""
		
		if (inp.value.length == 0)
			msg = "Please enter " + fld
		else if (inp.value.length < lenMin) 
			msg = fld + " must be at least " + lenMin + " characters long."
		else if (inp.value.length > lenMax) 
			msg = fld + " must be " + lenMax + " characters long or less."
		else 
			returnVal = true
			
		if (msg !== "") {
			alert(msg)
			if (inp.type == "text") {
				inp.select()
			}
			inp.focus()  
		}
		return returnVal
	}
	
	function valueSelected(inp, fld) {
		if (inp.value == "") {
			alert("Please select " + fld)
			inp.focus()  
			return false
		} else
		return true
	}

	function validEmail(inp) {
		var emailStr = inp.value
		var len = emailStr.length

		if(len == 0){
			alert("Please enter Email address.")
	   		inp.focus()
			return false
		}
		else {
			var emailPat=/^(.+)@(.+)$/
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			var validChars="\[^\\s" + specialChars + "\]"
			var quotedUser="(\"[^\"]*\")"
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
			var atom=validChars + '+'
			var word="(" + atom + "|" + quotedUser + ")"
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

			var matchArray=emailStr.match(emailPat)
			if (matchArray==null) {
				alert("Email address seems incorrect (check @ and .'s)")
				inp.select()
				inp.focus()
				return false
			}
			var user=matchArray[1]
			var domain=matchArray[2]

			// See if "user" is valid 
			if (user.match(userPat)==null) {
				// user is not valid
				alert("The username doesn't seem to be valid.")
				inp.select()
				inp.focus()  
				return false
			}

			/* if the e-mail address is at an IP address (as opposed to a symbolic
			   host name) make sure the IP address is valid. */
			var IPArray=domain.match(ipDomainPat)
			if (IPArray!=null) {
				// this is an IP address
				for (var i=1;i<=4;i++) {
					if (IPArray[i]>255) {
						alert("Destination IP address is invalid!")
						inp.select()
						inp.focus() 
						return false
					}
				}
				return true
			}
			// Domain is symbolic name
			var domainArray=domain.match(domainPat)
			if (domainArray==null) {
				alert("The domain name doesn't seem to be valid.")
				inp.select()
				inp.focus()
 				return false
			}

			/* Now we need to break up the domain to get a count of how many atoms
			   it consists of. */
			var atomPat=new RegExp(atom,"g")
			var domArr=domain.match(atomPat)
			var len=domArr.length
			if (domArr[domArr.length-1].length<2 || 
				domArr[domArr.length-1].length>3) {
				// the address must end in a two letter or three letter word.
   				alert("The address must end in a three-letter domain, or two letter country.")
				inp.select()
				inp.focus()  
 				return false
			}
			return true
	}
}
