// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Version: 1.0
// Author:  Mark Pilger
// Written: 04/16/2002
// Changed: 
// -----------------------------------------------------------------------------

// Declare variables
//	document.write('In validation.js');
	
// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: isPosInt
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: String
// Returns: Boolean
// Changed: 
// Usage:  Used to determine if a string is a positive integer.
// -----------------------------------------------------------------------------
function isPosInt(checkStr) 
{
	var checkOK = "0123456789";
//	alert("In isPosInt. \nNumber to check = " + checkStr);
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	if (checkStr == "") 
	{
		return (false);
	}

	for (posCounter1 = 0;  posCounter1 < checkStr.length;  posCounter1++)
	{
		ch = checkStr.charAt(posCounter1);
		for (posCounter2 = 0;  posCounter2 < checkOK.length;  posCounter2++)
			if (ch == checkOK.charAt(posCounter2))
				break;
			if (posCounter2 == checkOK.length)
			{
				allValid = false;
				break;
			}
			allNum += ch;
	}
	if (!allValid)
		{
			return (false);
		}
	return (true);
}

function $(id){
	return document.getElementById(id); 
}


// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: isOlderThan65
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: String
// Returns: Boolean
// Changed: 
// Usage:  Used to determine if a string is a positive integer.
// -----------------------------------------------------------------------------
function isOlderThan65(mth, dy, yr, fld) 
{
	var dateToday = new Date()
	var appBirthDate = new Date()
	appBirthDate.setMonth(mth-1)
	appBirthDate.setDate(dy)
	appBirthDate.setFullYear(yr)
	// 1000 milliseconds * 60 seconds * 60 minutes * 24 hours * 365.25 days
	var oneYear = (1000 * 60 * 60 * 24 * 365.25)
	var dateMinus65Years = new Date()
//	theYear = dateMinus65Years.getFullYear()
	dateMinus65Years.setFullYear(dateMinus65Years.getFullYear() - 65)
//	alert("dateMinus65Years = " + dateMinus65Years)
//	alert("appBirthDate = " + appBirthDate)
	var dateMinus65YearsMM = dateMinus65Years.getMonth()
	var dateMinus65YearsDD = dateMinus65Years.getDate()
	var dateMinus65YearsYY = dateMinus65Years.getFullYear()
	var bdayMM = appBirthDate.getMonth()
	var bdayDD = appBirthDate.getDate()
	var bdayYY = appBirthDate.getFullYear()
//	alert("dateMinus65Years.getTime() = " + dateMinus65Years.getTime())
//	alert("appBirthDate.getTime() = " + appBirthDate.getTime())
	var age = (appBirthDate.getTime() - dateMinus65Years.getTime())
//	alert("age = " + age)
	//  If age is negative, the applicant is older than 65 years old.
	if (age < 0)
	{
		return (true)
	}
	return (false);
}	

// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: isYoungerThan0
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: String
// Returns: Boolean
// Changed: 
// Usage:  Used to determine if a string is a positive integer.
// -----------------------------------------------------------------------------
function isYoungerThan0(mth, dy, yr, fld) 
{
	var dateToday = new Date()
	var appBirthDate = new Date()
	appBirthDate.setMonth(mth-1)
	appBirthDate.setDate(dy)
	appBirthDate.setFullYear(yr)
	if (appBirthDate > dateToday)
	{
		return (true)
	}
	return (false);
}	

// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: isValidDate
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: month, day, year, field name
// Returns: Boolean
// Changed: 
// Usage:  Used to if a month, day, and year evaluate to a valid year.  The field
//         name is used in error messages.
// -----------------------------------------------------------------------------
function isValidDate(mth, dy, yr, fld) 
{
//	alert("In isValidDate")
	if (yr.length !== 4)
	{
		alert("Please enter 4 digits for the year for the " + fld)
//		eval("ApplicantForm.usr" + fld + "YY.focus()");
//		eval("ApplicantForm.usr" + fld + "YY.select()");
		return false;
	} 
	else if (mth < 1 || mth > 12) 
	{
		alert("You must enter a valid month value between 1 - Jan. and 12 - Dec for the " + fld + ".  \nPlease try again.")
//		eval("ApplicantForm.usr" + fld + "MM.focus()");
///		eval("ApplicantForm.usr" + fld + "MM.select()");
		return false;
	} 
	else if (dy < 1 || dy > numDaysIn(mth, yr)) 
	{
		alert("You must enter a valid day for the month and year for the " + fld + ". \nPlease try again.")
//		eval("ApplicantForm.usr" + fld + "DD.focus()");
//		eval("ApplicantForm.usr" + fld + "DD.select()");
		return false;
	} 
	else return true;
}

function numDaysIn(mth,yr) 
{
	if (mth==4 || mth==6 || mth==9 || mth==11) return 30;
	else if ((mth==2) && leapYear(yr)) return 29;
	else if (mth==2) return 28;
	else return 31;
}

function leapYear(yr) 
{
	if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: ZipCodeValidator
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: Form
// Returns: Boolean
// Changed: 
// Usage:  Used to validate form data prior to submitting to a form handling routine.
// -----------------------------------------------------------------------------
function ZipCodeValidator(theForm)
{
//	alert("In ZipCodeValidator. \nValue to check = " + theForm.usrZipCode.value);
	var val = parseInt(theForm.usrZipCode.value)
	if (isNaN(val) || val < 54999 || val > 57000) {
		alert("Please enter a valid Minnesota Zip Code in the \"Zip Code\" field.")
		theForm.usrZipCode.focus();
		theForm.usrZipCode.select();
		return (false);
	}
	return (true);
}


function isEmptyField(theForm, fld, fldName){
	var elem = eval("theForm." + fld );
	if (elem.value  == "")
	{
		alert("Please enter " + fldName );
		elem.focus();
		return true;
	}
	return false;
}
function isValidEmail(theForm, fld, fldName){
	var elem = eval("theForm." + fld );
	var re = /^ *([a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+@(([a-zA-Z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-zA-Z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) *$/;
	if (!re.test(elem.value)) {
		alert("Please enter a valid " + fldName );
		elem.focus();
		return true;	
	}
	return false;
}

function fullApplicantValidator(theForm){
	if (isEmptyField(theForm, "firstName", "First Name")) return false;
	if (isEmptyField(theForm, "lastName", "Last Name")) return false;
	if (isEmptyField(theForm, "phone", "Day Time Phone")) return false;
	if (isEmptyField(theForm, "email", "Email")) return false;
	if (isValidEmail(theForm, "email", "Email")) return false;
	if (isEmptyField(theForm, "state", "State")) return false;
	return applicantValidator(theForm);
}

// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: applicantValidator
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: Form
// Returns: Boolean
// Changed: 
// Usage:  Used to validate form data prior to submitting to a form handling routine.
// -----------------------------------------------------------------------------
function applicantValidator(theForm) 
{
//	alert("In applicantValidator");


	if (!ZipCodeValidator(theForm))
		return false;
	
	var checkStr
	var mmStr
	var ddStr
	var yyStr
	var fldName
	
	// Evaluate the Applicant and Coverage dates to insure that they are valued.

		checkStr = "theForm.usrApplicant";
		fldName = "Applicant";
	
	
//		alert("checkStr = " + checkStr + "\nfldName = " + fldName);
	
//	mmStr = checkStr + "MM";
//	ddStr = checkStr + "DD";
//	yyStr = checkStr + "YY";
	
////		alert("mmStr = " + mmStr + "\nddStr = " + ddStr + "\nyyStr = " + yyStr);
//	// Check to see if the month, day, and year fields are empty.
//	if (eval(mmStr + ".value") == "" || eval(ddStr + ".value") == "" || eval(yyStr + ".value") == "")
//	{
//		alert("You must enter a valid date for the " + fldName + " to continue. \nPlease try again.");
//		eval(mmStr + ".focus()");
////			eval(mmStr + ".select()");
//		return false;
//	}
//	
	var cnt = 0;
	
	for (j = 0;  j < 2;  j++) 
	{
		//alert("For Loop value j = " + j);
		switch(j)
		{	
			case 0:
//				alert("In Case 1");
				checkStr = "theForm.usrApplicant";
				fldName = "Applicant";
				break;
			case 1:
//				alert("In Case 2");
				checkStr = "theForm.usrSpouse";
				fldName = "Spouse";
				break;
		}

		mmStr = checkStr + "MM";
		ddStr = checkStr + "DD";
		yyStr = checkStr + "YY";
		
//		alert("1.) checkStr = " + checkStr + "\nfldName = " + fldName + "\nFor Loop value j = " + j);
//		alert("2.) mmStr.value = " + eval(mmStr + ".value") + "\nddStr.value = " + eval(ddStr + ".value") + "\nyyStr.value = " + eval(yyStr + ".value") + "\nFor Loop value j = " + j);
		
		// Check to see if either the month, day, and year fields are valued inconsistantly.
		// If all 3 fields are blank, that's OK. Just continue.

		if ((eval(mmStr + ".value") == "") && (eval(ddStr + ".value") == "") && (eval(yyStr + ".value") == "")) 
		{
//			alert("3.) All three values are blank." + "\nFor Loop value j = " + j);
		// Else if all three fields are valued, continue validating the date fields.
		cnt ++;
		} 
	}
	if (cnt == 2 && theForm.dependents.value != "0") {
		alert("If this policy is only for your children, enter your oldest child\n as the applicant and all other subsequent children in the Child lines");
		theForm.usrApplicantMM.focus();
//			eval(mmStr + ".select()");
		return false;
	}
	
	
	
		checkStr = "theForm.usrApplicant";
		fldName = "Applicant";
	
	
//		alert("checkStr = " + checkStr + "\nfldName = " + fldName);
	
	mmStr = checkStr + "MM";
	ddStr = checkStr + "DD";
	yyStr = checkStr + "YY";
	
//		alert("mmStr = " + mmStr + "\nddStr = " + ddStr + "\nyyStr = " + yyStr);
	// Check to see if the month, day, and year fields are empty.
	if (eval(mmStr + ".value") == "" || eval(ddStr + ".value") == "" || eval(yyStr + ".value") == "")
	{
		alert("You must enter a valid date for the " + fldName + " to continue. \nPlease try again.");
		eval(mmStr + ".focus()");
//			eval(mmStr + ".select()");
		return false;
	}
	

	

	// Check to insure that all dates are valid.
//	alert("Right before FOR loop.");
	for (j = 1;  j < 3;  j++) 
	{
//		alert("For Loop value j = " + j);
		switch(j)
		{	
			case 1:
//				alert("In Case 1");
				checkStr = "theForm.usrApplicant";
				fldName = "Applicant";
				break;
			case 2:
//				alert("In Case 2");
				checkStr = "theForm.usrSpouse";
				fldName = "Spouse";
				break;
		}

		mmStr = checkStr + "MM";
		ddStr = checkStr + "DD";
		yyStr = checkStr + "YY";
		
//		alert("1.) checkStr = " + checkStr + "\nfldName = " + fldName + "\nFor Loop value j = " + j);
//		alert("2.) mmStr.value = " + eval(mmStr + ".value") + "\nddStr.value = " + eval(ddStr + ".value") + "\nyyStr.value = " + eval(yyStr + ".value") + "\nFor Loop value j = " + j);
		
		// Check to see if either the month, day, and year fields are valued inconsistantly.
		// If all 3 fields are blank, that's OK. Just continue.

		if ((eval(mmStr + ".value") == "") && (eval(ddStr + ".value") == "") && (eval(yyStr + ".value") == "")) 
		{
//			alert("3.) All three values are blank." + "\nFor Loop value j = " + j);
			continue;
		// Else if all three fields are valued, continue validating the date fields.
		} 
		else if (!(eval(mmStr + ".value") == "") && !(eval(ddStr + ".value") == "") && !(eval(yyStr + ".value") == "")) 
		{
//			alert("4.) All three values are valued." + "\nFor Loop value j = " + j);
			// Check to see if the month, day, and year fields are positive integers.
			if (!isPosInt(eval(mmStr + ".value")) || !isPosInt(eval(ddStr + ".value")) || !isPosInt(eval(yyStr + ".value"))) 
			{
//				alert("5.) One or more of the date fields has characters \nother than positive integers." + "\nFor Loop value j = " + j);
				alert("Please enter only positive integer values in the " + fldName + " date fields.");
				eval(mmStr + ".focus()");
				//eval(mmStr + ".select()");
				return (false);
			}
//			alert("6.) All three fields are positive integers." + "\nFor Loop value j = " + j);
			// Check to see if the combination of the month, day, and year evaluate to a valid date.		
			if (!isValidDate(eval(mmStr + ".value"), eval(ddStr + ".value"), eval(yyStr + ".value"), fldName)) return false;
//			alert("7.) The date entered is a valid date." + "\nFor Loop value j = " + j);
			// If the field is Coverage date don't check the age...
			if (fldName != "Coverage")
			{
			 
				// Check the age of each person to insure they are less than 65 years old.
				if (isOlderThan65(eval(mmStr + ".value"), eval(ddStr + ".value"), eval(yyStr + ".value"), fldName))
				{
					alert("If you are over 65 years of age, \nthis coverage would be a duplicate \nof Medicare //coverage.");					eval(mmStr + ".focus()");
//					eval(mmStr + ".focus()");
//					eval(mmStr + ".select()");
					location.href = "over65.asp"
					return (false);
				}
				if (isYoungerThan0(eval(mmStr + ".value"), eval(ddStr + ".value"), eval(yyStr + ".value"), fldName))
				{
					alert("Dates in the future cannot be used for birthdates.\nPlease try again.");
					eval(mmStr + ".focus()");
				//	eval(mmStr + ".select()");
					return (false);
				}
			}
		// Else the month, day, and year fields are valued inconsistantly.
		} 
		else 
		{
//			alert("8.)Some fields are valued and some are not." + "\nFor Loop value j = " + j);
			alert("You must enter a valid date for the " + fldName + " to continue. \nPlease try again.");
			eval(mmStr + ".focus()");
			//eval(mmStr + ".select()");
			return false;
		}
//		alert("9.) Going to next for loop value." + "\nFor Loop value j = " + j);
	}
//	alert("10.) Outside of For Loop");
	// Check to see if the Coverage Date is less than today or greater than 90 days in the future.
	var ninetyDays = (60 * 1000 * 60 * 24 * 90)
	var futureDate = new Date()
	var todayDate = new Date()
	var coverageDate = new Date()
	var dateInMS = futureDate.getTime()
	dateInMS += ninetyDays
	futureDate.setTime(dateInMS)
//	alert("Future Date after 90 days. \nfutureDate.getMonth = " + (futureDate.getMonth() + 1) + "\nfutureDate.getDate = " + futureDate.getDate() + "\nfutureDate.getFullYear = " + futureDate.getFullYear())
//	alert("coverage month = " + theForm.usrCoverageMM.value + " parseFloat = " + parseFloat(theForm.usrCoverageMM.value))
//	alert("coverage day = " + theForm.usrCoverageDD.value + " parseInt = " + parseInt(theForm.usrCoverageDD.value))
//	alert("coverage year = " + theForm.usrCoverageYY.value + " parseInt = " + parseInt(theForm.usrCoverageYY.value))
}			




// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: CompareCounter
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: Form
// Returns: Boolean
// Changed: 
// Usage:  Used to count the number of plans selected for comparison.  If there 
//         are more than 3 plans selected, display an alert message and tell the 
//         user to try again.
// -----------------------------------------------------------------------------
function compareCounter(theForm)
{
//	alert("In compareCounter.")

	var checkboxCount = 0;
	for (var i = 0; i < theForm.elements.length; i++) {
		if (theForm.elements[i].type == "checkbox") 
		{
			if (theForm.elements[i].checked) 
			{
//				alert("This field is a checkbox and it is checked. \nThe fields name is = " + theForm.elements[i].name + "\nCheckboxCount before increment = " + checkboxCount);
				checkboxCount = checkboxCount + 1;
			}
		}
	}
	
	
	if (checkboxCount == 0)
	{
		alert("Please select plans to compare.");
		return (false);
	}
	
	if (checkboxCount > 3)
	{
		alert("You selected more than three plans to compare.  Please try again.");
		return (false);
	}
	return (true);
}	

// -----------------------------------------------------------------------------
// Copyright (C) 2002 Mark Pilger. All Rights Reserved.
// Module:  validation.js
// Function: brochureValidator
// Version: 1.0
// Author:  Mark Pilger
// Written: 05/14/2002
// Inputs: Form
// Returns: Boolean
// Changed: 
// Usage:  Used to validate the request brochure form.
// -----------------------------------------------------------------------------
function brochureValidator(theForm)
{
  if (theForm.usrFirstName.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.usrFirstName.focus();
    return (false);
  }

  if (theForm.usrLastName.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.usrLastName.focus();
    return (false);
  }

  if (theForm.usrAddress1.value == "")
  {
    alert("Please enter a value for the \"Address 1\" field.");
    theForm.usrAddress1.focus();
    return (false);
  }

  if (theForm.usrCity.value == "")
  {
    alert("Please enter a value for the \"City\" field.");
    theForm.usrCity.focus();
    return (false);
  }

  if (theForm.usrState.value == "")
  {
    alert("Please enter a value for the \"State\" field.");
    theForm.usrState.focus();
    return (false);
  }

  if (theForm.usrZip.value == "")
  {
    alert("Please enter a value for the \"Zip Code\" field.");
    theForm.usrZip.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.usrZip.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Zip Code\" field.");
    theForm.usrZip.focus();
    return (false);
  }

  if (theForm.usrEmail.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.usrEmail.focus();
    return (false);
  }
  return (true);
}

function cleanCheckboxes(theForm, checkName){
	var checkCount = 1;
	var el = theForm.elements;
	for (count = 0; count < el.length; count++)
	{
		if (el[count].name == checkName + '_' + checkCount)
		{
			if (!el[count].disabled)
			{
				el[count].checked = false;
			}
			checkCount++;
		}
	}

}

function GetValue(el)
{
	if ( (el.length) && (!el.options) )
	{
		for (var i=0; i<el.length; i++)
			if (el[i].checked)
				return el[i].value;
		return '';
	}
	else
		return el.value;
}


function trim(str)
{
	str = '' + str;
	var re = /^ */;
	var res = str.replace(re, '');
	re = / *$/;
	return(res.replace(re, ''));
}

function ShowAlert(sAlertString, hForm, FormElement)
{
	alert(sAlertString);
	if ( (!hForm.elements[FormElement].disabled) && (!hForm.elements[FormElement].length) )
	{
		if (hForm.elements[FormElement].type != 'hidden')
			hForm.elements[FormElement].focus();
		if (!hForm.elements[FormElement].options) // select
			hForm.elements[FormElement].select();
	}
	return false;
}

function parse_int(str)
{
	str = '' + str;
	var re = /^0*/;
	var res = str.replace(re, '');
	return parseInt(res);
}



//-->



