/*validation.js*/
//method to test if a value passed in is blank

function isBlank(testValue) {
	if(testValue == "") {
		return true;
	} else {
		var allSpaces = new Boolean(true);
		//look through and make sure not all spaces
		findChars:
		for(i = 0;  i < testValue.length; i++) {
			if(testValue.charAt(i) != " ") {
				allSpaces = new Boolean(false);
				break findChars;
			}
		}
		
		//nothing but spaces
		if(allSpaces == true) {
			return true;
		}
	}
	
	return false;
}

//method to trim a string
function trim(testValue) {
	if(!testValue) {
		return null;
	} else if(isBlank(testValue)) {
		return null;
	} else {
	
		//trim leading spaces
		while(testValue.charAt(0) == ' ') {
			testValue = testValue.substring(1);
		}
		//trim trailing spaces
		while(testValue.charAt(testValue.length-1) == ' ') {
			testValue = testValue.substring(0,testValue.length-2);
		}

	}
	
	return testValue;
}

//method to determine if a string is all Alphabetic
function isAlpha(testValue) {
	if(!testValue) {
		return false;
	} else {

		var alphas = {"A":"A","B":"B","C":"C","D":"D","E":"E","F":"F","G":"G","H":"H","I":"I","J":"J","K":"K","L":"L","M":"M","N":"N","O":"O","P":"P","Q":"Q","R":"R","S":"S","T":"T","U":"U","V":"V","W":"W","X":"X","Y":"Y","Z":"Z","a":"a","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q":"q","r":"r","s":"s","t":"t","u":"u","v":"v","w":"w","x":"x","y":"y","z":"z"};

		//loop through characters to make sure not a char and not a decimal
		testAlpha:
		for(k = 0; k < testValue.length; k++) {
			if(!(testValue.charAt(k) in alphas)) {
				return false;
			}
		}
	}
	
	return true;
}

//method to determine if a string is all Numeric characters.  Not the same as isnan = no "-" or "." allowed
function isNumeric(testValue) {
	if(!testValue) {
		return false;
	} else {

		//loop through characters to make sure only numbers
		testNumeric:
		for(p = 0; p < testValue.length; p++) {
			if(isNaN(testValue.charAt(p))) {
				return false;
			}
		}
	}
	
	return true;
}

//method to determine if a string is AlphaNumeric
function isAlphaNum(testValue) {
	if(!testValue) {
		return false;
	} else {

		//loop through characters to make sure not a char and not a decimal
		testAlpha:
		for(n = 0; n < testValue.length; n++) {
			if(!(isAlpha(testValue.charAt(n)) || !isNaN(testValue.charAt(n)))) {
				return false;
			}
		}
	}
	
	return true;
}

//method to determine if a string is all Characters
function isCharacter(testValue) {
	if(!testValue) {
		return false;
	} else {

		//loop through characters to make sure not an AlphaNum
		testCharacters:
		for(m = 0; m < testValue.length; m++) {
			if(isAlphaNum(testValue.charAt(m))) {
				return false;
			}
		}
	}
	
	return true;
}


//method to determine of a test value matches a mask
//true if a match of format
//false if not a match
function matchesMask(testValue,mask) {
	if(!testValue && !mask) {
		alert("matchesMask() called without arguments");
		return false;
	} else if (!testValue) {
		alert("matchesMask() called without testValue");
		return false;
	} else if (!mask) {
		alert("matchesMask() called without mask");
		return false;
	} else {
		//not a match if lengths not identical
		if(testValue.length != mask.length) {
			return false;
		} else {
		
			//they are the same length, so char-by-char match
			//if char in a mask is '*', corresponding char in testValue can be anything
			//if char in a mask is '#', corresponding char in testValue must be a digit
			//if char in a mask is '?', corresponding char in testValue must be a letter
			//if char in a mask is '%', corresponding char in testValue must be a alphanumeric
			//if char in a mask is '.', corresponding char in testValue must be a character
			//otherwise, char must be a literal match

			var iterator = new Number(0);

			testMask:
			for(iterator = 0; iterator < testValue.length; iterator++) {
				if(mask.charAt(iterator) == '*') {
					continue testMask;
				}
				
				if(mask.charAt(iterator) == '#'){
					if(isNaN(testValue.charAt(iterator))) {
						return false;
					} else {
						continue testMask;
					}
				}
				
				if(mask.charAt(iterator) == '?') {
					if(!isAlpha(testValue.charAt(iterator))) {
						return false;
					} else {
						continue testMask;
					}
				}
				
				if(mask.charAt(iterator) == '.') {
					if(!isChar(testValue.charAt(iterator))) {
						return false;
					} else {
						continue testMask;
					}
				}
				
				if(mask.charAt(iterator) != testValue.charAt(iterator)) {
					return false;
				}
			}
		}
	}
	return true;
}

//method for validating if a string is an e-mail address
//depends:  utilities.js
function isValidEmail(testValue) {
	//not blank
	if(isBlank(testValue)) {
		return false;
	} else {
		//contains an "@" and only one at at least one character from first char and three char from last char
		if(testValue.indexOf('@') >= 1 && testValue.lastIndexOf('@') == testValue.indexOf('@') && testValue.indexOf('@') <= (testValue.length-3)) {
		
			//find the dots
			var dotIndex = findChar(testValue,'.');
			var atIndex = testValue.indexOf('@');
			
			//no dots?
			if(dotIndex.length == 0) {
				return false;
			} else {
				//make sure the dots are in valid places
				//not on either side of the @ and not the last character
				if(testValue.charAt(atIndex-1) == '.' || testValue.charAt(atIndex+1) == '.' || testValue.charAt(testValue.length-1) == '.') {
					return false;
				}
				
				//at least one dot after the "@"
				if(testValue.substring(atIndex).indexOf('.') < 0) {
					return false;
				}
				
				//not next to each other
				if(dotIndex.length > 1) {
					for(i = 0; i < dotIndex.length; i++) {
						if(dotIndex[i] == dotIndex[i+1] -1) {
							return false;
						}
					}
				}
				
				//at this point, you have a valid e-mail address
			}
		} else {
			//no valid '@'
			return false;
		}
	}

	//if you got here, you're valid
	return true;

}

//method for validating if a string is a phone number
//depends:  utilities.js
function isValidPhone(testValue) {

	//trim the testvalue
	testValue = trim(testValue);

	var isValid = new Boolean(false);

	//valid formats
	var validFormats = new Array(
	"##########",
	"1##########",
	"### ### ####",
	"###-###-####",
	"(###)#######",
	"1(###)#######",
	"(###)###-####",
	"(###)### ####",
	"1(###)#######",
	"1 ### ### ####",
	"1-###-###-####",
	"1(###)###-####",
	"1(###)###-####",
	"1-(###)###-####",
	"1 (###)### ####",
	"1 (###) ### ####",
	"1 (###) ###-####",
	"1-(###)-###-####"
	);

	//not blank
	if(isBlank(testValue)) {
		alert("isValidPhone() called with null testValue");
		return false;
	}  else  {
		//check if testValue is a valid format 
		checkValid:
		for(i = 0; i < validFormats.length; i++) {
			if(matchesMask(testValue,validFormats[i]) == true) {
				isValid = new Boolean(true);
				break checkValid;
			}
		}
	}
	
	if(isValid == true) {
		return true;
	} else {
		return false;
	}
}
