
//*************************************************
//*************************************************
//		**Child Function Dictionary**
//
//		vWords(fieldObject)				Returns true if value is only letters and white space						
//		vAlpha(fiekdObject)				Returns True if thisField is only alphas (no white space)
//		vAlphaNumberic(fieldObject)			Returns True if thisField is only alphas or numerics (no white space)
//		vNumBetween(fieldObject,min,max)		Returns True if value is a valid number between min/max		
//		vNum(fieldObject)				Returns true if value is only numbers
//		vWidth(fieldObject, width, strict)		Returns true if value is smaller than width
//									if strict, then it will return true only if value is exact width
//		vAtLeast(fieldObject, width)			Returns true if the value is at least the width
//		vSame(fieldObject, fieldObject, strict)		Returns true if values are the same
//									if strict, then case sensitive
//		vDifferent(fieldObject, fieldObject)		Returns true if values are different (not case sensitive)
//		vInString(fieldObject, theString)		Returns true if theString is in the field Object
//		vWordCount(fieldObject, allowed, keyWord)	Returns true is the fiels Object is within the words allowed
//		chop(fieldValue)				Returns value without leading and trailing spaces
//
//*************************************************
//*************************************************


//*******************************************
//*******************************************
//*******************************************
//*********** Parent Validating Functions ***
//*******************************************
//*******************************************
//*******************************************




//Parent Functions


//Zip Code Validation
function vZip(thisField,keyWord) {
	err = ""
	//Validate Zip for numbers
	if (!vNum(thisField))
	{
		err = "\n   " + keyWord + " zip may only contain numbers"
	}

	//Validate Zip for 5 digits
	if (!vWidth(thisField, 5, true))
	{
		err = err + "\n   " + keyWord + " zip must be 5 characters"
	}
	return err
}

		

//Phone Number validation
function vPhone(thisField1,thisField2,thisField3,keyWord) {
	err = ""
	goodphone = true
	if ((!vNum(thisField1)) || (!vWidth(thisField2, 3, true)))
		{
			goodphone = false
		}			
	if ((!vNum(thisField2)) || (!vWidth(thisField2, 3, true)))
		{
			goodphone = false
		}	
	if ((!vNum(thisField3)) || (!vWidth(thisField3, 4, true)))
		{
			goodphone = false
		}	
	if (goodphone == false)
		{
			err = "\n   " + keyWord + " number has errors"
		}
	return err
}



//Email Validation
function vEmail(thisField,keyWord) {
	err = ""
	thisValue = thisField.value

	//Regular expression for emails
	email_exp = /^[a-z]([a-z_0-9\-\.]*)@([a-z_0-9\.\-]*)\.+[a-z]{2,3}$/

	isgood = email_exp.test(thisValue.toLowerCase())

	//Override isgood value if there is no characters in thisValue

	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		err = "\n   " + keyWord + " email is invalid"
	}

	return err
}



//Date Validation		
function vDate(thisField1,thisField2,thisField3,keyWord) {
	now = new Date()
	year = now.getFullYear()
	err = ""

	//Make entries with one digit into two digits
	if (thisField1.value.length == 1) {
		thisField1.value = "0" + thisField1.value
	}
	if (thisField2.value.length == 1) {
		thisField2.value = "0" + thisField2.value
	}

	all = thisField1.value + thisField2.value + thisField3.value
	gooddate = true

	if ((!vNumBetween(thisField1,1,12)) || (!vNumBetween(thisField2,1,31)) || (!vNumBetween(thisField3,1900,year)))
		{
			gooddate = false
		}
	if ((!vAtLeast(thisField1,2)) || (!vAtLeast(thisField2,2)) || (!vAtLeast(thisField3,4)))
		{
			gooddate = false
		}
	if ((all.length > 0) && (all.length < 8))
		{
			gooddate = false
		}

	if (gooddate == false)
		{
			err = "\n   " + keyWord + " date has errors. Check format: mm/dd/yyyy"
		}
	return err

}


//Price Validation
function vPrice(thisField,keyWord) {
	err = ""

	thisValue = thisField.value
	isgood = true

	price_exp = /^[0-9]*(\.[0-9]{2})*$/

	isgood = price_exp.test(thisValue)

	//Override isgood value if there is no characters in thisValue

	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		err = "\n   " + keyWord + " price is invalid (Only numbers and decimal)"
	}

	return err
}











//*******************************************
//*******************************************
//*******************************************
//*********** Child Validating Functions ****
//*******************************************
//*******************************************
//*******************************************







//Word Count
function CountWords(theWords) {

	strSingleSpace = " "
	strDoubleSpace = "  "

  	holdWords = theWords

  	//replace punctuation with spaces

	doAgain = true
	while (doAgain == true) {
  		doAgain = false
		holdWords = holdWords.replace(";",strSingleSpace)
		holdWords = holdWords.replace(",",strSingleSpace)
		holdWords = holdWords.replace(".",strSingleSpace)
		holdWords = holdWords.replace("-",strSingleSpace)
		for (i=0;i<holdWords.length;i++) {
			if (holdWords.charAt(i) == ";") { 
				doAgain = true
			}
			if (holdWords.charAt(i) == ",") {
				doAgain = true
			}
			if (holdWords.charAt(i) == ".") {
				doAgain = true
			}
			if (holdWords.charAt(i) == "-") {
				doAgain = true
			}
		}
	}


  	//replace multiple spaces with single spaces

	doAgain = true
	while (doAgain == true) {
  		doAgain = false
		holdWords = holdWords.replace(strDoubleSpace,strSingleSpace)
		for (i=0;i<holdWords.length;i++) {
			if (holdWords.substr(i,2) == strDoubleSpace) { 
				doAgain = true
			}
		}
	}



  	//return total number of words
  	//WordCount = (UBound( Split( strX, cstrSingleSpace ) )) + 1

	WordCount = holdWords.split(strSingleSpace)
	WordCount = WordCount.length

	return WordCount

}

//*******************************************


//Alpha AND WHITESPACE ONLY VALIDATION*******
//Returns True if there are only white space and characters in thisField

function vWords(thisField) {
	thisValue = thisField.value
	isgood = true
	char_exp = /^[a-zA-Z]*$/	//Regular expression for letters only
	ws_exp = /^\s*$/			//Regular expression for whitespace
	for (i = 0; i < thisValue.length; i++) {
		c = thisValue.charAt(i)
		//Check every character in thisValue for letter value or whitespace value
		if (!(char_exp.test(c)) && !(ws_exp.test(c)))
			isgood = false

	}

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		return false
	}
	else {
		return true
	}

}

//*******************************************



//ALPHA ONLY VALIDATION*******
//Returns True if thisField is only alphas (no white space)
function vAlpha(thisField) {
	thisValue = thisField.value
	isgood = true
	char_exp = /^[a-zA-Z]*$/	//Regular expression for letters only

	isgood = char_exp.test(thisValue)

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		return false
	}
	else {
		return true
	}

}

//*******************************************



//ALPHA AND NUMERIC ONLY VALIDATION*******


//Returns True if thisField is only alphas or numerics (no white space)
function vAlphaNumeric(thisField) {
	thisValue = thisField.value
	isgood = true
	char_exp = /^[a-zA-Z0-9]*$/	//Regular expression for letters only
	
	isgood = char_exp.test(thisValue)

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		return false
	}
	else {
		return true
	}

}

//*******************************************


//Year VALIDATION********************
//Returns True if thisField is a valid Year
function vNumBetween(thisField,min,max) {
	thisValue = thisField.value
	isgood = true
	numonly = true
	number_exp = /^[0-9]*$/
	for (i = 0; i < thisValue.length; i++) {
		c = thisValue.charAt(i)
		//Check every character in thisValue for numeric value
		if (!(number_exp.test(c)))
			numonly = false
	}

	if (numonly) {
		if ((thisValue > max) || (thisValue < min)) {
			isgood = false
		} else {
			isgood = true
		}		
	} else {
		isgood = false
	}

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		return false
	}

	else {
		return true
	}

}

//*******************************************


//NUMBERS ONLY VALIDATION********************


//Returns True if thisField is only numbers
function vNum(thisField) {
	thisValue = thisField.value
	isgood = true
	number_exp = /^[0-9]*$/
	for (i = 0; i < thisValue.length; i++) {
		c = thisValue.charAt(i)
		//Check every character in thisValue for numeric value
		if (!(number_exp.test(c)))
			isgood = false
	}
	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true
	if (isgood == false) {
		return false
	}
	else {
		return true
	}
}

//*******************************************


//CHARACTER WIDTH VALIDATION*****************


//Returns True if thisField meets the width requirements
function vWidth(thisField, thisWidth, Strict) {
	thisValue = thisField.value
	//Strict = Does thisValue.length need to be exactly thisWidth
	if (Strict == false) {
		if (thisValue.length <= thisWidth) {
			isgood = true
		} else {
			isgood = false
		}
	} else {
		if (thisValue.length == thisWidth) {
			isgood = true
		} else {
			isgood = false
		}
	}

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true

	if (isgood == false) {
		return false
	} else {
		return true
	}

}



//STRING IS AT LEAST WIDTH**********************
//Returns True if thisField is at least 'thisWidth'
function vAtLeast(thisField, thisWidth) {
	thisValue = thisField.value
	if (thisValue.length < thisWidth) {
		isgood = false
	} else {
		isgood = true
	}

	//Override isgood value if there is no characters in thisValue
	if (thisValue.length == 0)
		isgood = true

	if (isgood == false) {
		return false
	} else {
		return true
	}
}





//*******************************************
//*********** String Functions **************
//*******************************************



//ARE TWO FIELDS THE SAME********************
//Returns True if thisField1 is the same as thisField2
function vSame(thisField1, thisField2, Strict) {
	if (Strict == true) {
		if (thisField1.value == thisField2.value) {
			return true
		} else {
			return false
		}
	} else {
		var f1 = thisField1.value
		var f2 = thisField2.value
		if (f1.toUpperCase() == f2.toUpperCase()) {
			return true
		} else {
			return false
		}
	}
}

//*******************************************



//ARE TWO FIELDS Different********************
//Returns True if thisField1 is different from thisField2
function vDifferent(thisField1, thisField2) {
	var f1 = thisField1.value
	var f2 = thisField2.value
	isgood = false
	if (f1.toUpperCase() == f2.toUpperCase()) {
		isgood = false
	} else {
		isgood = true
	}
	if (f1 == "") {
		isgood = true
	}

	return isgood

}

//*******************************************

//IS THE SUBSTRING IN THE MAIN STRING********
//Returns True if thisField has the string
function vInString(thisField, theString) {
	var InString = thisField.value
	isgood = false
	for (i=0;i<InString.length;i++) {
		chars = InString.substr(i,theString.length)
		if (chars == theString) {
			isgood = true
		}
	}
	return isgood
}

//*******************************************



//IS THE FIELD SMALLER THAN THE ALLOWED WORDS
//Returns True if thisField is smaller than the allowed words
function vWordCount(thisField, allowed, keyWord) {
	err = ""
	if (CountWords(thisField.value) > allowed) {
		err = "\n   " + keyWord + " exceeds maximum word count of " + allowed
	}
	return err
}



//*******************************************

//REMOVE LEADING AND TRAILING WHITESPACE*****
function chop(item){
	var tmp = ""
	var item_length = item.length;
	var item_length_minus_1 = item.length - 1;
	var i
	for (i = 0; i < item_length; i++) {
    		if (item.charAt(i) != ' ') {
			tmp += item.charAt(i)
		} else {
			if (tmp.length > 0) {
				if (item.charAt(i+1) != ' ' && i != item_length_minus_1) {
					tmp += item.charAt(i)
				}
			}
		}
	}

	return tmp

}

//*******************************************
//*******************************************
//*********** Other Functions ***************
//*******************************************


//FIELD SET FOCUS****************************
function setFocus(thisField) {
	//Puts focus on thisField
	thisField.focus()
}

//*******************************************

