function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;}
function CheckField(TheValue,Type, CheckType, Length) {
	var tempcheck = 1;var ponerror="";
	if(TheValue.length < Length ){ponerror += "- " + Type +" does not contain enough characters.\n";	}
	return ponerror}
function CheckNum(TheNumber,Type, NumLength) {
	var Integers = "0123456789";var integercounter=0;var ponerror="";
	for (i =0; i <= TheNumber.length -1; i++) {if (Integers.indexOf(TheNumber.charAt(i)) != -1) {integercounter++;} 	}
	if ( integercounter <= NumLength-1){ponerror += "- " + Type +" does not have the correct number of digits\n";}
	return ponerror;}
function MM_validateForm() { //v4.0
  var i,p,q,nm,z,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) {p=val.indexOf('@');z=val.lastIndexOf('.');
        if (p<1 || p==(val.length-1) || z<1 || z > (val.length-4)) errors+='- '+nm+' must contain an e-mail address.\n';
      	else if (val.search(/[^a-zA-Z0-9@._-]/) != -1) errors+='- '+nm+' contains invalid characters.\n';	  
	  } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 

	MM = (document.healthform.BirthMonth);
	DD = (document.healthform.BirthDay);
	YY = (document.healthform.BirthYear);
	errors += CheckField((document.healthform.FirstName.value),'FirstName',0,2);
	errors += CheckField((document.healthform.LastName.value),'LastName',0,2);	
	errors += CheckField((document.healthform.City.value),'City',0,2);
	errors += CheckField((document.healthform.Address.value),'Address',0,5);
	errors += CheckNum((document.healthform.HomePhoneArea.value + document.healthform.HomePhoneFirst.value + document.healthform.HomePhoneLast.value),'HomePhone',10);	
	errors += CheckNum((document.healthform.WorkPhoneArea.value + document.healthform.WorkPhoneFirst.value + document.healthform.WorkPhoneLast.value),'WorkPhone',10);

	zip = document.healthform.Zip.value;
	if (zip.length != 5 || zip.match(/[^0-9]/))
	            errors += "- Zip code is required\n";

	errors += CheckNum((document.healthform.Weight.value),'Weight',2);
	if (MM[MM.selectedIndex].value == "" || DD[DD.selectedIndex].value == "" || YY[YY.selectedIndex].value == "") errors += "- Date of birth is required\n";
	else if(!checkAge(DD[DD.selectedIndex].value, MM[MM.selectedIndex].value, YY[YY.selectedIndex].value))errors += "- Based on the information you provided, we are unable to provide you with an online quote at this time.\n";	
	if (errors) {
		alert('Please validate the following fields before continuing:\n\n'+errors);
		document.MM_returnValue = (errors == '');
		return false;
	}else {
		document.MM_returnValue = (errors == '');
		document.healthform.send.value = "Submitting Request";
		return true;
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function checkAge(day, month, year){
	var min_age = 18;
	var theirDate = new Date(year, (month-1), day);
	var today = new Date;
	timeold = (today.getTime() - theirDate.getTime());
    msPerYear = 24 * 60 * 60 * 1000 * 365;         		
	yearold = Math.floor(timeold / msPerYear);  
	if (yearold < min_age) return false;
	else return true;
}

