/* Functions for State of California calculators
	June 2004 - J.Lalic
*/
// Premium Functions ----------------------------------------------------------
function roundAccuracy2(num, acc){
	var factor=Math.pow(10,acc);
	return Math.floor(num*factor)/factor;
}
	
	
	function wants_check(who) {
		var form = document.prem_calc;
		with (form) {
			if(who=="saf" || who=="nonsaf") {
				myAmt = document.prem_calc.wants[document.prem_calc.wants.selectedIndex].value -0;
				//alert(myAmt);
			}
		}
	}
	
	function validate_age(who) {
		var form = document.prem_calc;
		if(who=="saf" || who=="nonsaf") {
			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.prem_calc;
		if(form.myAmt.value == ''){
			alert('Please enter Monthly Base Salary');
			form.myAmt.focus();
			return false;
		}
		if((document.getElementById('ben_option').options[1].selected == true)&&(document.prem_calc.myAmt.value>10910)){
			alert("The maximum monthly salary for this benefit option is $10,910.00.");
			document.prem_calc.myAmt.value=10910;
		} 
		if((document.getElementById('ben_option').options[2].selected == true)&&(document.prem_calc.myAmt.value>9231)){
			alert("The maximum monthly salary for this benefit option is $9,231.00.");
			document.prem_calc.myAmt.value=9231;
		}		

		return true;
	}
		
	function calc_prem(who) {
	
		resetSalary();
			
		//salary = roundAccuracy2(salary, -3);		
				
		var form = document.prem_calc;
		if(who=="nonsaf" && validate_age(who) && validate_amt(who)) {
			var salary = document.getElementById('myAmt').value;
			with (form) {
				if(document.getElementById('ben_option').options[0].selected == true){
					alert('Please select a Benefit Option');return false;
				}
				
				if(document.getElementById('ben_option').options[1].selected == true){
					plan1Prem = (salary * rate_lookup_plan1_nonsaf(age.value)) + .8;
					plan.value = formatAsCurrency(plan1Prem);
				} else if(document.getElementById('ben_option').options[2].selected == true){
					plan2Prem = (salary * rate_lookup_plan2_nonsaf(age.value)) + .8;
					plan.value = formatAsCurrency(plan2Prem);
					//document.getElementById('myAmt').value = formatAsCurrency(salary);
				}
			}
			form.myAmt.value = formatAsCurrency(salary);
		}
		
		if(who=="saf" && validate_age(who) && validate_amt(who)) {
			var salary = document.getElementById('myAmt').value;
			with (form) {			
				if(document.getElementById('ben_option').options[0].selected == true){
					alert('Please select a Benefit Option');return false;
				}				
				
				if(document.getElementById('ben_option').options[1].selected == true){
					plan1Prem = (salary * rate_lookup_plan1_saf(age.value)) + .8;
					//alert(plan1Prem);
					plan.value = formatAsCurrency(plan1Prem);
				} else if(document.getElementById('ben_option').options[2].selected == true){
					plan2Prem = (salary * rate_lookup_plan2_saf(age.value)) + .8;
					//alert(plan2Prem);
					plan.value = formatAsCurrency(plan2Prem);
				}
				//alert(plan.value);
			}
			form.myAmt.value = formatAsCurrency(salary);
		}
		
	}
	
	function rate_lookup_plan1_saf(myAge) {
		if(myAge < 30){rate=.00068}
		if(myAge >=30 && myAge <=39){rate=.00156}
		if(myAge >=40 && myAge <=49){rate=.00360}
		if(myAge >=50 && myAge <=59){rate=.00768}
		if(myAge >=60){rate=.01040}
		return rate;
	}
	
	function rate_lookup_plan1_nonsaf(myAge) {
		if(myAge < 30){rate=.00049}
		if(myAge >=30 && myAge <=39){rate=.00107}
		if(myAge >=40 && myAge <=49){rate=.00253}
		if(myAge >=50 && myAge <=59){rate=.00535}
		if(myAge >=60){rate=.00729}
		return rate;
	}

	function rate_lookup_plan2_saf(myAge) {
		if(myAge < 30){rate=.00117}
		if(myAge >=30 && myAge <=39){rate=.00283}
		if(myAge >=40 && myAge <=49){rate=.00656}
		if(myAge >=50 && myAge <=59){rate=.01405}
		if(myAge >=60){rate=.01895}
		return rate;
	}
	
	function rate_lookup_plan2_nonsaf(myAge) {
		if(myAge < 30){rate=.00084}
		if(myAge >=30 && myAge <=39){rate=.00199}
		if(myAge >=40 && myAge <=49){rate=.00457}
		if(myAge >=50 && myAge <=59){rate=.00980}
		if(myAge >=60){rate=.01330}
		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('ben_option').options[1].selected == true)&&(document.prem_calc.myAmt.value>10910)){
				alert("The maximum monthly salary for this benefit option is $10,910.00.");
				document.prem_calc.myAmt.value=10910;
			} 
			if((document.getElementById('ben_option').options[2].selected == true)&&(document.prem_calc.myAmt.value>9231)){
				alert("The maximum monthly salary for this benefit option is $9,231.00.");
				document.prem_calc.myAmt.value=9231;
			}*/

		//if(document.getElementById('basic_earnings').value<1000){
			//alert("The minimum salary is $1,000.");
			//document.getElementById('basic_earnings').value=1000;
		//}
	}