/* Functions for State of Arizona calculators
	September 2004 - jlalic
*/
// Premium Functions ----------------------------------------------------------
	
	function calc_prem(who) {
		var form = document.prem_calc;
		if(who=="em" && validate_age(who)) {
			if(form.myAmt.value>=5000 && form.myAmt.value<=300000){
				if(validate_amt(form.myAmt.value)){
					with (form) {
						monthlyPrem = (myAmt.value/1000) * rate_lookup(age.value);
						myPrem.value = formatAsCurrency(monthlyPrem);
						myAmt.value = formatAsCurrency(myAmt.value);
					}
				}
			}
			else {alert("The Coverage Amount for Additional Life and AD&D entered is not valid. Coverage amount is $5,000 increments to a maximum of $300,000."); form.myAmt.focus(); return false;}
		}
	
		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_age(who) {
		var form = document.prem_calc;
		if(who=="em") {
			if(!form.age.value.match(/\d/g)){alert("Please enter your age."); form.age.focus(); return false;}
		}
		return true;
	}
	
	function validate_amt(who) {
		var form = document.pre_calc;
		myWhole = who/1000;
		if(myWhole/5 != parseInt(myWhole/5)){
			alert("The coverage amount entered is not valid. Coverage amount is $5,000 increments to a maximum of $300,000."); form.myAmt.focus(); return false;
		}
		return true;
	}
	
	function rate_lookup(myAge) {
		if(myAge < 30){rate=.10}
		if(myAge >=30 && myAge <=34){rate=.12}
		if(myAge >=35 && myAge <=39){rate=.14}
		if(myAge >=40 && myAge <=44){rate=.24}
		if(myAge >=45 && myAge <=49){rate=.32}
		if(myAge >=50 && myAge <=54){rate=.52}
		if(myAge >=55 && myAge <=59){rate=.74}
		if(myAge >=60 && myAge <=64){rate=1.34}
		if(myAge >=65 && myAge <=69){rate=1.34}
		if(myAge >=70){rate=2.12}
		return rate;
	}

// Needs Functions ------------------------------------------------------------
function add_needs() {
		var form = document.needs_calc;
		
		with (form) {
			var y1=i_1_y.value.replace(/,/g,'')-0;
			var y2=i_2_y.value.replace(/,/g,'')-0;
			var y3=i_3_y.value.replace(/,/g,'')-0;

			var y4=t_1_y.value.replace(/,/g,'')-0;
			var y5=t_2_y.value.replace(/,/g,'')-0;
			var y6=t_3_y.value.replace(/,/g,'')-0;

			var y7=l_1_y.value.replace(/,/g,'')-0;
			var y8=l_2_y.value.replace(/,/g,'')-0;
			var y9=l_3_y.value.replace(/,/g,'')-0;
			var y10=l_4_y.value.replace(/,/g,'')-0;
			var y11=l_5_y.value.replace(/,/g,'')-0;

			var y12=r_1_y.value.replace(/,/g,'')-0;
			
			youTotal = y1+y2+y3+y4+y5+y6+y7+y8+y9+y10+y11+y12;
			
			if(isNaN(youTotal)) {
				//error("You");
				// do nothing
			}else {
				total_y.value = formatAsCurrency(youTotal);
			}
		}
}


function add_resources() {
	var form = document.needs_calc;
	
	with (form) {
		var x1=a_1_y.value.replace(/,/g,'')-0;
		var x2=a_2_y.value.replace(/,/g,'')-0;
	
		yTotal = x1+x2;
		
		if(isNaN(yTotal)) {
			// do nothing
		}else {
			tra_y.value = formatAsCurrency(yTotal);
		}
	}
}


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


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


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

	var tmp = new Array();
	var inc = new Array();
	
	function add_expenses() {
		var form = document.needs_calc;
		var expTotal=0;

		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);
		if(isNaN(expTotal)) {form.e_10.value = "";}
		
	}

	function add_income() {
		var form = document.needs_calc;
		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];
		
		// display the total income
		form.i_4.value = formatAsCurrency(incTotal);
		if(isNaN(incTotal)) {form.i_4.value = "";}
	
	}