var display=(navigator.appName=="Microsoft Internet Explorer")?'':'table-row';

function setConditions() {
	document.getElementById("insured_1_health_conditions_detail").value=(((document.getElementById("aids").checked)?"HIV/AIDS / ":"")+""+((document.getElementById("diabetes").checked)?"Diabetes / ":"")+""+((document.getElementById("cancer").checked)?"Cancer / ":"")+""+((document.getElementById("heart-attack").checked)?"Heart Attack / ":"")+""+((document.getElementById("high-blood-pressure").checked)?"High Blood Pressure / ":"")+""+((document.getElementById("asthma").checked)?"Asthma / ":"")+""+((document.getElementById("stroke").checked)?"Stroke / ":"")+""+((document.getElementById("depression").checked)?"Depression / ":"")+""+((document.getElementById("other-illness").checked)?"Other major illness not listed. / ":""));
}

function unSetConditions() {
	document.getElementById("aids").checked=false;document.getElementById("diabetes").checked=false;document.getElementById("cancer").checked=false;document.getElementById("heart-attack").checked=false;document.getElementById("high-blood-pressure").checked=false;document.getElementById("asthma").checked=false;document.getElementById("stroke").checked=false;document.getElementById("depression").checked=false;document.getElementById("other-illness").checked=false;
}

function resetSpouseInfo(form,clrQ){

	for(var j=0;j<eval("form.insured_2_gender").length;j++) {
		eval("form.insured_2_gender")[j].checked=false;
	} 
	eval("form.insured_2_smoker")[1].checked=true;
	
	fillDropdown("insured_2_dobMM","");
	fillDropdown("insured_2_dobDD","");
	fillDropdown("insured_2_dobYYYY","");
	fillDropdown("insured_2_heightFT","");
	fillDropdown("insured_2_heightIN","");
	form.insured_2_weight.value="";
	
	if(clrQ) {
		for(var j=0;j<eval("form.spouse_needs_insurance").length;j++) {
			eval("form.spouse_needs_insurance")[j].checked=false;
		}
	}

}

function validateHealth(form){
	
	if(!form.insured_1_gender[0].checked && !form.insured_1_gender[1].checked) {
		alert("Please select your gender.");
		return false;
	}

	if(form.insured_1_dobMM.value=="" || isNaN(form.insured_1_dobMM.value) || parseInt(form.insured_1_dobMM.value)<1 || parseInt(form.insured_1_dobMM.value)>12) {
		alert('Please select your date of birth.');
		form.insured_1_dobMM.value="";
		form.insured_1_dobMM.focus();
		return false;
	}
		
	if(form.insured_1_dobDD.value=="" || isNaN(form.insured_1_dobDD.value) || parseInt(form.insured_1_dobDD.value)<1 || parseInt(form.insured_1_dobDD.value)>31) {
		alert('Please enter your date of birth. ');
		form.insured_1_dobDD.value="";
		form.insured_1_dobDD.focus();
		return false;
	}
	
	if(form.insured_1_dobYYYY.value=="" || isNaN(form.insured_1_dobYYYY.value) || parseInt(form.insured_1_dobYYYY.value)<1886 || parseInt(form.insured_1_dobYYYY.value)>2007) {
		alert('Please enter your date of birth.');
		form.insured_1_dobYYYY.value="";
		form.insured_1_dobYYYY.focus();
		return false;
	}
	
	if(form.insured_1_heightFT.value=="") {
		alert("Please select your height in feet and inches.");
		form.insured_1_heightFT.focus();return false;
	}
	
	if(form.insured_1_heightIN.value=="") {
		alert("Please select your height in feet and inches.");
		form.insured_1_heightIN.focus();
		return false;
	}
	
	if(form.insured_1_weight.value=="" || isNaN(form.insured_1_weight.value) || parseInt(form.insured_1_weight.value)<1 || parseInt(form.insured_1_weight.value)>500) {
		alert("Please select your weight in pounds.");
		form.insured_1_weight.value="";
		form.insured_1_weight.focus();
		return false;
	}
	
	//Inusred 1 BMI must be between 10 and 100
	if(bmi(form.insured_1_weight.value,form.insured_1_heightFT.value,form.insured_1_heightIN.value)) {
		alert("Your height and weight appears to have been entered incorrectly. Please re-enter your height and weight.");
		form.insured_1_weight.focus();
		return false;
	}

	return true;

}

function validateHealth2(form) {
	var conditionYes=document.getElementById("insured_1_health_conditions_yes").checked;
	var conditionNo=document.getElementById("insured_1_health_conditions_no").checked;
	var conditionDetail=document.getElementById("insured_1_health_conditions_detail").value;
	
	if(form.insured_1_currently_covered.checked == false) {
		alert("Please answer the question if you are currently insured.");
		form.insured_1_currently_covered.focus();
		return false;
	}
	
	if(form.insured_1_smoker.checked == false) {
		alert("Please answer the question if you are a smoker.");
		form.insured_1_smoker.focus();
		return false;
	}
	
	if(conditionYes && trim(conditionDetail).length==0) {
		alert("Please tell us about any preexisting conditions.");
		return false;
	}
	
	if(conditionNo) {
		document.getElementById("insured_1_health_conditions_detail").value="None listed";
	}
	return true;
}

function validateHealth3(form){
	
	if(form.spouse_needs_insurance[0].checked){

		if(document.getElementById("insured_2_gender_m").checked == false && document.getElementById("insured_2_gender_f").checked == false) {
			alert("Please select your spouse's gender.");
			return false;
		}
		
		if(form.insured_2_dobMM.value=="" || isNaN(form.insured_2_dobMM.value) || parseInt(form.insured_2_dobMM.value)<1 || parseInt(form.insured_2_dobMM.value)>12) {
			alert('Please select your spouse\'s date of birth.');
			form.insured_2_dobMM.value="";
			form.insured_2_dobMM.focus();
			return false;
		}
		
		if(form.insured_2_dobDD.value=="" || isNaN(form.insured_2_dobDD.value) || parseInt(form.insured_2_dobDD.value)<1 || parseInt(form.insured_2_dobDD.value)>31) {
			alert('Please select your spouse\'s date of birth.');
			form.insured_2_dobDD.focus();
			return false;
		}
		
		if(form.insured_2_dobYYYY.value=="") {
			alert('Please select your spouse\'s date of birth. ');
			form.insured_2_dobYYYY.value="";
			form.insured_2_dobYYYY.focus();
			return false;
		}
		
		if(form.insured_2_heightFT.value=="") {
			alert("Please select your spouse's height in feet and inches.");
			form.insured_2_heightFT.focus();
			return false;
		}
		
		if(form.insured_2_heightIN.value=="") {
			alert("Please select your height in feet and inches.");
			form.insured_2_heightIN.focus();
			return false;
		}
		
		if(form.insured_2_weight.value=="" || isNaN(form.insured_2_weight.value) || parseInt(form.insured_2_weight.value)<1 || parseInt(form.insured_2_weight.value)>500) {
			alert("Please enter your spouse's weight in pounds.");
			form.insured_2_weight.value="";
			form.insured_2_weight.focus();
			return false;
		}
		//Inusred 1 BMI must be between 10 and 100
		if(bmi(form.insured_2_weight.value,form.insured_2_heightFT.value,form.insured_2_heightIN.value)) {
			alert("Your spouse's height and weight appears to have been entered incorrectly. Please re-enter your spouse's height and weight.");
			form.insured_2_weight.focus();
			return false;
		}

	}

//End of Spouse

//Children info

	for(i=3;i<=parseInt(eval(getDocObj("number_of_dependents")).value)+2;i++) {
	
		if(!eval("form.insured_"+i+"_gender")[0].checked && !eval("form.insured_"+i+"_gender")[1].checked) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's gender.");
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_dobMM")).value=="" ||isNaN(eval(getDocObj("insured_"+i+"_dobMM")).value) || parseInt(eval(getDocObj("insured_"+i+"_dobMM")).value)<1 || parseInt(eval(getDocObj("insured_"+i+"_dobMM")).value)>12) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's date of birth.");
			eval(getDocObj("insured_"+i+"_dobMM")).value="";
			eval(getDocObj("insured_"+i+"_dobMM")).focus();
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_dobDD")).value=="" ||isNaN(eval(getDocObj("insured_"+i+"_dobDD")).value) || parseInt(eval(getDocObj("insured_"+i+"_dobDD")).value)<1 || parseInt(eval(getDocObj("insured_"+i+"_dobDD")).value)>31) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's date of birth.");
			eval(getDocObj("insured_"+i+"_dobDD")).value="";
			eval(getDocObj("insured_"+i+"_dobDD")).focus();
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_dobYYYY")).value=="" ||isNaN(eval(getDocObj("insured_"+i+"_dobYYYY")).value) || parseInt(eval(getDocObj("insured_"+i+"_dobYYYY")).value)>2007 || parseInt(eval(getDocObj("insured_"+i+"_dobYYYY")).value)<1886) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's date of birth.");
			eval(getDocObj("insured_"+i+"_dobYYYY")).value="";
			eval(getDocObj("insured_"+i+"_dobYYYY")).focus();
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_heightFT")).value=="") {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's height in feet and inches.\nIf your child is an infant, please select '11' in the inches field and leave feet at 0.");
			eval(getDocObj("insured_"+i+"_heightFT")).focus();
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_heightIN")).value=="") {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's height in feet and inches.\nIf your child is an infant, please select '11' in the inches field and leave feet at 0.");
			eval(getDocObj("insured_"+i+"_heightIN")).focus();
			return false;
		}
		
		if(eval(getDocObj("insured_"+i+"_weight")).value=="" ||isNaN(eval(getDocObj("insured_"+i+"_weight")).value) || parseInt(eval(getDocObj("insured_"+i+"_weight")).value)<1 || parseInt(eval(getDocObj("insured_"+i+"_weight")).value)>500) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's weight in pounds.");
			eval(getDocObj("insured_"+i+"_weight")).value="";
			eval(getDocObj("insured_"+i+"_weight")).focus();
			return false;
		}
		
		//Inusred 1 BMI must be between 10 and 100
		if(bmi(eval(getDocObj("insured_"+i+"_weight")).value,eval(getDocObj("insured_"+i+"_heightFT")).value,eval(getDocObj("insured_"+i+"_heightIN")).value)) {
			alert("Your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's height and weight appears to have been entered incorrectly. Please re-enter your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5||i==6)?"rd":"th")+" child's height and weight.");
			form.insured_1_weight.focus();
			return false;
		}
	
	}
	
	return true;

}

function validateHealth4(form){
	if(!validateHealth(form) || !validateHealth2(form) || !validateHealth3(form)){
		return false;
	}
	
	document.getElementById('phone').value=document.getElementById('phone_1').value+document.getElementById('phone_2').value+document.getElementById('phone_3').value;

	if(form.first_name.value=="" || hasNum(form.first_name.value)) {
		alert("Please enter your first name.");
		form.first_name.focus();
		return false;
	}
	
	if(form.last_name.value=="" || hasNum(form.last_name.value) || form.last_name.value.length<2) {
		alert("Please enter your last name.");
		form.last_name.focus();
		return false;
	}
	
	if(form.address_1_street1.value=="") {
		alert("Please enter your address, including street name and number.");
		form.address_1_street1.focus();
		return false;
	}
	
	if(form.address_1_city.value=="") {
		alert("Please enter the name of the city or town you live in.");
		form.address_1_city.focus();
		return false;
	}
	
	if(form.address_1_state.value=="none") {
		alert("Please choose the state you live in.");
		return false;
	}
	
	if(form.address_1_zip.value=="" || isNaN(form.address_1_zip.value) || form.address_1_zip.value.replace(/ /g, "").length!=5) {
		alert("Please enter your zip code.");
		form.address_1_zip.value="";
		form.address_1_zip.focus();
		return false;
	}
	
	var stripped=form.phone.value.replace(/[\(\)\.\-\ ]/g,'');
	
	if(isNaN(parseInt(stripped))) {
		alert("Please check the accuracy of your phone number.");
		document.getElementById('phone_1').focus();
		return false;
	}
	
	if(!(stripped.length==10)) {
		alert("The phone number is the wrong length.\nMake sure you included an area code.\nExample:545-654-4454");
		document.getElementById('phone_1').focus();
		return false;
	}
	
	if(form.email.value == "") {
		alert("Please tell us your email address.");
		form.email.focus();
		return false;
	} else {
		if(!emailCheck(form.email.value)) {
			alert("Please enter a valid email address.\nYour email address should use the form \"name@domain.com\"");
			form.email.focus();
			return false;
		}
	}

}

function makePopUp(whereTo,winWidth,winHeight) {
	remote=window.open("","remotewin","width="+winWidth+",height="+winHeight+",menubar=0,toolbar=no,scrollbars=yes");
	remote.location.href=whereTo;
	remote.focus();
}

function getDocObj(elem,parent) {
	if(document.layers) {
		if(parent) {
			return "document."+parent+".document."+elem;
		} else {
			return "document."+elem;
		}
	} else if(document.all) {
		return "document.all."+ elem;
	} else if(document.getElementById) {
		return "document.getElementById('"+elem+"')";
	}
}

function displayCommentBox(commentId, flag) {
	if(flag) {
		eval(getDocObj(commentId)).style.display="block";
	}else{
		eval(getDocObj(commentId)).style.display="none";
	}
}

function fillDropdown(id, val) {
	for(var i=0; i<parseInt(eval(getDocObj(id)).options.length)-1; i++) {
		if(eval(getDocObj(id)).options[i].value==val) {
			eval(getDocObj(id)).options[i].selected=true;
			break;
		}
	}
}

function changeChildren(value,form){

	if(value==0) {
		for(i=3;i<=8;i++) {
			if(eval(getDocObj("insured_"+i+"_smoker_tr")).style.display == "none") {
				break;
			} else {
				eval(getDocObj("insured_"+i+"_smoker_tr")).style.display="none";
			}
		}
	}

	for(var i=8;i>parseInt(value)+2;i--) {
		for(var j=0;j<eval("form.insured_"+i+"_gender").length;j++) {
			eval("form.insured_"+i+"_gender")[j].checked=false;
		}
		fillDropdown("insured_"+i+"_heightFT","");
		fillDropdown("insured_"+i+"_heightIN","");
		eval(getDocObj("insured_"+i+"_dobMM")).value="";
		eval(getDocObj("insured_"+i+"_dobDD")).value="";
		eval(getDocObj("insured_"+i+"_dobYYYY")).value="";
		eval(getDocObj("insured_"+i+"_weight")).selectedIndex=0;
		for(var j=0;j<eval("form.insured_"+i+"_smoker").length;j++){eval("form.insured_"+i+"_smoker")[j].checked=false;}
		eval("form.insured_"+i+"_smoker")[1].checked=true;

	}

	if(value==1) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="none";
		eval(getDocObj("child_option3")).style.display="none";
		eval(getDocObj("child_option4")).style.display="none";
		eval(getDocObj("child_option5")).style.display="none";
		eval(getDocObj("child_option6")).style.display="none";
	}

	else if(value==2) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="";
		eval(getDocObj("child_option3")).style.display="none";
		eval(getDocObj("child_option4")).style.display="none";
		eval(getDocObj("child_option5")).style.display="none";
		eval(getDocObj("child_option6")).style.display="none";
	}

	else if(value==3) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="";
		eval(getDocObj("child_option3")).style.display="";
		eval(getDocObj("child_option4")).style.display="none";
		eval(getDocObj("child_option5")).style.display="none";
		eval(getDocObj("child_option6")).style.display="none";
	}

	else if(value==4) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="";
		eval(getDocObj("child_option3")).style.display="";
		eval(getDocObj("child_option4")).style.display="";
		eval(getDocObj("child_option5")).style.display="none";
		eval(getDocObj("child_option6")).style.display="none";
	}

	else if(value==5) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="";
		eval(getDocObj("child_option3")).style.display="";
		eval(getDocObj("child_option4")).style.display="";
		eval(getDocObj("child_option5")).style.display="";
		eval(getDocObj("child_option6")).style.display="none";
	}

	else if(value==6) {
		eval(getDocObj("child_option1")).style.display="";
		eval(getDocObj("child_option2")).style.display="";
		eval(getDocObj("child_option3")).style.display="";
		eval(getDocObj("child_option4")).style.display="";
		eval(getDocObj("child_option5")).style.display="";
		eval(getDocObj("child_option6")).style.display="";
	}

	else {
		eval(getDocObj("child_option1")).style.display="none";
		eval(getDocObj("child_option2")).style.display="none";
		eval(getDocObj("child_option3")).style.display="none";
		eval(getDocObj("child_option4")).style.display="none";
		eval(getDocObj("child_option5")).style.display="none";
		eval(getDocObj("child_option6")).style.display="none";
	}

}

function emailCheck(emailStr){

   var emailPat=/^(.+)@(.+)$/
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
   var validChars="\[^\\s" + specialChars + "\]"
   var quotedUser="(\"[^\"]*\")"
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
   var matchArray=emailStr.match(emailPat)

   if(matchArray==null){alert("Email address seems incorrect (check @ and .'s)");return false;}

   var user=matchArray[1];
   var domain=matchArray[2];

   if(user.match(userPat)==null){alert("The username of the email address doesn't seem to be valid.");return false;}

   var IPArray=domain.match(ipDomainPat);

   if(IPArray!=null){for(var i=1;i<=4;i++){if(IPArray[i]>255){alert("Destination IP address is invalid!");return false;}}return true;}

   var domainArray=domain.match(domainPat);

   if(domainArray==null){alert("The domain name doesn't seem to be valid.");return false;}

   var atomPat=new RegExp(atom,"g");
   var domArr=domain.match(atomPat);
   var len=domArr.length;

   if(domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3){alert("The email address must end in a three-letter domain, or two letter country.");return false;}

   if(len<2){var errStr="This address is missing a hostname!";alert(errStr);return false;}

   return true;

}

function ltrim(s){return s.replace(/^\s*/,"");}

function rtrim(s){return s.replace(/\s*$/,"");}

function trim(s){return rtrim(ltrim(s));}

function hasNum(s) {
	for(var i=0; i<s.length; i++) {
		if(!isNaN(s.charAt(i)))
			return true;
	}
	return false;
}

function unSetSpouse(form) {
	document.getElementById('insured_2_gender_m').checked=false;
	document.getElementById('insured_2_gender_f').checked=false;
	form.insured_2_dobMM.value="";
	form.insured_2_dobDD.value="";
	form.insured_2_dobYYYY.value="";
	form.insured_2_heightFT.value="";
	form.insured_2_heightIN.value="";
	form.insured_2_weight.value="";
}

//Checks if BMI is valid, returns true if BMI is not valid
function bmi(w,hFT,hIN){
	//calculate height in inches
	var h = ((hFT * 12) + (hIN * 1));
	//calculate BMI
	var bmi = 703 * (w/(h*h));
	//Pass if BMI is not valid
	if(bmi > 100 || bmi < 10)
		return true;
	return false;
}
