/* Needs Functions for Syracuse University*/

// calc.js

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


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

// LTD Needs Functions ---------------------------------------------------------------------

	var ltdtmp = new Array();
	var ltdinc = new Array();
	
	function add_needs() {
		
		var form = document.needs_calc;
			
		ltdtmp[0] = form.m_1.value.replace(/,/g,'')-0;
		ltdtmp[1] = form.m_2.value.replace(/,/g,'')-0;
		ltdtmp[2] = form.m_3.value.replace(/,/g,'')-0;
		ltdtmp[3] = form.m_4.value.replace(/,/g,'')-0;
		ltdtmp[4] = form.m_5.value.replace(/,/g,'')-0;
		ltdtmp[5] = form.m_6.value.replace(/,/g,'')-0;
		ltdtmp[6] = form.m_7.value.replace(/,/g,'')-0;
		ltdtmp[7] = form.m_8.value.replace(/,/g,'')-0;
		ltdtmp[8] = form.m_9.value.replace(/,/g,'')-0;
				
		// add each item in tmp array to total
		ltdtmpTotal = ltdtmp[0]+ltdtmp[1]+ltdtmp[2]+ltdtmp[3]+ltdtmp[4]+ltdtmp[5]+ltdtmp[6]+ltdtmp[7]+ltdtmp[8];
		
		// display the total expenses
		form.total_m.value = formatAsCurrency(ltdtmpTotal);
		form.total_me.value = formatAsCurrency(ltdtmpTotal);
		if(isNaN(ltdtmpTotal)) {form.total_m.value = "";}
		
	}
	
	function add_income() {
		var form = document.needs_calc;
		
		ltdinc[0] = form.i_1.value.replace(/,/g,'')-0;
		ltdinc[1] = form.i_2.value.replace(/,/g,'')-0;
		ltdinc[2] = form.i_3.value.replace(/,/g,'')-0;
		
		// add each item in tmp array to total
		ltdincTotal = ltdinc[0]+ltdinc[1]+ltdinc[2];
        // leave out "take home pay" from total
        ltdincPostDisability = ltdinc[1] + ltdinc[2];
		
		// display the total income
		form.total_i.value = formatAsCurrency(ltdincTotal);
		form.total_mi.value = formatAsCurrency(ltdincPostDisability);
		if(isNaN(ltdincTotal)) {form.total_i.value = "";}
		find_diff();
	}
	
	function find_diff() {
		var form = document.needs_calc;
		
        if(form.total_i.value=="" || form.total_m.value=="") {
            alert("Please enter your expenses and income.");
        } 
		else {
            var difTotal = ltdtmpTotal - ltdincPostDisability;
            
			if(difTotal <= 0) {
				form.total_vol.value = 0;
			} 
			else {
				form.total_vol.value = formatAsCurrency(difTotal);
			}
		}
       
	}

	
//LTD Premium function --------------------------------------------------------
	function calc_prem_ltd(yr) {
		var year = yr;
		var form = document.prem_calc_ltd;
		
		//if(validate_age() && validate_salary()) {
		if(form.myLTDAge.value=="" || form.em_salary.value=="") {
			alert("Your age and annual salary are both required.");
			return false;
		}
		if(document.prem_calc_ltd.em_salary.value>20000){
			
			document.prem_calc_ltd.emLTDPrem.value = "";
			alert("The Monthly Base Salary entered is not valid. This figure has been capped at $20000.");
			document.prem_calc_ltd.em_salary.value = 20000;
			return false;
		}
		else {
			with (form) {

				//mySalary = salary.value.replace(/,/g,'');
				
				/*
				monthlyPrem = (mySalary) * rate_lookup_60(myLTDAge.value)/2;
				ltdPrem.value = formatAsCurrency(monthlyPrem);
				prem60.value = formatAsCurrency(monthlyPrem);
				*/
				if(year=='2008'){
					monthlyPrem = ((em_salary.value * rate_lookup_2008(myLTDAge.value)));
				} else {
					monthlyPrem = ((em_salary.value * rate_lookup(myLTDAge.value)));
				}
				emLTDPrem.value = formatAsCurrency(monthlyPrem);
				em_salary.value=formatAsCurrency(em_salary.value);
			}
		}
		
	}
	
	/* function rate_lookup_60(myLTDAge) {
		if(myLTDAge < 25){rate=.00160}
		if(myLTDAge >=25 && myLTDAge <=29){rate=.160}
		if(myLTDAge >=30 && myLTDAge <=34){rate=.160}
		if(myLTDAge >=35 && myLTDAge <=39){rate=.160}
		if(myLTDAge >=40 && myLTDAge <=44){rate=.160}
		if(myLTDAge >=45 && myLTDAge <=49){rate=.160}
		if(myLTDAge >=50 && myLTDAge <=54){rate=.160}
		if(myLTDAge >=55 && myLTDAge <=59){rate=.160}
		if(myLTDAge >=60 && myLTDAge <=64){rate=.160}
		if(myLTDAge >=65){rate=.160}
		return rate;
	} */
	
	// OLD rates
	function rate_lookup_2008(myLTDAge) {
		if(myLTDAge < 29){rate=0.00160}
		if(myLTDAge >=30 && myLTDAge <=34){rate=0.00190}
		if(myLTDAge >=35 && myLTDAge <=39){rate=0.00240}
		if(myLTDAge >=40 && myLTDAge <=44){rate=0.00350}
		if(myLTDAge >=45 && myLTDAge <=49){rate=0.00530}
		if(myLTDAge >=50 && myLTDAge <=54){rate=0.00770}
		if(myLTDAge >=55 && myLTDAge <=59){rate=0.00990}
		if(myLTDAge >=60 && myLTDAge <=64){rate=0.01030}
		if(myLTDAge >=65 && myLTDAge <=69){rate=0.01100}
		if(myLTDAge >=70 && myLTDAge <=74){rate=0.01420}
		if(myLTDAge >=75){rate=0.02160}
		return rate;
	}
	// New rates here...MF 9/8/2008
	function rate_lookup(myLTDAge) {
		if(myLTDAge < 30){rate=0.00200}
		if(myLTDAge >=30 && myLTDAge <=34){rate=0.00240}
		if(myLTDAge >=35 && myLTDAge <=39){rate=0.00300}
		if(myLTDAge >=40 && myLTDAge <=44){rate=0.00440}
		if(myLTDAge >=45 && myLTDAge <=49){rate=0.00660}
		if(myLTDAge >=50 && myLTDAge <=54){rate=0.00960}
		if(myLTDAge >=55 && myLTDAge <=59){rate=0.01240}
		if(myLTDAge >=60 && myLTDAge <=64){rate=0.01290}
		if(myLTDAge >=65 && myLTDAge <=69){rate=0.01370}
		if(myLTDAge >=70 && myLTDAge <=74){rate=0.01770}
		if(myLTDAge >=75){rate=0.02700}
		return rate;
	}

