
function noSpaces(str) {
	return str.replace(/\s*/g, '')
}
function validate2(e){
  $('line_index').value = FamilyInfo.line_index;
  $('num_child').value = FamilyInfo.child_numb;
  var keycode;
	if (e.keyCode){
		code = e.keyCode;
	}
	else if (e.which){
		code = e.which;
	}
	if(code == 13 && FamilyInfo.validate()) {
		document.frmAffIndQuote.submit()
	}
}

function age_in_years(dob_dd,dob_mm,dob_yy){
var today=new Date()
if(dob_yy!=''){
if(dob_mm>=today.getMonth() && dob_dd>=today.getDate()){
	var age=today.getFullYear()-dob_yy
	}
	else{
	var age=today.getFullYear()-dob_yy-1
	}
	return age
	}
	else{
	return
	}
	
}

function validate_gender(gender_index){
	if(document.getElementById('gender_'+gender_index).value=="M") {
		document.getElementById('pregnancy_'+gender_index).checked=false;
		document.getElementById('pregnancy_'+gender_index).disabled=true;
	}
	else {
		document.getElementById('pregnancy_'+gender_index).disabled=false;
	}
}

var genderIndex = new Object()
genderIndex['M'] = 1
genderIndex['F'] = 2

var feetIndex = new Object()
feetIndex['4'] = 1
feetIndex['5'] = 2
feetIndex['6'] = 3
feetIndex['7'] = 4

var feetIndex2 = new Object()
feetIndex2['0'] = 1
feetIndex2['1'] = 2
feetIndex2['2'] = 3
feetIndex2['3'] = 4
feetIndex2['4'] = 5
feetIndex2['5'] = 6
feetIndex2['6'] = 7
feetIndex2['7'] = 8
feetIndex2['8'] = 9

var FamilyInfo = {
	line_index:    0,
	child_numb:    0,
	xml:           '<?xml version="1.0"?><thrive></thrive>',
	
spouse_row:	'<input type=\'hidden\' id="relation_FID" name="relation_FID" value="spouse">\n' +
	'<fieldset class="form-wht" id="family_info_FID">\n' +
'<div style="float: left; width: 100%;">\n' +
	'<h4 class="user">Spouse</h4>\n' +
	'<a class="close" href="javascript:FamilyInfo.delete_line(\'FID\', \'spouse\')" id="">Remove</a>\n' +
'</div>\n' +
'<ul class="formfilled" style="margin-right:-3px;">\n' +
	'<li>Gender \n' + '<br/>\n' +
	'<span> \n' +
		'<select name="spouse_gender" id="gender_FID">\n' +
				'<option  style="font-size: 9pt;" value="">-&nbsp;-</option>\n' +
				'<option  style="font-size: 9pt;" value="M">Male</option>\n' +
	    		'<option  style="font-size: 9pt;" value="F">Female</option>\n' +
    	'</select>\n' +
		'<input type="hidden" name="spouse_gender_required" value="Please select your spouse\'s gender">\n' +
    '</span>\n' +
	'</li>\n' +
	'<li>Date of Birth\n' + '<br/>\n' +
		'<span>\n' +
			'<input  style="font-size: 9pt;" size="3" maxlength="2" type="text" class="text" name="spouse_dob_mm" id="id_mm_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="mm">\n' +
			'<input  style="font-size: 9pt;"  size="2" maxlength="2" type="text" class="text" name="spouse_dob_dd" id="id_dd_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="dd">\n' +
			'<input  style="font-size: 9pt;"  size="4" maxlength="4" type="text" class="text" name="spouse_dob_yy" id="id_yy_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="yyyy">\n' +
			
			'<input type="hidden" name="spouse_dob_mm_required" value="Please enter spouse\'s date of birth month">\n' +
			'<input type="hidden" name="spouse_dob_dd_required" value="Please enter spouse\'s date of birth day">\n' +
			'<input type="hidden" name="spouse_dob_yy_required" value="Please enter spouse\'s date of birth year">\n' +
			
			'<input type="hidden" name="spouse_dob_mm_isInteger" value="Invalid spouse\'s date of birth month">\n' +
			'<input type="hidden" name="spouse_dob_dd_isInteger" value="Invalid spouse\'s date of birth day">\n' +
			'<input type="hidden" name="spouse_dob_yy_isInteger" value="Invalid spouse\'s date of birth year">\n' +
			
		'</span>\n' +
	'</li>\n' +
	'<li>Height\n' + '<br/>\n' +
		'<span>\n' +
			'<select name="spouse_heightFT" id="feet_FID">\n' + '<br/>\n' +
				'<option  style="font-size: 9pt;" value="">ft</option>\n' +
				'<option  style="font-size: 9pt;" value="4">4</option>\n' +
			    '<option  style="font-size: 9pt;" value="5">5</option>\n' +
			    '<option  style="font-size: 9pt;" value="6">6</option>\n' +
			    '<option  style="font-size: 9pt;" value="7">7</option>\n' +
			'</select>\n' +
			'<input type="hidden" name="spouse_heightFT_required" value="Please enter spouse\'s height in feet">\n' +
	   	'</span>\n' +
	   	'<span>\n' +
			'<select name="spouse_heightIN" id="inches_FID">\n' + '<br/>\n' +
				'<option  style="font-size: 9pt;" value="">in</option>\n' +
				'<option  style="font-size: 9pt;" value="0">0</option>\n' +
			    '<option  style="font-size: 9pt;" value="1">1</option>\n' +
			    '<option  style="font-size: 9pt;" value="2">2</option>\n' +
			    '<option  style="font-size: 9pt;" value="3">3</option>\n' +
			    '<option  style="font-size: 9pt;" value="4">4</option>\n' +
			    '<option  style="font-size: 9pt;" value="5">5</option>\n' +
			    '<option  style="font-size: 9pt;" value="6">6</option>\n' +
			    '<option  style="font-size: 9pt;" value="7">7</option>\n' +
			    '<option  style="font-size: 9pt;" value="8">8</option>\n' +
			    '<option  style="font-size: 9pt;" value="9">9</option>\n' +
			    '<option  style="font-size: 9pt;" value="10">10</option>\n' +
			    '<option  style="font-size: 9pt;" value="11">11</option>\n' +
			 '</select>\n' +
			'<input type="hidden" name="spouse_heightIN_required" value="Please enter spouse\'s height in inches">\n' +
		 '</span>\n' +
	 '</li>\n' +
	 '<li>Weight\n' + '<br/>\n' +
		 '<span>\n' +
			'<input  style="font-size: 9pt;"  size="3"  class="text" maxlength="3" type="text" name="spouse_weight" id="weight_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="lb.">\n' +
			'<input type="hidden" name="spouse_weight_required" value="Please enter spouse\'s weight">\n' +
			'<input type="hidden" name="spouse_weight_isInteger" value="Please enter a number for spouse\'s weight">\n' +
	 	'</span>\n' +
	 '</li>\n' +
     '<li>Smoker?\n' + '<br/>\n' +
	  	'<span>\n' +
		 	'<input type="checkbox" name="spouse_smoker" id="use_tobacco_FID" value="1">\n' +
	 	'</span>\n' +
	 '</li>\n' +
     '<li class="hrow">\n' +
'</ul>\n' +
'</fieldset>\n',
			    
	children_row: '<input type=\'hidden\' id="relation_FID" name="relation_FID" value="children">\n' +
 '<fieldset class="form-wht" id="family_info_FID">\n' +
'<div style="float: left; width: 100%;">\n' +
	'<h4 class="user" style="float: left;">Child</h4>\n' +
	'<a class="close" href="javascript:FamilyInfo.delete_line(\'FID\', \'child\')" id="">Remove</a>\n' +
'</div>\n' +
'<ul class="formfilled" style="margin-right:-3px;">\n' +
	'<li>Gender \n' + '<br/>\n' +
	'<span> \n' +
		'<select name="child_CN_gender" id="gender_FID">\n' +
				'<option  style="font-size: 9pt;" value="">-&nbsp;-</option>\n' +
				'<option  style="font-size: 9pt;" value="M">Male</option>\n' +
	    		'<option  style="font-size: 9pt;" value="F">Female</option>\n' +
    	'</select>\n' +
		'<input type="hidden" name="child_CN_gender_required" value="Please select your child\'s gender">\n' +
    '</span>\n' +
	'</li>\n' +
	'<li>Date of Birth\n' + '<br/>\n' +
		'<span>\n' +
			'<input   style="font-size: 9pt;" size="3" maxlength="2" type="text" class="text" name="child_CN_dob_mm" id="id_mm_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="mm">\n' +
			'<input   style="font-size: 9pt;" size="2" maxlength="2" type="text" class="text" name="child_CN_dob_dd" id="id_dd_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="dd">\n' +
			'<input   style="font-size: 9pt;" size="4" maxlength="4" type="text" class="text" name="child_CN_dob_yy" id="id_yy_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="yyyy">\n' +
			
			'<input type="hidden" name="child_CN_dob_mm_required" value="Please enter child\'s date of birth month">\n' +
			'<input type="hidden" name="child_CN_dob_dd_required" value="Please enter child\'s date of birth day">\n' +
			'<input type="hidden" name="child_CN_dob_yy_required" value="Please enter child\'s date of birth year">\n' +
			
			'<input type="hidden" name="child_CN_dob_mm_isInteger" value="Invalid child\'s date of birth month">\n' +
			'<input type="hidden" name="child_CN_dob_dd_isInteger" value="Invalid child\'s date of birth day">\n' +
			'<input type="hidden" name="child_CN_dob_yy_isInteger" value="Invalid child\'s date of birth year">\n' +
			
		'</span>\n' +
	'</li>\n' +
	'<li>Height\n' + '<br/>\n' +
		'<span>\n' +
   		   '<select name="child_CN_height" id="feet_FID">\n' +
			'<option  style="font-size: 9pt;" value="">ft</option>\n' +
			'<option  style="font-size: 9pt;" value="0">0</option>\n' +
			'<option  style="font-size: 9pt;" value="1">1</option>\n' +
			'<option  style="font-size: 9pt;" value="2">2</option>\n' +
			'<option  style="font-size: 9pt;" value="3">3</option>\n' +
			'<option  style="font-size: 9pt;" value="4">4</option>\n' +
	 		'<option  style="font-size: 9pt;" value="5">5</option>\n' +
	 		'<option  style="font-size: 9pt;" value="6">6</option>\n' +
	 		'<option  style="font-size: 9pt;" value="7">7</option>\n' +
	   	  '</select>\n' +
			'<input type="hidden" name="child_CN_height_required" value="Please enter child\'s height in feet">\n' +
		'</span>\n' +
	   	'<span>\n' +
			'<select name="child_CN_heightIN" id="inches_FID">\n' +
				'<option  style="font-size: 9pt;" value="">in</option>\n' +
				'<option  style="font-size: 9pt;" value="0">0</option>\n' +
			    '<option  style="font-size: 9pt;" value="1">1</option>\n' +
			    '<option  style="font-size: 9pt;" value="2">2</option>\n' +
			    '<option  style="font-size: 9pt;" value="3">3</option>\n' +
			    '<option  style="font-size: 9pt;" value="4">4</option>\n' +
			    '<option  style="font-size: 9pt;" value="5">5</option>\n' +
			    '<option  style="font-size: 9pt;" value="6">6</option>\n' +
			    '<option  style="font-size: 9pt;" value="7">7</option>\n' +
			    '<option  style="font-size: 9pt;" value="8">8</option>\n' +
			    '<option  style="font-size: 9pt;" value="9">9</option>\n' +
			    '<option  style="font-size: 9pt;" value="10">10</option>\n' +
			    '<option  style="font-size: 9pt;" value="11">11</option>\n' +
			 '</select>\n' +
			'<input type="hidden" name="child_CN_heightIN_required" value="Please enter child\'s height in inches">\n' +
		 '</span>\n' +
	 '</li>\n' +
	 '<li>Weight\n' + '<br/>\n' +
		 '<span>\n' +
			'<input   style="font-size: 9pt;" size="3"  class="text" maxlength="3" type="text" name="child_CN_weight" id="weight_FID" onclick="this.value=\'\';" onfocus="this.value=\'\'" value="lb.">\n' +
			'<input type="hidden" name="child_CN_weight_required" value="Please enter child\'s weight">\n' +
			'<input type="hidden" name="child_CN_weight_isInteger" value="Please enter a number for child\'s weight">\n' +
	 	'</span>\n' +
	 '</li>\n' +
     '<li>Smoker?\n' + '<br/>\n' +
	  	'<span>\n' +
		 	'<input type="checkbox" name="child_CN_smoker" id="use_tobacco_FID" value="1">\n' +
	 	'</span>\n' +
	 '</li>\n' +
'</ul>\n' +
'</fieldset>\n',

	row_style:     'resultrow1',
	
 				   
	add_spouse: function(gender, dob_mm,dob_dd,dob_yy, height, weight, use_tobacco) {
						this.line_index++
						var row = this.spouse_row 
						row = row.replace(/FID/g, this.line_index)
						new Insertion.Bottom($('family_info'), row)
						
						if((gender.toLowerCase())=='male') gender = 'M';
						if((gender.toLowerCase())=='female') gender = 'F';
						$('gender_' + this.line_index).selectedIndex = genderIndex[gender];
						
						/*$('dob_mm_' + this.line_index).value = noSpaces(dob_mm);
						$('dob_dd_' + this.line_index).value = noSpaces(dob_dd);
						$('dob_yy_' + this.line_index).value = noSpaces(dob_yy);*/
						var ft='',ic='';
						if(height != ''){
							heightArray = height.split(/\./);
							ft = heightArray[0];
							ic = heightArray[1];
						}
						$('feet_' + this.line_index).selectedIndex = feetIndex[ft];
						$('inches_' + this.line_index).selectedIndex = parseInt(ic)+1;
						
						$('weight_' + this.line_index).value = noSpaces(weight)
						
						$('use_tobacco_' + this.line_index).checked = (use_tobacco == 1 ? true : false) 
						
						if($(('spouse_cell_id'))){
							document.getElementById('spouse_cell_id').innerHTML = ""
						}
  				   },
   				   
 	add_children: function(gender, dob_mm,dob_dd,dob_yy, height, weight, use_tobacco) {
						this.line_index++;
						this.child_numb++;
						var row = this.children_row 
						row = row.replace(/FID/g, this.line_index);
						row = row.replace(/CN/g, this.child_numb)
						
						new Insertion.Bottom($('family_info'), row)
						
						if((gender.toLowerCase())=='male') gender = 'M';
						if((gender.toLowerCase())=='female') gender = 'F';
						$('gender_' + this.line_index).selectedIndex = genderIndex[gender];
						
						/*$('dob_mm_'+this.line_index).value = dob_mm;
						$('dob_dd_'+this.line_index).value = dob_dd;
						$('dob_yy_'+this.line_index).value = dob_yy;*/
						
						var ft='',ic='';
						if(height != ''){
							heightArray = height.split(/\./);
							ft = heightArray[0];
							ic = heightArray[1];
						}
						$('feet_' + this.line_index).selectedIndex = feetIndex2[ft];
						$('inches_' + this.line_index).selectedIndex = parseInt(ic)+1;
						
						$('weight_' + this.line_index).value = noSpaces(weight)
						
						$('use_tobacco_' + this.line_index).checked = (use_tobacco == 1 ? true : false) 
						

  				   },
   				   			   
 	validate:      function(){
					var msg = "";
					var validZipChars = "0123456789";
					var ZipCode = $('ZipCode').value;
					if (ZipCode.length !=5) {
						msg += "Please enter your 5 digit zip code.\n";
					}else{
						var temp = "";
						for (var i=0; i < ZipCode.length; i++) {
							temp = "" + ZipCode.substring(i, i+1);
							if (validZipChars.indexOf(temp) == "-1") {
								msg += "Invalid characters in your zip code.  Please try again.";
							}
						}
					}
					var fields = new Array();
					fields[0] = "relation";
					fields[1] = "dob";
					fields[2] = "gender";
					fields[3] = "height";
					fields[4] = "weight";
					var childCount=0;
					
					for(i=1;i<= this.line_index;i++){
					if($('relation_' + i)){
				      var rel = ($('relation_' + i).value == 'children')?'child':$('relation_' + i).value;
				      if($('relation_' + i).value == 'children'){
							childCount++;
							rel=rel+""+childCount;
						}
				      for(j=0;j <= 4;j++){
					     var field_value = ''
					     
					     	 if(fields[j] != 'dob' && fields[j] != 'height') {
						     	if($(fields[j]+'_' + i)) {
							    	field_value=$(fields[j]+'_' + i).value;
							     	if(fields[j] == 'gender'){
								    	if(field_value == ''){
								     		if($('relation_' + i).value == 'children'){
				      							msg += "Please enter "+rel+"'s Gender.\n";
				      						}
				      						else{ 
				      							msg += "Please enter "+rel+"'s Gender.\n";
				      						}
								     	}
							     	}
							     	if(fields[j] == 'weight'){
								    	if(field_value == ''){
								     		if($('relation_' + i).value == 'children'){
				      							msg += "Please enter "+rel+"'s Weight.\n";
				      						}
				      						else{ 
				      							msg += "Please enter "+rel+"'s Weight.\n";
				      						}
				      					}
				      					else if(isNaN($(fields[j]+'_' + i).value)||($(fields[j]+'_' + i).value <= 0)){
											if($('relation_' + i).value == 'children'){
					      						msg += "Please enter valid "+rel+"'s Weight.\n";
					      					}
					      					else{ 
					      						msg += "Please enter a valid "+rel+"'s Weight.\n";
					      					}
										}
								    }
							     }
							}
					     else{
					     	if(fields[j] == 'dob'){
						        if($('dob_mm_' + i).value != '' && $('dob_dd_' + i).value != '' && $('dob_yy_' + i).value != ''){
						        var age=age_in_years($('dob_dd_' + i).value,$('dob_mm_' + i).value,$('dob_yy_' + i).value)
						        if((age>=64 || age<18) && $('relation_' + i).value != 'children'){
			      						msg += "Please ensure "+rel+"'s age is between 18 and 65.\n";
									}
								if(age>=24 && $('relation_' + i).value == 'children'){
									msg += "Please ensure dependent's age is less than 24.\n";
									}								
						        }
						        else
						        {
						        msg += "Please enter "+rel+"'s Date of Birth.\n";
						        }
							}
							if(fields[j] == 'height'){ 
						        if($('feet_' + i).value == ''){
									if($('relation_' + i).value == 'children'){
		      							msg += "Please enter "+rel+"'s Height(Feet).\n";
		      						}
		      						else{ 
		      							msg += "Please enter "+rel+"'s Height(Feet).\n";
		      						}
								}
					            else if(isNaN($('feet_' + i).value)||(($('feet_' + i).value==0)&&($('inches_' + i).value==0))){
									if($('relation_' + i).value == 'children'){
			      						if(($('feet_' + i).value==0)&&($('inches_' + i).value==0)){
			      							msg += "Please enter valid "+rel+"'s Height.\n";
			      						}
			      						else{
			      							msg += "Please enter valid "+rel+"'s Height(Feet).\n";
			      						}
			      						
			      					}
			      					else{ 
			      						msg += "Please enter a valid "+rel+"'s Height(Feet).\n";
			      					}
								}
					            
					            if($('inches_' + i).value == ''){
									if($('relation_' + i).value == 'children'){
		      							msg += "Please enter "+rel+"'s Height(Inch).\n";
		      						}
		      						else{ 
		      							msg += "Please enter "+rel+"'s Height(Inch).\n";
		      						}
		      					}
		      					else if(isNaN($('inches_' + i).value)){
									if($('relation_' + i).value == 'children'){
			      						msg += "Please enter valid "+rel+"'s Height(Inch).\n";
			      					}
			      					else{ 
			      						msg += "Please enter a valid "+rel+"'s Height(Inch).\n";
			      					}
								}
							 }
					     }
					  }
					}
				   }
					if(msg != ''){alert(msg);return false;}
					return true;
				}, 
 
 	delete_line: function(index, member_type){
    				if(confirm('Are you sure you want to delete this record ?')){
						Element.remove('family_info_'+index)
						Element.remove('relation_'+index);
						if(member_type == 'spouse'){
							document.getElementById('spouse_cell_id').innerHTML = '<a class="button btn-addspouse" href="javascript:FamilyInfo.add_spouse(\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\');" style="left:0;"><span style="display:none">Add Spouse</span></a>'
						}
					}
				}    			   

}
			
