// Health Form Validation



var healthFormValidation = function() {
	var runValidation = function(frmvalidator) {
		// form name
		var form = document.forms[frmvalidator.formobj._sfm_form_name];
		
		// validate gender
		frmvalidator.addValidation("insured_1_gender","selone_radio",SEL_GENDER);

		//Validate DOB month
		frmvalidator.addValidation("insured_1_dobMM","req", DOB_MM_REQ);
		frmvalidator.addValidation("insured_1_dobMM","numeric", DOB_MM_NUMERIC);
		frmvalidator.addValidation("insured_1_dobMM","lt=13",DOB_MM_LESS_THAN);
		frmvalidator.addValidation("insured_1_dobMM","gt=0",DOB_MM_GREATER_THAN);	
					
		//Validate DOB day
		frmvalidator.addValidation("insured_1_dobDD","req",DOB_DD_REQ);
		frmvalidator.addValidation("insured_1_dobDD","numeric", DOB_DD_NUMERIC);
		frmvalidator.addValidation("insured_1_dobDD","lt=32",DOB_DD_LESS_THAN);
		frmvalidator.addValidation("insured_1_dobDD","gt=0",DOB_DD_GREATER_THAN);
			
		//Validate DOB year
		frmvalidator.addValidation("insured_1_dobYYYY","req",DOB_YYYY_REQ);
		frmvalidator.addValidation("insured_1_dobYYYY","num",DOB_YYYY_NUMERIC);
		frmvalidator.addValidation("insured_1_dobYYYY", "minlen=4", DOB_YYYY_MIN_LEN)
		frmvalidator.addValidation("insured_1_dobYYYY","gt="+(d.getFullYear()-101),DOB_YYYY_GREATER_THAN);	
		frmvalidator.addValidation("insured_1_dobYYYY","lt="+(d.getFullYear()+1),DOB_YYYY_LESS_THAN);	
			
		//Validate Height Feet
		frmvalidator.addValidation("insured_1_heightFT","req",HEIGHT_FT_REQ);
		frmvalidator.addValidation("insured_1_heightFT","num",HEIGHT_FT_NUMERIC);
		frmvalidator.addValidation("insured_1_heightFT","lt=8",HEIGHT_FT_LESS_THAN);
		frmvalidator.addValidation("insured_1_heightFT","gt=0",HEIGHT_FT_GREATER_THAN);
			
		//Validate Height inches
		frmvalidator.addValidation("insured_1_heightIN","req",HEIGHT_IN_REQ);
		frmvalidator.addValidation("insured_1_heightIN","num",HEIGHT_IN_NUMERIC);
		frmvalidator.addValidation("insured_1_heightIN","lt=12",HEIGHT_IN_LESS_THAN);
		frmvalidator.addValidation("insured_1_heightIN","gt=-1",HEIGHT_IN_GREATER_THAN);
			
		//Validate Weight
		frmvalidator.addValidation("insured_1_weight","req",WEIGHT_REQ);
		frmvalidator.addValidation("insured_1_weight","num",WEIGHT_NUMERIC);
		frmvalidator.addValidation("insured_1_weight","gt=0",WEIGHT_GREATER_THAN);
		
		//validate medical conditions
		frmvalidator.addValidation("insured_1_health_conditions","selone_radio",HEALTH_CONDITIONS_SEL);	
		if(form.insured_1_health_conditions[0].checked){
		
			var conditionsDetail=form.insured_1_health_conditions_detail.value;	
			
			//If have conditions was selected and no conditions were selected;
			if(trim(conditionsDetail).length<3 || conditionsDetail == "none selected") {
				frmvalidator.addValidation("insured_1_health_conditions_detail","req",HEALTH_CONDITIONS_REQ);			
			}
		}
		
		// Validate Spouse
		if(dependents.spouse()) {
			// Spouse's gender
			frmvalidator.addValidation("insured_2_gender","selone_radio", SPOUSE_SEL_GENDER);
				
			// Spouse's birth month
			frmvalidator.addValidation("insured_2_dobMM", "req", SPOUSE_DOB_MM_REQ);
			frmvalidator.addValidation("insured_2_dobMM", "num", SPOUSE_DOB_MM_NUMERIC);
			frmvalidator.addValidation("insured_2_dobMM", "gt=0", SPOUSE_DOB_MM_GREATER_THAN);
			frmvalidator.addValidation("insured_2_dobMM", "lt=13", SPOUSE_DOB_MM_LESS_THAN);
			
			// Spouse's birth day
			frmvalidator.addValidation("insured_2_dobDD", "req", SPOUSE_DOB_DD_REQ);
			frmvalidator.addValidation("insured_2_dobDD", "num", SPOUSE_DOB_DD_NUMERIC);
			frmvalidator.addValidation("insured_2_dobDD", "gt=0", SPOUSE_DOB_DD_GREATER_THAN);
			frmvalidator.addValidation("insured_2_dobDD", "lt=32", SPOUSE_DOB_DD_LESS_THAN);
				
			// Spouse's birth year
			frmvalidator.addValidation("insured_2_dobYYYY", "req", SPOUSE_DOB_YYYY_REQ);
			frmvalidator.addValidation("insured_2_dobYYYY", "num", SPOUSE_DOB_YYYY_NUMERIC);
			frmvalidator.addValidation("insured_2_dobYYYY", "minlength=4", SPOUSE_DOB_YYYY_MIN_LEN);
			frmvalidator.addValidation("insured_2_dobYYYY", "gt=" + (d.getFullYear()-101), SPOUSE_DOB_YYYY_GREATER_THAN);
			frmvalidator.addValidation("insured_2_dobYYYY", "lt=" + (d.getFullYear()+1), SPOUSE_DOB_YYYY_LESS_THAN);
				
			// Spouse's Height (Feet)
			frmvalidator.addValidation("insured_2_heightFT", "req", SPOUSE_HEIGHT_FT_REQ);
			frmvalidator.addValidation("insured_2_heightFT", "num", SPOUSE_HEIGHT_FT_NUMERIC);
			frmvalidator.addValidation("insured_2_heightFT","lt=8", SPOUSE_HEIGHT_FT_LESS_THAN);
			frmvalidator.addValidation("insured_2_heightFT","gt=0", SPOUSE_HEIGHT_FT_GREATER_THAN);
				
			// Spouse's Height (Weight)
			frmvalidator.addValidation("insured_2_heightIN", "req", SPOUSE_HEIGHT_IN_REQ);
			frmvalidator.addValidation("insured_2_heightIN", "num", SPOUSE_HEIGHT_IN_NUMERIC);
			frmvalidator.addValidation("insured_2_heightIN", "lt=12", SPOUSE_HEIGHT_IN_LESS_THAN);
			frmvalidator.addValidation("insured_2_heightIN", "gt=-1", SPOUSE_HEIGHT_IN_GREATER_THAN);
			
			// Spouse's Weight (in lbs)
			frmvalidator.addValidation("insured_2_weight", "req", SPOUSE_WEIGHT_REQ);
			frmvalidator.addValidation("insured_2_weight", "num", SPOUSE_WEIGHT_NUMERIC);
			frmvalidator.addValidation("insured_2_weight", "gt=0", SPOUSE_WEIGHT_GREATER_THAN);
			
			// Spouse smoker status
			frmvalidator.addValidation("insured_2_smoker","selone_radio",SPOUSE_SEL_SMOKING);
		}
		// Validate Children
		for(var j=1; j<=dependents.children(); j++) {
			var i = j + 2;
			// Child's gender
			frmvalidator.addValidation("insured_" + i + "_gender","selone_radio", CHILD_SEL_GENDER);
			
			// Child's birth month
			frmvalidator.addValidation("insured_" + i + "_dobMM", "req", CHILD_DOB_MM_REQ);
			frmvalidator.addValidation("insured_" + i + "_dobMM", "num", CHILD_DOB_MM_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_dobMM", "gt=0", CHILD_DOB_MM_GREATER_THAN);
			frmvalidator.addValidation("insured_" + i + "_dobMM", "lt=13", CHILD_DOB_MM_LESS_THAN);
	
			// Child's birth day
			frmvalidator.addValidation("insured_" + i + "_dobDD", "req", CHILD_DOB_DD_REQ);
			frmvalidator.addValidation("insured_" + i + "_dobDD", "num", CHILD_DOB_DD_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_dobDD", "gt=0", CHILD_DOB_DD_GREATER_THAN);
			frmvalidator.addValidation("insured_" + i + "_dobDD", "lt=32", CHILD_DOB_DD_LESS_THAN);
				
			// Child's birth year
			frmvalidator.addValidation("insured_" + i + "_dobYYYY", "req", CHILD_DOB_YYYY_REQ);
			frmvalidator.addValidation("insured_" + i + "_dobYYYY", "num", CHILD_DOB_YYYY_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_dobYYYY", "minlen=4", CHILD_DOB_YYYY_MIN_LEN);
			frmvalidator.addValidation("insured_" + i + "_dobYYYY", "gt="+(d.getFullYear()-101), CHILD_DOB_YYYY_GREATER_THAN);
			frmvalidator.addValidation("insured_" + i + "_dobYYYY", "lt="+(d.getFullYear()+1), CHILD_DOB_YYYY_LESS_THAN);				

			// Child's Height (Feet)
			frmvalidator.addValidation("insured_" + i + "_heightFT", "req", CHILD_HEIGHT_FT_REQ);
			frmvalidator.addValidation("insured_" + i + "_heightFT", "num", CHILD_HEIGHT_FT_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_heightFT","lt=8", CHILD_HEIGHT_FT_LESS_THAN);
			frmvalidator.addValidation("insured_" + i + "_heightFT","gt=0", CHILD_HEIGHT_FT_GREATER_THAN);
				
			//Child's Height (Weight)
			frmvalidator.addValidation("insured_" + i + "_heightIN", "req", CHILD_HEIGHT_IN_REQ);
			frmvalidator.addValidation("insured_" + i + "_heightIN", "num", CHILD_HEIGHT_IN_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_heightIN", "gt=-1", CHILD_HEIGHT_IN_GREATER_THAN);
			frmvalidator.addValidation("insured_" + i + "_heightIN", "lt=12", CHILD_HEIGHT_IN_LESS_THAN);
				
			// Child's Weight (in lbs)
			frmvalidator.addValidation("insured_" + i + "_weight", "req", CHILD_WEIGHT_REQ);
			frmvalidator.addValidation("insured_" + i + "_weight", "num", CHILD_WEIGHT_NUMERIC);
			frmvalidator.addValidation("insured_" + i + "_weight", "gt=0", CHILD_WEIGHT_GREATER_THAN);
		}
		
		// Validate First Name
		frmvalidator.addValidation("first_name","req",FIRST_NAME_REQ);
		frmvalidator.addValidation("first_name","maxlength=35",FIRST_NAME_MAX_LENGTH);
		frmvalidator.addValidation("first_name","name",FIRST_NAME_NAME);		
			
		// Validate Last Name
		frmvalidator.addValidation("last_name","req",LAST_NAME_REQ);
		frmvalidator.addValidation("last_name","maxlength=35",LAST_NAME_MAX_LENGTH);		
		frmvalidator.addValidation("last_name","name",LAST_NAME_NAME);
			
		// Validate Address
		frmvalidator.addValidation("address_1_street1","req",ADDRESS_STREET_REQ);		
		frmvalidator.addValidation("address_1_street1","address", ADDRESS_STREET_ADDRESS);
		
		// Validate city
		frmvalidator.addValidation("address_1_city","req",ADDRESS_CITY_REQ);
		frmvalidator.addValidation("address_1_city","city",ADDRESS_CITY_CITY);
		frmvalidator.addValidation("address_1_city","minlength=2", ADDRESS_CITY_MIN_LENGTH);		
		
		// Validate state
		frmvalidator.addValidation("address_1_state","req",ADDRESS_STATE_REQ);
		
		// Validate Zip Code
		frmvalidator.addValidation("address_1_zip","req",ADDRESS_ZIP_REQ);	
		frmvalidator.addValidation("address_1_zip","num",ADDRESS_ZIP_NUMERIC);		
		frmvalidator.addValidation("address_1_zip","minlen=5",ADDRESS_ZIP_MIN_LENGTH);
			
		// Validate phone1_1
		frmvalidator.addValidation("phone1","req",PHONE_1_REQ);	
		frmvalidator.addValidation("phone1","numeric",PHONE_1_NUMERIC);
		frmvalidator.addValidation("phone1","minlength=3",PHONE_1_MIN_LENGTH);		
			
		// Validate phone1_2		
		frmvalidator.addValidation("phone_2","req",PHONE_2_REQ);
		frmvalidator.addValidation("phone_2","numeric",PHONE_2_NUMERIC);		
		frmvalidator.addValidation("phone_2","minlength=3",PHONE_2_MIN_LENGTH);
			
			
		// Validate phone1_3		
		frmvalidator.addValidation("phone3","req",PHONE_3_REQ);
		frmvalidator.addValidation("phone3","numeric",PHONE_3_NUMERIC);
		frmvalidator.addValidation("phone3","minlength=4",PHONE_3_MIN_LENGTH);
			
		// Validate Email
		frmvalidator.addValidation("email","req",EMAIL_REQ);
		frmvalidator.addValidation("email","email",EMAIL_EMAIL);
		
		// Fix the phone number
		frmvalidator.setAddnlValidationFunction(concatenatePhone);
	};
	// Phone number fix script
	var concatenatePhone = function() {
		document.getElementById('phone').value=
			document.getElementById('phone1').value + 
			document.getElementById('phone_2').value + 
			document.getElementById('phone3').value;
	}
	
	// Reset Validation
	var resetValidation = function(frmvalidator) {
		frmvalidator.clearAllValidations();
		runValidation(frmvalidator)
	};
	return {
		run: function(frmvalidator) {
			runValidation(frmvalidator);
		},
		reset: function(frmvalidator) {
			resetValidation(frmvalidator);
		}
	}
}();


// Show and hide health conditions
var healthConditions = function() {
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
			
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	var bindFunctionality = function() {
		// Does the user have health conditions?
		var toggles = document.getElementsByName("insured_1_health_conditions");
		for(var i=0; i<toggles.length; i++) {
			toggles[i].onclick = showHideConditions;
		}
		var inputElems = document.getElementById("healthProfileConditions").getElementsByTagName("input");
		for(var i=0; i<inputElems.length; i++) {
			inputElems[i].onclick = setConditions;
		}
	};
	var showHideConditions = function() {
		if(this.value=="yes") {
			document.getElementById("healthProfileConditions").style.display="block";
			healthFormValidation.reset(frmvalidator);
		}
		else {
			document.getElementById("healthProfileConditions").style.display="none";
			unSetConditions();
			healthFormValidation.reset(frmvalidator);
		}
	};
	var setConditions = function() {
		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. / ":""));
	};
	var unSetConditions = function() {
		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="";
	};
	return {
		init: function() {
				addLoadEvent(bindFunctionality);
		}
	}
}();

// Add Children and Spouse
var dependents = function() {
	var spouse = false;
	var children = 0;
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
			
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	var bindFunctionality = function() {
		// Hover styles
		document.getElementById("controlAddSpouse").onmouseover = changePointer;
		document.getElementById("controlAddChild").onmouseover = changePointer;
		document.getElementById("controlAddSpouseDisable").onmouseover = changePointer;
		document.getElementById("controlAddChildDisable").onmouseover = changePointer;
		document.getElementById("removeChild1").onmouseover = changePointer;
		document.getElementById("removeChild2").onmouseover = changePointer;
		document.getElementById("removeChild3").onmouseover = changePointer;
		document.getElementById("removeChild4").onmouseover = changePointer;
		document.getElementById("removeChild5").onmouseover = changePointer;
		document.getElementById("removeChild6").onmouseover = changePointer;
		document.getElementById("removeSpouse").onmouseover = changePointer;
		
		// Add Dependents
		document.getElementById("controlAddSpouse").onclick = addSpouse;
		document.getElementById("controlAddChild").onclick = addChild;
		
		// Remove Dependents
		document.getElementById("removeSpouse").onclick = removeSpouse;
		for(var i=1; i<7; i++) {
			document.getElementById("removeChild"+i).onclick = removeChild;
		}
	};
	var changePointer = function() {
		this.style.cursor = "pointer";
	}
	var addChild = function() {
		if(children<6) {
			children++;
			document.getElementById("childForm"+children).style.display = "block";
			healthFormValidation.reset(frmvalidator);
			if(children==6) {
				this.style.display = "none";
			}
		}
	};
	var addSpouse = function() {
		spouse = true;
		document.getElementById("spouseForm").style.display = "block";
		this.style.display = "none";
		healthFormValidation.reset(frmvalidator);
	};
	var removeSpouse = function() {
		spouse = false;
		document.getElementById("controlAddSpouse").style.display = "block";
		document.getElementById("spouseForm").style.display = "none";
		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;
		healthFormValidation.reset(frmvalidator);
		
	};
	var removeChild = function() {
		var child = parseInt(this.id.slice(-1));
		document.getElementById("childForm"+children).style.display = "none";
		for(var i=(child+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("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.getElementById("controlAddChild").style.display = "block";
		}
		children--;
		healthFormValidation.reset(frmvalidator);
	};
	return {
		init: function() {
			addLoadEvent(bindFunctionality);
		},
		children: function() {
			return children;
		},
		spouse: function() {
			return spouse;
		}
	}
}();
var emailHide = function () {
	var emailStyles;
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
			
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	var bindFunctionality = function () {
		document.getElementById("email").onfocus = emailFocus;
		document.getElementById("email").onblur = emailBlur;
	};
	var emailFocus = function() {
		if(this.style.backgroundImage!="none") {
			emailStyles = this.style.backgroundImage;
			this.style.backgroundImage = "none";
		}
	};
	var emailBlur = function() {
		if(this.value=="") {
			this.style.backgroundImage = emailStyles;
		}
	}
	return {
		init: function() {
			addLoadEvent(bindFunctionality);
		}
	};
}();
dependents.init();
healthConditions.init();
emailHide.init();
