/* Functions for State of Nevada calculators
	April 2003 - jmildenb
*/
// Premium Functions ----------------------------------------------------------
	
	
	function wants_check(who) {
		var form = document.prem_calc;
		with (form) {
			if(who=="act" || who=="ret") {
				myAmt = document.prem_calc.wants[document.prem_calc.wants.selectedIndex].value -0;
				//alert(myAmt);
			}
			
			if(who=="sp") {
				myAmt2 = document.prem_calc.wants2[document.prem_calc.wants2.selectedIndex].value -0;
				maxRounded = 250000;
			}
			
		}
	}
	
	function validate_age(who) {
		var form = document.prem_calc;
		if(who=="em" || who=="ret") {
			if(!form.age.value.match(/\d/g)){alert("Please enter your age."); form.age.focus(); return false;}
		}
		if(who=="sp") {
			if(!form.age2.value.match(/\d/g)){alert("Please enter your age."); form.age2.focus(); return false;}
		}
		return true;
	}
	
	function calc_prem(who) {
		var form = document.prem_calc;
		if(who=="ret" && validate_age(who)) {
			with (form) {
				myAmt = document.prem_calc.wants[document.prem_calc.wants.selectedIndex].value -0;
				//if(myAmt==10000) {
					//monthlyPrem = 19.70;
				//} else {
					//monthlyPrem = 19.70 + ((myAmt-10000)/1000) * rate_lookup_ret(age.value);
				//}
				monthlyPrem=(myAmt/1000) * rate_lookup_ret(age.value);
				myPrem.value = formatAsCurrency(monthlyPrem);
			}
		}
		
		if(who=="em" && validate_age(who)) {
			with (form) {
				monthlyPrem = (myAmt/1000) * rate_lookup(age.value);
				myPrem.value = formatAsCurrency(monthlyPrem);
			}
		}
		if(who=="sp" && validate_age(who)) {
			with (form) {
				monthlyPrem = (myAmt2/1000) * rate_lookup_sp(age2.value);
				myPrem2.value = formatAsCurrency(monthlyPrem);
			}
		}
	}
	
	function rate_lookup(myAge) {
		if(myAge < 30){rate=.11}
		if(myAge >=30 && myAge <=34){rate=.12}
		if(myAge >=35 && myAge <=39){rate=.14}
		if(myAge >=40 && myAge <=44){rate=.20}
		if(myAge >=45 && myAge <=49){rate=.31}
		if(myAge >=50 && myAge <=54){rate=.45}
		if(myAge >=55 && myAge <=59){rate=.73}
		if(myAge >=60 && myAge <=64){rate=.82}
		if(myAge >=65 && myAge <=69){rate=1.48}
		if(myAge >=70){rate=2.20}
		return rate;
	}
	
	function rate_lookup_sp(myAge) {
		if(myAge < 30){rate=.07}
		if(myAge >=30 && myAge <=34){rate=.08}
		if(myAge >=35 && myAge <=39){rate=.10}
		if(myAge >=40 && myAge <=44){rate=.16}
		if(myAge >=45 && myAge <=49){rate=.27}
		if(myAge >=50 && myAge <=54){rate=.41}
		if(myAge >=55 && myAge <=59){rate=.69}
		if(myAge >=60 && myAge <=64){rate=.78}
		if(myAge >=65 && myAge <=69){rate=1.44}
		if(myAge >=70){rate=2.16}
		return rate;
	}

	function rate_lookup_ret(myAge) {
		if(myAge < 50){rate=.53}
		if(myAge >=50 && myAge <=54){rate=.64}
		if(myAge >=55 && myAge <=59){rate=.98}
		if(myAge >=60 && myAge <=64){rate=1.47}
		if(myAge >=65 && myAge <=69){rate=2.43}
		if(myAge >=70 && myAge <=74){rate=3.55}
		if(myAge >=75 && myAge <=79){rate=5.82}
		if(myAge >=80 && myAge <=84){rate=7.69}
		if(myAge >=85 && myAge <=89){rate=17.62}
		if(myAge >=90 && myAge <=94){rate=29.97}
		if(myAge >=95){rate=39.70}
		return rate;
	}
	
	


// Life Needs Functions ------------------------------------------------------------
function add_needs() {
		var form = document.needs_calc;
		
		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() {
	var form = document.needs_calc;
	
	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() {
	var form = document.needs_calc;
	add_needs();
	add_resources();
	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);
			}
		}
	}
	
}


// STD Needs Functions ------------------------------------------------------------------------

	var tmp = new Array();
	var inc = new Array();
	
	function add_expenses_std() {
		var form = document.needs_calc_std;

		tmp[0] = form.e_1.value.replace(/,/g,'')-0;
		tmp[1] = form.e_2.value.replace(/,/g,'')-0;
		tmp[2] = form.e_3.value.replace(/,/g,'')-0;
		tmp[3] = form.e_4.value.replace(/,/g,'')-0;
		tmp[4] = form.e_5.value.replace(/,/g,'')-0;
		tmp[5] = form.e_6.value.replace(/,/g,'')-0;
		tmp[6] = form.e_7.value.replace(/,/g,'')-0;
		tmp[7] = form.e_8.value.replace(/,/g,'')-0;
		tmp[8] = form.e_9.value.replace(/,/g,'')-0;
		
		// add each item in tmp array to total
			expTotal = tmp[0]+tmp[1]+tmp[2]+tmp[3]+tmp[4]+tmp[5]+tmp[6]+tmp[7]+tmp[8];
		
		// display the total expenses
		form.e_10.value = formatAsCurrency(expTotal);
		form.total_sme.value = formatAsCurrency(expTotal);
		if(isNaN(expTotal)) {form.e_10.value = "";}
		
	}

	function add_incomes() {
		var form = document.needs_calc_std;
		var incTotal=0;
		
		inc[0] = form.i_1.value.replace(/,/g,'')-0;
		inc[1] = form.i_2.value.replace(/,/g,'')-0;
		inc[2] = form.i_3.value.replace(/,/g,'')-0;
		incTotal = inc[0]+inc[1]+inc[2];
		incPostDisability = inc[1] + inc[2];
		
		// display the total income
		form.i_4.value = formatAsCurrency(incTotal);
		form.total_smi.value = formatAsCurrency(incPostDisability);
		if(isNaN(incTotal)) {form.i_4.value = "";}
		find_diffstd();
	}
	
		function find_diffstd() {
		var form = document.needs_calc_std;
		
        if(form.e_10.value=="" || form.i_4.value=="") {
            alert("Please enter your expenses and income.");
        } 
		else {
            var difTotal = expTotal - incPostDisability;
            
			if(difTotal <= 0) {
				form.total_vols.value = 0;
			} 
			else {
				form.total_vols.value = formatAsCurrency(difTotal);
			}
		}
       
	}
	

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.");
}


