// Premium Functions for Life and STD ----------------------------------------------------------

function calc_prem(who) {
	var form = document.prem_calc;
	
	//to calculate premium for self			
	if(who=="em1" && validate_age(who) && validate_amt(who)) {
		with (form) {
		monthlyPrem = (myAmt.value/1000) * rate_lookup3(age.value);				
		//alert("myAmt = " + myAmt.value + "\nrate = " + rate);
		moPremMbr1.value = formatAsCurrency(monthlyPrem);
		}			
	}		
		
	//calculate premium for dependents
	if(who=="dep") {
		with (form) {
			monthlyPrem = depAmt.value;
			depPrem.value = formatAsCurrency(monthlyPrem);
		}
	}
}

function rate_lookup3(myAge) {
		if(myAge < 25){rate=.042}
		if(myAge >=25 && myAge <=29){rate=.051}
		if(myAge >=30 && myAge <=34){rate=.069}
		if(myAge >=35 && myAge <=39){rate=.071}
		if(myAge >=40 && myAge <=44){rate=.076}
		if(myAge >=45 && myAge <=49){rate=.134}
		if(myAge >=50 && myAge <=54){rate=.197}
		if(myAge >=55 && myAge <=59){rate=.363}
		if(myAge >=60 && myAge <=64){rate=.627}
		if(myAge >=65 && myAge <=69){rate=1.131}
		if(myAge >=70){rate=2.031}
		return rate;
}

function validate_age(who) {
	var form = document.prem_calc;
	//alert(who);
	if(who=="em1") {		
		if(!document.prem_calc.age.value.match(/\d/g)){
			alert("Please enter your age.");
			form.age.focus();
			return false;
		}
	}
	return true;
}

function validate_amt(who) {
	var form = document.prem_calc;
	//alert(who);
	if (who=="em1") {
		if (form.myAmt.value == "") {
			alert('Please select the benefit amount');
			form.myAmt.focus();
			return false;
		}	
	}
	return true;
}

// Life Needs Functions ------------------------------------------------------------
function add_needs_life() {
		var form = document.needs_calc_life;
		
		with (form) {
			var y1=i_1_y.value.replace(/,/g,'')-0;
			var s1=i_1_s.value.replace(/,/g,'')-0;
			var y2=i_2_y.value.replace(/,/g,'')-0;
			var s2=i_2_s.value.replace(/,/g,'')-0;
			var y3=i_3_y.value.replace(/,/g,'')-0;
			var s3=i_3_s.value.replace(/,/g,'')-0;
			var y4=t_1_y.value.replace(/,/g,'')-0;
			var s4=t_1_s.value.replace(/,/g,'')-0;
			var y5=t_2_y.value.replace(/,/g,'')-0;
			var s5=t_2_s.value.replace(/,/g,'')-0;
			var y6=t_3_y.value.replace(/,/g,'')-0;
			var s6=t_3_s.value.replace(/,/g,'')-0;
			var y7=l_1_y.value.replace(/,/g,'')-0;
			var s7=l_1_s.value.replace(/,/g,'')-0;
			var y8=l_2_y.value.replace(/,/g,'')-0;
			var s8=l_2_s.value.replace(/,/g,'')-0;
			var y9=l_3_y.value.replace(/,/g,'')-0;
			var s9=l_3_s.value.replace(/,/g,'')-0;
			var y10=l_4_y.value.replace(/,/g,'')-0;
			var s10=l_4_s.value.replace(/,/g,'')-0;
			var y11=l_5_y.value.replace(/,/g,'')-0;
			var s11=l_5_s.value.replace(/,/g,'')-0;
			var y12=r_1_y.value.replace(/,/g,'')-0;
			var s12=r_1_s.value.replace(/,/g,'')-0;
			
			youTotal = y1+y2+y3+y4+y5+y6+y7+y8+y9+y10+y11+y12;
			spouseTotal = s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12;
			
			if(isNaN(youTotal)) {
				//error("You");
				// do nothing
			}else {				
				total_y.value = formatAsCurrency(youTotal);
				
			}
			if(isNaN(spouseTotal)) {
				//error("Spouse");
				// do nothing
			}else {
				total_s.value = formatAsCurrency(spouseTotal);
			}
			
		}
}


function add_resources_life() {
	var form = document.needs_calc_life;
	
	with (form) {
		var x1=a_1_y.value.replace(/,/g,'')-0;
		var z1=a_1_s.value.replace(/,/g,'')-0;
		var x2=a_2_y.value.replace(/,/g,'')-0;
		var z2=a_2_s.value.replace(/,/g,'')-0;
		
		yTotal = x1+x2;
		sTotal = z1+z2;
		
		if(isNaN(yTotal)) {
			// do nothing
		}else {
			tra_y.value = formatAsCurrency(yTotal);
		}
		if(isNaN(sTotal)) {
			// do nothing
		}else {
			tra_s.value = formatAsCurrency(sTotal);
		}
	}
}


function find_dif_life() {
	alert("needs");
	var form = document.needs_calc_life;
	add_needs_life();
	add_resources_life();
	with (form) {	// error handling moved here
		if(isNaN(youTotal-yTotal)) {
			total_y.value = "";
			tra_y.value = "";
			total_vol_y.value = "";
			error("You");
		}else {
			if(youTotal-yTotal <= 0) {
				total_vol_y.value=0;
			}else {
				total_vol_y.value=formatAsCurrency(youTotal-yTotal);
			}
		}
		if(isNaN(spouseTotal-sTotal)) {
			total_s.value = "";
			tra_s.value = "";
			total_vol_s.value = "";
			error("Spouse");
		}else {
			if(spouseTotal-sTotal <= 0) {
				total_vol_s.value=0;
			}else {
				total_vol_s.value=formatAsCurrency(spouseTotal-sTotal);
			}
		}
	}
	
}