function validateField(fld, type, required) {
  window.status = "";
	if (required) {
		var spaces = "";
		if (fld.value == "") {
			return false;
		}
		for (var j = 0; j < fld.value.length + 1; j++)  {
			if (fld.value == spaces)  {
				alert(fld.name + " is required and must have a value.");
				//fld.focus();
				//fld.select();
				return false;
			}
			spaces += " ";
		}
	}
	// WOW 6.5.04.11
	// allow for PJ special values
	if (fld.value && fld.value.indexOf ("*PJ_") == 0) {
		return true;
	}
	
	if (type == "planetj.database.field.StringField") {
		return validateString(fld);
	}
	else if (type == "planetj.database.field.NumberField") {
		return validateNumber(fld);
	}

	else if (type == "planetj.database.field.BigIntegerField") {
		return validateNumber(fld);
	}

	else if (type == "planetj.database.field.BigDecimalField") {
		return validateNumber(fld);
	}
	else if (type == "planetj.database.field.SocialSecurityNumberField") {
	  	return validateSocialSecurityNumber(fld);
	}
	else if (type == "planetj.database.field.PhoneNumberAreaCodeField") {
		return validateAreaCode(fld);
	}
	else if (type == "planetj.database.field.PhoneNumberField") {
		return validatePhoneNumber(fld);
	}
	else if (type == "planetj.database.field.FaxNumberField") {
		return validatePhoneNumber(fld);
	}
	else if (type == "planetj.database.field.ZipCodeField") {
		return validateZipCode(fld);
	}
	else if (type == "planetj.database.field.ZipCodeSuffixField") {
		return validateZipCodeSuffix(fld);
	}
	else if (type == "planetj.database.field.EmailField") {
		return validateEmail(fld);
	}
	else if (type == "planetj.database.field.CreditCardNumberField") {
			return validateCreditCardNumber(fld);
	}
}

function displayFieldStatus(fld, required, maxLength, externalName) {
	if (externalName == null || externalName.length == 0) {
		externalName = fld.name;
	}
	if (required) {
			window.status = externalName + " is required and must have a value.  The maximum length is " + maxLength + ".";
			return false;
	}
	else {
		  window.status = externalName + " must have a length of " + maxLength + " or less.";
			return false;
	}
}

function validateString(fld) {
	return true;
}

function validateNumber(fld) {
	// WOW 6.5.04.11
	// allow for PJ special values
	if (fld.value && fld.value.indexOf ("*PJ_") == 0) {
		return true;
	}
	if (isNaN(fld.value)) {
		len = fld.value.length;
		
		// allow for value such as 99- to be treated as numbers
		if (fld.value.charAt (len - 1) == '-' && fld.value.charAt (0) != '-') {
			if (!isNaN (fld.value.substring (0, len - 1)))
				return true;
		}
		
		// allow for values such as (99) to be treated as numbers
		if (fld.value.charAt (0) == '(' && fld.value.charAt (len - 1) == ')') {
			if (!isNaN (fld.value.substring (1, len - 1)))
				return true;
		}	
		alert("The field " + fld.name + " must be a number.");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
		return true;
	}
}


function validateNumber(fld, extName) {
	// WOW 6.5.04.11
	// allow for PJ special values
	if (fld.value && fld.value.indexOf ("*PJ_") == 0) {
		return true;
	}
	if (extName == null || extName.length == 0) {
		extName = fld.name;
	}

	if (isNaN(fld.value)) {
		len = fld.value.length;
		
		// allow for value such as 99- to be treated as numbers
		if (fld.value.charAt (len - 1) == '-' && fld.value.charAt (0) != '-') {
			if (!isNaN (fld.value.substring (0, len - 1)))
				return true;
		}
		
		// allow for values such as (99) to be treated as numbers
		if (fld.value.charAt (0) == '(' && fld.value.charAt (len - 1) == ')') {
			if (!isNaN (fld.value.substring (1, len - 1)))
				return true;
		}	
		alert(extName + " must be a number.");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
		return true;
	}
}


function validateSocialSecurityNumber(fld) {
  var ssn = fld.value;
  
  // WOW 6.5.04.12
  // allow blank values!
  if (ssn == null || ssn =='')
  	return true;
  
  var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
  var numDashes = ssn.split('-').length - 1;
  if (matchArr == null || numDashes == 1) {
    alert("Invalid Social Security #:  Must be 9 digits or in the form 123-45-6789.");
		//fld.focus();
		//fld.select();
		return false;
  }
  else 
  if (parseInt(matchArr[1],10)==0) {
    alert("Invalid Social Security #:  Cannot start with 000.");
		//fld.focus();
		//fld.select();
		return false;
  }
}

/**
 * Gets a String by removing any characters in the given remove 
 * String from the specifed String
 *
 * @param str String to remove characters from
 * @param removeStr String containing characters to remove
 * @return String without the characters in the given bag
 */
function stripCharacters(str, removeStr) {
  var returnStr = "";

  for (var i = 0; i < str.length; i++) {
	  var c = str.charAt(i);
		if (removeStr.indexOf(c) == -1) {
		  returnStr += c;
		}
	}
	
	return returnStr;
}

/**
 * Validates the given HTML input field's value to determine if
 * it contains a valid area code.  (Note: if invalid, the user
 * is prompted)
 *
 * @param fld HTML input field containing value to validate
 * @return whether or note the field contains valid area code
 */
function validateAreaCode(fld) {
  var number = stripCharacters(fld.value, "() ");
	if (number.length != 0 && (isNaN(number) || number.length != 3)) {
	  alert("Invalid Area Code:  Must be 3 digits in the form 123.");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
	  return true;
	}
}

/**
 * Validates the given HTML input field's value to determine if
 * it contains a valid phone number.  (Note: if invalid, the user
 * is prompted)
 *
 * @param fld HTML input field containing value to validate
 * @return whether or note the field contains valid phone number
 */
function validatePhoneNumber(fld) {
  var number = stripCharacters(fld.value, "- ().");
  if (number.length != 0 && (isNaN(number) || number.length > 11 || number.length < 7)) {
	  alert("Invalid Phone Number:  Valid Formats: 123.456.7890, 123-456-7890 (123) 456-7890.");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
	  return true;
	}
}

function validateZipCode(fld) {
  var number = stripCharacters(fld.value, "- ");
	if (number.length != 0 && (isNaN(number) || number.length > 9)) {
		alert("Invalid Zip Code:  Valid Formats: 12345, 12345-6789");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
		return true;
	}
}

function validateZipCodeSuffix(fld) {
  var number = stripCharacters(fld.value, "- ");
	if (number.length != 0 && (isNaN(number) || number.length != 4)) {
		alert("Invalid Zip Code Suffix:  Valid Formats: 1234, -1234");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
		return true;
	}
}

function validateEmail(fld) {
  if (fld.value == null || fld.value.length == 0) {
    return true;
  }
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(fld.value)){
    return true;
  }
  alert("Invalid E-mail Address! Please re-enter.")
  return false;
}

function validateCreditCardNumber(fld) {
  var number = stripCharacters(fld.value, " ");
	if (number.length != 0 && (isNaN(number) || number.length != 16)) {
		alert("Invalid Credit Card Number");
		//fld.focus();
		//fld.select();
		return false;
	} 
	else {
		return true;
	}
}


/**
 * Sets the status of the window to assist the user in what needs to be
 * entered in the HTML input for the field's value.
 */
function displayBigDecimalFieldStatus(fld, required, size, scale, externalName) {
	if (externalName == null || externalName.length == 0) {
		externalName = fld.name;
	}
	if (required) {
			window.status = externalName + " is required and must have a value with a max of " + size + "  digits to the left of a decimal and up to " + scale + " digits to the right of the decimal.";
			return false;
	}
	else {
		  window.status = externalName + " must have a value with a max of " + size + "  digits to the left of a decimal and up to " + scale + " digits to the right of the decimal.";
			return false;
	}
}


/**
 * Validate the given HTML input field's value to see if it
 * is a valid BigDecimal with the given size and scale.
 *
 * @param fld HTML input field containing value to validate
 * @param type class name of Java field HTML input was generated for
 * @param required whether or not the field's value is required
 * @param size the maximum number of digits the field's value allows
 *        to the left of the decimal.
 * @param scale the maximum number of digits the field's value allows
 *        to the right of the decimal.
 * @param extName external name for the field
 * @return whether or note the field contains valid BigDecimal
 */
function validateBigDecimalField(fld, type, required, size, scale, extName) {
  if (extName == null || extName.length == 0) {
	extName = fld.name;
  }
  // first validate if field is a number and/or is required
	var valid = validateField(fld, type, required, extName);
		
	// if valid so far
	if (valid) {
	  // check to see if correct number of digits to right
		// and left of input have been given [remove any commas
		// user may have entered]
		var value = stripCharacters(fld.value, ",-");
		var decimalIndex = value.indexOf('.');
		var leftOfDecimal = null;
		var rightOfDecimal = null;
		if (decimalIndex == -1) {
			leftOfDecimal = value;
		}
		else {
			if (scale == 0) {
				alert("Invalid value, number cannot have decimal.");
				return false;
			}
			leftOfDecimal = value.substring(0, decimalIndex);
			rightOfDecimal = value.substring(decimalIndex + 1, value.length);
		}
		
		// note that size is the maximum number of digits in the ENTIRE field value,
		// not the number of digits to the left of the decimal (JE 12/02/2008)
		maxNonDecimal = size - scale;
		
		if ( (leftOfDecimal != null && leftOfDecimal.length > maxNonDecimal )
			|| (rightOfDecimal != null && rightOfDecimal.length > scale) ) {
			alert("Invalid valid.  Value should only contain up to " + maxNonDecimal + " digits before the decimal and up to " + scale + " decimals.");
			return false;
		}
	}
	
	return valid;
}


/**
 * Validate the given HTML input field's value to see if it
 * is correct.
 *
 * @param fld HTML input field containing value to validate
 * @param type class name of Java field HTML input was generated for
 * @param required whether or not the field's value is required
 * @param extName external name of the field.  used for user friendly
 *        messages.
 * @return whether or not the field contains a valid value.
 */
function validateField(fld, type, required, extName) {
  if (extName == null || extName.length == 0) {
	extName = fld.name;
  }

  window.status = "";
	if (required) {
		var spaces = "";
		if (fld.value == "") {
			return false;
		}
		for (var j = 0; j < fld.value.length + 1; j++)  {
			if (fld.value == spaces)  {
				alert("The field (" + extName + ") is required and must have a value.");
				return false;
			}
			spaces += " ";
		}
	}
	if (type == "planetj.database.field.StringField") {
		return validateString(fld);
	}
	else if (type == "planetj.database.field.NumberField") {
		return validateNumber(fld, extName);
	}

	else if (type == "planetj.database.field.BigIntegerField") {
		return validateNumber(fld, extName);
	}

	else if (type == "planetj.database.field.BigDecimalField") {
		return validateNumber(fld, extName);
	}
	else if (type == "planetj.database.field.SocialSecurityNumberField") {
	  	return validateSocialSecurityNumber(fld);
	}
	else if (type == "planetj.database.field.PhoneNumberAreaCodeField") {
		return validateAreaCode(fld);
	}
	else if (type == "planetj.database.field.PhoneNumberField") {
		return validatePhoneNumber(fld);
	}
	else if (type == "planetj.database.field.FaxNumberField") {
		return validatePhoneNumber(fld);
	}
	else if (type == "planetj.database.field.ZipCodeField") {
		return validateZipCode(fld);
	}
	else if (type == "planetj.database.field.ZipCodeSuffixField") {
		return validateZipCodeSuffix(fld);
	}
	else if (type == "planetj.database.field.EmailField") {
		return validateEmail(fld);
	}
	else if (type == "planetj.database.field.CreditCardNumberField") {
			return validateCreditCardNumber(fld);
	}
}

