// Title:			Validation Functions
// File:			validationFuntions.js
// Purpose:			Validates various values
// Author:			Troy Martin - Scripted Motion, LLC
// Last Updated:	04/06/06


//-----------------------------------------------------------------------
// trim
// Purpose: Removes leading and trailing whitespace from a string
// In: str = The string that needs to be trimmed    
// Returns: The trimmed string
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
}


//-----------------------------------------------------------------------
// Name validation
// Purpose: Validates a name
// In: nm = The name that needs to be validated    
// Returns: Boolean - True if the name is valid. False if not.
//			Errors are stored in errors, which is an array passed by 
//          reference by the caller
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateName(nm, errors)
{
	// Create a regular expression (regex) to make sure the name:
	// 1. Contains at least 1 character, but no more than 120 characters. (inclusive)
	// 2. Only contains the following characters: {a-z,A-Z,space,.,comma, -}
	// Notes: 	The blank spots below that are followed by {0,2} are spaces...
	//			The hyphens have to come immediately after the left bracket, or 
	//			immediately before the right bracket because they are special
	//			metacharacters when they are inside square brackets. The period
	//			has to be escaped because it is a special metacharacter anywhere

	var namePattern = /^[a-z \.,-]{1,60}$/i;
	
	if(namePattern.test(nm) == false)
	{
		errors.push("Invalid name");
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// E-Mail validation
// Purpose: Validates an e-mail address
// In: 	emailAddress = The e-mail address that needs to be validated
//		errors - An array the caller passes by reference where
//               errors are stored    
// Returns: Boolean - True if the email address is valid. False if not.
//		    Stores errors in the errors array, which is passed by 
//          reference by the caller.
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateEmailAddress(emailAddress, errors)
{
	// Create a regular expression (regex) to validate an e-mail address
	// The regex used here is the regex used by Pear's QuickForm
	var emailPattern = /^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}| [0-9]{1,3})(\]?)$/;
	if(emailPattern.test(emailAddress) == false)
	{
		errors.push("The email address appears to be invalid");
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// Purpose: Validates a North American phone number
// In: 	phone - The phone number that needs to be validated
//		errors - An array the caller passes by reference where
//               errors are stored  
// Returns: Boolean - True if the email address is valid. False if not.
//		    Stores errors in the errors array, which is passed by 
//          reference by the caller.
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateNorthAmericanPhoneNumber(phone, errors)
{
	// Create a regex to verify the phone number meets the following criteria:
	// 1. Matches the following constraint:
	// zero or one (0 or 1) (zero or one space) (zero or one ( or . or -) (zero or one set of three digits) 
	// (zero or one ) . - or space) (3 digits) (zero or one . - or space) (four digits) (zero or one space) 
	// (zero or one x or ext or extension followed by zero or one . followed by zero or one space 
	// followed by from 1-10 digits))
	
	var phonePattern = /^[01]?\s?[\(\.-]?(\d{3})?[\)\.-]?\s?(\d{3})[\s\.-]?(\d{4})\s?((x|ext|extension)[.]?\s?\d{1,10})?$/;
	
	if(phonePattern.test(phone) == false)
	{
		errors.push("Invalid North American phone number");			
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// Purpose: Validates a company name
// In: 	company - The company name that needs to be validated
//		errors - An array the caller passes by reference where
//               errors are stored
// Returns: true if the company name is valid, false if not
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateCompany(company, errors)
{
	// I don't know of any realistic limits to impose on company names other than
	// limiting the length to some arbitrary value, so this function only returns false
	// if the company name is empty or greater than 60 characters.
	
	if(company.length < 1 || company.length > 60)
	{
		errors.push("The company name has an invalid length");			
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------
// Purpose: Validates the message, aka body, of a contact form
// In: 	message - The message that needs to be validated
//		errors - An array the caller passes by reference where
//               errors are stored
// Returns: true if the message is valid, false if not
// Precondition: N/A
// Postcondition: N/A	
//-----------------------------------------------------------------------
function validateMessage(message, errors)
{
	// I don't know of any realistic limits to impose on a message other than
	// limiting the length to some arbitrary value, so this function only returns false
	// if the message is empty or greater than 400 characters.
	
	if(message.length < 1)
	{
		errors.push("The message is too short: " + message.length + " characters");			
		return false;
	}
	if(message.length > 5000)
	{
		errors.push("The message is too long: " + message.length + " characters");			
		return false;
	}
	return true;
}
