
var defaultEmptyOK = false;
var whitespace = " \t\n\r";
var mPrefix = "Please enter valid info for the '";
var mSuffix = "' field.";
var digitsInSocialSecurityNumber = 9;	
var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP|NA";
var phoneNumberDelimiters = "()- ";
var digits = "0123456789";
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}


function isEmpty(str) 
{
	return ((str == null) || (str.length == 0));
}

function isDigit (c) 
{
	return ((c >= "0") && (c <= "9"))
}

function isInteger (s)
{   
	var i;
	if (isEmpty(s)) 
		if (isInteger.arguments.length == 1) 
			return defaultEmptyOK;
		else 
			return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {  
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function isWhitespace (str)
{   
	var i;
    if (isEmpty(str)) return true;
		
    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (whitespace.indexOf(c) == -1)  return false;

    }
    return true;
}

function isInternationalPhoneNumber (str)
{   if (isEmpty(str))
	{ 
		if (isInternationalPhoneNumber.arguments.length == 1) 
			return defaultEmptyOK;
		else 
			return (isInternationalPhoneNumber.arguments[1] == true);
	}
    return (isPositiveInteger(str))
}

function isEmail (str)
{   
	if (isEmpty(str)) return false;
	
    if (isWhitespace(str)) return false;
   
    var i = 1;
    var sLength = str.length;
    while ((i < sLength) && (str.charAt(i) != "@")) i++;
  
    if ((i >= sLength) || (str.charAt(i) != "@")) 
		return false;		 
    else 
		i += 2;
		
    while ((i < sLength) && (str.charAt(i) != ".")) i++;
   
    if ((i >= sLength - 1) || (str.charAt(i) != ".")) 
		return false;
    else 
		return true;
}

function isStateCode(str)
{   
	if (isEmpty(str)) 
		if (isStateCode.arguments.length == 1) 
			return defaultEmptyOK;
		else return (isStateCode.arguments[1] == true);
	else
		return ( (USStateCodes.indexOf(str) != -1) &&
             (str.indexOf(USStateCodeDelimiter) == -1) )
}

function stripCharsNotInBag (str, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function stripInitialWhitespace (str)
{   
	var i = 0;
	while ((i < str.length) && (whitespace.indexOf(str.charAt(i)) != -1))
		i++;   
    return str.substring (i, str.length);
}

function radioChecked(radioField, msgStr)
{
	for (i=0; i< radioField.length; i++) 
	{
		if (radioField[i].checked) return true;
	}
	return (warnInvalidWoSelect (radioField[0], msgStr));
}

function checkInternationalPhone (theField, emptyOK)
{   
	if (checkInternationalPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
    else
    {
		var normalizedWPhone = stripCharsNotInBag(theField.value, validWorldPhoneChars);
		alert("norm= " + normalizedWPhone);
		if (!isInternationalPhoneNumber(normalizedPhone, false)) 
			return warnInvalid (theField, "Please enter a valid phone number (only digits, \"-\", and \"+\" allowed");
		else 
			return true;
    }
}

function checkSSN (theField)
{   
	if (isEmpty(theField.value))
		return warnInvalid (theField, "Please enter a value for the Social Security Number field.");	
	if (isInteger(theField.value) && theField.value.length == digitsInSocialSecurityNumber)
		return true;
	else 
		return warnInvalid (theField, "Please enter a 9 digit Social Security Number.");
}

function checkEmail (theField, emptyOK)
{   
	if (checkEmail.arguments.length == 1) 
		emptyOK = defaultEmptyOK;

    theField.value = stripInitialWhitespace (theField.value);
		
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
    else if (!isEmail(theField.value)) 
       return	warnInvalid (theField, "Please enter a valid email address (like user@srvr.com).");
    else 
		return true;
}

function checkStateCode (theField, emptyOK)
{   
	if (checkStateCode.arguments.length == 1) 
		emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
    else
    {  theField.value = theField.value.toUpperCase();
       if (!isStateCode(theField.value, false)) 
			return (warnInvalid (theField, "Please enter a valid state abbreviation \(like CA for California\)."));
       else 
			return true;
    }
}

function checkForEmptyString (theField, msgStr, emptyOK)
{   
    if (checkForEmptyString.arguments.length == 2) 
		emptyOK = defaultEmptyOK;
    
    theField.value = stripInitialWhitespace (theField.value);
    
    if ((emptyOK == true) && (isEmpty(theField.value)))
		return true;
		
    if (isWhitespace(theField.value)) 
		return warnEmpty (theField, msgStr);
    else 
		return true;
}

function checkForOnlyLetters(theField, msgStr, emptyOK)  
{
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ- ";
	var allValid = true;
	theField.value = stripInitialWhitespace(theField.value);
	var checkStr = theField.value;
	if (checkForOnlyLetters.arguments.length == 2) emptyOK = defaultEmptyOK;
	
	if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
	if (isEmpty(theField.value))
		return warnInvalid (theField, "Please enter a value for the \"" + msgStr + "\" field.");
	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 (!allValid)
	{
		alert("Please enter only letter characters in the \"" + msgStr + "\" field.");
		theField.focus();
		return (false);
	}
	else
		return (true);
}

function checkForOnlyDigitsDashes(theField, msgStr)  
{
	var checkOK = "0123456789-- ";
  	var allValid = true;
	theField.value = stripInitialWhitespace (theField.value);
	var checkStr = theField.value ;
	
	if (isEmpty(theField.value))
		return warnInvalid (theField, "Please enter a value for the \"" + msgStr + "\" field.");
	
	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 (!allValid)
	{
		alert("Please enter only numbers and \"-\" characters in the \"" + msgStr + "\" field.");
		theField.focus();
		return (false);
	}
	else
		return (true);
}


function checkForOnlyDigits(theField, msgStr)  
{
	var checkOK = "0123456789";
  	var allValid = true;
	theField.value = stripInitialWhitespace (theField.value);
	var checkStr = theField.value ;
	
	if (isEmpty(theField.value))
		return warnInvalid (theField, "Please enter a value for the \"" + msgStr + "\" field.");
	
	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 (!allValid)
	{
		alert("Please enter only numbers in the \"" + msgStr + "\" field.");
		theField.focus();
		return (false);
	}
	else
		return (true);
}

function checkForOnlyDigitsDashesParens(theField, msgStr)  
{
	var checkOK = "0123456789-()";
  	var allValid = true;
	theField.value = stripInitialWhitespace (theField.value);
	var checkStr = theField.value;
	if (isEmpty(theField.value))
		return warnInvalid (theField, "Please enter a value for the \"" + msgStr + "\" field.");
	
	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 (!allValid)
	{
		alert("Please enter only numbers \"-\" , \"\(\" and \"\)\" characters in the \"" + msgStr + "\" field.");
		theField.focus();
		return (false);
	}
	else
		return (true);		
}

function warnEmpty (theField, msgStr)
{   
	theField.focus();
    alert(mPrefix + msgStr + mSuffix);
    return false;
}

function warnInvalid (theField, msgStr)
{   
	theField.focus();
    theField.select();
    alert(msgStr);
    return false;
}
function warnInvalidWoSelect (theField, msgStr)
{   
	theField.focus();
    alert(msgStr);
    return false;
}


function CheckNumericLength(FieldValue,MaxCount)
// This function checks the numeric length of an input string and returns false if it exceeds maxlength
{
	var i,Count,Flag;
	Count = 0;
	//ValToCompare = DoTrim(FieldValue);
	Flag = true;
	for (i=0;i<FieldValue.length;i++)
	{
		
		if (!(IsNumber(FieldValue.charAt(i))))
		{
			Flag = false;
			break;
		}
		else
		{
			Count++;
		}
	}
	if (Flag)
	{
		if (Count != MaxCount)
		{
			Flag = false;
		}
	}
	return Flag;
}

function IsNumber(CharIn)
// This function returns true if the input character is a number 0 to 9
{
	var Flag,i;
	Flag = false;
	var Temp = new Array("0","1","2","3","4","5","6","7","8","9");
	for (i=0 ; i<Temp.length;i++)
	{
		if (Temp[i]==CharIn)
		{
			Flag = true;
			break;
		}
	}
	return Flag;
}
function DoReplaceForTel(strIn)
// This function removes special characters from the telephone number (such as " ",")","(" and "-" and returns the set 
// of characters only
{
	var strTemp,strRet;
	strTemp = strIn;
	strRet = "";
	for (i=0;i<strTemp.length;i++)
	{
		if ((strTemp.charAt(i) != " ") &&(strTemp.charAt(i) != "(") && (strTemp.charAt(i) != ")") &&
			(strTemp.charAt(i) != "-"))
		{
			strRet = strRet + strTemp.charAt(i);
		}
	}
	return strRet;
}

function CheckTelephone(Field1,Field2)
{
	var Flag;
	Flag = false;
	if (! CheckNumericLength(DoReplaceForTel(Field1.value),3))
	{
		alert("Area code should have 3 digits");
		Flag = true;
		Field1.focus();
	}
	if (! Flag)
	{
		if (! CheckNumericLength(DoReplaceForTel(Field2.value),7))
		{
			alert("Telephone Number should have 7 digits");
			Flag = true;
			Field2.focus();
		}
	}
	return Flag;
}


<!-- INSERT VARIABLES TO BE VALIDATED HERE -->

function validateForm(theForm)
{	
	if (! checkForEmptyString(theForm.first_name, "First Name")) return (false);
	if (! checkForEmptyString(theForm.last_name, "Last Name")) return (false);


}


