
/*
Recieves a string and takes out all characters that would not be relevent to a formal name
(city, person's name, etc.), and makes sure it fits within certain size constraints
*/
function validatename(fField, strName, bFormLevel, nMin, nMax, bRequired){
//Number field validation
	var iErr = 0
	var strMsg = new String("")
	var strReturn = new String("")
	var strNumbers = new String("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'- .,")
	var strTheName = new String(fField.value)

	// Taking out alpha characters.
	for(var nForCnt = 0; nForCnt < strTheName.length; nForCnt++) {
		temp = "" + strTheName.substring(nForCnt, nForCnt + 1)
		if (strNumbers.indexOf(temp) > "-1"){
			strReturn = strReturn + temp;
		}
		fField.value = strReturn
	}
	strTheName = strReturn

	// Checking to see if the field is empty
	if(strTheName.length == 0) {
		if(bRequired == true) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] requires a value\n"
		}
	}
	else {
		// Checking for the min
		if ((strTheName.length < nMin) && (iErr == 0)) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be less than " + nMin + " characters in length\n"
		}
		// Checking for the max
		if ((strTheName.length > nMax) && (iErr == 0)) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be more than " + nMax + " characters in length\n"
		}
	}

	// If there's an error, say so.
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n");
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if(bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

/*
This function recieves a number, and takes out all characters that aren't a
number or a dash and removes it. It also allows max and min values(not numeric
values, but lengths of the strings), so if a number has to be within certain
lengths, this will check to see.
*/
function validatenumber(fField, strName, bFormLevel, nMin, nMax, bRequired){
//Number field validation
	var iErr = 0
	var strMsg = new String("")
	var strReturn = new String("")
	var strNumbers = new String("0123456789-")
	var iTheNumber = new String(fField.value)

	// Taking out alpha characters.
	for(var nForCnt = 0; nForCnt < iTheNumber.length; nForCnt++) {
		temp = "" + iTheNumber.substring(nForCnt, nForCnt + 1)
		if (strNumbers.indexOf(temp) > "-1"){
			strReturn = strReturn + temp;
		}
		fField.value = strReturn
	}
	iTheNumber = strReturn

	// Checking to see if the field is empty
	if((bRequired == true) && (iTheNumber.length == 0)) {
		iErr = 1;
		strMsg = strMsg + " - [" + strName + "] requires a value\n"
	}

	// Checking for the min
	if ((iTheNumber.length < nMin) && (iErr == 0)) {
		if(!((bRequired == false) && (iTheNumber.length == 0))) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be less than " + nMin + " characters in length\n"
		}
	}
	// Checking for the max
    if ((iTheNumber.length > nMax) && (iErr == 0)) {
		iErr = 1;
		strMsg = strMsg + " - [" + strName + "] should not be more than " + nMax + " characters in length\n"
	}

	// If there's an error, say so.
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n");
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if(bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

/*
This function recieves a number with decimals, and takes out all characters that aren't a
number, a dash, or a decimal point and removes it. It also allows max and min values(not numeric
values, but lengths of the strings), so if a number has to be within certain
lengths, this will check to see.
*/
function validatedecimal(fField, strName, bFormLevel, nMin, nMax, bRequired){
//Number field validation
	var iErr = 0
	var strMsg = new String("")
	var strReturn = new String("")
	var strNumbers = new String("0123456789-\.")
	var iTheNumber = new String(fField.value)

	// Taking out alpha characters.
	for(var nForCnt = 0; nForCnt < iTheNumber.length; nForCnt++) {
		temp = "" + iTheNumber.substring(nForCnt, nForCnt + 1)
		if (strNumbers.indexOf(temp) > "-1"){
			strReturn = strReturn + temp;
		}
		fField.value = strReturn
	}
	iTheNumber = strReturn

	// Checking to see if the field is empty
	if((bRequired == true) && (iTheNumber.length == 0)) {
		iErr = 1;
		strMsg = strMsg + " - [" + strName + "] requires a value\n"
	}

	// Checking for the min
	if ((iTheNumber.length < nMin) && (iErr == 0)) {
		if(!((bRequired == false) && (iTheNumber.length == 0))) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be less than " + nMin + " characters in length\n"
		}
	}
	// Checking for the max
    if ((iTheNumber.length > nMax) && (iErr == 0)) {
		iErr = 1;
		strMsg = strMsg + " - [" + strName + "] should not be more than " + nMax + " characters in length\n"
	}

	// If there's an error, say so.
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n");
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if(bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

/*
This function formats a phone number to our standard ###-###-####.
*/
function validatephone(fField, strName, bFormLevel, bRequired){
  // Phone field validation
	var iErr = 0
	var strMsg = new String("")
	var strNumbers = new String("0123456789")
	var strTemp = new String("")
	var strPhoneNum = new String(fField.value)
	var iVlen = strPhoneNum.length
	var strReturn = new String("")

	if(iVlen > 0) {
  //Weeding out alpha characters, and formatting phone number correctly
		for(var nForCnt = 0; nForCnt < iVlen; nForCnt++) {
			strTemp = "" + strPhoneNum.substring(nForCnt, nForCnt + 1)
			if (strNumbers.indexOf(strTemp) > "-1"){
				strReturn = strReturn + strTemp;
			}
			if ((strReturn.length == 3) || (strReturn.length == 7)) {
				strReturn = strReturn + "-";
			}
			fField.value = strReturn
		}
		strPhoneNum = strReturn
		iVlen = strPhoneNum.length
	}

	//A real phone number is 12 digits long, so if field value isn't, its not a real phone number
	if(iVlen != 12) {
	//Get a count from the field value; cannot be null if field is required
		if((iVlen == 0) && bRequired) {
			strMsg = strMsg + " - [" + strName + "] requires a value\n"
			iErr = 1
		}
		else {
			if(iVlen > 0) {
				strMsg = strMsg + " - \"" + strPhoneNum + "\" is not a valid phone number for [" + strName + "]\n"
				iErr = 1
			}
		}
	}


  //A real phone number is 12 digits long, so if field value isn't, it's not a real phone number
	if(iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n")
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if (bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

/*
This function recieves a text field and min and max values, checks to
make sure the text fits within them, then makes sure it and it's confirm field
are identical in value.
*/
function validatepassword(fPassField, fConfirmField, strPassName, strConfirmName, bFormLevel, nMin, nMax, bRequired) {
	var iErr = 0
	var strMsg = new String("")
	var strText = new String(fPassField.value)
	var strConfirmText = new String(fConfirmField.value)
	
	// Get a count from the field value; cannot be null if field is required
	if(strText.length == 0) {
		if(bRequired) {
			iErr = 1;
			strMsg = strMsg + " - [" + strPassName + "] requires a value\n"
		}
	}
	else {
		for(var nForCnt = 0; nForCnt < strText.length; nForCnt++) {
			if(strText.substring(nForCnt, nForCnt + 1) == " ") {
				iErr = 1;
				strMsg = strMsg + " - [" + strPassName + "] should not contain any spaces\n"
			}
		}

		if((strText.length < nMin) && (iErr == 0)) {
			iErr = 1;
			strMsg = strMsg + " - [" + strPassName + "] should not be less than " + nMin + " characters in length\n"
		}
	
		if((strText.length > nMax) && (iErr == 0)) {
			iErr = 1;
			strMsg = strMsg + " - [" + strPassName + "] should not be more than " + nMax + " characters in length\n"
		}

		if((fPassField.value.toLowerCase() != fConfirmField.value.toLowerCase()) && (iErr == 0)) {
			iErr = 1;
			strMsg = strMsg + " - The [" + strPassName + "] and [" + strConfirmName + "] fields do not match\n"
		}
	}
	
	// Error processing and exit
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n")
			fPassField.select()
			fPassField.focus()
			return false
		}
	}
	else {
		if (bFormLevel) {
			return strMsg
		}
		else {
			return true
		}
	}
}

/*
This function recieves a text field and min and max values, then checks to
make sure the text fits within them.
*/
function validatetext(fField, strName, bFormLevel, nMin, nMax, bRequired) {
	var iErr = 0
	var strMsg = new String("")
	var strText = new String(fField.value)
	
	// Get a count from the field value; cannot be null if field is required
	if(strText.length == 0) {
		if(bRequired) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] requires a value\n"
		}
	}
	else {
		if(strText.length < nMin) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be less than " + nMin + " characters in length\n"
		}
	
		if(strText.length > nMax) {
			iErr = 1;
			strMsg = strMsg + " - [" + strName + "] should not be more than " + nMax + " characters in length\n"
		}
	}
	
	// Error processing and exit
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n")
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if (bFormLevel) {
			return strMsg
		}
		else {
			return true
		}
	}
}

/*
This function recieves a zip code, and validates and formats it, in either
five or nine digit format.
*/
function validatezipcode(fField, strName, bFormLevel, bRequired){
	var iErr = 0
	var strMsg = new String("")
	var strNumbers = new String("0123456789")
	var strTemp = new String("")
	var strZipCode = new String(fField.value)
	var iVlen = strZipCode.length
	var strReturn = new String("")

	if(iVlen > 0) {
  //Weeding out alpha characters, and formatting zip code correctly
		for(var nForCnt = 0; nForCnt < iVlen; nForCnt++) {
			strTemp = "" + strZipCode.substring(nForCnt, nForCnt + 1)
			if (strNumbers.indexOf(strTemp) > "-1") {
				strReturn = strReturn + strTemp;
			}
		}
		if(strReturn.length > 5) {
			strReturn = strReturn.substring(0,5) + "-" + strReturn.substring(5,9)
		}
		fField.value = strReturn
		strZipCode = strReturn
		iVlen = strZipCode.length
	}

  //A real Zip Code is either 5 or 10 digits long, so if field value isn't, its not a real Zip Code
		if((iVlen != 5) && (iVlen != 10)) {
  //Get a count from the field value; cannot be null if field is required
			if((iVlen == 0) && bRequired) {
				strMsg = strMsg + " - [" + strName + "] requires a value\n"
				iErr = 1
			}
			else {
				if(iVlen > 0) {
					strMsg = strMsg + " - \"" + strZipCode + "\" is not a valid zip code for [" + strName + "]\n"
					iErr = 1
				}
			}
		}

  //A real zip code is 5 or 10 digits long, so if field value isn't, its not a real zip code
	if(iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n")
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if (bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

/*
Recieves a string and validates and formats it as an e-mail address
*/
function validateemailaddress(fField, strName, bFormLevel, nMax, bRequired){
//Number field validation
	var iErr = 0
	var iAt = -1
	var iDot = -1
	var bAtFound = false
	var strTemp = new String("")
	var strMsg = new String("")
	var strReturn = new String("")
	var strNumbers = new String("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz-1234567890@_.")
	var strTheEmailAddress = new String(fField.value)

	// Taking out alpha characters.
	for(var nForCnt = 0; nForCnt < strTheEmailAddress.length; nForCnt++) {
		strTemp = "" + strTheEmailAddress.substring(nForCnt, nForCnt + 1)
		if (strNumbers.indexOf(strTemp) > "-1"){
			if(strTemp == "@") {
				if(bAtFound == false) {
					bAtFound = true
				}
				else {
					strTemp = ""
				}
			}
			strReturn = strReturn + strTemp
		}
		fField.value = strReturn
	}
	strTheEmailAddress = strReturn

	// Checking to see if the field is empty
	if((bRequired == true) && (strTheEmailAddress.length == 0)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] requires a value\n"
	}

	// Checking to make sure string size does not exceed max length
	if((iErr == 0) && (strTheEmailAddress.length > nMax)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] cannot exceed " + nMax + " characters\n"
	}

	// Finding "@" and ".".
	if(iErr == 0) {
		for(var nForCnt = 0; nForCnt < strTheEmailAddress.length; nForCnt++) {
			strTemp = "" + strTheEmailAddress.substring(nForCnt, nForCnt + 1)
			if(strTemp == "@") { 
				iAt = nForCnt
			}
			if(strTemp == ".") { 
				iDot = nForCnt
			}
		}
	}
	
	// If there's not "@" and/or "." then it's not a valid e-mail address
	if(((iAt == -1) || (iDot == -1)) && (iErr == 0)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] is not a valid e-mail address\n"
	}

	// If there isn't at least one character in front of the "@", then it's not a valid e-mail address
	if((iAt < 1) && (iErr == 0)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] is not a valid e-mail address\n"
	}

	// If there isn't at least one character between the "@" and the ".", then it's not a valid e-mail address
	if(((iDot - iAt) < 2) && (iErr == 0)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] is not a valid e-mail address\n"
	}

	// If there isn't at least two characters after the ".", then it's not a valid e-mail address
	if(((strTheEmailAddress.length - iDot) < 3) && (iErr == 0)) {
		iErr = 1
		strMsg = strMsg + " - [" + strName + "] is not a valid e-mail address\n"
	}

	// If there's an error, say so.
	if (iErr == 1) {
		if(bFormLevel) {
			return strMsg
		}
		else {
			alert("Please correct the following before submitting the form:\n" + strMsg + "\n");
			fField.select()
			fField.focus()
			return false
		}
	}
	else {
		if(bFormLevel) {
			return String("")
		}
		else {
			return true
		}
	}
}

