// Form Validation Functions
// http://www.dithered.com/javascript/form_validation/index.html
// code by Chris Nott (chris@dithered.com)
// appended by byron tredwell 
// Modifications:
// Date        Programmer     Description 
// 05/19/03    H. Skinner     Modified the isValidEmail function to allow for
//                            4 characters after the '.' in the e-mail address.
//                            scan: HDS01
 
// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
	if (string.length < min || string.length > max) return false;
	else return true;
}

// Check that a credit card number is valid based using the LUHN formula
function isValidCreditCard(number) {
	var checksum = 0;
	var numDigits = 0;
	var doubleDigit = 0;
	
	number = removeSpaces(number);
	if (!isValidLength(number, 13, 16)) return false;
	if (!isNumeric(number)) return false;
	
	for (var i = number.length - 1; i >= 0; i--) {
		numDigits++;
		digit = number.charAt(i);
		
		if (numDigits % 2 != 0) checksum += parseInt(digit);
		else {
			doubleDigit = parseInt(digit) * 2;
			if (doubleDigit >= 10) checksum += 1 + (doubleDigit - 10);
			else checksum += doubleDigit;
		}
	}
	if ((checksum % 10) != 0) return false;

	return true;
}

// Check that an email address is valid --> NAME@DOMAIN.SFX
function isValidEmail(address) {
	if (address.indexOf('@') < 1) return false;
	if (!address.indexOf('.')) return false;
	
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	var suffix = domain.substring(domain.indexOf('.') + 1);
	
	while(suffix.indexOf('.')!=-1){
	 suffix = suffix.substring(suffix.indexOf('.') + 1);
	}
	
  if (domain.indexOf('.') == 0) return false;
  if (domain.indexOf('..') != -1) return false;
	// HDS01...Changed valid suffix length from 3 to 4  
	if (!isValidLength(suffix, 1, 4)) return false;
	
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
	if (domain.indexOf('@') != -1 || domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
	if (suffix.indexOf('@') != -1 || suffix.indexOf('(') != -1 || suffix.indexOf(')') != -1 || suffix.indexOf('<') != -1 || suffix.indexOf('>') != -1 || suffix.indexOf(',') != -1 || suffix.indexOf(';') != -1 || suffix.indexOf(':') != -1 || suffix.indexOf('\\') != -1 || suffix.indexOf('"') != -1 || suffix.indexOf('[') != -1 || suffix.indexOf(']') != -1 || suffix.indexOf(' ') != -1) return false;
	return true;
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmailLoose(address) {
	if (address.indexOf('@') < 3) return false;
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
	if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
	return true;
}


// Check that a US zip code is valid
function isValidZipcode(zipcode) {
	zipcode = removeSpaces(zipcode);
	if (!(zipcode.length == 5) || !isNumeric(zipcode)) return false;
	return true;
}


// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
	if (postalcode.search) {
		postalcode = removeSpaces(postalcode);
		if (postalcode.length == 6 && postalcode.search(/^\w\d\w\d\w\d$/) != -1) return true;
		else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1) return true;
		else return false;
	}
	return true;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
	if (string.replace) {
		string.replace(/[<>\"\'%;\)\(&\+]/, '');
	}
	return string;
}

// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
	var newString  = '';
	var substring  = '';
	beginningFound = false;
	
	// copy characters over to a new string
	// retain whitespace characters if they are between other characters
	for (var i = 0; i < string.length; i++) {
		
		// copy non-whitespace characters
		if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
			
			// if the temporary string contains some whitespace characters, copy them first
			if (substring != '') {
				newString += substring;
				substring = '';
			}
			newString += string.charAt(i);
			if (beginningFound == false) beginningFound = true;
		}
		
		// hold whitespace characters in a temporary string if they follow a non-whitespace character
		else if (beginningFound == true) substring += string.charAt(i);
	}
	return newString;
}

