/*********************************************************************
//	
//   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: 
//       6350  Mike Lugenbeel  02/06/07  Add IsValidUserID function.
//
**********************************************************************/


// 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 reUserID = /^[a-zA-Z0-9\x20]{6,32}$/;

// Other holding variables
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeter = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var sTestValue;
var sInstruction;

// 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 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 iZIPCode = " a 5 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 name@domain.extension; e.g. info@pacificselectproperty.com.";
var iUserID = " characters A-Z, a-z, space and/or 0-9.";
var iDatMessage;
var iDay = " a day number between 1 and 31.";
var iMonth = " a month number between 1 and 12.";

var iYear = " a 4 digit year number.";
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 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;

// 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)
    }
}


/**********************************************************************
/
/   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)
	}
}

/**********************************************************************
/
/   isValidUserID - 6350 MikeL 02/05/2007
/
/   Purpose:    checks to see if string represents a valid User ID
/
/   Arguments:  s - the string to check
/
/   Returns:    boolean
/
**********************************************************************/
function isValidUserID (s)
{   if (isEmpty(s)) 
       	if (isValidUserID.arguments.length == 1) return defaultEmptyOK;
       	else return (isValidUserID.arguments[1] == true); 
    else 
	{	
		return reUserID.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) ||
           (s.length == digitsInZIPCode2)));
}


/**********************************************************************
/
/   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)
	}
}


/**********************************************************************
/
/   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.
    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)
	{	//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 sFieldName; //6350 Mike Lugenbeel
	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 = "";

	//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);
	}

	// 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 (isIntegerInRange (vValue, vRangeVal1, vRangeVal2))
			{	
				isValid = true;	
			}else{	
				sInstruction = iInteger + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";   
			}
			break; 							
		case "zip": 
			if (isZIPCode (vValue))
			{
				isValid = true;  
			}else{
				sInstruction = iZIPCode;  
			}
			break; 							
		case "phone": 
			if (!isEmpty(vValue))
			{	if (checkUSPhone (vValue, false))
				{	isValid = true;	}
				else
				{	sInstruction = iUSPhone;	}
			}
			else
			{	isValid = true;  }
			break; 							
		case "year":  
			if (isYear (vValue))
			{	isValid = true;	}
			else
			{	sInstruction = iYear;	}
			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)) 
			{	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 = iFloat + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			break; 
		case "decrng":
			if (isFloatInRange (vValue, vRangeVal1, vRangeVal2))
			{	isValid = true;	}
			else
			{	sInstruction = iFloat + " between " + vRangeVal1 + " and " + vRangeVal2 + ".";	}
			if (isValid)
			{ 
				var i = vValue.length - vValue.indexOf(".");
				if (i <= 2)
					{	isValid = true;	}
				else
				{	isValid = false;
					sInstruction = iFloat + " to one decimal place (example: 10.3).";	}
			}
			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;					
		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":
			isValid = true;
			break;
		case "textrng":
			if (vValue.length >= vRangeVal1 && vValue.length <= vRangeVal2)
			{	isValid = true;   }
			else
			{    sInstruction = " " + vRangeVal1.toString() + " to " + vRangeVal2.toString() + " characters in length.";   }
// 6350 MikeL - Validate UserID. Should not contain special characters.
			if (isValid) 
			{
				sFieldName = getFieldName(sString);
				if (sFieldName == "userid") 
				{
					isValid = false;
					if (isValidUserID(vValue)) 
					{
						isValid = true;
					}
					else 
					{
						sInstruction = iUserID; 
					}
				}
			}						
			break;
		case "select":
			isValid = true;
			break;
		default:
			isValid = true;
			break; 							
	}
	//perform label and message painting
	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
/				sInstruction - the error message to paint
/				intPos - <not used>
/
/   Returns:    - none -
/
**********************************************************************/
function deliverErrorMessage(oForm, oControl, sInstruction, intPos) {

	var frmName = oControl.name;
	var strLabelName = getFieldName(frmName)
	var sLabel = "label-" + strLabelName;
	var sMessage = "message-" + strLabelName;
	var sOldLabel;
	var oLabel=getObjById(sLabel,1);

	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("*", "");
	}
	else if ( (strBrowser == "ie4") || (strBrowser == "ie5") ) {
		sOldLabel = document.all[sLabel].innerText;
		sOldLabel = sOldLabel.replace("*", "");
	}
		
	// if the field is empty
	if (sInstruction == "") {
		if (strBrowser == "ns4") {
			displayErrors += "   *" + strLabelName + ": " + mMissingValPrefix + strLabelName + ".\n";
		}else{
			displayErrors += "   *" + sOldLabel + ": " + mMissingValPrefix + sOldLabel + ".<br>";
		}
	}
	// if the field has bad data
	else{
		if (strBrowser == "ns4") {
			displayErrors += "   *" + strLabelName + ": " + mValPrefix + sInstruction + "\n";
		}else{
			displayErrors += "   *" + sOldLabel + ": " + mValPrefix + sInstruction + "<br>";
		}
	}
}


/**********************************************************************
/
/   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-" + strLabelName;
	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 != "namedinsured2")) {
		sField = "namedinsured";
	}
	pos = sString.indexOf("exp");
	if (pos > 0)  {
		sField = "expdate";
	}
	
	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
}


/**********************************************************************
/
/   validate
/
/   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 page, please\ncorrect them and resubmit this form.\n\n";
	}else{
		displayErrors = "The following errors appeared on this page, please<br>correct them and resubmit this form.<br><br>";
	}

	bNamedInsuredError = false;
				
	// 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 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, phone1, phone2, street, city, state, zip) {	
	var iCount = 0;
	var isValidForm = true;
	
			
	// initialize the variable to hold the error messages
	if (strBrowser == "ns4") {
		displayErrors = "The following errors appeared on this page, please\ncorrect them and resubmit this form.\n\n";
	}else{
		displayErrors = "The following errors appeared on this page, please<br>correct them and resubmit this form.<br><br>";
	}

	bNamedInsuredError = false;
				
	// loop through controls on form
	for (var i = 0; i < 8; 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;
		     }
		if (i == 2)     
		if (phone1.name == "phone-1-primaryphone") {
			 oControl = phone1;
		     oName = oControl.name;
		     }
		if (i == 3)     
		if (phone2.name == "phone-0-secondaryphone") {
			 oControl = phone2;
		     oName = oControl.name;
		     }
		if (i == 4)     
		if (street.name == "text-1-propertystreet") {
		     oControl = street;
		     oName = oControl.name;
		     }
		if (i == 5)     
		if (city.name == "text-1-propertycity") {
			 oControl = city;
		     oName = oControl.name;
		     }
		if (i == 6)     
		if (state.name == "select-1-propertystate") {
			 oControl = state;
		     oName = oControl.name;
		     }
		if (i == 7)     
		if (zip.name == "zip-1-propertyzipcode") {
		     oControl = zip;
		     oName = oControl.name;
		     }	   
			
		{	// Call main validation routines
			if(validateContent(frmQuote, oControl, i)) {
				if(!validateData(frmQuote, oControl, i)) {
					isValidForm=false;
					}
			}else{
				isValidForm=false;
			}
			
		}
	}		
  return isValidForm;
}
						