/*********************************************************************
//	
//   Validation.js
//
//	 Purpose: This client side page contains commonly used JavaScript
//			  validation functions required by the PSQ Replacement
//			  web site.
//
//   Created: Robert Held		1/18/02
//
//	 History: 
//
**********************************************************************/


// Regular expressions
var reLetter = /^[a-zA-Z]$/;
var reAlphabetic = /^[a-z A-Z]+$/;
var reAlphanumeric = /^[a-z A-Z0-9]+$/;
var reDigit = /^\d/;
var reInteger = /^\d+$/;
var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
var reSignedInteger = /^([+]|[-])?\d+$/;
var reSignedFloat = /^((([+]|[-])?\d+(\.\d*)?)|(([+]|[-])?(\d*\.)?\d+))$/;
var reEmail = /^.+\@.+\.\w{2,3}$/;
var reUSPhone = /^([2-9]\d{9})|([2-9]\d{2}[-.\s]\d{3}[-.\s]\d{4})$/;
var reZipCode = /^\d{5}$/;
var reZipCode2 = /^\d{4}$/;
var reNumUnits = /^[1-9]$/;
var reSSN = /^(\d{3}-\d{2}-\d{4})|(\d{9})$/;	//14285

// Other holding variables
var digits = "0123456789";
var phoneNumberDelimiters = "";
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 4;
var sTestValue;
var sInstruction;
var bCoAppInfofirst = "0";
var bCoAppInfomiddle = "0";
var bCoAppInfolast = "0";
var bCoAppInfosuffix = "0";
var bCoAppInfoDOB = "0";
var bCoAppInfoSSN1 = "0";
var bCoAppInfoSSN2 = "0";
var bCoAppInfoSSN3 = "0";
var bCoAppInfoError = "0";
var bCoAppSSN = "0";
var bCoAppSSN2 = "0";
var bCoAppSSN3 = "0";
var bCoAppSSNlength = "0";
var bCoAppSSN2length = "0";
var bCoAppSSN3length = "0";
var sState;
var sProductType;
var bNoSSNEdit = "0";
var bAppSSN = "0";
var nowYear;
var sysDate;
var bPrevStreet = "0";
var bPrevCity = "0";
var bPrevState = "0";
var bPrevZip = "0";
var bPrevZip2 = "0";
var bPrevError = "0";
var sInstructionZip = "";
var sInstructionZip2 = "";

// Message variable for NS4 browsers
var displayErrors;

// General message parts for error alerts
var mMissingValPrefix = "You must enter a valid ";
var mValPrefix = "Invalid data. Value for this field must be";


// Specific message instructions
var iInteger = " an integer";
var iNegInt = " a valid integer less than 0.";
var iPosInteger = " a valid integer greater than or equal to 0.";
var iFloat = " a valid number.";
var iFloat2 = " a valid number";
var iAlphabetic = " letters of the alphabet only.";
var iAlphaNum = " valid letters or digits only.";
var iStateCode = " a valid two character U.S. state abbreviation (for example CA for California). Please reenter it now.";
var iValidStateForProduct = " a state where these products are currently offered -- currently this is only California.";
var iNumUnits = " a valid integer between 1 and 9.";
var iZIPCode = " a 5 digit U.S. ZIP Code.";
var iZIPCode2 = " a 4 digit U.S. ZIP Code.";
var iUSPhone = " a 10 digit U.S. phone number (for example 415-555-1212).";
var iEmail = " an e-mail address in the format of name@domain.extension";
var iDatMessage;
var iDay = " a day number between 1 and 31.";
var iMonth = " a month number between 1 and 12.";
var iCoAppInfo = " CoApplicant data is incomplete.  Must have a first and last name, birth date, and SSN.";
var iCoAppInfo2 = " CoApplicant data is incomplete.  Must have a first and last name.";				//14285
var iSSN = " a valid number with a format of 999-99-9999.";
var iSSN2 = " a valid number with a format of 999999999 or 999-99-9999.";					//14285
var iYear = " a 4 digit year number.";
var iYear2 = " a year between 1800 and next year.";
var iDatePrefix = "The Day, Month, and Year for ";
var iDateSuffix = " do not form a valid date.";


// Prompts
var pEntryPrompt = "Please enter a ";
var pStateCode = "2 character code (for example CA).";
var pZIPCode = "5 digit U.S. ZIP Code.";
var pZIPCode2 = "4 digit U.S. ZIP Code.";
var pUSPhone = "10 digit U.S. phone number (for example 415-555-1212).";
var pDay = "day number between 1 and 31.";
var pMonth = "month number between 1 and 12.";
var pYear = "2 or 4 digit year number.";

// By default are fields allowed to be empty?
var defaultEmptyOK = true;

// Date validation array info
var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;
sysDate = new Date();
nowYear = sysDate.getFullYear();

// US State array info
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";

var bNamedInsuredError = false;

/**********************************************************************
/
/   makeArray
/
/   Purpose:    builds an 'n' element array and returns it
/
/   Arguments:  n - an integer representing the number of elements
/				    in the array to be created
/
/   Returns:    an array with 'n' elements
/
**********************************************************************/
function makeArray(n) 
{	for (var i = 1; i <= n; i++) 
	{	this[i] = 0
   	} 
   	return this
}


/**********************************************************************
/
/   isEmpty
/
/   Purpose:    checks to see if a variable is empty by checking
/				to see if it is equal to null or of zero length
/
/   Arguments:  s - the variable to be checked
/
/   Returns:    boolean
/
**********************************************************************/
function isEmpty(s)
{   

return ((s == null) || (s.length == 0));
	
}


/**********************************************************************
/
/   makeArray
/
/   Purpose:    builds an 'n' element array and returns it
/
/   Arguments:  n - an integer representing the number of elements
/				    in the array to be created
/
/   Returns:    an array with 'n' elements
/
**********************************************************************/
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


/**********************************************************************
/
/   isDigit
/
/   Purpose:    checks to see if a character is a number from 0-9 by
/				comparing it against a regular expression
/
/   Arguments:  c - the character to check
/
/   Returns:    boolean
/
**********************************************************************/
function isDigit (c)
{   return reDigit.test(c)
}


/**********************************************************************
/
/   isAlphabetic
/
/   Purpose:    checks to see if a character is a letter from a-z by
/			    comparing it against a regular expression
/
/   Arguments:  s - the character to check
/
/   Returns:    boolean
/
**********************************************************************/
function isAlphabetic (s)
{   var i;
    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);
    else 
	{	return reAlphabetic.test(s)
    }
}

/**********************************************************************
/
/   isAlphaNumeric
/
/   Purpose:    checks to see if a character is an alphanumeric by
/			    comparing it against a regular expression
/
/   Arguments:  s - the character to check
/
/   Returns:    boolean
/
**********************************************************************/
function isAlphaNumeric (s)
{   var i;
    if (isEmpty(s)) 
       if (isAlphaNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphaNumeric.arguments[1] == true);
    else {
       return reAlphanumeric.test(s)
    }
}
/**********************************************************************
/
/   IsNumeric
/
/   Purpose:    checks to see if a value is numeric
/			    
/
/   Arguments:  strString - the value to check
/
/   Returns:    boolean
/
**********************************************************************/
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
		
         {
         blnResult = false;
         }
      }
    
   return blnResult;
   }


/**********************************************************************
/
/   isInteger
/
/   Purpose:    checks to see if a string represents an integer by
/			    comparing it against a regular expression
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

	return reInteger.test(s);
}


/**********************************************************************
/
/   isFloat
/
/   Purpose:    checks to see if a string represents a 'float' by
/			    comparing it against a regular expression
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isFloat (s)
{   if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    return reFloat.test(s)
}


/**********************************************************************
/
/   reformat
/
/   Purpose:    
/
/   Arguments:  s - the string to reformat
/
/   Returns:    the reformatted string
/
**********************************************************************/
function reformat (s)
{   var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) 
	{	arg = reformat.arguments[i];
       	if (i % 2 == 1) resultString += arg;
       	else 
		{	resultString += s.substring(sPos, sPos + arg);
           	sPos += arg;
       	}
    }
    return resultString;
}


/**********************************************************************
/
/   isUSPhoneNumber
/
/   Purpose:    checks to see if a string represents a valid US
/				phone number
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       	if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       	else 
       	{
       	return (isUSPhoneNumber.arguments[1] == true);
		return (isInteger(s) && s.length == digitsInUSPhoneNumber);
		}
    else 
	{	
		return reUSPhone.test(s)
	}
}


/**********************************************************************
/
/   isZIPCode
/
/   Purpose:    checks to see if a string is in a valid US
/				ZipCode format
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isZIPCode (s)
{  	var bResult;
	if (isEmpty(s)) {
       	if (isZIPCode.arguments.length == 1) {
			return defaultEmptyOK;
			}
       	else 
       		{
       		return (isZIPCode.arguments[1] == true);
			return (isInteger(s) && (s.length == digitsInZIPCode1)); 
		
            }
     }      
     else
          {
       return reZipCode.test(s);
        }
	
     
}

function isZIPCode2 (s)
{  	var bResult;
	if (isEmpty(s)) {
       		if (isZIPCode2.arguments.length == 1) {
			   return defaultEmptyOK;
			  }
       		else
       		{
       		 return (isZIPCode2.arguments[1] == true);
			return (isInteger(s) &&	(s.length == digitsInZIPCode2));
			
        	 } 
	} 
         else
     {
		
        return reZipCode2.test(s); 
     }      
}

function isNumUnits (s)
{  	var bResult;
	if (isEmpty(s)) {
       	if (isNumUnits.arguments.length == 1) {
			return defaultEmptyOK;
			}
       	else 
       		{
	
       		return (isNumUnits.arguments[1] == true);
			return (isInteger(s) && (s.length == 1)); 
		
            }
     }      
     else
          {
          
       return reNumUnits.test(s);
        }
	
     
}
/**********************************************************************
/
/   isEMail
/
/   Purpose:    checks to see if a string is contains a correctly
/				formatted e-mail address by comparing it against a
/				regular expression
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);    
    else 
	{	
		return reEmail.test(s)
	}
}

/**********************************************************************
/   14285
/   isSSN
/
/   Purpose:    checks to see if a string contains a correctly
/		formatted social security number by comparing it against a
/		regular expression
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);    
    else 
	{	
		return reSSN.test(s)
	}
}

/**********************************************************************
/
/   isStateCode
/
/   Purpose:    checks to see if a string contains a valid US state
/				code
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isStateCode(s)
{
	if (isEmpty(s)) {
		if (isStateCode.arguments.length == 1) {
				return defaultEmptyOK;
		}
       	else {
				return (isStateCode.arguments[1] == true);
		}
	}
    return ((USStateCodes.indexOf(s) != -1) &&
            (s.indexOf(USStateCodeDelimiter) == -1))
}


/**********************************************************************
/
/   isYear
/
/   Purpose:    checks to see if a string contains a valid integer
/				representing a year
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isYear (s)
{   if (isEmpty(s)) 
       	if (isYear.arguments.length == 1) return defaultEmptyOK;
    	else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    // this should be restricted to four digit years, two
    // digit years will not be acceptable.
    if ((s < 1800) || (s > nowYear)) return false;
    return (s.length == 4);
}

function isYear2 (s)
{   if (isEmpty(s)) 
       	if (isYear2.arguments.length == 1) return defaultEmptyOK;
    	else return (isYear2.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    // this should be restricted to four digit years, two
    // digit years will not be acceptable.
    if (!isEmpty(s)) {
      if (s == 0) return true;
      if ((s < 1800) || (s > (nowYear + 1))) return false;
    return (s.length == 4);
    }
}
/**********************************************************************
/
/   isSignedInteger
/
/   Purpose:    checks to see if a string contains a signed integer
/				value using a regular expression
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isSignedInteger (s)
{   if (isEmpty(s)) 
       	if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       	else return (isSignedInteger.arguments[1] == true);
    else 
	{	return reSignedInteger.test(s)
    }
}


/**********************************************************************
/
/   isNonnegativeInteger
/
/   Purpose:    checks if a string contains a non-negative integer
/			    value
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ((isEmpty(s) && secondArg)  || (parseInt (s) >= 0)));
}


/**********************************************************************
/
/   isNegativeInteger
/
/   Purpose:    checks if a string contains a negative integer value
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;
    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ((isEmpty(s) && secondArg)  || (parseInt (s) < 0)));
}


/**********************************************************************
/
/   isIntegerInRange
/
/   Purpose:    checks if a string contains an integer value that
/               falls within a specified range of values
/
/   Arguments:  s - the string to check
/				a - the lower end of range
/				b - the upper end of range
/
/   Returns:    boolean
/
**********************************************************************/
function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);
    if (!isInteger(s, false)) return false;
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


/**********************************************************************
/
/   isFloatInRange
/
/   Purpose:    checks if a string contains a float value that falls
/				within a specified range of values
/
/   Arguments:  s - the string to check
/				a - the lower end of range
/				b - the upper end of range
/
/   Returns:    boolean
/
**********************************************************************/
function isFloatInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isFloatInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isFloatInRange.arguments[1] == true);
    if (!isFloat(s, false)) return false;
    var num = parseFloat (s);

    return ((num >= a) && (num <= b));
}


/**********************************************************************
/
/   isMonth
/
/   Purpose:    checks if a string contains an valid integer containing
/				a month value (1-12)
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);

    //Remove leading zero or else parseInt will always return zero.
    if (s.substr(0,1) == "0") {
       s = s.substr(1)
    }

    return isIntegerInRange (s, 1, 12);
}


/**********************************************************************
/
/   isDay
/
/   Purpose:    checks if a string contains a valid integer containing
/				a day value (1-31)
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   

    //Remove leading zero or else parseInt will always return zero.
    if (s.substr(0,1) == "0") {
       s = s.substr(1)
    }

    return isIntegerInRange (s, 1, 31);
}


/**********************************************************************
/
/   daysInFebruary
/
/   Purpose:    retrieves the number of days in February for a given
/				year
/
/   Arguments:  year - a variable containing an integer representing
/					   the year to test
/
/   Returns:    an integer containing the number of days in February
/				for the tested year
/
**********************************************************************/
function daysInFebruary (year)
{		
	return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


/**********************************************************************
/
/   isDate
/
/   Purpose:    checks if a the day, month, year information passed
/				in represents a valid date
/
/   Arguments:  year - an integer representing the year
/				month - an integer representing the month
/				day - an integer representing the day
/
/   Returns:    boolean
/
**********************************************************************/
function isDate (year, month, day)
{   if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


/**********************************************************************
/
/   warnInvalid
/
/   Purpose:    notifies the end user that one of the field values
/				the entered is invalid.  this function pops-up an
/				alert box.
/
/   Arguments:  theField - the field with the invalid entry
/				
/				s - the string message to be displayed to the end user
/					in the alert box
/
/   Returns:    boolean
/
**********************************************************************/
function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}


/**********************************************************************
/
/   checkString
/
/   Purpose:    validates that a field's value is a string
/
/   Arguments:  theField - the field to check
/
/				s - the message to display if the field is empty
/
/				emptyOK - a boolean value that says whether or not
/						  this field is allowed to be empty during
/						  validation
/
/   Returns:    boolean
/
**********************************************************************/
function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}


/**********************************************************************
/
/   checkStateCode
/
/   Purpose:    validates that a field's value is a valid US
/				state code
/
/   Arguments:  theField - the field to check
/
/				emptyOK - is this field allowed to be empty? (boolean)
/
/   Returns:    boolean
/
**********************************************************************/
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, iStateCode);
       else return true;
    }
}


/**********************************************************************
/
/   isValidStateForProduct
/
/   Purpose:    Verifies that the product is offered in the US state
/				of the property address the end user has entered.
/				Currently only CA and WA are valid states.
/
/   Arguments:  sStateName - the two char state name abbreviation
/							 to test
/
/   Returns:    boolean
/
**********************************************************************/
function isValidStateForProduct(sStateName)
{  
	if (sStateName == "CA") {
		return true;
	}
	else {
   		return false;
	}   
}


/**********************************************************************
/
/   reformatZIPCode
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    
/
**********************************************************************/
function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}


/**********************************************************************
/
/   checkZIPCode
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { 
		var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      	if (!isZIPCode(normalizedZIP, false)) 
        	return warnInvalid (theField, iZIPCode);
	    else 
	    {  // if you don't want to insert a hyphen, comment next line out
	        theField.value = reformatZIPCode(normalizedZIP)
	        return true;
	    }
    }
}


/**********************************************************************
/
/   reformatUSPhone
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    
/
**********************************************************************/
function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


/**********************************************************************
/
/   checkUSPhone
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkUSPhone (theField, emptyOK)
{   
	if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;

    if ((emptyOK == true) && (isEmpty(theField.value))) 
	{
		return true;
    }else{ 
		var normalizedPhone = stripCharsInBag(theField, phoneNumberDelimiters)
       	if (!isUSPhoneNumber(normalizedPhone)) 
	   	{
          	//return warnInvalid (theField, iUSPhone);
		  	return false;
		}else{  
	   		// if you don't want to reformat as (123) 456-789, comment next line out
          	theField.value = reformatUSPhone(normalizedPhone)
          	return true;
       }
    }
}


/**********************************************************************
/
/   checkYear
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else return true;
}


/**********************************************************************
/
/   checkMonth
/
/   Purpose:   
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return warnInvalid (theField, iMonth);
    else return true;
}


/**********************************************************************
/
/   checkDay
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkDay (theField, emptyOK)
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return warnInvalid (theField, iDay);
    else return true;
}


/**********************************************************************
/
/   checkDate
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay)
{   if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

/**********************************************************************
/
/   validateContent
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function validateContent(oForm, oControl, intPos)
{	
	// set values into holding variables.
	var sContentFlag = getContentFlag(oControl.name);
	var isValidContent = false;
	iInstruction = "";
	
	
	// Check if the field requires content
	//if ((parseInt(sContentFlag) == 0) || (oControl.name == "num-1-appSSN1") || (oControl.name == "num-1-appSSN2") || (oControl.name == "num-1-appSSN3"))		//14285
	if (parseInt(sContentFlag) == 0) 	//14285
	 
	{	//Not required, no need to validate for content.
		isValidContent = true;
	}else{	
		//Must validate for content
		if (oControl.type.toUpperCase() == "SELECT-ONE") {
			if ((oControl.selectedIndex == 0) && (oControl.value == "")){
				deliverErrorMessage(oForm, oControl, iInstruction, intPos);
				isValidContent = false;
			}else{
				isValidContent = true;
			}
		}else if (oControl.type.toUpperCase() == "RADIO") {
			if (!oControl.checked) {
				deliverErrorMessage(oForm, oControl, iInstruction, intPos);
				isValidContent = false;
			}else{
				isValidContent = true;
			}
		}else{
			if (isEmpty(oControl.value)) {
				deliverErrorMessage(oForm, oControl, iInstruction, intPos);
				isValidContent = false;
			}else{
				isValidContent = true;
			}
		}
	}
	return isValidContent	
}


/**********************************************************************
/
/   validateData
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function validateData(oForm, oControl, intPos)
{	// Declare local holding variables for parsing
	var iTemp;
	var iTemp2;
	var sType;
	var sString = oControl.name;
	var pos = sString.indexOf("-");
	var rangepos1 = sString.indexOf("_");
	var rangepos2;
	var vRangeVal1;
	var vRangeVal2;
	var isValid = false;
	var vValue = oControl.value;
	var isRequired = false;
	
	// Set variable values
	sInstruction = "";
	sInstructionZip2 = "";
	bCoAppInfoError = "0";		//14285

	//Extract type value
	if (rangepos1 > 0)
	{	//There are range values in sString
		sType = sString.substring(0, rangepos1);
		iTemp = rangepos1 + 1;
		rangepos2 = sString.indexOf("_", iTemp);
		vRangeVal1 = sString.substring(iTemp, rangepos2);
		iTemp = rangepos2 + 1;
		vRangeVal2 = sString.substring(iTemp, pos);
	}else{	
		//There are no range values in sString
		sType = sString.substring(0, pos);
	}

	isRequired = getContentFlag(oControl.name)=="1";
	// Perform validation on data type, 
	// set specific instruction for error alert if required
	switch (sType)
	{	// Here follows all possible types to be validated from web site
		case "intrng": 
			if (isRequired || (!isEmpty(vValue) && !isRequired)){
				if (isIntegerInRange (vValue, vRangeVal1, vRangeVal2))
				{	
					isValid = true;	
				}else{	
					sInstruction = iInteger + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";   
				}
			}else{
				isValid = true;
			}
			break; 							
		case "zip": 
			
			if (oControl.name == "zip-0-previouszip") {
				if (vValue == "") {
					bPrevZip = "0";
				} else {
					bPrevZip = "1";
				}
			}
			if (isZIPCode (vValue))
			  
			
			{
				isValid = true; 
				
			}else{
				if (oControl.name == "zip-0-previouszip") {
					isValid = true;
					sInstructionZip = iZIPCode;
				} else {
					sInstruction = iZIPCode;
				}
			}
			break; 	
		case "zip4": 
			if (oControl.name == "zip4-0-previouszip2") {
				if (vValue == "") {
					bPrevZip2 = "0";
				} else {
					bPrevZip2 = "1";
				}
			}
			if (isZIPCode2 (vValue))
			{
				isValid = true;  
			}else{	
				if (oControl.name == "zip4-0-previouszip2") {
					isValid = true;
					sInstructionZip2 = iZIPCode2;
				} else {
					sInstruction = iZIPCode2;
				}
			}
			break; 										
		case "phone": 
			if (!isEmpty(vValue))
			{	if (checkUSPhone (vValue, false))
				{	isValid = true;	}
				else
				{	sInstruction = iUSPhone;	}
			}
			else
			{	isValid = true;  }
			break; 							
		case "year":  
			if (isYear2 (vValue))
			{	isValid = true;	}
			else	
			   {
			   	sInstruction = iYear2;
				}
			break; 							
		case "digit":
			if (!isEmpty(vValue)) 
			{	if (isDigit(vValue))
				{	isValid = true;		}
				else
				{	sInstruction = iFloat;	}
			}
			else
			{	isValid = true;		}
			break; 							
		case "pos": 
			if (isNonnegativeInteger (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iPosInteger;		}
			break; 							
		case "sig": 
			if (isSignedInteger (vValue))
			{	isValid = true;		}
			else
			{	sInstruction = iInteger + ".";	}			
			break; 							
		case "lng":  
			if (isSignedInteger (vValue))
			{	isValid = true;		}
			else
			{	sInstruction = iInteger + ".";	}			
			break; 							
		case "yearrng":
			if (isYear (vValue))
			{	if (isIntegerInRange (vValue, vRangeVal1, vRangeVal2))
				{	isValid = true;	}
				else
				{	sInstruction = " a valid year between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			}
			else
			{	sInstruction = iYear;	}
			break; 							
		case "ziprng": 
			if (isZIPCode (vValue))
			{	if (isIntegerInRange (vValue, vRangeVal1, vRangeVal2))
				{	isValid = true;		}
				else
				{	sInstruction = " a valid zipcode between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			}
			else
			{	sInstruction = iZIPCode;	}
			break; 							
		case "neg": 
			if (isNegativeInteger (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iNegInt;	}
			break; 							
		case "int":
			if (isInteger (vValue))	
			{	isValid = true;	}
			else
			{	sInstruction = iInteger + ".";	}
			break; 							
		case "date": 
			if (!isEmpty(vValue)) {
				//14285
				if (oControl.name == "date-0-coappDOB") {
					bCoAppInfoDOB = "1"; 
				} else { 
					bCoAppInfoDOB = "0";
				}			
				if (CheckDateFormat(vValue)) {
					isValid = true;
				} else {
					sInstruction = iDatMessage;
				}
			} else {
				isValid = true;
			}
			break; 							
		case "curr":
			sTestValue = vValue;
			sTestValue = stripCharsInBag (sTestValue, "$")  
			if (isFloat (sTestValue))
			{	isValid = true;	}
			else
			{	sInstruction = iFloat;	}
			break; 							
		case "dec":  
			if (isFloat (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iFloat;	}
			break; 							
		case "currrng": 
			sTestValue = vValue; 
			sTestValue =  stripCharsInBag (sTestValue, "$")  
			if (isFloatInRange (sTestValue, vRangeVal1, vRangeVal2))
			{	isValid = true;	}
			else
			{	sInstruction = iFloat2 + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			break; 
		case "decrng":
			//if ((oControl.name == "decrng_.01_999.9-0-distancetoshore") && ((sState == "FL") || (sState == "SC") || (sState == "NC") || (sState == "TX"))) {
				//if (isFloatInRange (vValue, vRangeVal1, vRangeVal2))
				//	{	isValid = true;	}
				//else
				//	{	sInstruction = iFloat2 + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
				//if (isValid)
				//{ 
				//	if (sString != "decrng_.01_999.9-0-distancetoshore") {
				//		var j = vValue.indexOf(".");
				//	if (j != -1) {
				//		var i = vValue.length - vValue.indexOf(".");
				//		if (i <= 3) {
				//			isValid = true;	}
				//		else
				//		{	isValid = false;
				//			sInstruction = iFloat2 + " to two decimal places (example: 10.33).";	}
				//	}
				//}
			//}	
			//}
			//else
			//{ isValid = true; }
			if (isFloatInRange (vValue, vRangeVal1, vRangeVal2))
				{	isValid = true;	}
			else
				{	sInstruction = iFloat2 + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			break; 
		case "alpha":   
			if (isAlphabetic (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iAlphabetic;	}			
			break; 							
		case "alphanum": 
			if (isAlphaNumeric (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iAlphaNum;	}			
			break; 							
		case "state": 
			sTestValue =  vValue.toUpperCase();
			if (isStateCode(sTestValue)) {
				if (oControl.name == "state-1-propertystate") {
					if (isValidStateForProduct(sTestValue)) {
						isValid = true;
					}
					else {
						sInstruction = iValidStateForProduct;
					}
				}
				else {
					isValid = true;
				}
			}
			else {
				sInstruction = iStateCode;
			}
			break; 		
		case "email":
			if (isEmail (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iEmail;	}
			break;	
		//14285 Add SSN type
		case "ssn":
			if ((oControl.name == "ssn-0-coappSSN") && (vValue != "")) {
				bCoAppInfoSSN1 = "1";
			}
			if (isSSN (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iSSN2;	}

			//14285 Make sure on co-app that if the first name is filled in that the last name is as well and vice versa.
			if (oControl.name == "ssn-0-coappSSN") {
				if (((bCoAppInfofirst == "1" && bCoAppInfolast == "0") ||
				     (bCoAppInfofirst == "0" && bCoAppInfolast == "1")) ||
				    ((bCoAppInfofirst == "0" || bCoAppInfolast == "0") &&
				     (bCoAppInfoSSN1 == "1" || bCoAppInfoDOB == "1"))) {
					if (!isValid) deliverErrorMessage(oForm, oControl, sInstruction, intPos);
					sInstruction = iCoAppInfo2;
					bCoAppInfoError = "1";
					isValid = false;
				}
			}

			break;
case "num":
    //14285 Begin Comment Out
    //bCoAppInfoError = "0";
    //bNoSSNEdit = 0;	
    //if ((oControl.name == "num-0-coappSSN1") && (vValue != "")) {
    //	isValid = true;
    //	bCoAppInfoSSN1 = "1";
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //		bCoAppSSN = "0"
    //		bCoAppSSNlength = "0";
    //		if (vValue.length < 3) {
    //		   bCoAppSSN = "1"; }
    //		}
    //	else if (!IsNumeric(vValue)) {
    //		bCoAppSSN = "1";
    //		
    //		//sInstruction = iFloat;
    //		}	
    //		
    //	 }
    //else if ((oControl.name == "num-0-coappSSN1") && (vValue == "")) 
    //	{bCoAppInfoSSN1 = "0";
    //	 isValid = true;}
    //
    //if ((oControl.name == "num-0-coappSSN2") && (vValue != "")) {
    //	isValid = true;				
    //	bCoAppInfoSSN2 = "1"; 
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //	    if (vValue.length < 2) {
    //		   bCoAppSSN = "1"; }
    //		}
    //	else if (!IsNumeric(vValue)) {
    //		bCoAppSSN = "1";
    //		isValid = true;
    //		//sInstruction = iFloat;
    //		}	
    //	}
    //else if ((oControl.name == "num-0-coappSSN2") && (vValue == ""))
    //	{bCoAppInfoSSN2 = "0";
    //	 isValid = true;
    //	 }
    //
    //if ((oControl.name == "num-0-coappSSN3") && (vValue != "")) {
    //	isValid = true;
    //	bCoAppInfoSSN3 = "1"; 
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //		bCoAppSSN = "0";
    //		if (vValue.length < 4) {
    //		   bCoAppSSN = "1"; } 
    //		
    //		}
    //	else if (!IsNumeric(vValue))
    //	{	
    //		IsValid = false;
    //		bCoAppSSN3 = "1";
    //		if (bCoAppSSN != "1") {
    //			sInstruction = iSSN;
    //			deliverErrorMessage(oForm, oControl, sInstruction, intPos);
    //			}
    //		}
    //	
    //	}
    //else if ((oControl.name == "num-0-coappSSN3") && (vValue == ""))
    //	{bCoAppInfoSSN3 = "0";
    //	 isValid = true;
    //	 }
    //
    //if ((oControl.name == "num-1-appSSN1") && (vValue != "")) {
    //	isValid = true;
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //		bAppSSN = "0";
    //		if (vValue.length < 3) {
    //		   bAppSSN = "1"; }
    //		 }
    //	else if (!IsNumeric(vValue)) {
    //		bAppSSN = "1";
    //		isValid = true;
    //		//sInstruction = iFloat;
    //		}	
    //		
    //	 }
    //	 
    //else if ((oControl.name == "num-1-appSSN1") && (vValue == "")) 
    //	{ isValid = true;
    //bAppSSN = "1";
    //	  
    //	}
    //
    //if ((oControl.name == "num-1-appSSN2") && (vValue != "")) {
    //	isValid = true;				
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //	    if (vValue.length < 2) {
    //		   bAppSSN = "1"; }
    //		 }
    //	else if (!IsNumeric(vValue)) {
    //		bAppSSN = "1";
    //		isValid = true;
    //		//sInstruction = iFloat;
    //		}	
    //	}
    //else if ((oControl.name == "num-1-appSSN2") && (vValue.length == 0))
    //	{bAppSSN = "1";
    //	 isValid = true;
    //	 }
    //
    //if ((oControl.name == "num-1-appSSN3") && (vValue != "")) {
    //	isValid = true;
    //	if (IsNumeric(vValue))
    //	{	isValid = true;
    //		if (vValue.length < 4) {
    //		   bAppSSN = "1"; }
    //		 }
    //	else if (!IsNumeric(vValue))
    //	{	
    //		IsValid = true;
    //		bAppSSN = "1";
    //		//if (bCoAppSSN != "1") {
    //		//	sInstruction = iFloat;
    //		//	deliverErrorMessage(oForm, oControl, sInstruction, intPos);
    //		//	}
    //		}
    //	
    //	}
    //else if ((oControl.name == "num-1-appSSN3") && (vValue == ""))
    //	{ isValid = true;
    //	  bAppSSN = "1";
    //	 }
    //14285 End Comment Out

    //if ((bCoAppInfoSSN3 == "0") && (bCoAppInfoSSN2 == "0") && (bCoAppInfoSSN1 == "0") &&
    //     (bCoAppInfoDOB == "0") && (bCoAppInfofirst == "0") && (bCoAppInfomiddle == "0") &&
    //     (bCoAppInfolast == "0") && (bCoAppInfosuffix == "0")){
    //	isValid = true;
    //	
    //	
    //}
    //else
    //if (oControl.name == "num-0-coappSSN1") {
    //	isValid = true; }
    //else 
    //if (oControl.name == "num-0-coappSSN2") {
    //	isValid = true;  }

    //else
    if ((oControl.name == "num-1-bedroomcount") || (oControl.name == "num-0-numberofunits") || (oControl.name == "num-1-distancetofirehydrant") || (oControl.name == "num-1-distancetofirestation")){

        if (IsNumeric(vValue)) {
            if (oControl.name == "num-0-numberofunits") {
                if (vValue.length == 1) {
                    if(isNumUnits(vValue))
                     {
                        isValid = true;
                    } else {

                        sInstruction = iNumUnits;
                    }

                }
                else {

                    IsValid = false;
                    sInstruction = iNumUnits;
                }
            }

            else // all other controls
            
                if ((oControl.name == "num-1-distancetofirehydrant") || (oControl.name == "num-1-distancetofirestation")) {
                    if (vValue.length > 0) {
                        if (vValue < "1") {
                            sInstruction = iFloat;
                            isValid = false;
                        }
                        else {
                            isValid = true;
                        }
                    }
                    else {
                        isValid = true;
                    }
                }
                                   else            
            {

                isValid = true;
            }
            
          
        }    //not numeric 
        
           else {
            if (oControl.name == "num-0-numberofunits") {

                if ((sProductType == "4") || (sProductType == "6"))
                { isValid = true; }
                else {                    isValid = false;
                    sInstruction = iNumUnits;                }
            }

            else {
                isValid = false;
                sInstruction = iFloat;            }
        }
    }
    else {
        if ((IsNumeric(vValue)) || (!isRequired && vValue.length == 0)) {
            isValid = true;
        }
    }
    //20179			
    if ((oControl.name == "num-0-finishedatticsqft") && (IsNumeric(vValue) || vValue.length == 0)) {
        isValid = true;
    }
    if ((oControl.name == "num-0-unfinishedatticsqft") && (IsNumeric(vValue) || vValue.length == 0)) {
        isValid = true;
    }
    if ((oControl.name == "num-0-priorpremium") && (IsNumeric(vValue) || vValue.length == 0)) {
        isValid = true;
    }

    //14285 Begin comment out
    //else
    //if (((oControl.name == "num-0-coappSSN3") &&
    //
    //	(bCoAppInfoSSN3 == "1") && (bCoAppInfoSSN2 == "1") && (bCoAppInfoSSN1 == "1") &&
    //	(bCoAppInfofirst == "1") && (bCoAppInfomiddle == "1") && (bCoAppInfolast == "1") &&
    //	(bCoAppInfosuffix == "1") && (bCoAppInfoDOB == "1")) || ((oControl.name == "num-0-coappSSN3") &&
    //
    //	(bCoAppInfoSSN3 == "0") && (bCoAppInfoSSN2 == "0") && (bCoAppInfoSSN1 == "0") &&
    //	(bCoAppInfofirst == "0") && (bCoAppInfomiddle == "1") && (bCoAppInfolast == "0") &&
    //	(bCoAppInfosuffix == "1") && (bCoAppInfoDOB == "0"))){
    //		bNoSSNEdit = "1";
    //	    isValid = true;
    //					
    //}else if (oControl.name == "num-0-coappSSN3") 
    //    {
    //    sInstruction = iCoAppInfo;
    //	bCoAppInfoError = "1";
    //	isValid = false; }
    //14285 End comment out

    //if (bCoAppInfoError != "1") {
    //	
    //	if (IsNumeric(vValue))
    //	{	isValid = true;	}
    //	else
    //	{	
    // 		IsValid = false;
    //		sInstruction = iFloat;	}

    //14285 Begin comment out	
    //if ((bCoAppSSN == "1") && (bCoAppInfoError != "1") && (oControl.name == "num-0-coappSSN2"))
    //	{	
    //		isValid = false;
    //		sInstruction = iSSN;	}
    //	
    //if ((bAppSSN == "1") && (oControl.name == "num-1-appSSN3"))
    //	{	
    //		isValid = false;
    //		sInstruction = iSSN;	}
    //14285 End Comment out

    break;							
		case "chkbox":
			// All checkboxes should be 0 in the validate section
			isValid = true;
			break; 							
		case "pass":
			isValid = true;
			break; 							
		case "txtarea":
			isValid = true;
			break; 							
		case "hid":
			isValid = true;
			break; 							
		case "sub":
			isValid = true;
			break;
		case "text":
		
			if ((oControl.name == "text-0-namedinsuredfirst2") && (vValue != "")) {
				bCoAppInfofirst = "1"; 
				} 
				else if ((oControl.name == "text-0-namedinsuredfirst2") && (vValue == ""))
				{bCoAppInfofirst = "0";
				}			
			 if (oControl.name == "text-0-namedinsuredmiddle2") {
				bCoAppInfomiddle = "1"; 
				} 
				
			 if	((oControl.name == "text-0-namedinsuredlast2") && (vValue != "")) {
				bCoAppInfolast = "1"; 
				} 
				else if ((oControl.name == "text-0-namedinsuredlast2") && (vValue == ""))
				{bCoAppInfolast = "0";
				}
			 if ((oControl.name == "text-0-previousstreet") && (vValue == "")) {
				bPrevStreet = "0";
				}
			 else if ((oControl.name == "text-0-previousstreet") && (vValue != "")) {
				bPrevStreet = "1";
				}
			 if ((oControl.name == "text-0-previouscity") && (vValue == "")) {
				bPrevCity = "0";
				}
			 else if ((oControl.name == "text-0-previouscity") && (vValue != "")) {
				bPrevCity = "1";
				}	
			isValid = true;
			break;
		case "textrng":
			if (vValue.length >= vRangeVal1 && vValue.length <= vRangeVal2)
				isValid = true;
			else
			    sInstruction = " " + vRangeVal1.toString() + " to " + vRangeVal2.toString() + " characters in length.";
			break;
		case "select":
			if (oControl.name == "select-0-namedinsuredsuffix2") {
				bCoAppInfosuffix = "1"; 
				} 
			if (oControl.name == "select-1-propertystate") {
				sState = oControl.value; 
				} 
			if (oControl.name == "select-1-producttype") {
				sProductType = oControl.value; 
				} 
			if ((oControl.name == "select-0-previousstate") && ((vValue == "")||(vValue == "Select"))) {
				bPrevState = "0";
				}
			else if ((oControl.name == "select-0-previousstate") && (vValue != "")) {
				bPrevState = "1";
				} 		
			isValid = true;
			break;
		default:
			isValid = true;
			break; 							
	}
	if (oControl.name == "zip4-0-previouszip2") {
		if (bPrevStreet == "0" && bPrevCity == "0" && bPrevState == "0" && bPrevZip == "0" && bPrevZip2 == "0") {
			isValid = true; }
		else if (bPrevStreet == "0" || bPrevCity == "0" || bPrevState == "0" || bPrevZip == "0" || sInstructionZip != "" || sInstructionZip2 != "") {
		if (bPrevStreet == "0") {	
			bPrevError = "1";
			//sInstruction = " a valid Previous Street"; 
			oControl = getObjById("text-0-previousstreet", 0);
			isValid = false;
			
			deliverErrorMessage(oForm, oControl, sInstruction, intPos); }
		if (bPrevCity == "0") {
			bPrevError = "1";
			oControl = getObjById("text-0-previouscity", 0);
			isValid = false;
			
			deliverErrorMessage(oForm, oControl, sInstruction, intPos); }
		if (bPrevState == "0") {
			bPrevError = "1";
			oControl = getObjById("select-0-previousstate", 0);
			isValid = false;
			
			deliverErrorMessage(oForm, oControl, sInstruction, intPos); } 
		//if (bPrevZip == "0") {
		//	bPrevError = "1";
		//	oControl = getObjById("zip-0-previouszip", 0);
		//	isValid = false;
		//	deliverErrorMessage(oForm, oControl, sInstruction, intPos); }
		
		if ((bPrevZip == "1") && (sInstructionZip != "")) {
				bPrevError = "1";
				oControl = getObjById("zip-0-previouszip", 0);
				isValid = false;
				sInstruction = sInstructionZip;
				deliverErrorMessage(oForm, oControl, sInstruction, intPos); 
				sInstructionZip = "";} 
		else if (bPrevZip == "0") {
			bPrevError = "1";
			oControl = getObjById("zip-0-previouszip", 0);
			isValid = false;
			deliverErrorMessage(oForm, oControl, sInstruction, intPos); } 
		else if ((bPrevZip2 == "1") && (sInstructionZip2 != "")) {
			bPrevError = "1";
			oControl = getObjById("zip4-0-previouszip2", 0);
			isValid = false;
			sInstruction = sInstructionZip2;
			deliverErrorMessage(oForm, oControl, sInstruction, intPos);
			 }  
			
		
		}	
	}
	//perform label and message painting
	if (bPrevError == "0") {
	if (isValid)
	{	// Fine, continue	
		resetValidControl(oForm, oControl, sInstruction, intPos)
	}else{
		deliverErrorMessage(oForm, oControl, sInstruction, intPos);	
	}
	}
	return isValid
}

/**********************************************************************
/
/   validateYear
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function validateYear(oForm, oControl, intPos)
{	// Declare local holding variables for parsing
	var vValue = oControl.value;
	sInstruction = "";
	
	if (vValue > 1959) {
	   isValid = false;						  
	   sInstruction = " older than 1960 when Retrofit Discount applies."; }
	else {
	   isValid = true; }
	   
	if (isValid)
	{	// Fine, continue	
		resetValidControl(oForm, oControl, sInstruction, intPos)
	}else{
		deliverErrorMessage(oForm, oControl, sInstruction, intPos)	
	}
	return isValid   
}	

/**********************************************************************
/
/   CheckDateFormat
/
/   Purpose:    
/
/   Arguments:  date
/
/   Returns:    boolean
/
**********************************************************************/
function CheckDateFormat(date)
{	// Function will return true if date is good.  Validation will insure 
	// that the date string can be formatted.
	var sTempDate = date;
	var bag = "/";
	var year;
	var month;
	var day;
	
	if (sTempDate.length == 0) return true;
	//look for valid non-digit separator characters and discard
	sTempDate = stripCharsInBag (sTempDate, bag)
	// Check for possible valid string lengths
	if(sTempDate.length != 8)
	{	//String not a valid length 	
		iDatMessage = " in mmddyyyy, or mm/dd/yyyy format.";
		return false;
	}
	month = sTempDate.substr(0, 2);
	day = sTempDate.substr(2, 2);
	year = sTempDate.substr(4);
	//Check for true date
	if (!isDate (year, month, day))
	{	// This is a bad date
		iDatMessage = " a valid date.";
		return false;
	}	
	return true;
}


/**********************************************************************
/
/   deliverErrorMessage
/
/   Purpose:    Paint an error message in the message <DIV> tag
/               for this control.
/
/   Arguments:  oForm - the form object
/				oControl - the form element that is being validated
/				 - the error message to paint
/				intPos - not used.
					 9999 - Do not use message value prefix.
/
/   Returns:    - none -
/
**********************************************************************/
function deliverErrorMessage(oForm, oControl, sInstruction, intPos) {

	var frmName = oControl.name;
	var strLabelName = getFieldName(frmName)
	var sLabel = "label-" + strLabelName;
	var sMessage = "message-errors";
	var sOldLabel;
	
	var oLabel=getObjById(sLabel,1);

	if (isNaN(intPos)) intPos = 0;		//12479

	if (strLabelName == "namedinsured") {
	    bNamedInsuredError = true;
	}
    
    
	changeVisibility(sMessage, visible);
	// capture the name of this field from the label
	// (this is to be used in the error message)
	if (strBrowser == "ns6") {
		// I am unable to read just the text of this
		// tag using the .innerText property, so
		// instead, we read the .innerHTML property
		// and then strip out the HTML tags using
		// regular expressions.
		//
		// Note: also, for this function we can't use
		// the getObjById(sLabel, 1) call but instead
		// must use getObjById(sLabel, 0).
		//
		var oNSLabel = getObjById(sLabel,0);
		var reClosingTags=/<\/.*>/g;
		var reOpeningTags=/<.*>/;
		var s = oNSLabel.innerHTML;
		s = s.replace(reClosingTags, "");
		s = s.replace(reOpeningTags, "");
		sOldLabel = s;
		//sOldLabel = sOldLabel.replace("*", "");	//14772
		sOldLabel = sOldLabel.replace(/\*+/g,"");	//14772
		
	}
	else if ( (strBrowser == "ie4") || (strBrowser == "ie5") ) {
		sOldLabel = document.all[sLabel].innerText;
		//sOldLabel = sOldLabel.replace("*", "");	//14772
		sOldLabel = sOldLabel.replace(/\*+/g,"");	//14772
	}
	
	if ((sLabel == "label-namedinsuredfirst") || (sLabel == "label-namedinsuredlast")){
			sOldLabel = ("Applicant " + sOldLabel + " Name");
			strLabelName = "Applicant";
		}	
		
	if ((sLabel == "label-namedinsuredfirst2") || (sLabel == "label-namedinsuredlast2")) {
			sOldLabel = "Co-Applicant Name " + sOldLabel;
			strLabelName = "Co-Applicant Name";
		}
	//if ((bCoAppInfoError == "1") && (oControl.name == "num-0-coappSSN3")){		//14285
	if ((bCoAppInfoError == "1") && (oControl.name == "ssn-0-coappSSN")) {			//14285
		sOldLabel = "Co-Applicant Information";
		strLabelName = "Co-Applicant Information";
		}
			
	// if the field is empty
	if (sInstruction == "") {
		if (strBrowser == "ns4") {
				displayErrors += "   *" + strLabelName + ": " + mMissingValPrefix + strLabelName + ".\n";
				}
			else{
				displayErrors += "<li>" + sOldLabel + ": " + mMissingValPrefix + sOldLabel + ".</li>";
				}
			
	}
	// if the field has bad data
	else{
		if (strBrowser == "ns4") {
			if (bCoAppInfoError == "1") {
				displayErrors += "   *" + strLabelName + ": " + "\n";
			}else{	
				displayErrors += "*" + strLabelName + ": " + mValPrefix + sInstruction + "</li>";
				}
		}else{
			if (bCoAppInfoError == "1") {
				displayErrors += "<li>" + sOldLabel + ": " + sInstruction + "</li>";
				}
			else {
				//12479 - Add ability to just use instruction sent in.
				if (intPos == 9999) {
					displayErrors += "<li>" + sOldLabel + ": " + sInstruction + "</li>";
				} else {
					displayErrors += "<li>" + sOldLabel + ": " + mValPrefix + sInstruction + "</li>";
				}
			}
		}
	}
}


/**********************************************************************
/
/   resetValidControl
/
/   Purpose:    Erases the error messages that have been written
/               next to a control.
/
/   Arguments:  oForm - the form object
/				oControl - the form element that is being validated
/				sInstruction - <not used>
/				intPos - <not used>
/
/   Returns:    - none -
/
**********************************************************************/
function resetValidControl(oForm, oControl, sInstruction, intPos) {

	// Get the control parts
	var frmName = oControl.name;
	var strLabelName = getFieldName(frmName)
	var sLabel = "label-" + strLabelName;
	var sMessage = "message-errors";
	var oLabel=getObjById(sLabel,1);

	//Only reset named insured if there has been no error yet on the named insured fields.
	if (((strLabelName == "namedinsured") && (bNamedInsuredError == false)) ||
		(strLabelName != "namedinsured")) {
		// reset label values so the color and
		// fontweight are normal and hide the
		// check mark.
		if ( (strBrowser == "ie4") || (strBrowser == "ie5") || (strBrowser == "ns6") ) {
			oLabel.color = "#000000";
		}
		//changeVisibility(sMessage, hidden);
	}
}


/**********************************************************************
/
/   getFieldName
/
/   Purpose:    Retrieve the field name for a control minus the
/               additional characters that are used to express
/               whether or not a field is required and the data type.
/
/   Arguments:  sString - the control element name from the HTML.
/
/   Returns:    sField - a string containing the field name.
/
**********************************************************************/
function getFieldName(sString) {
	var pos = sString.indexOf("-");
	var pos2 = pos + 3;
	var sField;
	if (pos > 0) {
		sField = sString.substr(pos2);
	}
	else {
		sField = sString;
	}

	pos = sString.indexOf("namedinsured");
		
	if ((pos > 0) && (sString == "namedinsuredfirst")) {
		sField = "namedinsuredfirst";
	}
	if ((pos > 0) && (sString == "namedinsuredmiddle")) {
		sField = "namedinsured";
	} 
	if ((pos > 0) && (sString == "namedinsuredlast")) {
		sField = "namedinsuredlast";
	}
	if ((pos > 0) && (sString == "namedinsuredsuffix")) {
	    sField = "namedinsured";
	} 
	if ((pos > 0) && (sString == "namedinsured2")){
	    	sField = "namedinsured2";
	}
	if ((pos > 0) && (sString == "namedinsuredfirst2")) {
		sField = "namedinsuredfirst2";
	}
	if ((pos > 0) && (sString == "namedinsuredmiddle2")) {
	    sField = "namedinsured";
	}
	if ((pos > 0) && (sString == "namedinsuredlast2")) {
		sField = "namedinsuredlast2";
	}
	if ((pos == 0) && (sString == "namedinsuredsuffix2")) {
	    sField = "namedinsured";
	}
	if ((pos > 0) && (sString == "namedinsuredmiddle2")) {
		sField = "namedinsured";
	}
	if ((pos > 0) && (sString == "namedinsuredsuffix")) {
		sField = "namedinsured";
	}
	if ((pos > 0) && (sString == "namedinsuredsuffix2")) {
		sField = "namedinsured";
	}
	pos = sString.indexOf("appSSN");
	if ((pos > 0) && (sString != "0")) {
		sField = "appSSN";
	}
	pos = sString.indexOf("coappSSN");
	if ((pos > 0) && (sString != "coappSSN")) {
		sField = "coappSSN";
	}
	pos = sString.indexOf("propertyzipcode");
	if ((pos > 0) && (sString != "propertyzipcode")) {
		sField = "propertyzipcode";
	}
	pos = sString.indexOf("mailingzipcode");
	if ((pos > 0) && (sString != "mailingzipcode")) {
		sField = "mailingzipcode";
	}
	pos = sString.indexOf("previouszip");
	if ((pos > 0) && (sString != "previouszip")) {
		sField = "previouszip";
	}
	pos = sString.indexOf("endorsementzip4");
	
	if ((pos > 0) && (sString != "endorsementzip-")) {
		sField = sField.replace("endorsementzip4", "endorsementzip");
	}	
		
	pos = sString.indexOf("exp");
	if (pos > 0)  {
		sField = "expdate";
	}
	
    //20179
	pos = sString.indexOf("priorexpirationdate");
	if ((pos > 0) && (sString != "priorexpirationdate")) {
		sField = "priorexpirationdate";
	}
	
	return sField;
}


/**********************************************************************
/
/   getContentFlag
/
/   Purpose:    Retrieves the 'content flag' from the name of a
/				form element.  The content flag will either be a
/				'0' (indicating that the field does not require
/				content and can be empty) or a '1' (indicating that
/				the field is required).
/
/   Arguments:  sString - the control element name from the HTML.
/
/   Returns:    sContentFlag - a string containing a '0' or a '1'.
/
**********************************************************************/
function getContentFlag(sString) {	
	var pos = sString.indexOf("-");
	var pos2 = pos + 1;
	var sContentFlag = sString.substr(pos2, 1);
	
	return sContentFlag
}


/**********************************************************************
/
/   
/
/   Purpose:    
/
/   Arguments:  
/
/   Returns:    boolean
/
**********************************************************************/
function validateForm(oForm) {	
	var iCount = 0;
	var isValidForm = true;
	var isInter = false;
	var cansell = false;
	var isecondtime = 0;

	// initialize the variable to hold the error messages
	if (strBrowser == "ns4") {
		displayErrors = "The following errors appeared on this application, please\ncorrect them and resubmit this form.\n\n";
	}else{
		displayErrors = "<BR>The following errors appeared on this application, please correct them and resubmit this form.<br><br><ul style='margin-left=16'>";
	}

	bNamedInsuredError = false;
	//7714
	bPrevError = "0";			
				
	// loop through controls on form
	for (var i = 0; i < oForm.elements.length; i++)
	{			
		// isolate current control
		var oControl = oForm.elements[i];
		var oName = oControl.name;
		var sField = getFieldName(oControl.name);
		if (sField == "isinternational") {
			if (oControl.checked == true) {
				isInter = true;
			}
		}
		if (sField == "licensetosell") {
			if (oControl.checked == true) {
				cansell = true;
			}
		}
		if (
			( (oControl.type.toUpperCase() == "BUTTON") ||
			  (oControl.type.toUpperCase() == "PASSWORD") ||
			  (oControl.type.toUpperCase() == "RADIO") ||
			  (oControl.type.toUpperCase() == "SELECT") ||
			  (oControl.type.toUpperCase() == "SELECT-ONE") ||
			  (oControl.type.toUpperCase() == "TEXT") ||
			  (oControl.type.toUpperCase() == "TEXTAREA") ) 
			  &&
			( (oControl.type.toUpperCase() != "SUBMIT")&&
			  (oControl.type.toUpperCase() != "HIDDEN")&&
			  (oControl.type.toUpperCase() != "RESET")&&
			  (oControl.type.toUpperCase() != "CHECKBOX") )
			)
		{	// Call main validation routines
		  if (sField=="coveragedeclined" && oControl.disabled == true) {
		    //8142 - Skip editing of coveragedeclined field when its disabled.
		  } else {
			// If mailing state or mailing zip, don't edit if is international is checked.
			if (((sField != "mailingstate") && (sField != "mailingzipcode") && (sField != "licensetosell")) ||
				((isInter == false) && ((sField == "mailingstate") || (sField == "mailingzipcode"))) ||
				((sField == "licensetosell") && (isecondtime == 2) && (cansell != true))) {
				if(validateContent(oForm, oControl, i)) {
					if(!validateData(oForm, oControl, i)) {
						isValidForm=false;
					}
				}else{
					isValidForm=false;
				}
			}else{
				resetValidControl(oForm, oControl, "", 0);
			}
		  }
		}
	}
	
	return isValidForm;
}

//function validateFormWIP(name1, name2, dob, ssn1, ssn2, ssn3, street, city, state, zip, zip2, producttype, isSave) {		//14285
function validateFormWIP(name1, name2, street, city, state, zip, zip2, producttype, isSave) {					//14285
	var iCount = 0;
	var isValidForm = true;
	
	//7714
	bPrevError = "0";

	// initialize the variable to hold the error messages
	if (strBrowser == "ns4") {
		displayErrors = "The following errors appeared on this application, please\ncorrect them and resubmit this form.\n\n";
	}else{
		displayErrors = "<BR>The following errors appeared on this application, please correct them and resubmit this form.<br><br><ul style='margin-left=16'>";
	}
	bNamedInsuredError = false;	
	// loop through controls on form
	for (var i = 0; i < 12; i++)
	{			
		// isolate current control
		var oControl;
		var oName;
			
		if (i == 0) 
		if (name1.name == "text-1-namedinsuredfirst") {
		     oControl = name1;
		     oName = oControl.name;
		     }
		if (i == 1)     
		if (name2.name == "text-1-namedinsuredlast") {
		     oControl = name2;
		     oName = oControl.name;
		     }
//14285		if (i == 2)     
//14285		if (dob.name == "date-1-appDOB") {
//14285		     oControl = dob;
//14285		     oName = oControl.name;
//14285		     }
//14285		if (i == 3)     
//14285		if (ssn1.name == "num-1-appSSN1") {
//14285		     oControl = ssn1;
//14285		     oName = oControl.name;
//14285		     }
//14285		if (i == 4)     
//14285		if (ssn2.name == "num-1-appSSN2") {
//14285		     oControl = ssn2;
//14285		     oName = oControl.name;
//14285		     }
//14285		if (i == 5)     
//14285		if (ssn3.name == "num-1-appSSN3") {
//14285		     oControl = ssn3;
//14285		     oName = oControl.name;
//14285		     }
		if (i == 6) 
		if (street.name == "text-1-propertystreet") {
		     oControl = street;
		     oName = oControl.name;
		     }
		if (i == 7)     
		if (city.name == "text-1-propertycity") {
			 oControl = city;
		     oName = oControl.name;
		     }
		if (i == 8)     
		if (state.name == "select-1-propertystate") {
			 oControl = state;
		     oName = oControl.name;
		     }
		if (i == 9)     
		if (zip.name == "zip-1-propertyzipcode") {
		     oControl = zip;
		     oName = oControl.name;
		     }	
		if (i == 10)
		if (zip2.name == "zip4-0-propertyzipcode2") {
		     oControl = zip2;
		     oName = oControl.name;
		     }		       
		if ((i == 11) && (!isSave))
		if (producttype.name == "select-1-producttype") {
			oControl = producttype;
			oName = oControl.name;
			}
		 
			
		{	// Call main validation routines
		if ((i < 2) || (i > 5)) {
			if(validateContent(frmQuote, oControl, i)) {
				if(!validateData(frmQuote, oControl, i)) {
					isValidForm=false;
					}
			}else{
				isValidForm=false;
			}
		 }	
		}
	}		
  return isValidForm;
}
						