//Begin Sub Functions

//subNotEmpty: Evaluates if a submission is present or blank

function subNotEmpty(inputStr){
	if (inputStr == null || inputStr == ""){
		return false
	}
	return true
}

//subAreaFormat: Evaluates if a submission is in the correct Area Code format (3 numbers)

function subAreaFormat(inputStr){
	if (inputStr.length != 3){
		return false
	}
	if (inputStr.length == 3){
		for (var x = 0; x < inputStr.length; x++){
			var eachChar = inputStr.charAt(x)
			if (eachChar < "0" || eachChar > "9"){
				return false
			}
		}
	}
	return true
}

//subPhoneFormat: Evaluates if a submission is in the correct Phone Number format (7 characters all numbers or 8 characters xxx-xxxx)

function subPhoneFormat(inputStr){
	if (inputStr.length < 7 || inputStr.length > 8){
		return false
	}
	if (inputStr.length == 7){
		for (var x = 0; x < inputStr.length; x++){
			var eachChar = inputStr.charAt(x)
			if (eachChar < "0" || eachChar > "9"){
				return false
			}
		}
	}
	if (inputStr.length == 8){
		for (x = 0; x < inputStr.length; x++){
			var eachChar = inputStr.charAt(x)
			if (x == 3 && eachChar == "-"){
				continue
			}
			if (eachChar < "0" || eachChar > "9"){
				return false
			}
		}
	}
	return true
}


//Begin main validation functions

//isAlphaRequired: Evaluates if a submission is present and all letters

function isAlphaRequired(){
	for (i = 0; i < isAlphaRequiredList.length; i++){
		var inputStr = eval(isAlphaRequiredList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			alert("Your " + isAlphaRequiredMsg[i] +  " is required.")
			eval (isAlphaRequiredList[i] + ".focus")
			return false
		}
		for (x = 0; x < inputStr.length; x++){
			var eachChar = inputStr.charCodeAt(x)
			if (eachChar >= "48" && eachChar <= "57"){
				alert("Your " + isAlphaRequiredMsg[i] + " should not contain any numbers.")
				eval (isAlphaRequiredList[i] + ".focus")
				return false
			}
		}
	}
	return true
}

//isNotEmpty: Evaluates a submission to make sure it is present (not empty)

function isNotEmpty(){
	for (var i = 0; i < isNotEmptyList.length; i++){
		var inputStr = eval(isNotEmptyList[i] + ".value")
		if (!subNotEmpty(inputStr)){
			alert("Your " + isNotEmptyMsg[i] + " is required.")
			eval(isNotEmptyList[i] + ".focus()")
			return false
		}
	}
	return true
}

//isAreaCodeRequired: First checks to see if area code is present then checks the format

function isAreaCodeRequired(){
	for (var i = 0; i < isAreaCodeRequiredList.length; i++){
		var inputStr = eval(isAreaCodeRequiredList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			alert("Your " + isAreaCodeRequiredMsg[i] + " is required.")
			eval (isAreaCodeRequiredList[i] + ".focus")
			return false
		}
		if (!subAreaFormat(inputStr)){
			alert("Your " + isAreaCodeRequiredMsg[i] + " seems to be incorrect.")
			eval (isAreaCodeRequiredList[i] + ".focus")
			return false
		}
	}
	return true
}

//isPhoneRequired: Checks to see if a phone is present then checks the format

function isPhoneRequired(){
	for (i = 0; i < isPhoneRequiredList.length; i++){
		var inputStr = eval(isPhoneRequiredList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			alert("Your " + isPhoneRequiredMsg[i] +  " is required.")
			eval (isPhoneRequiredList[i] + ".focus()")
			return false
		}
		if (!subPhoneFormat(inputStr)){
			alert("Your " + isPhoneRequiredMsg[i] + " seems to be incorrect.")
			eval (isPhoneRequiredList[i] + ".focus()")
			return false
		}
	}
	return true
}

//isAreaCodeNotReq: Checks to see if an area code that is not required is present if it is requires the rest of the phone number

function isAreaCodeNotReq(){
	for (var i = 0; i < isAreaCodeNotReqList.length; i++){
		var inputStr = eval(isAreaCodeNotReqList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			var secondStr = eval(isPhoneNotReqList[i] + ".value")
			if (!subNotEmpty(secondStr)){
				return true
			} else {
				alert("You have specified a " + isPhoneNotReqMsg[i] + " but have not specified an area code.")
				eval (isAreaCodeNotReqList[i] + ".focus")
				return false
			}
		}
		if (!subAreaFormat(inputStr)){
			alert("Your " + isAreaCodeNotReqMsg[i] + " seems to be incorrect.")
			eval (isAreaCodeNotReqList[i] + ".focus")
			return false
		}
	}
	return true
}

//isPhoneNotReq: Checks to see if a phone number which is not required is present  If yes, makes sure the area code is present

function isPhoneNotReq(){
	for (i = 0; i < isPhoneNotReqList.length; i++){
		var inputStr = eval(isPhoneNotReqList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			var secondStr = eval(isAreaCodeNotReqList[i] + ".value")
			if (!subNotEmpty(secondStr)){
				return true
			} else {
				alert("You have specified a " + isAreaCodeNotReqMsg[i] + " but you have not specified a number.")
				eval (isPhoneNotReqList[i] + ".focus")
				return false
			}
		}
		if (!subPhoneFormat(inputStr)){
			alert("Your " + isPhoneNotReqMsg[i] + " seems to be incorrect.")
			eval (isPhoneNotReqList[i] + ".focus()")
			return false
		}
		
	}
	return true
}

//isSSN: Checks to see if a submission is in the correct SSN format

function isSSN(){
	for (var i = 0; i < isSSNList.length; i++){
		var inputStr = eval(isSSNList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			alert("Your " + isSSNMsg[i] + " is required.")
			eval (isSSNList[i] + ".focus()")
			return false
		}
		if (inputStr.length < 11 || inputStr.length > 11){
			alert("Your " + isSSNMsg[i] + " seems to be incorrect.")
			eval (isSSNList[i] + ".focus()")
			return false
		}
		if (inputStr.length == 11){
			for (var x = 0; x < inputStr.length; x++){
				var eachChar = inputStr.charAt(x)
				if (x == 3 && eachChar == "-" || x == 3 && eachChar == " "){
					continue
				}
				if (x == 6 && eachChar == "-" || x == 6 && eachChar == " "){
					continue
				}
				if (eachChar < "0" || eachChar > "9"){
					alert("Your " + isSSNMsg[i] + " seems to be incorrect.")
					eval (isSSNList[i] + ".focus()")
					return false
				}
			}
		}
	}
	return true
}

//isPasswordCorrect: Checks to see if a password and the re-typed password match

function isPasswordCorrect(){
	if (eval(isPasswordCorrectList[0] + ".value") != eval(isPasswordCorrectList[1] + ".value")){
		alert("Your " + isPasswordCorrectMsg[0] + " do not match. Please try again.")
		eval (isPasswordCorrectList[0] + ".focus()")
		return false
	}
	return true
}

//isValidEmail: checks to see if a submission is in the correct email format

function isValidEmail () {
	for (var i = 0; i < isValidEmailList.length; i++){
		var inputStr = eval(isValidEmailList[i] + ".value")
		var inputStr = inputStr.toString()
		if (!subNotEmpty(inputStr)){
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var firstChars=validChars
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom="(" + firstChars + validChars + "*" + ")"
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=inputStr.match(emailPat)
		if (matchArray==null) {
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) {
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
	    			if (IPArray[i]>255) {
					alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
					eval (isValidEmailList[i] + ".focus()")
					return false
	    			}
    			}
    			return true
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
		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) {
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
		//if (domArr[domArr.length-1].length==2 && len<2) {
		//	alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
		//	eval (isValidEmailList[i] + ".focus()")
		//	return false
		//}
		if (domArr[domArr.length-1].length==3 && len<2) {
			alert("Your " + isValidEmailMsg[i] + " seems to be incorrect.")
			eval (isValidEmailList[i] + ".focus()")
			return false
		}
	}
	return true;
}

//isOtherRequired: Checks to see if a field is other then checks to see if other is specified

function isOtherRequired(){
	for (var i = 0; i < isOtherRequiredList.length; i++){
		var inputStr = eval(isOtherRequiredList[i] + ".value")
		var inputStr = inputStr.toString()
		
		if (!subNotEmpty(inputStr)){
			alert("Your " + isOtherRequiredMsg[i] + " is required.")
			return false

		} else {
				
			var valueStr = eval(isOtherRequiredList[i] + ".text")
			
			if ( valueStr == "Other" ){
			
			//kw 3/13/2003
			//TEMP CODE UNTIL WE ADD AN OTHER FOR POSITION
			//
			var valueMsg = OtherFieldMsg[i]

			if ( !(valueMsg.length == "17") ) {
			
				var secondStr = eval(OtherFieldList[i] + ".value")
				if (!subNotEmpty(secondStr)){
					alert("Please specify " + OtherFieldMsg[i] + ".")
					eval (OtherFieldList[i] + ".focus()")
					return false
				}
			}//kw 3/13/2003
				
				
			} else {
				var secondStr = eval(OtherFieldList[i] + ".value")
				if (subNotEmpty(secondStr)){
					alert("You have specified a "+ isOtherRequiredMsg[i] + " from the drop down menu and should not specify "  + OtherFieldMsg[i] + ".")
					eval (OtherFieldList[i] + ".focus()")
					return false
				}

			}//end other field message
		}

	} // end loop
	return true
} // end function



function isRadioRequired (){
	for (var i = 0; i < isRadioRequiredList.length; i++){
		var inputStr = isRadioRequiredList[i]
		for ( var j = 0; j < eval(inputStr).length; j++ ){
			if ( eval(inputStr)[j].checked ){
				return true
				break;
			} 
		}
		alert("Please specify " + isRadioRequiredMsg[i] + "." );
		return false
		break;
	}
	return true
}

			
function dispatcher(validationFunc){
	this.doValidate = validationFunc
}

var dispatchLookup = new Array()
dispatchLookup["isNotEmpty"] = new dispatcher(isNotEmpty)
dispatchLookup["isPhoneRequired"] = new dispatcher(isPhoneRequired)
dispatchLookup["isPhoneNotReq"] = new dispatcher(isPhoneNotReq)
dispatchLookup["isAreaCodeRequired"] = new dispatcher(isAreaCodeRequired)
dispatchLookup["isAreaCodeNotReq"] = new dispatcher(isAreaCodeNotReq)
dispatchLookup["isSSN"] = new dispatcher(isSSN)
dispatchLookup["isPasswordCorrect"] = new dispatcher(isPasswordCorrect)
dispatchLookup["isValidEmail"] = new dispatcher(isValidEmail)
dispatchLookup["isOtherRequired"] = new dispatcher(isOtherRequired)
dispatchLookup["isAlphaRequired"] = new dispatcher(isAlphaRequired)
dispatchLookup["isRadioRequired"] = new dispatcher(isRadioRequired)

