/* Functions for State of New Mexico calculators
	September 2004 - jlalic
	modified DEC 2007 - mfeeny
*/
// Premium Functions ----------------------------------------------------------

function stripInput(a) {
	var modAmt = a;
	var empty = "";
	//var dollar = "$";
	modAmt = modAmt.replace(/,/g,empty);
	modAmt = modAmt.replace(/\$/g,empty);
	//alert(modAmt);
	return modAmt;
}

function roundAccuracy(num, acc){
	var factor=Math.pow(10,acc);
	return Math.ceil(num*factor)/factor;
}
	
	function calc_prem(who) {
		var form = document.prem_calc;
		if(who=="em1" && validate_age(who) && validate_amt(who)) {
			with (form) {
				monthlyPrem = (myAmt.value/1000) * rate_lookup(age.value);
				//myPrem.value = formatAsCurrency(monthlyPrem);
				moPremMbr1.value = formatAsCurrency(monthlyPrem);
			}
		}
		
		if(who=="em2" && validate_age(who) && validate_amt(who) && validate_mult(form.mult.value)) {
			
			with (form) {
			var annualEarnings = stripInput(myAmt2.value);
				
				var rounded = roundAccuracy(annualEarnings, -3);
				//alert(rounded);
				var rounded_mult = rounded * mult.value;
				//alert(rounded_mult);
				monthlyPrem = (rounded_mult/1000) * rate_lookup(age2.value);
				//myPrem.value = formatAsCurrency(monthlyPrem);
				moPremMbr2.value = formatAsCurrency(monthlyPrem);
			}
		}
		
		if(who=="sp") {
			with (form) {
				monthlyPrem = wants2.value;
				myPrem2.value = formatAsCurrency(monthlyPrem);
			}
		}
		if(who=="std") {
			with (form) {
				monthlyPrem = (mySTDAmt.value * .0089);
				stdPrem.value = formatAsCurrency(monthlyPrem);
				mySTDAmt.value = formatAsCurrency(mySTDAmt.value);
			}
		}
	}

	function validate_mult(multval) {
		var form = document.prem_calc;
		if (multval == "") {
			alert("Please select 1,2,3,4 or 5 times your annual earnings");
			form.mult.focus();return false
		}
		return true;
	}
	
	function validate_age(who) {
		var form = document.prem_calc;
		if(who=="em1") {
			if(!form.age.value.match(/\d/g)){alert("Please enter your age."); form.age.focus(); return false;}
		}
		if(who=="em2") {
			if(!form.age2.value.match(/\d/g)){alert("Please enter your age."); form.age2.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 one of the Additional Life & AD&D options');
				form.myAmt.focus();return false;
			}	
		}
		if (who == "em2") {
			if (form.myAmt2.value == "") {
				alert('Please enter your annual earnings');
				form.myAmt2.focus();return false;
			}	
		}
		return true;
	}
	
	function rate_lookup(myAge) {
		if(myAge < 30){rate=.080}
		if(myAge >=30 && myAge <=34){rate=.110}
		if(myAge >=35 && myAge <=39){rate=.110}
		if(myAge >=40 && myAge <=44){rate=.158}
		if(myAge >=45 && myAge <=49){rate=.215}
		if(myAge >=50 && myAge <=54){rate=.349}
		if(myAge >=55 && myAge <=59){rate=.554}
		if(myAge >=60 && myAge <=64){rate=.928}
		if(myAge >=65 && myAge <=69){rate=1.489}
		if(myAge >=70){rate=3.611}
		return rate;
	}

// Life Needs Functions ------------------------------------------------------------
	
	
/* Functions for New Mexcio calculators
	March 2007 - aschotland
*/


// 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() {
	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);
			}
		}
	}
	
}

function error(column) {	//pass the name of column, "you" or "spouse"
	alert("The calculator has encountered a non-numerical value\nin the "+column+" column.\n\nPlease verify that all fields contain only numerical values.\n\nThank you.");
}