function CheckForAlpha(theField) 
{ 
  var checkOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
  var checkStr = theField; 
  var allValid = true; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
  } 
  return(allValid);
} 

function CheckForCaps(theField) 
{ 
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
  var checkStr = theField; 
  var allValid = true; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
  } 
  return(allValid); 
} 

function CheckForNumerics(theField) 
{ 
  var checkOK = "0123456789-.,"; 
  var checkStr = theField; 
  var allValid = true; 
  var decPoints = 0; 
  var allNum = ""; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
    if (ch == ".") 
    { 
      allNum += "."; 
      decPoints++; 
    } 
    else if (ch != ",") 
      allNum += ch; 
  } 
  if (decPoints > 1) 
    { 
      allValid = false; 
    } 
        
        return(allValid); 
} 

function ContainsAlpha(theField) 
{ 
  var checkOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
  var checkStr = theField;
  var allValid = false; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
		return true;
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
  } 
  return(allValid); 
} 

function ContainsNumeric(theField) 
{ 
  var checkOK = "0123456789"; 
  var checkStr = theField;
  var allValid = false; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
		return true;
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
  } 
  return(allValid); 
} 

function isEmailAddress(addr)
{
	var atPos;
	var allvalid = true;
	if (addr.length == 0)
		return true;
	atPos = addr.indexOf("@");
	if (atPos == -1)
		return false;
	if (	(addr.indexOf(" ") != -1) ||
		(addr.indexOf("/") != -1) ||
		(addr.indexOf(";") != -1) ||
		(addr.indexOf(":") != -1) ||
		(addr.indexOf(",") != -1) ||
		(addr.indexOf("<") != -1) ||
		(addr.indexOf(">") != -1) )
		return false;
	else
		if ((addr.indexOf("@", atPos+1) != -1) || (addr.indexOf(".",atPos) == atPos+1) || (addr.indexOf(".",atPos) == -1))
			return false;
	return (true); 
}

function isPhoneNumber(theField)
{ 
	var CleanedString=""; 
	var index = 0; 
	var ch;
	var checkOK = "0123456789"; 

	if (theField.length == 0)
		return true;

//	Walk through the input string and collect only number characters, appending them to CleanedString
	while (index <= theField.length)
		{
	    ch = theField.charAt(index);
		for (j = 0;  j < checkOK.length;  j++)
			{
			if (ch == checkOK.charAt(j))
				{
				CleanedString = CleanedString + ch;
		        break;
		        }
			}
			index = index + 1;
		}

//	If CleanedString is exactly 10 digits long, then format it and allow form submission
	if (CleanedString.length == 10)
		{
		return "(" + CleanedString.substring(0,3) + ") " + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10);
		}
 
//	If CleanedString is not 10 digits longs, show an alert and cancel form submission
	else
		return false;
} 


function ContainsSpecial(theField) 
{ 
  var checkOK = "!@#$%^&*(){}[];:<>,./?\|"; 
  var checkStr = theField;
  var allValid = false; 
  for (i = 0;  i < checkStr.length;  i++) 
  { 
    ch = checkStr.charAt(i); 
    for (j = 0;  j < checkOK.length;  j++) 
      if (ch == checkOK.charAt(j)) 
		return true;
        break; 
    if (j == checkOK.length) 
    { 
      allValid = false; 
      break; 
    } 
  } 
  return(allValid); 
} 

function _validate(theelement) {
	if (typeof(theelement) != "undefined")
	{
		if (theelement.required == 'yes')
			{
			if (theelement.value.length == 0)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' is required');
				else
					alert(theelement.name + ' is required');
		 		theelement.focus();
				return false;
				}
			}
		if (theelement.minlength > 0)
			{
			if (theelement.value.length < theelement.minlength)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' min length is ' + theelement.minlength);
				else
					alert(theelement.name + ' min length is ' + theelement.minlength);
		 		theelement.focus();
				return false;
				}
			}
		if (theelement.maxlength > 0)
			{
			if (theelement.value.length > theelement.maxlength)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' max length is ' + theelement.maxlength);
				else
					alert(theelement.name + ' max length is ' + theelement.maxlength);
		 		theelement.focus();
				return false;
				}
			}

		if (theelement.minvalue != '')
			{
			if (theelement.value < theelement.minvalue)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' min value is ' + theelement.minvalue);
				else
					alert(theelement.name + ' min value is ' + theelement.minvalue);
		 		theelement.focus();
				return false;
				}
			}

		if (theelement.phone == 'yes')
			{
			var PhoneNumber;
			PhoneNumber = isPhoneNumber(theelement.value);
			if (PhoneNumber == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must be a valid phone number (area code + exchange + ext)');
				else
					alert(theelement.name + ' must be a valid phone number (area code + exchange + ext)');
		 			theelement.focus();
				return false;
				}
			else
				if (PhoneNumber.length == 14)
					theelement.value = PhoneNumber;
			}

		if (theelement.email == 'yes')
			{
			if (isEmailAddress(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must be a valid email address');
				else
					alert(theelement.name + ' must be a valid email address');
		 			theelement.focus();
				return false;
				}
			}

		if (theelement.maxvalue != '')
			{
			if (theelement.value > theelement.maxvalue)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' max value is ' + theelement.maxvalue);
				else
					alert(theelement.name + ' max value is ' + theelement.maxvalue);
		     		theelement.focus();
				return false;
				}
			}

		if (theelement.alpha == "yes")
			{
			if (CheckForAlpha(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must be all alpha');
				else
					alert(theelement.name + ' must be all alpha');
		     		theelement.focus();
				return false;
				}
			}

		if (theelement.containsnumeric == "yes")
			{
			if (ContainsNumeric(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must contain at least one numeric digit');
				else
					alert(theelement.name + ' must contain at least one numeric digit');
		     		theelement.focus();
				return false;
				}
			}

		if (theelement.containsspecial == "yes")
			{
			if (ContainsSpecial(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must contain at least one of the following special characters: !@#$%^&*(){}[];:<>,./?\|');
				else
					alert(theelement.name + ' must contain at least one of the following special characters: !@#$%^&*(){}[];:<>,./?\|');
		     		theelement.focus();
				return false;
				}
			}

		if (theelement.containsalpha == "yes")
			{
			if (ContainsAlpha(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must contain at least one alpha character');
				else
					alert(theelement.name + ' must contain at least one alpha character');
		     		theelement.focus();
				return false;
				}
			}

		if (theelement.caps == "yes")
			{
			if (CheckForCaps(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must be all caps');
				else
					alert(theelement.name + ' must be all caps');
		     		theelement.focus();
				return false;
				}
			}
		
		if (theelement.date == "yes")
			{
				if (theelement.value.length > 0)
				{
					if (isDate(theelement) == false)
						{
						if (theelement.caption != 'undefined')
							alert(theelement.caption + ' must be a valid date');
						else
							alert(theelement.name + ' must be a valid date');
		 				theelement.focus();
						return false;
						}
				}
			}
		
		
		if (theelement.numeric == "yes")
			{
			if (isNumeric(theelement.value) == false)
				{
				if (theelement.caption != 'undefined')
					alert(theelement.caption + ' must be a valid number');
				else
					alert(theelement.name + ' must be a valid number');
		 		theelement.focus();
				return false;
				}
			}
		}
     	return true;
}

function checkinfo() {

var i = 0;
for (i=0;i<document.forms.info.elements.length;i++) 
	{
	
	if (_validate(document.forms.info.elements[i]) == false)
		return false;
	}
     	return true;
}

function isDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. \nFormat mm/dd/yyyy.';
	if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        
    } else {
        alert(errorMessage);
        fld.focus();
        return false;
    } 
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
