/**
 * Retrieve the absolute coordinates of an element.
 *
 * @param element
 *   A DOM element.
 * @return
 *   A hash containing keys 'x' and 'y'.
 */
function getAbsolutePosition(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getAbsolutePosition(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};
/**
 * Retrieve the coordinates of the given event relative to the center
 * of the widget.
 *
 * @param event
 *   A mouse-related DOM event.
 * @param reference
 *   A DOM element whose position we want to transform the mouse coordinates to.
 * @return
 *    A hash containing keys 'x' and 'y'.
 */
function getRelativeCoordinates(event, reference) {
  var x, y;
  event = event || window.event;
  var el = event.target || event.srcElement;

  if (!window.opera && typeof event.offsetX != 'undefined') {
    // Use offset coordinates and find common offsetParent
    var pos = { x: event.offsetX, y: event.offsetY };

    // Send the coordinates upwards through the offsetParent chain.
    var e = el;
    while (e) {
      e.mouseX = pos.x;
      e.mouseY = pos.y;
      pos.x += e.offsetLeft;
      pos.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Look for the coordinates starting from the reference element.
    var e = reference;
    var offset = { x: 0, y: 0 }
    while (e) {
      if (typeof e.mouseX != 'undefined') {
        x = e.mouseX - offset.x;
        y = e.mouseY - offset.y;
        break;
      }
      offset.x += e.offsetLeft;
      offset.y += e.offsetTop;
      e = e.offsetParent;
    }

    // Reset stored coordinates
    e = el;
    while (e) {
      e.mouseX = undefined;
      e.mouseY = undefined;
      e = e.offsetParent;
    }
  }
  else {
    // Use absolute coordinates
    var pos = getAbsolutePosition(reference);
    x = event.pageX  - pos.x;
    y = event.pageY - pos.y;
  }
  // Subtract distance to middle
  return { x: x, y: y };
}
var pu=true;
var spouse = 0;
var children = 0;

/* Set the conditions to the Hidden field. */
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("pregnant").checked)?"Pregnant / ":"")+""+((document.getElementById("stroke").checked)?"Stroke / ":"")+""+((document.getElementById("depression").checked)?"Depression / ":"")+""+((document.getElementById("other-illness").checked)?"Other major illness not listed. / ":""));
}

/* un-checked the checkboxes from health conditions and clears the hidden field
   for health conditions*/
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("pregnant").checked=false;
	document.getElementById("stroke").checked=false;
	document.getElementById("depression").checked=false;
	document.getElementById("other-illness").checked=false;
	document.getElementById("insured_1_health_conditions_detail").value="";
}

//date format
var now = new Date();
var year = now.getFullYear()-2;

// Health Profile Begin
function HealthProfile(form){
	
	/* variables for the health condition radio button and hidden field*/
	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;	
	
		//Gender must be selected for insured 1
	if(!form.insured_1_gender[0].checked && !form.insured_1_gender[1].checked){
		alert("Please select your gender.");
		form.insured_1_gender[0].focus();
		pu=true;
		return false;
	}
	
	//Date of birth month must be a valid month
	if(form.insured_1_dobMM.value=="" || !isIntegerHealthProfiles(form.insured_1_dobMM.value) || isNaN(form.insured_1_dobMM.value) || parseInt((form.insured_1_dobMM.value*1))<1 || parseInt((form.insured_1_dobMM.value*1))>12 || hasSpaceHealthprofile(form.insured_1_dobMM.value)) {
		alert('Your date of birth appears to have been entered incorrectly. Please re-enter your date of birth, using the format "Month, Day, Year."\nExample: 8/20/1970');
		form.insured_1_dobMM.value="";
		form.insured_1_dobMM.focus();
		pu=true;
		return false;
	}

	//Date of birth day must be a valid day
	if(form.insured_1_dobDD.value=="" || !isIntegerHealthProfiles(form.insured_1_dobDD.value)  || isNaN(form.insured_1_dobDD.value) || parseInt(form.insured_1_dobDD.value*1)<1 || parseInt(form.insured_1_dobDD.value*1)>31 || hasSpaceHealthprofile(form.insured_1_dobDD.value)) {
		alert('Your date of birth appears to have been entered incorrectly. Please re-enter your date of birth, using the format "Month, Day, Year."\nExample: 8/20/1970');
		form.insured_1_dobDD.value="";
		form.insured_1_dobDD.focus();
		pu=true;
		return false;
	}

	//Date of birth year must be a valid year
	if(form.insured_1_dobYYYY.value==""|| !isIntegerHealthProfiles(form.insured_1_dobYYYY.value)  || isNaN(form.insured_1_dobYYYY.value) || parseInt(form.insured_1_dobYYYY.value)<1886 || parseInt(form.insured_1_dobYYYY.value)>year || hasSpaceHealthprofile(form.insured_1_dobYYYY.value)) {
		alert('Your date of birth appears to have been entered incorrectly. Please re-enter your date of birth, using the format "Month, Day, Year."\nExample: 8/20/1970');
		form.insured_1_dobYYYY.value="";
		form.insured_1_dobYYYY.focus();
		pu=true;
		return false;
	}

	//Insured 1 weight has to be a number greated than 1	
	if(form.insured_1_heightFT.value=="" || !isIntegerHealthProfiles(form.insured_1_heightFT.value) || isNaN(form.insured_1_heightFT.value) || parseInt(form.insured_1_heightFT.value)>7) {
		alert("Please select your height in feet and inches.");
		form.insured_1_heightFT.value="";
		form.insured_1_heightFT.focus();
		pu=true;
		return false;
	}

	//Insured 1 height in inches must be selected
	if(form.insured_1_heightIN.value=="" || isNaN(form.insured_1_heightIN.value) || !isIntegerHealthProfiles(form.insured_1_heightIN.value) || parseInt(form.insured_1_heightIN.value)>11) {		
			alert("Please select your height in feet and inches.");	
			form.insured_1_heightIN.value="";
			form.insured_1_heightIN.focus();
			pu=true;
			return false;		
	}
		
	//Insured 1 weight has to be a number greated than 1
	if(form.insured_1_weight.value=="" || isNaN(form.insured_1_weight.value) || parseInt(form.insured_1_weight.value)<1) {
			alert("Please enter your weight in pounds.\nExample: 200");
			form.insured_1_weight.value="";
			form.insured_1_weight.focus();
			pu=true;
			return false;
		}
		

	//Insured 1 BMI must be between 10 and 100
	if(bmiHealthProfile(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.value="";
		form.insured_1_weight.focus();
		pu=true;
		return false;
	}	
	if(conditionYes && trim(conditionDetail).length==0) {
		alert("Please specify  pre-existing health conditions. If you have no pre-existing conditions, please select \"No\".");
		return false;
	}
	
	if(conditionNo) {
		document.getElementById("insured_1_health_conditions_detail").value="None listed";
	}		
	return true;
}

//Checks if BMI is valid, returns true if BMI is not valid
function bmiHealthProfile(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;
}
function hasSpaceHealthprofile(s) {
	for(var i=0;i<s.length;i++)
		if(s.charAt(i)==" ")
			return true;
	return false;
}

function isIntegerHealthProfiles(s){
	return (s.toString().search(/^[0-9]+$/)==0);
}

function noNum(s){
	for(var i = 0; i < s.length; i++)
		if(!isNaN(s.charAt(i)) && s.charAt(i)!=" ")
			return false;
	return true;
}
function showHidden(id, value) {
	if(value=="yes")
		document.getElementById(id).style.display='';
	else
		document.getElementById(id).style.display='none';
}
function displayCommentBox(commentId, val) {
	if(val == "yes") {
		eval(getDocObj(commentId)).style.display="";
	}
	
	if(val == "no") {
		eval(getDocObj(commentId)).style.display="none";
	}
}


// End Health profile



// Spouse Profile Begin 
add_spouse = new Image();
add_spouse.src = "images/add-spouse.jpg";
add_spouse_gray = new Image();
add_spouse_gray.src = "images/add-spouse-gray.jpg";

//add Spouse function
function Spouse(form){
	//If Spouse Selected
	if(spouse==1){

		//spouse gender
		if(!form.insured_2_gender[0].checked && !form.insured_2_gender[1].checked){
			alert("Please enter your spouse's gender.");
			form.insured_2_gender[0].focus();
			pu=true;
			return false;
		}
				
		//insured 2 date of birth month must be a valid month
		if(form.insured_2_dobMM.value=="" || !isIntegerSpouse(form.insured_2_dobMM.value) || isNaN(form.insured_2_dobMM.value) || parseInt(form.insured_2_dobMM.value*1)<1 || parseInt(form.insured_2_dobMM.value*1)>12 || hasSpaceSpouse(form.insured_2_dobMM.value)) {
			alert("Your spouse's date of birth appears to have been entered incorrectly. Please re-enter your spouse's date of birth, using the format \"Month, Day, Year.\"\nExample: 8/21/1970");
			form.insured_2_dobMM.value="";
			form.insured_2_dobMM.focus();
			pu=true;
			return false;
		}
		
		//insured 2 date of birth day must be a valid day
		if(form.insured_2_dobDD.value=="" || !isIntegerSpouse(form.insured_2_dobDD.value) || isNaN(form.insured_2_dobDD.value) || parseInt(form.insured_2_dobDD.value*1)<1 || parseInt(form.insured_2_dobDD.value*1)>31 || hasSpaceSpouse(form.insured_2_dobDD.value)) {
			alert("Your spouse's date of birth appears to have been entered incorrectly. Please re-enter your spouse\'s date of birth, using the format \"Month, Day, Year.\"\nExample: 8/21/1970");
			form.insured_2_dobDD.value="";			
			form.insured_2_dobDD.focus();
			pu=true;
			return false;
		}
		
		//insured 2 date of birth day must be a valid year
		if(form.insured_2_dobYYYY.value=="" || !isIntegerSpouse(form.insured_2_dobYYYY.value) || isNaN(form.insured_2_dobYYYY.value) || parseInt(form.insured_2_dobYYYY.value)<1886 || parseInt(form.insured_2_dobYYYY.value)>2009 ||  hasSpaceSpouse(form.insured_2_dobYYYY.value)) {
			alert("Your spouse's date of birth appears to have been entered incorrectly. Please re-enter your spouse's date of birth, using the format \"Month, Day, Year.\"\nExample: 8/21/1970");
			form.insured_2_dobYYYY.value="";			
			form.insured_2_dobYYYY.focus();
			pu=true;
			return false;
		}
		
		//insured 2 height in feet must be selected
		if(form.insured_2_heightFT.value=="" || !isIntegerSpouse(form.insured_2_heightFT.value) || isNaN(form.insured_2_heightFT.value) || parseInt(form.insured_2_heightFT.value)>7) {
			alert("Please select your spouse's height in feet and inches.");
			form.insured_2_heightFT.value="";			
			form.insured_2_heightFT.focus();
			pu=true;
			return false;
		}
		
		//insured 2 height in inches must be selected
		if(form.insured_2_heightIN.value=="" || !isIntegerSpouse(form.insured_2_heightIN.value) || isNaN(form.insured_2_heightIN.value) || parseInt(form.insured_1_heightIN.value)>11) {
			alert("Please select your spouse's height in feet and inches.");
			form.insured_2_heightIN.value="";			
			form.insured_2_heightIN.focus();
			pu=true;
			return false;
		}
		
		//insured 2 weight must be a number greater then 1
		if(form.insured_2_weight.value=="" || !isIntegerSpouse(form.insured_2_weight.value) || isNaN(form.insured_2_weight.value) || parseInt(form.insured_2_weight.value)<1) {
			alert("Please enter your spouse's weight in pounds.");
			form.insured_2_weight.value="";			
			form.insured_2_weight.focus();
			pu=true;
			return false;
		}
		
		//insured 2 BMI must be between 10 and 100
		if(bmiSpouse(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.value="";
			form.insured_2_weight.focus();
			pu=true;
			return false;
		}
	}
	return true;
}
//Checks if BMI is valid, returns true if BMI is not valid
function bmiSpouse(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;
}
//check for Space 
function hasSpaceSpouse(s) {
	for(var i=0;i<s.length;i++)
		if(s.charAt(i)==" ")
			return true;
	return false;
}

// Check for Integer Numbers
function isIntegerSpouse(s){
	return (s.toString().search(/^[0-9]+$/)==0);
}
// Add Spouse
function addSpouse() {
	if(spouse==0) {
		document.getElementById('spouse').style.display='';
		spouse = 1;
		document.images['add-spouse'].src=add_spouse_gray.src;
	}
}

// Delete Spouse
function deleteSpouse() {
	document.getElementById('spouse').style.display='none';
	spouse = 0;
	document.images['add-spouse'].src=add_spouse.src;
	
	//Reset Spouse Values
	document.getElementById('insured_2_gender_male').checked=false;
	document.getElementById('insured_2_gender_female').checked=false;
	document.getElementById('insured_2_dobMM').value='';
	document.getElementById('insured_2_dobDD').value='';
	document.getElementById('insured_2_dobYYYY').value='';
	document.getElementById('insured_2_heightFT').value='';
	document.getElementById('insured_2_heightIN').value='';
	document.getElementById('insured_2_weight').value='';
	document.getElementById('insured_2_smoker_yes').checked=false;
	document.getElementById('insured_2_smoker_no').checked=false;
}

// Spouse Profile End


// Child Profile Begin
add_child = new Image();
add_child.src = "images/add-child.jpg";
add_child_gray = new Image();
add_child_gray.src = "images/add-child-gray.jpg";

// add childres
function Children(form){
	
	for(i=3;i<=(children*1+2);i++) {
		//gender must be selected
		if(eval(getDocObj("insured_"+i+"_gender_male")).checked==false && eval(getDocObj("insured_"+i+"_gender_female")).checked==false) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"th")+" child\'s gender.");
			;
			eval(getDocObj("insured_"+i+"_gender_male")).focus();
			pu=true;
			return false;
		}
		//date of birth month must be a valid month
		if(eval(getDocObj("insured_"+i+"_dobMM")).value==""|| !isIntegerChild(eval(getDocObj("insured_"+i+"_dobMM")).value) ||isNaN(eval(getDocObj("insured_"+i+"_dobMM")).value*1) || parseInt(eval(getDocObj("insured_"+i+"_dobMM")).value*1)<1 || parseInt(eval(getDocObj("insured_"+i+"_dobMM")).value*1)>12 || hasSpaceChild(eval(getDocObj("insured_"+i+"_dobMM")).value)) {
			alert("Your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"th")+" child\'s date of birth appears to have been entered incorrectly. Please enter the date numerically, using the format \"Month, Day, Year.\"\nExample: 8/20/06");
			eval(getDocObj("insured_"+i+"_dobMM")).value="";
			eval(getDocObj("insured_"+i+"_dobMM")).focus();
			pu=true;
			return false;
		}
		//date of birth day must be a valid day
		if(eval(getDocObj("insured_"+i+"_dobDD")).value=="" || !isIntegerChild(eval(getDocObj("insured_"+i+"_dobDD")).value) ||isNaN(eval(getDocObj("insured_"+i+"_dobDD")).value) || parseInt(eval(getDocObj("insured_"+i+"_dobDD")).value*1)<1 || parseInt(eval(getDocObj("insured_"+i+"_dobDD")).value*1)>31 || hasSpaceChild(eval(getDocObj("insured_"+i+"_dobDD")).value)) {
			alert("Your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"th")+" child\'s date of birth appears to have been entered incorrectly. Please enter the date numerically, using the format \"Month, Day, Year.\"\nExample: 8/20/06");
			eval(getDocObj("insured_"+i+"_dobDD")).value="";			
			eval(getDocObj("insured_"+i+"_dobDD")).focus();
			pu=true;
			return false;
		}
		//date of birth year must be a valid year
		if(eval(getDocObj("insured_"+i+"_dobYYYY")).value=="" || !isIntegerChild(eval(getDocObj("insured_"+i+"_dobYYYY")).value) ||isNaN(eval(getDocObj("insured_"+i+"_dobYYYY")).value) || parseInt(eval(getDocObj("insured_"+i+"_dobYYYY")).value)>2008 || parseInt(eval(getDocObj("insured_"+i+"_dobYYYY")).value)<1886 || hasSpaceChild(eval(getDocObj("insured_"+i+"_dobYYYY")).value)) {
			alert("Your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"th")+" child's date of birth appears to have been entered incorrectly. Please enter the date numerically, using the format \"Month, Day, Year.\"\nExample: 8/20/06");
			eval(getDocObj("insured_"+i+"_dobYYYY")).value="";			
			eval(getDocObj("insured_"+i+"_dobYYYY")).focus();
			pu=true;
			return false;
		}
		//height in feet must be selected
		if(eval(getDocObj("insured_"+i+"_heightFT")).value=="" || !isIntegerChild(eval(getDocObj("insured_"+i+"_heightFT")).value)|| isNaN(eval(getDocObj("insured_"+i+"_heightFT")).value) || parseInt(eval(getDocObj("insured_"+i+"_heightFT")).value)>7) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"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")).value="";			
			eval(getDocObj("insured_"+i+"_heightFT")).focus();
			pu=true;
			return false;
		}
		//height in inches must be selected
		if(eval(getDocObj("insured_"+i+"_heightIN")).value=="" || !isIntegerChild(eval(getDocObj("insured_"+i+"_heightIN")).value) || isNaN(eval(getDocObj("insured_"+i+"_heightIN")).value) || parseInt(eval(getDocObj("insured_"+i+"_heightIN")).value)>11) {
			alert("Please select your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"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")).value="";			
			eval(getDocObj("insured_"+i+"_heightIN")).focus();
			pu=true;
			return false;
		}
		//weight must be a number greater than 1
		if(eval(getDocObj("insured_"+i+"_weight")).value=="" || !isIntegerChild(eval(getDocObj("insured_"+i+"_weight")).value) ||isNaN(eval(getDocObj("insured_"+i+"_weight")).value) || parseInt(eval(getDocObj("insured_"+i+"_weight")).value)<1) {
			alert("Please enter your "+(i-2)+((i==3)?"st":(i==4)?"nd":(i==5)?"rd":(i==6||i==7)?"th":"th")+" child's weight in pounds.");
			eval(getDocObj("insured_"+i+"_weight")).value="";			
			eval(getDocObj("insured_"+i+"_weight")).focus();
			pu=true;
			return false;
		}
		//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)?"rd":(i==6||i==7)?"th":"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)?"rd":(i==6||i==7)?"th":"th")+" child's height and weight.");
			eval(getDocObj("insured_"+i+"_weight")).value="";			
			eval(getDocObj("insured_"+i+"_weight")).focus();
			pu=true;
			return false;
		}
	}
	return true;
}
// Get the input Name or Id
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+"')";
	}
}


// Fill DropDown
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;
		}
	}
}
// Add Child
function addChild() {
	if(children<6) {
		children++;
		document.getElementById('child'+children).style.display='';
		if(children==6)
			document.images['add-child'].src=add_child_gray.src;
	}
}

// Delete Child
function deleteChild(x) {
	for(i=(x+2);i<(children+2);i++) {
		document.getElementById('insured_'+i+'_gender_male').checked=document.getElementById('insured_'+(i*1+1)+'_gender_male').checked;
		document.getElementById('insured_'+i+'_gender_female').checked=document.getElementById('insured_'+(i*1+1)+'_gender_female').checked;
		document.getElementById('insured_'+i+'_dobMM').value=document.getElementById('insured_'+(i*1+1)+'_dobMM').value;
		document.getElementById('insured_'+i+'_dobDD').value=document.getElementById('insured_'+(i*1+1)+'_dobDD').value;
		document.getElementById('insured_'+i+'_dobYYYY').value=document.getElementById('insured_'+(i*1+1)+'_dobYYYY').value;
		document.getElementById('insured_'+i+'_heightFT').value=document.getElementById('insured_'+(i*1+1)+'_heightFT').value;
		document.getElementById('insured_'+i+'_heightIN').value=document.getElementById('insured_'+(i*1+1)+'_heightIN').value;
		document.getElementById('insured_'+i+'_weight').value=document.getElementById('insured_'+(i*1+1)+'_weight').value;
	}
	
	document.getElementById('child'+children).style.display='none';
	
	document.getElementById('insured_'+(children*1+2)+'_gender_male').checked=false;
	document.getElementById('insured_'+(children*1+2)+'_gender_female').checked=false;
	document.getElementById('insured_'+(children*1+2)+'_dobMM').value='';
	document.getElementById('insured_'+(children*1+2)+'_dobDD').value='';
	document.getElementById('insured_'+(children*1+2)+'_dobYYYY').value='';
	document.getElementById('insured_'+(children*1+2)+'_heightFT').value='';
	document.getElementById('insured_'+(children*1+2)+'_heightIN').value='';
	document.getElementById('insured_'+(children*1+2)+'_weight').value='';
	
	if(children==6)
		document.images['add-child'].src=add_child.src;
	
	children--;
	
	document.getElementById('body').style.height="0px";
}

//Change Child
function changeChildren(value,form) {
	for(var i=8;i>parseInt(value)+2;i--) {
		fillDropdown("insured_"+i+"_gender","");
		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")).value="";
		eval(getDocObj("insured_"+i+"_smoker")).checked=false;
	}

	if(value==1) {
		eval(getDocObj("child_option")).style.display="";
		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";
	} else if(value==2) {
		eval(getDocObj("child_option")).style.display="";
		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";
	} else if(value==3) {
		eval(getDocObj("child_option")).style.display="";
		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";
	} else if(value==4) {
		eval(getDocObj("child_option")).style.display="";
		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";
	} else if(value==5) {
		eval(getDocObj("child_option")).style.display="";
		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";
	} else if(value==6) {
		eval(getDocObj("child_option")).style.display="";
		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="";
	} else {
		eval(getDocObj("child_option")).style.display="none";
		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";
	}
}
// Checking for No Numbers
function noNumChild(s){
	for(var i = 0; i < s.length; i++)
		if(!isNaN(s.charAt(i)) && s.charAt(i)!=" ")
			return false;
	return true;
}

// Checking for Integer Numbers
function isIntegerChild(s){
	return (s.toString().search(/^[0-9]+$/)==0);
}

//Checks if BMI is valid, returns true if BMI is not valid
function bmiChild(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;
}

//check for Space 
function hasSpaceChild(s) {
	for(var i=0;i<s.length;i++)
		if(s.charAt(i)==" ")
			return true;
	return false;
}

// End Child Profile

// About You Profile Form Begin
function AboutYou(form){	

	//First name cannot be blank
	if(form.first_name.value=="") {
		alert("Please enter your first name.");
		form.first_name.focus();
		pu=true;
		return false;
	}
	//First name cannot contain a number
	if(!noNum(form.first_name.value) || isInteger(form.first_name.value) || !fnameBadChars(form.first_name.value)) {
		alert("Your first name appears to have been entered incorrectly. Please re-enter your first name.");
		form.first_name.focus();
		return false;
	}
	//Last name cannot be blank
	if(form.last_name.length < 1 || form.last_name.value=="") {
		alert("Please enter your last name.");
		form.last_name.focus();
		pu=true;
		return false;
	}
	//Last name cannot contain a number
	if(!noNum(form.last_name.value) || !lnameBadChars(form.last_name.value) || isInteger(form.last_name.value)) {
		alert("Your last name appears to have been entered incorrectly. Please re-enter your last name.");
		form.last_name.focus();
		pu=true;
		return false;
	}	
	//Street address must contain at least 1 letter and 1 number
	if(form.address_1_street1.value=="" || !validAddress(form.address_1_street1.value)  || !addressBadChars(form.address_1_street1.value)) {
		alert("Your address appears to have been entered incorrectly. Please re-enter your address, including your street name and number, and avoid using any of the following characters:<br/>! $ %  / ^  * + = { } | < > ` ~ & ( ) { } [ ] \\ | : ; / _ ");
		form.address_1_street1.focus();
		pu=true;
		return false;
	}
	//City cannot be blank
	if(form.address_1_city.value=="" || (form.address_1_city.value.length <= 2)) {
		alert("Please enter the name of your city or town.");
		form.address_1_city.focus();
		pu=true;
		return false;
	}
	//City cannot contain a number
	if(!noNum(form.address_1_city.value) || isInteger(form.address_1_city.value) || !CityBadChars(form.address_1_city.value)) {
		alert("The name of your city appears to have been entered incorrectly. Please re-enter the name of your city or town.");
		form.address_1_city.focus();
		pu=true;
		return false;
	}		
	//State must be selected
	if(form.address_1_state.value=="") {
		alert("Please select the state you live in.");
		form.address_1_state.focus();
		pu=true;
		return false;
	}
	//Zip must be 5 numbers
	if(form.address_1_zip.value=="" || !isInteger(form.address_1_zip.value) || isNaN(form.address_1_zip.value) || form.address_1_zip.value.length != 5) {
		alert("Your Zip code appears to have been entered incorrectly. Please re-enter your Zip code.");
		form.address_1_zip.focus();
		pu=true;
		return false;
	}
	
	// Check Phone number -- 1st text box --
	if(form.phone1_1.value.length<3 || isNaN(form.phone1_1.value) || !isInteger(form.phone1_1.value)){
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");
		document.getElementById('phone1_1').focus();
		pu=true;
		return false;	
	}
	
	// Check phone number -- 2nd textbox --
	if(form.phone1_2.value.length<3 || isNaN(form.phone1_2.value) || !isInteger(form.phone1_2.value)){
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");				
		document.getElementById('phone1_2').focus();
		pu=true;
		return false;	
	}
	
	//check phone number --3rd text box--
	if(form.phone1_3.value.length<4 || isNaN(form.phone1_3.value) || !isInteger(form.phone1_3.value)){
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");
		document.getElementById('phone1_3').focus();
		pu=true;
		return false;	
	}
	
	document.getElementById('phone').value=form.phone1_1.value+form.phone1_2.value+form.phone1_3.value;
	
	var stripped=form.phone.value.replace(/[\(\)\.\-\ ]/g,'');

	//Phone number must be all numbers
	if(isNaN(stripped) || stripped.length!=10) {
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");
		document.getElementById('phone1_1').focus();
		pu=true;
		return false;
	}
	if(form.email.value == "" || form.email.value == null){
		alert("Please enter your Email address.");
		form.email.focus();
		return false;
	}

	if(validEmail(form.email.value)==false){
		alert("Your email address appears to have been entered incorrectly. Please re-enter your email address, using the format \"name@domain.com\"");
		form.email.focus();
		return false;
	}
 return true;	
}

//check for email
function validEmail(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	// check for email not to start with . EX. .name
	if (str.indexOf(at)==-1){
	   return false;
	}
	
	// check that email does not start with @.
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	
	// check that email does not 
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){		
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }
	 
	 // checks that email does not end like name@.com.
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }
	 
	 // checks thast email does not end with '@' Ex. name.company@name
	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert('eight');		 
		return false;
	 }

	 return true;					
}
	
	/********** Phone2 its not currently used on individualhealthquotes forms **************//*
	if(form.phone2_1.value.length<3 || isNaN(form.phone2_1.value) || form.phone2_2.value.length<3 ||  isNaN(form.phone2_2.value) || form.phone2_3.value.length<4 || isNaN(form.phone2_3.value)){
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");
		document.getElementById('phone2_1').focus();
		return false;	
	}
	
	document.getElementById('phone2').value=form.phone2_1.value+form.phone2_2.value+form.phone2_3.value;
	
	stripped=form.phone2.value.replace(/[\(\)\.\-\ ]/g,'');

	//Phone number must be all numbers
	if(isNaN(stripped) || stripped.length!=10) {
		alert("Your phone number appears to have been entered incorrectly. Please re-enter your phone number.");
		document.getElementById('phone2_1').focus();
		return false;
	}
*//*******************************************************************************/		
	
	
// Create popups
var posx = 0;
var posy = 0;

// Create popups
function popup(s, event) {
	closeOthers(s);
	var e = event || window.event;
	var pos = getRelativeCoordinates(event, document.getElementById(s));
	document.getElementById(s).style.left=(posx*1+10)+"px";
	document.getElementById(s).style.top=(posy*1+10)+"px";
	document.getElementById(s).style.display='';
}

// Hide Divs or popups
var hideDiv;
function hide(s) {
	hideDiv = s;
	window.setTimeout('hidePopup()',1300);
}
// hide popUps
function hidePopup() {
	if(document.getElementById('address_1_state').style.display=='none') state();
	document.getElementById(hideDiv).style.display='none';
}
// close other 
function closeOthers(s) {
	if(s!="health-info") document.getElementById('health-info').style.display='none';
	if(s!="health-conditions") document.getElementById('health-conditions').style.display='none';
	if(s!="contact-info") { document.getElementById('contact-info').style.display='none'; if(document.getElementById('address_1_state').style.display=='none') state(); }
	if(s!="security-info") document.getElementById('security-info').style.display='none';
}
//hide state
function hideSelect() {
	document.getElementById('address_1_state').style.display='none';
}
//show state
function showSelect() {
	window.setTimeout('state()', 1300);
}
//display states
function state() {
	document.getElementById('address_1_state').style.display='';
}
// makes a popup
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();
}


//trim left and right spaces
function ltrim(s){return s.replace(/^\s*/,"");}

function rtrim(s){return s.replace(/\s*$/,"");}

function trim(s){return rtrim(ltrim(s));}
// trim left and right spaces.
//function ltrim(s){return s.replace(/^\s*/,"");}
//function rtrim(s){return s.replace(/\s*$/,"");}
//	function trim(s){return rtrim(ltrim(s));}

//check for numbers
function noNum(s){
	for(var i = 0; i < s.length; i++)
		if(!isNaN(s.charAt(i)) && s.charAt(i)!=" ")
			return false;
	return true;
}
//check for integer numbers
function isInteger(s){
	return (s.toString().search(/^[0-9]+$/)==0);
}

function validAddress(s) {
	var numbers = false;
	var letters = false;
	for(var i = 0; i < s.length; i++) {
		if(!isNaN(s.charAt(i)) && s.charAt(i)!=" ")
			numbers = true;
		else
			letters = true;
		if(numbers && letters)
			return true;
	}
	return false;	
}
//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;
}
// check for bad characters for first name, and city. allows only apost, hypens, periods.
function fnameBadChars(s){
	for(var i=0;i<s.length;i++){		
		if(s.charAt(i)=="!" || s.charAt(i)=="#"  || s.charAt(i)=="$" || s.charAt(i)=="%" || s.charAt(i)=="%" || s.charAt(i)=="^" || s.charAt(i)=="*" || s.charAt(i)=="+" || s.charAt(i)=="=" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="|" || s.charAt(i)=="<" || s.charAt(i)==">" || s.charAt(i)=="`" || s.charAt(i)=="~" || s.charAt(i)=="&" || s.charAt(i)=="(" || s.charAt(i)==")" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="[" || s.charAt(i)=="]" || s.charAt(i)=="\\" || s.charAt(i)=="|" || s.charAt(i)=="\"" || s.charAt(i)==":" || s.charAt(i)==";" || s.charAt(i)=="/" || s.charAt(i)=="_" || s.charAt(i)==",")
			return false;
	}
	return true;
}
// check for bad characters for first name, and city. allows only apost, hypens, periods.
function CityBadChars(s){
	for(var i=0;i<s.length;i++){		
		if(s.charAt(i)=="!" || s.charAt(i)=="#"  || s.charAt(i)=="$" || s.charAt(i)=="%" || s.charAt(i)=="%" || s.charAt(i)=="^" || s.charAt(i)=="*" || s.charAt(i)=="+" || s.charAt(i)=="=" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="|" || s.charAt(i)=="<" || s.charAt(i)==">" || s.charAt(i)=="`" || s.charAt(i)=="~" || s.charAt(i)=="&" || s.charAt(i)=="(" || s.charAt(i)==")" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="[" || s.charAt(i)=="]" || s.charAt(i)=="\\" || s.charAt(i)=="|" || s.charAt(i)=="\"" || s.charAt(i)==":" || s.charAt(i)==";" || s.charAt(i)=="/" || s.charAt(i)=="_" || s.charAt(i)=="," || s.charAt(i)==".")
			return false;
	}
	return true;
}
//check for bad characters on address field allows only apost, hypens, periods, pound, comma.
function addressBadChars(s){
	for(var i=0;i<s.length;i++){		
		if(s.charAt(i)=="!" || s.charAt(i)=="$" || s.charAt(i)=="%" || s.charAt(i)=="%" || s.charAt(i)=="^" || s.charAt(i)=="*" || s.charAt(i)=="+" || s.charAt(i)=="=" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="|" || s.charAt(i)=="<" || s.charAt(i)==">" || s.charAt(i)=="`" || s.charAt(i)=="~" || s.charAt(i)=="&" || s.charAt(i)=="(" || s.charAt(i)==")" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="[" || s.charAt(i)=="]" || s.charAt(i)=="\\" || s.charAt(i)=="|" || s.charAt(i)=="\"" || s.charAt(i)==":" || s.charAt(i)==";" || s.charAt(i)=="/"  || s.charAt(i)=="_")
			return false;
	}
	return true;
}
// check for bad characters for last name. allows only apost, hypens, periods, and commas.
function lnameBadChars(s){
	for(var i=0;i<s.length;i++){		
		if(s.charAt(i)=="!" || s.charAt(i)=="#"  || s.charAt(i)=="$" || s.charAt(i)=="%" || s.charAt(i)=="%" || s.charAt(i)=="^" || s.charAt(i)=="*" || s.charAt(i)=="+" || s.charAt(i)=="=" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="|" || s.charAt(i)=="<" || s.charAt(i)==">" || s.charAt(i)=="`" || s.charAt(i)=="~" || s.charAt(i)=="&" || s.charAt(i)=="(" || s.charAt(i)==")" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="[" || s.charAt(i)=="]" || s.charAt(i)=="\\" || s.charAt(i)=="|" || s.charAt(i)=="\"" || s.charAt(i)==":" || s.charAt(i)==";" || s.charAt(i)=="/" || s.charAt(i)=="_")
			return false;
	}
	return true;
}
//check for bad characters on fields.
function badChars(s){
	for(var i=0;i<s.length;i++){		
		if(s.charAt(i)=="!" || s.charAt(i)=="#"  || s.charAt(i)=="$" || s.charAt(i)=="%" || s.charAt(i)=="%" || s.charAt(i)=="^" || s.charAt(i)=="*" || s.charAt(i)=="+" || s.charAt(i)=="=" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="|" || s.charAt(i)=="<" || s.charAt(i)==">" || s.charAt(i)=="`" || s.charAt(i)=="~" || s.charAt(i)=="&" || s.charAt(i)=="(" || s.charAt(i)==")" || s.charAt(i)=="{" || s.charAt(i)=="}" || s.charAt(i)=="[" || s.charAt(i)=="]" || s.charAt(i)=="\\" || s.charAt(i)=="|" || s.charAt(i)=="\"" || s.charAt(i)=="'" || s.charAt(i)==":" || s.charAt(i)==";" || s.charAt(i)=="/" || s.charAt(i)=="-"  || s.charAt(i)=="_" || s.charAt(i)==",")
			return false;
	}
	return true;
}
//check for space
function hasSpace(s) {
	for(var i=0;i<s.length;i++)
		if(s.charAt(i)==" ")
			return true;
	return false;
}
// Show/hide comments
function showHidden(id,value) {
	if(value=="yes")
		document.getElementById(id).style.display='block';
	else
		document.getElementById(id).style.display='none';
}
// Show/hide comments
function displayCommentBox(id,value) {
	if(value)
		document.getElementById(id).style.display='block';
	else
		document.getElementById(id).style.display='none';
}
// hides Popups 
function noPopUp() {
	pu=false;
}
// Shows Popup
function sh_popup() {
	if(pu) {
		var windowReference = window.open('opener.html','survey','width=1,height=1');		
		windowReference.focus();
	}
}
// About You End

// Submit button function
// Checked Form Validation on submit button
function checkSubmitUF(form){
	
	return (HealthProfile(form) && Spouse(form) && Children(form) && AboutYou(form));
}