var todayDate = new Date();	
var spouse=false;
// Show/Hide Conditions and reset validation
function showconditions(id, value) {
	if(value=="yes"){
		document.getElementById(id).style.display='';
	}
	else {
		document.getElementById(id).style.display='none';
	}
	
	resetSecondStepValidation(frmvalidator);	
}

// Show hide spouse 
function showSpouse(id, value) {
	if(value=="yes"){
		document.getElementById(id).style.display='';
		spouse=true;
	}
	else {
		document.getElementById(id).style.display='none';
		spouse=false;
	}
	resetThirdStepValidation(frmvalidator);
}

// Reset validation for step 3
function resetThirdStepValidation(frmvalidator) {
	frmvalidator.clearAllValidations();
	thirdStepValidation(frmvalidator);	

}

// Reset Validation for step 2
function resetSecondStepValidation(frmvalidator) {
	frmvalidator.clearAllValidations();
	secondStepValidation(frmvalidator);
}
// Reset Validation for step 4
function resetFourthStepValidation(frmvalidator) {
	frmvalidator.clearAllValidations();
	fourthStepValidation(frmvalidator);
}
//First Step Validation
function firstStepValidation(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","dontselect=0", DOB_MM_SEL);
		
	//Validate DOB day
	frmvalidator.addValidation("insured_1_dobDD","dontselect=0", DOB_DD_SEL);
		
	//Validate DOB year		
	frmvalidator.addValidation("insured_1_dobYYYY","dontselect=0",DOB_YYYY_SEL);

	//Validate Height Feet
	frmvalidator.addValidation("insured_1_heightFT","dontselect=0",HEIGHT_FT_SEL);
		
	//Validate Height inches
	frmvalidator.addValidation("insured_1_heightIN","dontselect=0",HEIGHT_IN_SEL);
		
	//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);

	frmvalidator.setAddnlValidationFunction("firstStepCustomValidation");
}

function firstStepCustomValidation(){
	var today=new Date();
	var day=document.getElementById('insured_1_dobDD').value;
	var month=document.getElementById('insured_1_dobMM').value;
	var year=document.getElementById('insured_1_dobYYYY').value;
	if( isInFuture((month-1),day,year,today)){
		alert(DOB_IN_FUTURE);
		return false;
	}	
}

//Second Step Validation
function secondStepValidation(frmvalidator){
	// Create a variable to pull in the form name
	var form = document.forms[frmvalidator.formobj._sfm_form_name];
	
	frmvalidator.addValidation("insured_1_smoker","selone_radio",SMOKER_REQ);
	
	if(form.insured_1_health_conditions_yes.checked == true){ 
		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);			
		}
	}		

}

//Third Step Validation
function thirdStepValidation(frmvalidator){
	// Create a variable to pull in the form name
	var form = document.forms[frmvalidator.formobj._sfm_form_name];
	//Validate if Spouse is selected	
	if(form.spouse_needs_insurance_yes.checked == true){	
		
		// Spouse's gender
		frmvalidator.addValidation("insured_2_gender","selone_radio", SPOUSE_SEL_GENDER);
		
		// Spouse's birth month
		frmvalidator.addValidation("insured_2_dobMM", "dontselect=0", SPOUSE_DOB_MM_SEL);
		
		// Spouse's birth day
		frmvalidator.addValidation("insured_2_dobDD", "dontselect=0", SPOUSE_DOB_DD_SEL);
		
		// Spouse's birth year		
		frmvalidator.addValidation("insured_2_dobYYYY", "dontselect=0", SPOUSE_DOB_YYYY_SEL);

		// Spouse's Height (Feet)
		frmvalidator.addValidation("insured_2_heightFT", "dontselect=0", SPOUSE_HEIGHT_FT_SEL);
		
		// Spouse's Height (Weight)
		frmvalidator.addValidation("insured_2_heightIN", "dontselect=0", SPOUSE_HEIGHT_IN_SEL);
		
		// 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);

		frmvalidator.addValidation("insured_2_smoker","selone_radio",SPOUSE_SEL_SMOKING);
	}
	
	//Validate Children
	for(i=3; i<=parseInt(document.getElementById('number_of_dependents').value)+2; i++) {	
		// Child's gender		
		frmvalidator.addValidation("insured_" + i + "_gender","selone_radio", CHILD_SEL_GENDER);
		
		// Child's birth month
		frmvalidator.addValidation("insured_" + i + "_dobMM", "dontselect=0", CHILD_DOB_MM_SEL);
		
		// Child's birth day
		frmvalidator.addValidation("insured_" + i + "_dobDD", "dontselect=0", CHILD_DOB_DD_SEL);

		// Child's birth year
		frmvalidator.addValidation("insured_" + i + "_dobYYYY", "dontselect=0", CHILD_DOB_YYYY_SEL);
		
		// Child's Height (Feet)
		frmvalidator.addValidation("insured_" + i + "_heightFT", "dontselect=0", CHILD_HEIGHT_FT_SEL);

		//Child's Height (Weight)
		frmvalidator.addValidation("insured_" + i + "_heightIN", "dontselect=0", CHILD_HEIGHT_IN_REQ);
	
		// 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); 
	}
	
	frmvalidator.setAddnlValidationFunction("thirdStepCustomValidation");
}

function thirdStepCustomValidation(frmvalidator){
	var today=new Date();
	var MSG=null;
	for(var i=2; i<9; i++){
		if(i==2){//spouse
			if(spouse != true){continue;}
			MSG=SPOUSE_DOB_IN_FUTURE;			
		}else{//child
			var childinst = document.getElementById("child_option" + (i-2));			
			if(childinst == null || childinst.style.display == "none") {
				break;
			}
			
			MSG=CHILD_DOB_IN_FUTURE;	
		}
		
		var day=document.getElementById('insured_'+i+'_dobDD').value;
		var month=document.getElementById('insured_'+i+'_dobMM').value;
		var year=document.getElementById('insured_'+i+'_dobYYYY').value;
		
		if( isInFuture((month-1),day,year,today)){
			alert(MSG);
			return false;
		}	
	}
}

//Fourth Step Validation
function fourthStepValidation(frmvalidator){
	//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","minlength=2",LAST_NAME_MIN_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","1alpha_1digit", ADDRESS_STREET_ALPHADIGIT);
	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 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("phone_1","req",PHONE_1_REQ);	
	frmvalidator.addValidation("phone_1","numeric",PHONE_1_NUMERIC);
	frmvalidator.addValidation("phone_1","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("phone_3","req",PHONE_3_REQ);
	frmvalidator.addValidation("phone_3","numeric",PHONE_3_NUMERIC);
	frmvalidator.addValidation("phone_3","minlength=4",PHONE_3_MIN_LENGTH);
	
	// Validate Email
	frmvalidator.addValidation("email","req",EMAIL_REQ);
	frmvalidator.addValidation("email","email",EMAIL_EMAIL);
	
	frmvalidator.setAddnlValidationFunction("doCustomValidation");
}

// set medical conditions
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. / ":""));
}

// unset medical 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="";
}

// Delete Spouse data if No is selected
function unSetSpouse(){	
	//Reset Spouse Values
	document.getElementById('insured_2_gender').selectedIndex=0;	
	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').selectedIndex=0;
}

function changeChildren(value,form){
	if(value==1) {
		document.getElementById("child_option1").style.display="";				
		document.getElementById("child_option2").style.display="none";
		document.getElementById("child_option3").style.display="none";
		document.getElementById("child_option4").style.display="none";
		document.getElementById("child_option5").style.display="none";
		document.getElementById("child_option6").style.display="none";
	}

	else if(value==2) {
		document.getElementById("child_option1").style.display="";
		document.getElementById("child_option2").style.display="";		
		document.getElementById("child_option3").style.display="none";
		document.getElementById("child_option4").style.display="none";
		document.getElementById("child_option5").style.display="none";
		document.getElementById("child_option6").style.display="none";
	}

	else if(value==3) {
		document.getElementById("child_option1").style.display="";
		document.getElementById("child_option2").style.display="";
		document.getElementById("child_option3").style.display="";		
		document.getElementById("child_option4").style.display="none";
		document.getElementById("child_option5").style.display="none";
		document.getElementById("child_option6").style.display="none";
	}

	else if(value==4) {
		document.getElementById("child_option1").style.display="";
		document.getElementById("child_option2").style.display="";
		document.getElementById("child_option3").style.display="";
		document.getElementById("child_option4").style.display="";		
		document.getElementById("child_option5").style.display="none";
		document.getElementById("child_option6").style.display="none";
	}

	else if(value==5) {
		document.getElementById("child_option1").style.display="";
		document.getElementById("child_option2").style.display="";
		document.getElementById("child_option3").style.display="";
		document.getElementById("child_option4").style.display="";
		document.getElementById("child_option5").style.display="";		
		document.getElementById("child_option6").style.display="none";
	}

	else if(value==6) {
		document.getElementById("child_option1").style.display="";
		document.getElementById("child_option2").style.display="";
		document.getElementById("child_option3").style.display="";
		document.getElementById("child_option4").style.display="";
		document.getElementById("child_option5").style.display="";
		document.getElementById("child_option6").style.display="";		
	}

	else {
		document.getElementById("child_option1").style.display="none";
		document.getElementById("child_option2").style.display="none";
		document.getElementById("child_option3").style.display="none";
		document.getElementById("child_option4").style.display="none";
		document.getElementById("child_option5").style.display="none";
		document.getElementById("child_option6").style.display="none";
	}
	
	resetThirdStepValidation(frmvalidator);
}

function doCustomValidation(){
	document.getElementById('phone').value=document.getElementById('phone_1').value+document.getElementById('phone_2').value+document.getElementById('phone_3').value;	
}


// ZIP/State Validation

var zipStateLookUp = function() {
	var stateField = "";
	var stateValue = "";
	var zipField = "";
	var zipValue = "";
	var formField = "";
	var ajaxZipState;
	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() {
		stateField = document.getElementById(stateField);
		zipField = document.getElementById(zipField);
		formField = document.getElementsByName(formField)[0];
		if((formField !=null) && (stateField !=null) && (zipField !=null)){
			// Make sure you check all cases
			stateField.onblur = attemptCheck;
			stateField.onchange = attemptCheck; 
			zipField.onblur = attemptCheck;
			zipField.onkeyup = attemptCheck;
			zipField.onchange = attemptCheck;
			zipField.onfocus = attemptCheck;
		}
	};
	var checkFieldValueReq = function (fieldName) {
		return (getFieldValueLength(fieldName)==0)? false : true;
	};
	var getFieldValueLength = function (fieldName) {
		var fieldValue = fieldName.value;
		try {
			fieldValue = trim(fieldName.value);
		}
		catch(e){
			fieldValue = fieldValue.replace(/^\s+|\s+$/g,""); 
		}
		return fieldValue.length;
	};
	var confirmZipState = function () {
		try {
			ajaxZipState = new XMLHttpRequest();
			
		}
		catch(e) {
			try {
				ajaxZipState = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
				 ajaxZipState = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					ajaxZipState = false;
				}
			}
		}
		if(ajaxZipState) {
			try {
				ajaxZipState.onreadystatechange = ajaxZipStateReadyStateChange;
				ajaxZipState.open("POST", "/zip.jsp", true);
				ajaxZipState.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				ajaxZipState.setRequestHeader("Content-length", zipField.value.length);
				ajaxZipState.setRequestHeader("Connection", "close");
				ajaxZipState.send("zip="+zipField.value);
			}
			catch(e){}
		}
	};
	var ajaxZipStateReadyStateChange = function() {
		if(ajaxZipState.readyState == 4) {
			if((ajaxZipState.status >= 200 && ajaxZipState.status < 300)|| (ajaxZipState.status == 304) || (ajaxZipState.status == 1223)) {
				var checkedState = trim(ajaxZipState.responseXML.getElementsByTagName("state")[0].firstChild.data);
					if(checkedState == stateField.value){
						resetFourthStepValidation(frmvalidator);	
					}
					else {
						frmvalidator.addValidation("address_1_zip","regexp=^[A-Za-z]{20,}","Please check your zip code and your state.");
					}
			}
		}
	};
	var attemptCheck = function() {
		var success = checkFieldValueReq(stateField);
		success = success & checkFieldValueReq(zipField);
		success = success & (zipField.value.length==5);
		if(success && (stateField.value!=stateValue || zipField.value!=zipValue)) {
			stateValue = stateField.value;
			zipValue = zipField.value;
			resetFourthStepValidation(frmvalidator);	
			confirmZipState();
		}
	};
	return {
		init: function(state, zip, form) {
				stateField = state;
				zipField = zip;
				formField = form;
				addLoadEvent(bindFunctionality);
		}
	};
}();
zipStateLookUp.init("address_1_state", "address_1_zip", "HealthForm");