// JavaScript library for form field validation

// max chars count in request textarea.
var maxCount = 500;

//Trim's always a good function to have!
String.prototype.trim = function() {
 // skip leading and trailing whitespace
 // and return everything in between
  return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");

}

function alertError(field, message)
{
	alert(message);
	field.focus();
}

function requiredTextBox(field, fieldName)
{
	alertError(field, fieldName + " is required.");
}

function invalidFormatTextBox(field, fieldName, format)
{
	alertError(field, fieldName + " must be in " + format + " format.");
}

function mutuallyExclusiveTextBox(field, fieldName1, fieldName2)
{
	alertError(field, "You may not enter a " + fieldName1 + " and a " + fieldName2 + " at the same time.");
}

function requiredDropDownList(field, fieldName, multiple)
{
	if(multiple)
	{
		alertError(field, "You must select at least one " + fieldName + ".");
	}
	else
	{
		alertError(field, "You must select a " + fieldName + ".");
	}
}

function requiredCheckBox(field, fieldName, multiple)
{
	alertError(field, "You must select a " + fieldName + ".");
}


// 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;
}

//IllegalCharacter
//Used in email validation
function hasIllegalCharacters(string, illegalchars)
{	
	for(var i = 0; i < illegalchars.length; i++)
	{
		if(string.indexOf(illegalchars[i]) != -1) 
		{
			return true;
		}
	}
	return false;
}

//Function used to check invalid parenthesis
//Used in checking invalid parenthesis
//Parameter passed is string to be checked and fieldname for alert message
function containsInvalidParenthesis(thisObj, fieldName)
{	
	var illegalchars = new Array("(" , ")");
	
	thisObj.value = thisObj.value.trim();
	
	if (thisObj.value != "" ){
	
		for(var i = 0; i < illegalchars.length; i++)
		{
			if(thisObj.value.indexOf(illegalchars[i]) != -1) 
			{
				alert("'" + fieldName + "' contains invalid characters '(' and/or ')'.");
				thisObj.focus();
				return false;
			}	
		}
	}
	
	return true;
}

//isLongerThan
//Used in email validation
function isLongerThan(val, biggerThan)
{
	if(val.trim() == "")
		return false;
	if(val.length <= biggerThan)
		return false;
	return true;
}

//this turns a string separated by spaces into an array
//Used in email validation
function stringToArray(string)
{
	var charArray = new Array();
	var index = string.indexOf(" ");
	var arrayIndex = 0;
	while(index != -1)
	{
		charArray[arrayIndex] = string.charAt(index - 1);
		arrayIndex++;
		index = string.indexOf(" ", index + 1);
	}
	return charArray;
}

// check email
function isEmail (s)
{   
	// must have '@' and '.'
	var pattern = /^([a-zA-Z0-9\-\_\.\&\~]{1,})@([a-zA-Z0-9\-\_\.\&\~]+\.[a-zA-Z]{2,})$/
    var res = s.search(pattern);
    return (res == 0);
    //return isValidEmail(s);
}

// count char in textarea. 
function countCharacters(field)
{
	fieldObj = document.getElementById(field);
	if(fieldObj.value.trim().length > maxCount)
	{
		alert("The maximum allowed characters is " + maxCount + ".  You have " + fieldObj.value.trim().length + " characters.");
		fieldObj.value = String(fieldObj.value).substring(0,maxCount);
		return false;
	}
	return true;
}