//(c) 2005 Great Lakes Higher Education Corporation
//Created: 19 October 2005 Michael Westphal
//
//SUMMARY:
// The purpose of this JavaScript library contains simple 
// String validation via Regular Expressions. It also
// includes some common regular expressions.
//
//DEPENDANCIES:
// None
//
//
//FUNCTIONS:
// validateStr(str, regexp)
// validateStr(str, regexp, errorNm)
//

// 
// REGULAR EXPRESSIONS:
//
// Alpha-Numeric Characters
// Example: 
// 	234 Red Oak St.
//
var alphaNumRegExp 	= 	/(^[A-Za-z0-9\ \-\#\/\.\,\&]+$)/i;
var alphaNumRegExpStrict = /(^[A-Za-z0-9\ ]+$)/i;

//
// Alphabetic Characters
// Example: 
//	John Smith
//
var alphaRegExp 	= 	/(^[A-Za-z\ ]+$)/i;

//
// State
// Examples:
//  St. Paul
//  Madison
//
var cityRegExp		=	/(^[A-Za-z\ .]+$)/i;

//
// State Code 
// Example: 
//	WI
//
var stateRegExp 	= 	/^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NE|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY|PR)$/i;

// 
// Zip Code 
// Examples: 
//	99999
//	99999-9999
//
var zipRegExp 		= 	/(^\d{5}$)|(^\d{5}-\d{4}$)/;

//
// Phone Number
// Examples: 
//	(999)999-9999 
//	999-999-9999 
//	999 999 9999 
//	9999999999
//  999.999.9999
// Additionally: 
//	A phone number's area code and
//	exchange number cannot begin 
//	with a zero 
//
var phoneRegExp 	= 	/^((\([1-9]\d{2}\) ?)|([1-9]\d{2}[ \-.]?))[1-9]\d{2}[.\- ]?\d{4}$/;

//
// Email Address
// Examples:
//	someone@somewhere.com 
//	someone@somewhere.com.us
//	some.one@somewhere.com
//  some1@some-where.com
//
var emailRegExp 	= 	/(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9\-_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)|(^[a-z]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)([.][a-z]{2})$)/i;


/**
 * NAME: validateStr
 *
 * PURPOSE: Validates a string against a regular
 *	    expression. 
 *
 * PARAMETERS:	str 	- String
 *				regexp 	- Regular Expression
 *
 * RETURNS: Boolean
 */
function validateStr(str, regexp){
	return regexp.test(str);
}


/**
 * NAME: validateStr
 *
 * PURPOSE: Validates a string against a regular
 *	    expression. If the validation fails, it
 *	    populates an error message. It always,
 *	    at the very least, returns an empty String.	
 *
 * EXAMPLES: If validation where to fail and the passed in 
 *	     errorNm was "First Name" the following two 
 *	     messages could occur:
 *
 *		1) "First Name is required"
 *			(If the passed in String is empty)
 *
 *		2) "The First Name you entered is invalid"
 *			(If the regexp validation fails)
 *	
 *
 * PARAMETERS:	str 	- String
 *				regexp 	- Regular Expression
 *				errorNm - String
 *
 * RETURNS: String
 */
function validateStr(str, regexp, errorNm){
	var msg = "";

	if(str == null || str == ""){
		msg = errorNm + " is required.\n";
	}
	else if(!regexp.test(str)){
		msg = "The " + errorNm + " you entered is invalid\n";
	}

	return msg;
}