////////////////////////////////////////////////////////////////////////////////
//
//	supervalidator.js
//	Version 1.0
//	Revised 6/22/02
//	Copyright 2002 by Kevin Harville
//	www.kevinharville.com
//	Use granted to any party leaving this header
//
//	Associated file:  supervalidator.htm
//
////////////////////////////////////////////////////////////////////////////////

//	VARIABLES ///////////////////////////////////////////////////////////////////
var strError =""
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var namePunctuation = " '-."
var colorPunctuation = "#"
var anyName = lowercaseLetters + uppercaseLetters + namePunctuation
var anyColor = colorPunctuation + digits + uppercaseLetters + lowercaseLetters
var phoneDelimiters = "()- x+ ";
//You may wish to remove letters from var anyPhone.  Currently this allows "1-800-Web-Pros", etc.
var anyPhone = digits + phoneDelimiters + lowercaseLetters + uppercaseLetters;

var found
var i
var j
	
//	FUNCTION OVERVIEW //////////////////////////////////////////////////////////
//field:	The name of the field in end user-friendly terms
//entry:	The actual form entry
//low:		The low value, such as the lowest valid number, or the lowest entry length
//high:		The high value, such as the highest valid number, or the highest entry length in characters

//	isEmail									Check for valid Email input
//	isEntry(field, entry)					Check whether string is filled
//	isInRange(field, entry, low, high)		Check for an integer in any given range
//	isInteger(field, entry)					Check for any integer 
// 	isLength(field, entry, low, high)		Check to make sure the entry is within length parameters
//	isName(field, entry)					Check whether string is potentially a name
//	isPhone(strField, phone, low, high)		Check for any phone characters.  7 is good low, 26 good high, for big intl # with ext.
//	isAlphabetic(field, entry)				Check for alphabetic input
//	isAlphaNumeric							Check for alphanumeric input
//	isSSN4									Check to see if it is a STRING of 4 numbers
//	FUNCTIONS ////////////////////////////////////////////////////////////////////

// isColor checks to see if the enry is possibly a valid web color
function isColor(field, entry){	
	
	var found = false
	if ((entry.length == 0) || (entry == "")){
		 	found = true;
	}
	else
	{
		for (j=0; j < entry.length; j++)
		{
			found = false;
			for (i=0; i < anyColor.length; i++)
			{
				
				if (entry.charAt(j) == anyColor.charAt(i)){
				 	found = true;
				 	break;
				}
			
			}
			
			if (found == false) break;
		}
	}
	
	if (found == false){
		strError = "The " + field + " entry \"" + entry + "\" must be alphanumeric characters or '#' only.\n";
		strError = strError + "You may use a color name or hexadecimal value from the color mixer\n\n";
	 	return strError;
	}
	return "";	
}

// isEmail checks to see if the entry is Email  /////////////////////////////////////////////////////////
function isEmail(field, entry) {
var at = entry.indexOf("@")
var period = entry.lastIndexOf(".")
if (((entry.len < 6) || at == -1) || (period == -1) || (period < at + 2))	
	{
	return "The value for the " + field + " field is not a valid email address\n\n";
	}
	return "";
}

//isEntry just checks for any entry /////////////////////////////////////////////////	
function isEntry(field, entry) {
	if ((entry == null) || (entry.length < 1)){
		return "Please complete the " + field + " field.\n\n";
	}
	return "";
}

//	isInRange checks to see if the number is in a range ///////////////////////////////
function isInRange(field, entry, low, high) {
	if ((entry == null) || (entry.length < 1)){
		return "Please complete the " + field + " field.\n\n";
	}
	if((isNaN(entry) || (entry < low) || (entry > high)) && (high > low)) {
		//You can enter a lower number than low for high to use "any higher number"
		return "The " + field + " field must be a number between " + low + " and " + high + ".\n\n";
		}
	if (isNaN(entry) || (entry < low)){
		return "The " + field + " field must be a a number equal to or larger than " + low + ".\n\n";
		}	
	return "";
}

// isInteger checks to see if the number is an integer. /////////////////////////////////////////////////
function isInteger(field, entry) {
	if (parseInt(entry) != entry) {
		return "The " + field + " field must be an integer.\n\n";
	}
	return "";
}

//	isLength makes sure the entry is long enough. //////////////////////////////////// 
function isLength(field, entry, low, high) {
	
	if (((entry.length < low) || (entry.length > high)) && (high > low)){
		//You can enter a lower number than low for high to use "any higher number"
			return "The " + field + " field must be between " + low + " and " + high + " characters in length.\n\n";
		}
	if (entry.length < low){	
			return "The " + field + " field must be larger than " + (low - 1) + " characters in length.\n\n";
	}
	return "";
}

//isName checks to see if the input is Alhabetic  ////////////////////////////////////////////////////
function isName(field, entry){
	
	if ((entry == null) || (entry.length < 1)){
		return "Please complete the " + field + " field.\n\n";
	}
	
	for (j=0; j < entry.length; j++)
	{
		for (i=0; i < anyName.length; i++)
		{
			found = false;
			if (entry.charAt(j) == anyName.charAt(i)){
				 found = true;
				 break;
				 }
		}
	}	if (found == false) return "The " + field + " field must be alphabetic characters only.\n\n"
	return "";	
}

// isNumeric Checks for any number //////////////////////////////////////////////////////////////////////
function isNumeric(field, entry) {
	if (isNaN(entry)) {
		return "The " + field + " field must be numeric.\n\n";
	}
	return "";
}

// isPhone validates all characters are in phone numbers
function isPhone(field, entry, low, high){

	//Check for proper character input
	for (j=0; j < entry.length; j++)
	{
		for (i=0; i < anyPhone.length; i++)
		{
			found = false;
			if (entry.charAt(j) == anyPhone.charAt(i)){
				 found = true;
				 break;
				 }
		}
	}	if (found == false) return "The " + field + " field has invalid characters.\n\n"
	
	//Check for proper length		
	if (((entry.length < low) || (entry.length > high)) && (high > low)){
		//You can enter a lower number than low for high to use "any higher number"
			return "The " + field + " field must be between " + low + " and " + high + " characters in length.\n\n";
		}
	if (entry.length < low){	
			return "The " + field + " field must be larger than " + (low - 1) + " characters in length.\n\n";
	}
	return "";	
}

function isSSN4(field, entry){	

	for (j=0; j < entry.length; j++)
	{
		for (i=0; i < digits.length; i++)
		{
			found = false;
			if (entry.charAt(j) == digits.charAt(i)){
				 found = true;
				 break;
			}
		}
		if (found == false) break;
	}
	
	if (entry.length != 4) found = false;
	
	if (found == false){
		strError = "The " + field + " field must be 4 numeric characters 0 - 9.\n\n";
		return strError;
	}
	return "";	
}


//////////////////////////////////// END