/*
  jsCommonValidation.  Written by Joe McCormack, www.virtualsecrets.com.
*/

/*
  Used when the user needs to correct invalid form element data
*/
function jsValidate_numberPattern(data) {
/* Returns false if number string has more than 0-9 */
var result = true; var accuratePattern = /[^0-9]/;
if (accuratePattern.test(data) == true) { result = false;  }
return result;
}
function jsValidate_phoneNumberPattern(data) {
/* Returns false if phone string has more than (), -, 0-9, + (for international numbers) */
var result = true; var accuratePattern = /[^0-9\(\)-]/;
if (accuratePattern.test(data.replace(/\+/g, "")) == true) { result = false; }
return result;
}
function jsValidate_zipcodeNumberPattern(data) {
/* Returns false if zipcode string has more than -, 0-9 */
var result = true; var accuratePattern = /[^0-9-]/;
if (accuratePattern.test(data) == true) { result = false; }
return result;
}
function jsValidate_stringPattern(data) {
/* Returns false if string has more than a-z, A-Z and whitespace */
var result = true; var accuratePattern = /[^a-zA-Z\s]/;
if (accuratePattern.test(data) == true) { result = false; }
return result;
}
function jsValidate_nameStringPattern(data) {
/* Returns false if string has more than a-z, A-Z, -, ' and whitespace */
var result = true; var accuratePattern = /[^a-zA-Z\-\s']/;
if (accuratePattern.test(data) == true) { result = false; }
return result;
}
function jsValidate_emailStringPattern(data) {
/* Returns false if email string is invalid */
var result = true; var accuratePattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
if (accuratePattern.test(data) == false) { result = false; }
return result;
}
/*
  Used when correction of invalid form element data should be done by the script
*/
function jsCorrect_numberPattern(data) {
/* Preserves only 0-9 */
data = data.replace(/[^0-9]/g, "");
return data;
}
function jsCorrect_phoneNumberPattern(data) {
/* Preserves only (), -, 0-9, + (for international numbers) */
data = data.replace(/[^0-9\(\)\-\+]/g, "");
return data;
}
function jsCorrect_zipcodeNumberPattern(data) {
/* Preserves only 0-9 and - */
data = data.replace(/[^0-9\-]/g, "");
return data;
}
function jsCorrect_stringPattern(data) {
/* Preserves only a-z, A-Z and whitespace */
data = data.replace(/[^a-zA-Z\s]/g, "");
return data;
}
function jsCorrect_nameStringPattern(data) {
/* Preserves only a-z, A-Z, -, ' and whitespace */
data = data.replace(/[^a-zA-Z\-\'\s]/g, "");
return data;
}
function jsCorrect_emailStringPattern(data) {
/* Preserves email syntax - does not validate syntax */
data = data.replace(/[^a-zA-Z0-9\!\#\$\%\&\'\*\+\/\=\?\^\`\{\|\}\~\"\_\-\@\[\.\]]/g, "");
return data;
}
/*
  Miscellaneous string manipulation
*/
function jsFilter_stringLeftClip(data) {
/* Returns the string with all leading whitespace removed */
data = data.replace(/^\s+/, "");
return data;
}
function jsFilter_stringRightClip(data) {
/* Returns the string with all trailing whitespace removed */
data = data.replace(/\s+$/, "");
return data;
}
function jsCompare_repetitiveStringPattern(dtype, data) {
/* Returns false when more than one instance of character is detected in a string, and when no instance of character is present */
var result = true; var inquiry = data.indexOf(dtype);
if (inquiry > -1) {
		   var inquirySets = data.split(dtype);
		   if (inquirySets.length > 2) { result = false; }
		  }
else { result = false; }
return result;
}
