
///////
//Form validation code
/// validate10NumericDigits
/// formatPhoneNumber
/// extractDigits

///////
// validate10NumericDigits - makes sure that there are exactly 10 numeric digits in the given field
function validate10NumericDigits(field) {
	return (validateXNumericDigits(field, 10));
}

///////
// validateXNumericDigits - makes sure that there are exactly X numeric digits in the given field
function validateXNumericDigits(field, numDigits) {
	if (field == null || field.value == '') {
		return false;
	}
	if (isNaN(numDigits)) {
		return false;
	}
	digits = "0123456789";
	digitCount = 0;
	for (i=0; i<field.length; i++) {
		if (digits.indexOf(field.charAt(i)) >= 0) {
			digitCount++;
		}
	}
	if (digitCount == numDigits) {
		extractDigits(field);
		return true;
	}
	return false;
}

// formatPhoneNumber - takes the field itself and outputs the given phone number in the format xxx-xxx-xxxx
function formatPhoneNumber(obj) {
	if (obj == null || obj.value == null) {
		return;
	}
	value = extractDigits(obj.value);
	formattedValue = "";
	for (i=0; i<value.length; i++) {
		if (i == 3 || i == 6) {
			formattedValue += '-';
		}

		formattedValue += value.charAt(i);
	}
	obj.value = formattedValue;
}

// isAllNumeric - a utility method to check to see if a given string value contains all numeric digits (0123456789) - a second parameter contains allowable other characters
// (if the given field is null or empty, we return false)
function isAllNumeric(field, otherAllowedCharacters) {
	if (field == null || field == '') {
		return false;
	}
	digits = "0123456789";
	allowedCharacters = digits + otherAllowedCharacters;
	for (i=0; i<field.length; i++) {
		if (allowedCharacters.indexOf(field.charAt(i)) < 0) {
			return false;
		}
	}
	return true;
}

// extractDigits - a utility method to return just the number digits within a given string value
function extractDigits(field) {
	if (field == null || field == "") {
		return "";
	}
	digits = "0123456789";
	newField = "";
	for(i=0; i<field.length; i++) {
		if (digits.indexOf(field.charAt(i)) >= 0) {
			newField += field.charAt(i);
		}
	}
	return newField;
}