/* Functions for The California State University calculators
	Feb 2005 - jlalic
*/
// Premium Functions ----------------------------------------------------------
	
	
	function wants_check(who) {
		var form = document.prem_calc;
		with (form) {
			if(who=="act") {
				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") {
			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=="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);
			}
		}
		
		if(who=="ch"){
			with (form) {
				monthlyPrem = wants3.value;
				myPrem3.value = formatAsCurrency(monthlyPrem);
			}
		}
	}
	
	function rate_lookup(myAge) {
		if(myAge < 30){rate=.06}
		if(myAge >=30 && myAge <=34){rate=.09}
		if(myAge >=35 && myAge <=39){rate=.10}
		if(myAge >=40 && myAge <=44){rate=.16}
		if(myAge >=45 && myAge <=49){rate=.28}
		if(myAge >=50 && myAge <=54){rate=.44}
		if(myAge >=55 && myAge <=59){rate=.76}
		if(myAge >=60 && myAge <=64){rate=.80}
		if(myAge >=65 && myAge <=69){rate=1.39}
		if(myAge >=70){rate=2.22}
		return rate;
	}
	
	function rate_lookup_sp(myAge) {
		if(myAge < 30){rate=.07}
		if(myAge >=30 && myAge <=34){rate=.09}
		if(myAge >=35 && myAge <=39){rate=.11}
		if(myAge >=40 && myAge <=44){rate=.18}
		if(myAge >=45 && myAge <=49){rate=.32}
		if(myAge >=50 && myAge <=54){rate=.49}
		if(myAge >=55 && myAge <=59){rate=.83}
		if(myAge >=60 && myAge <=64){rate=.87}
		if(myAge >=65 && myAge <=69){rate=1.52}
		if(myAge >=70){rate=2.28}
		return rate;
	}

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


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


