var shortTermOffered = false;

function ageChanged(theField) {
 
    if ((trim(theField.value) != "") && (isNumber(theField.value)) ) {
        if (isShortTermAgeRange(theField.value)) {      
            
            if (confirm("If you are between the ages of 18 and 24, short term insurance, underwritten by The Chesapeake Life Insurance Company, is ideal for your age group. Please click OK to receive an instant online quote.")) {
                document.location.href = "/short.asp";
            }           
            shortTermOffered = true;
        }    
    }        
}

function resetShortTermOffered() {
    shortTermOffered = false;
}

function isShortTermAgeRange(value) {
    //if (value >= 18 && value <= 24) {      
    //    return true;
    //}
    //else {
        return false;
    //}
}


function showStateMessage(theStateField) {
    if (theStateField.value == "WA") {
        alert("Please note: Life, Dental, Vision, Prescription Drug, Critical Illness, Disability and Hospital Indemnity plans are NOT available in the state of " + theStateField.text + ". Health Insurance plans are only available to self employed individuals and small business owners. To return to the form and submit a request for a health insurance quote, click OK.");
    }    
}


function validateRequestPhoneQuote(theForm) {

    //alert(shortTermOffered)
    //alert(isShortTermAgeRange(theForm.age.value))
    
    // Don't run validation if we are in the middle of the age check
    if (!shortTermOffered && isShortTermAgeRange(theForm.age.value)) {
        return false;
    }

    var errorString = "";
    var ageLimitString = "";
    var numEmployeesString = "";
    var validStateString = "";
    var insuranceCompanyString = "";
        
    errorString += checkRequired(theForm.first_name,"First Name");
    errorString += checkName(theForm.first_name,"First Name");
    errorString += checkRequired(theForm.last_name,"Last Name");
    errorString += checkName(theForm.last_name,"Last Name");    
    errorString += checkRequired(theForm.primary_phone_1,"Primary Phone");
    errorString += checkEmail(theForm.email,"Email")
    errorString += checkPhone(theForm.primary_phone_1,theForm.primary_phone_2,theForm.primary_phone_3,"Primary Phone");        
    errorString += checkPhone(theForm.mobile_phone_1,theForm.mobile_phone_2,theForm.mobile_phone_3,"Cell Phone");            

    if (theForm.secondary_phone_1 != null) {
        errorString += checkPhone(theForm.secondary_phone_1,theForm.secondary_phone_2,theForm.secondary_phone_3,"Secondary Phone");
    }        

    errorString += checkRequired(theForm.address_line_1,"Address");    
    errorString += checkRequired(theForm.city,"City");
    errorString += checkCity(theForm.city,"City");    
    errorString += checkSelected(theForm.state_id,"State");
    errorString += checkZip(theForm.zip_code,"ZIP Code");
    errorString += checkRequired(theForm.age,"Age");

    //errorString += checkRadioRequired(theForm.responsible,"Payment Responsibility");
    
    if (theForm.num_employees != null) { errorString += checkNumber(theForm.num_employees,"Number of Employees"); }           
    if (theForm.numDependents != null) { errorString += checkNumber(theForm.numDependents,"Number of Dependents"); }
    if (theForm.dependentAge1 != null) { errorString += checkNumber(theForm.dependentAge1,"Age of Depenedent 1"); }
    if (theForm.dependentAge2 != null) { errorString += checkNumber(theForm.dependentAge2,"Age of Depenedent 2"); }
    if (theForm.dependentAge3 != null) { errorString += checkNumber(theForm.dependentAge3,"Age of Depenedent 3"); }
    
    ageLimitString += checkAgeLimits(theForm.age,18,63);
               
    validStateString += checkState(theForm.state_id.options[theForm.state_id.selectedIndex],"HI,ND,SD,MN,NY,VT,NJ")
    
    if (theForm.num_employees != null) {    
        numEmployeesString += checkNumEmployeesLimits(theForm.num_employees,theForm.state_id.options[theForm.state_id.selectedIndex]);
    }
        
    insuranceCompanyString += checkInsuranceCompany(theForm.insurance_company.options[theForm.insurance_company.selectedIndex],"4,30");

    if (validStateString.length == 0) {
        if (ageLimitString.length == 0) {
            if (numEmployeesString.length == 0) {
                if (insuranceCompanyString.length == 0) {
                    if (errorString.length == 0) {
                        return true;
                    } else {
                        alert(errorString);
                        return false;
                    }            
                } else {
                    alert(insuranceCompanyString);
                    return false;
                }
            } else {
                alert(numEmployeesString);
                return false;
            }            
        } else {
            alert(ageLimitString);
            return false;    
        }
    } else {
        alert(validStateString)
        return false;
    }

}
function validateRequestQuote(theForm) {

    // Don't run validation if we are in the middle of the age check
    if (!shortTermOffered && isShortTermAgeRange(theForm.age.value)) {
        return;
    }

    var errorString = "";
    var ageLimitString = "";
    var numEmployeesString = "";
    var validStateString = "";
    var insuranceCompanyString = "";
            
    errorString += checkRequired(theForm.first_name,"First Name");
    errorString += checkName(theForm.first_name,"First Name");
    errorString += checkRequired(theForm.last_name,"Last Name");
    errorString += checkName(theForm.last_name,"Last Name");    
    errorString += checkEmail(theForm.email,"Email")

    if (theForm.email2 != null) {
        errorString += checkRequired(theForm.email,"Email");
        errorString += checkMatching(theForm.email, theForm.email2, "Email Addresses")
    }
    else {
        errorString += checkEitherOr(theForm.primary_phone_1, theForm.email, "Primary Phone or Email");    
    }

    errorString += checkPhone(theForm.primary_phone_1,theForm.primary_phone_2,theForm.primary_phone_3,"Primary Phone");        
    errorString += checkPhone(theForm.mobile_phone_1,theForm.mobile_phone_2,theForm.mobile_phone_3,"Cell Phone");            

    if (theForm.secondary_phone_1 != null) {
        errorString += checkPhone(theForm.secondary_phone_1,theForm.secondary_phone_2,theForm.secondary_phone_3,"Secondary Phone");
    }        

    errorString += checkRequired(theForm.address_line_1,"Address");    
    errorString += checkRequired(theForm.city,"City");
    errorString += checkCity(theForm.city,"City");    
    errorString += checkSelected(theForm.state_id,"State");
    errorString += checkZip(theForm.zip_code,"ZIP Code");
    errorString += checkRequired(theForm.age,"Age");

    //errorString += checkRadioRequired(theForm.responsible,"Payment Responsibility");
   
    if (theForm.num_employees != null) { errorString += checkNumber(theForm.num_employees,"Number of Employees"); }           
    if (theForm.numDependents != null) { errorString += checkNumber(theForm.numDependents,"Number of Dependents"); }
    if (theForm.dependentAge1 != null) { errorString += checkNumber(theForm.dependentAge1,"Age of Depenedent 1"); }
    if (theForm.dependentAge2 != null) { errorString += checkNumber(theForm.dependentAge2,"Age of Depenedent 2"); }
    if (theForm.dependentAge3 != null) { errorString += checkNumber(theForm.dependentAge3,"Age of Depenedent 3"); }
    
    ageLimitString += checkAgeLimits(theForm.age,18,63);
            
    validStateString += checkState(theForm.state_id.options[theForm.state_id.selectedIndex],"HI,ND,SD,MN,NY,VT,NJ")

    if (theForm.num_employees != null) {    
        numEmployeesString += checkNumEmployeesLimits(theForm.num_employees,theForm.state_id.options[theForm.state_id.selectedIndex]);
    }
        
    insuranceCompanyString += checkInsuranceCompany(theForm.insurance_company.options[theForm.insurance_company.selectedIndex],"4,30");

    if (validStateString.length == 0) {
        if (ageLimitString.length == 0) {
            if (numEmployeesString.length == 0) {
                if (insuranceCompanyString.length == 0) {
                    if (errorString.length == 0) {
                        return true;
                    } else {
                        alert(errorString);
                        return false;
                    }            
                } else {
                    alert(insuranceCompanyString);
                    return false;
                }
            } else {
                alert(numEmployeesString);
                return false;
            }            
        } else {
            alert(ageLimitString);
            return false;    
        }
    } else {
        alert(validStateString)
        return false;
    }

}
function validate411(theForm) {

    // Don't run validation if we are in the middle of the age check
    if (!shortTermOffered && isShortTermAgeRange(theForm.age.value)) {
        return;
    }
    
    var errorString = "";
    var ageLimitString = "";
    var validStateString = "";
    
    errorString += checkRequired(theForm.first_name,"First Name");
    errorString += checkName(theForm.first_name,"First Name");
    errorString += checkRequired(theForm.last_name,"Last Name");
    errorString += checkName(theForm.last_name,"Last Name");    
	errorString += checkRequired(theForm.last_name,"Last Name");
    errorString += checkName(theForm.last_name,"Last Name");
    errorString += checkRequired(theForm.primary_phone_1,"Primary Phone");
    errorString += checkPhone(theForm.primary_phone_1,theForm.primary_phone_2,theForm.primary_phone_3,"Primary Phone");        
    errorString += checkPhone(theForm.mobile_phone_1,theForm.mobile_phone_2,theForm.mobile_phone_3,"Cell Phone");                
    errorString += checkRequired(theForm.address_line_1,"Address");    
    errorString += checkRequired(theForm.city,"City");
    errorString += checkCity(theForm.city,"City");    
    errorString += checkSelected(theForm.state_id,"State");
    errorString += checkZip(theForm.zip_code,"ZIP Code");
    errorString += checkRequiredNumber(theForm.age,"Age");    
    errorString += checkEmail(theForm.email,"Email")
    ageLimitString += checkAgeLimits(theForm.age,18,63);    
    validStateString += checkState(theForm.state_id.options[theForm.state_id.selectedIndex],"ND,SD,MN,NY,VT,NJ")
        
    if (validStateString.length == 0) {    
        if (ageLimitString.length == 0) {
            if (errorString.length == 0) {
                return true;
            } else {
                alert(errorString);
                return false;
            }
        } else {
            alert(ageLimitString);
            return false;    
        }     
    } else {
        alert(validStateString)
        return false;
    }     
}
function checkRequired(theField, fieldName) {
        
    if (trim(theField.value) == "") {
        return "Please enter your " + fieldName + "\n";    
    } else {
        return "";
    }
}
/* Validates theField as being equal to theOtherField. */
function checkMatching(theField, theOtherField, fieldNames) {
    if (theField.value != theOtherField.value) {
        return "The entries for " + fieldNames + " do not match\n";    
    } else {
        return "";
    }
}
/* Validates that either theField or theOtherField is non-empty */
function checkEitherOr(theField, theOtherField, message) {
    if (trim(theField.value) == "" && trim(theOtherField.value) == "") {
        return "Please enter your " + message + "\n";    
    } else {
        return "";
    }
}
function checkState(theField, invalidStatesList) {
    var statesArray = invalidStatesList.split(",")
    
    for (i=0;i<statesArray.length;i++) {
        if (theField.value == statesArray[i]) {
            return "Sorry, we do not currently offer benefits to owners in " + theField.text;
        }    
    }
    return "";   
}
function checkNumEmployeesLimits(theField, theStateField) {
    var lowerLimit
    var upperLimit
    
    theState = theStateField.value
    
    if (theState == "TX" || theState == "GA") {
        lowerLimit = 0
        upperLimit = 49
    } else if (theState == "MI") {
        lowerLimit = 0
        upperLimit = 14    
    }  else {
        lowerLimit = 0
        upperLimit = 9   
    }

    if ((trim(theField.value) != "") && (isNumber(theField.value)) ) {
        if (theField.value < lowerLimit) {
            return "Sorry, we do not offer benefits to owners in " + theStateField.text + " with fewer than " + lowerLimit + " employees";
        } else {
            if (theField.value > upperLimit) {
                return "Sorry, we do not offer benefits to owners in " + theStateField.text + " with more than " + upperLimit + " employees";
            } else {
                return "";        
            }
        }
    } else {
        return "";   
    }
}

function checkInsuranceCompany(theField, invalidCompanies) {
    
    var companiesArray = invalidCompanies.split(",")
    
    for (i=0;i<companiesArray.length;i++) {
        if (theField.value == companiesArray[i]) {
            return "This offer is for those who are not currently MEGA or MIDWEST customers. Thank You.";
        }    
    }
    return "";   
}

function checkAgeLimits(theField, lowerLimit, upperLimit) {
    if ((trim(theField.value) != "") && (isNumber(theField.value)) ) {
        if (theField.value < lowerLimit) {
            return "Sorry, we do not offer benefits to owners under the age of " + lowerLimit;
        } else {
            if (theField.value > upperLimit) {
                return "Sorry, we do not offer benefits to owners over the age of " + upperLimit;
            } else {
                return "";        
            }
        }
    } else {
        return "Please enter a valid age";   
    }
}

function checkAgeLimitsRedirect(theField, lowerLimit, upperLimit) {
    if ((trim(theField.value) != "") && (isNumber(theField.value)) ) {
        if (theField.value < lowerLimit) {
            return "1";
        } else {
            if (theField.value > upperLimit) {
                return "2";
            } else {
                return "0";        
            }
        }
    } else {
        return "3";   
    }
}

function checkRequiredNumber(theField, fieldName) {
    if ( (trim(theField.value) == "") || (!isNumber(theField.value)) ) {
        return "Please enter a number for " + fieldName + "\n";    
    } else {
        return "";
    }
}
function checkNumber(theField, fieldName) {
    if (!isNumber(theField.value)) {
        return fieldName + " must be a number\n";    
    } else {
        return "";
    }
}
function checkSelected(theField, fieldName) {
    if (theField.options[theField.selectedIndex].value == "") {
        return "Please select your " + fieldName + "\n";    
    } else {
        return "";
    }
}
function checkRadioRequired(theField, fieldName) {
    for (i = 0; i < theField.length;i++) {
        if (theField[i].checked) {
            return "";
        }
    }
    return "Please make a selection for " + fieldName + "\n";
}
function checkZip (theField, fieldName) {
    if ( (trim(theField.value).length != 5) || (!isNumber(theField.value)) ) {
        return "Your " + fieldName + " must be 5 numbers\n";    
    } else {
        return "";
    }
}
function checkPhone(field1, field2, field3, fieldName) {
    var fullPhone = trim(field1.value) + trim(field2.value) + trim(field3.value);
    
    if (fullPhone != "") {
        // Check total phone length
        if ( (fullPhone.length != 10) || (!isNumber(fullPhone)) ) {
            return fieldName + " must be a valid phone number\n";
        } else {
            // Check area code and prefix
            if ((field1.value == "555") || 
                (field1.value == "800") || 
                (field1.value == "888") || 
                (field1.value == "900") || 
                (field2.value == "555") || 
                (field1.value.substring(0,1) == '1') || 
                (field1.value.substring(0,1) == '0')) {
                
                return fieldName + " must be a valid phone number\n";
                
            } else {
                // Check that prefix and suffix aren't just the same number over and over.
                if ((fullPhone.substring(3,4) == fullPhone.substring(3,4)) &&
                    (fullPhone.substring(3,4) == fullPhone.substring(4,5)) &&
                    (fullPhone.substring(3,4) == fullPhone.substring(5,6)) &&
                    (fullPhone.substring(3,4) == fullPhone.substring(6,7)) &&
                    (fullPhone.substring(3,4) == fullPhone.substring(7,8)) &&
                    (fullPhone.substring(3,4) == fullPhone.substring(8,9)) &&                    
                    (fullPhone.substring(3,4) == fullPhone.substring(9,10))) {

                    return fieldName + " must be a valid phone number\n";            

                } else {                                    
                    return "";
                }
            }
        }
    } else {
        return "";
    }
}
function checkEmail(theField,fieldName) {
    var isEmail = false;
  
    // allow blanks
    if (trim(theField.value) == "") {
        return "";
    }
    
    // are regular expressions supported?
    var supported = 0;
    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
  
    if (!supported) {
        isEmail = (theField.value.indexOf(".") > 2) && (theField.value.indexOf("@") > 0);
    } else {
        var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
        var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  
        isEmail = (!r1.test(theField.value) && r2.test(theField.value));
    }
      
    if (!isEmail) {
        return fieldName + " must be a valid email address\n";
    } else {
        return "";
    }
}
function checkName (theField, fieldName) {
    // The string of valid characters for names
	var valid = "abcdefghijklmnopqrstuvwxyz'- ";
    var char1, char2, char3;

    // Check that name has enough characters
    if (trim(theField.value).length == 1) {
    	return fieldName + " must be a valid name\n";    
    }
    
    // Check for invalid characters
	for (var i=0; i<theField.value.length;i++) {
		if (valid.indexOf(theField.value.substring(i,i+1).toLowerCase()) == "-1") {
			return fieldName + " must be a valid name\n";
		}
	}

    // Check for repeated character sequences
	for (var i=0; i < theField.value.length - 2;i++) {
        char1 = theField.value.substring(i,i+1)
        char2 = theField.value.substring(i+1,i+2)
        char3 = theField.value.substring(i+2,i+3)                
        
        if ((char1 == char2) && (char2 == char3)) {
            return fieldName + " must be a valid name\n";
        }
    }
        
    // Otherwise, we are clean
    return "";
    
}
function checkCity (theField, fieldName) {
    // The string of valid characters for cities
	var valid = "abcdefghijklmnopqrstuvwxyz'- ";
    var char1, char2, char3;

    // Check that name has enough characters
    if (trim(theField.value).length == 1) {
    	return fieldName + " must be a valid city\n";    
    }
    // Check for invalid characters
	for (var i=0; i<theField.value.length;i++) {
		if (valid.indexOf(theField.value.substring(i,i+1).toLowerCase()) == "-1") {
			return fieldName + " must be a valid city\n";
		}
	}
        
    // Otherwise, we are clean
    return "";
    
}
function trim(s) {
    while (s.substring(0,1) == ' ') {
        s = s.substring(1,s.length);
    }
    while (s.substring(s.length-1,s.length) == ' ') {
        s = s.substring(0,s.length-1);
    }
    return s;
}
function isNumber(input) {
    var numbers="0123456789";
    var CharacterCheck;
    var counter = 0; 
    
    for (var i=0; i < input.length; i++) {
        CharacterCheck = input.substring(i, i+1); 
        if (numbers.indexOf(CharacterCheck) != -1)
            counter ++;
    } 

    if (counter != input.length) {
        return false;
    } else
        return true;
}
