/* Functions for DC calculators
	November 2006 - AS
*/
// Premium Functions ----------------------------------------------------------
var period;
	function roundAccuracy2(num, acc){
	//alert(num + " - " + acc);
	var factor=Math.pow(10,acc);
	//alert("factor = " + factor);
	/*var prodfloor = Math.floor(num*factor)/factor;
	alert("prodfloor = " + prodfloor);*/
	return Math.floor(num*factor)/factor;
	}
	
	function roundAccuracy3(num, acc){
	//alert(num + " - " + acc);
	//var factor=Math.pow(10,acc);
	//alert("factor = " + factor);
	var monthly_rounded = Math.round(num*100)/100;
	//alert("monthly_rounded = " + monthly_rounded);
	return Math.round(num*100)/100;
	}
		
	function validate_age(who) {
		//alert('validate');
		var form = document.prem_calc;
		//if(who=="saf" || who=="nonsaf") {
		if((who=="ltd")||(who=="std")) {
			if(!form.age.value.match(/\d/g)){
				alert("Please enter your age.");
				form.age.focus();
				return false;
			}
		}
	return true;
	}
	
	function salaryMax() {
		if(document.getElementById('myAmt').value>90000){
			alert("Please enter salary amount of $90,000 or less.");
			document.getElementById('myAmt').focus();
			return false;			
		}else {
			return true
		}
	}
	
	function init() {
		//alert("clearing premium field");
		document.getElementById('planperiod').value = "";
		
	}
	
	function calc_prem(who) {
		init();
		resetSalary();
		var annualben = document.getElementById('myAmt').value;
		var monthly = (annualben / 12);
		//alert("annualben = " + annualben + "\nmonthly = " + monthly);			
		//passes 'monthly' to rounding fx and stores result in 'myAmt_hold'
		document.getElementById('myAmt_hold').value = roundAccuracy3(monthly, -3);
		
		var form = document.prem_calc;				
		if(who=="ltd" && validate_age(who) && salaryMax() && formatAsCurrency(monthly)) {
			//alert('validation returns true');
			
			with (form) {
				monthlyPrem = (document.getElementById('myAmt_hold').value) * rate_lookup_ltd(age.value)/100;
				myPrem2 = (monthlyPrem)*12;
				myPrem3 = (myPrem2)/26;
				//alert("LTD Premium\nmyPrem2 = " + myPrem2 + "\nmyPrem3 = " + myPrem3);
				document.getElementById('myAmt').value = formatAsCurrency(document.getElementById('myAmt').value);
				form.planperiod.value = formatAsCurrency(myPrem3);
			}
		}
		
		if(who=="std" && validate_age(who) && salaryMax() && formatAsCurrency(monthly)) {
			//alert('validation returns true');
			
			with (form) {
				//alert('myAmt_hold = ' + document.getElementById('myAmt_hold').value);
				monthlyPrem = (document.getElementById('myAmt_hold').value) * rate_lookup_std(age.value)/100;
				myPrem2 = (monthlyPrem)*12;
				myPrem3 = (myPrem2)/26
				//alert("STD Premium\nmyPrem2 = " + myPrem2 + "\nmyPrem3 = " + myPrem3);
				document.getElementById('myAmt').value = formatAsCurrency(document.getElementById('myAmt').value);
				form.planperiod.value = formatAsCurrency(myPrem3);
			}
		}
		
		/*document.getElementById('myAmt').value = formatAsCurrency(document.getElementById('myAmt').value);
		form.planperiod.value = formatAsCurrency(myPrem3);*/
		
		}
	
	function rate_lookup_ltd(myAge) {
		//if(myAge < 30){rate=.00084}
		if(myAge <=25){rate=.35}
		if(myAge >=25 && myAge <=29){rate=.43}
		if(myAge >=30 && myAge <=34){rate=.52}
		if(myAge >=35 && myAge <=39){rate=.65}
		if(myAge >=40 && myAge <=44){rate=.93}
		if(myAge >=45 && myAge <=49){rate=1.41}
		if(myAge >=50 && myAge <=54){rate=2.09}
		if(myAge >=55 && myAge <=59){rate=2.78}
		if(myAge >=60 && myAge <=64){rate=2.95}
		if(myAge >=65){rate=3.16}
		return rate;
	}
	
	function rate_lookup_std(myAge) {
		//if(myAge < 30){rate=.00084}
		if(myAge >=17 && myAge <=49){rate=.93}
		if(myAge >=50 && myAge <=69){rate=1.26}
		return rate;
	}
	
	function resetSalary() {
		if(document.getElementById('myAmt').value != ""){
			document.getElementById('myAmt').value=document.getElementById('myAmt').value.replace(/,/g,"");
			document.getElementById('myAmt').value=document.getElementById('myAmt').value.replace(/[$]/g,"");
			document.getElementById('myAmt').value=document.getElementById('myAmt').value.split(".",1)
		}
		//if(document.getElementById('basic_earnings').value<1000){
			//alert("The minimum salary is $1,000.");
			//document.getElementById('basic_earnings').value=1000;
		//}
	}
	

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

