/* function to trim whitespace from beginning and end of string */

function trimString(str) { 
	return str.replace(/^\s+|\s+$/g, '');
};

/* function to strip non-numeric characters from a string */

function stripAlphaChars(str) {  
	return str.replace(/[^0-9]/g, '');  
}

/* function to test if string is empty */
function isEmpty(str) {	
	if(str == null || str == "" || str.length < 3)
		return true;
	return false;
};

/* function to test if string doesn't contain nums*/ 
function isAlpha(str) {
	var pattern = /(^[A-Za-z]+$)/;
	if(pattern.test(str))
		return true;
	return false;
};
	
/* function to test validity of phone number field */

function isValidPhone(p_phone){

	p_phone = stripAlphaChars(p_phone);

	var valid = true;
	
	// we are allowing the format 12345 or 12345-6789
	if ( p_phone == null || p_phone == "" || p_phone.length < 10 || p_phone.length > 10 || isNaN(parseInt(p_phone,10))) {
		valid = false;
	}
	
	for ( var i = 0; i < p_phone.length; i++ ){	
	
		if (isNaN(p_phone.charAt(i)))
			return false;
		}
	
	return valid;
}

/* function to test validity of zip code field */

function isValidZipCode(p_zip) {

	p_zip = stripAlphaChars(p_zip);

	var valid = true;
	
	// we are allowing the format 12345 or 12345-6789
	if (p_zip == null || p_zip == "" || p_zip.length < 5 || p_zip.length > 10 || isNaN(parseInt(p_zip,10)))
		valid = false;
	else if( p_zip.length > 5 && p_zip.charAt(5) != "-" )
		valid = false;
	else if( p_zip.length > 5 && p_zip.charAt(5) == "-" && p_zip.length != 10 )
		valid = false;
	
	for(var i=0;i<p_zip.length;i++) {	
		if(isNaN(p_zip.charAt(i)))
			return false;
	}
	
	return valid;
}

/* function to test validity of email address */

function  isValidEmail(p_email, mustValidate) {
	if(mustValidate) {
	p_email = trimString( p_email );
	emailpat = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
	if( !emailpat.test( p_email ) )
  	return false;
	return true;
 	}
}
	
/* function to compare email to verify email fields */

function verifyEmail(p_email, p_emailVerify){
	if( p_email.toLowerCase == p_emailVerify.toLowerCase )
		return true;
	return false;
}

