/* 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=.00076}
		if(myAge >=30 && myAge <=39){rate=.00173}
		if(myAge >=40 && myAge <=49){rate=.00400}
		if(myAge >=50 && myAge <=59){rate=.00853}
		if(myAge >=60){rate=.01156}
		return rate;
	}
	
	function rate_lookup_plan1_nonsaf(myAge) {
		if(myAge < 30){rate=.00054}
		if(myAge >=30 && myAge <=39){rate=.00119}
		if(myAge >=40 && myAge <=49){rate=.00281}
		if(myAge >=50 && myAge <=59){rate=.00594}
		if(myAge >=60){rate=.00810}
		return rate;
	}

	function rate_lookup_plan2_saf(myAge) {
		if(myAge < 30){rate=.00130}
		if(myAge >=30 && myAge <=39){rate=.00314}
		if(myAge >=40 && myAge <=49){rate=.00729}
		if(myAge >=50 && myAge <=59){rate=.01561}
		if(myAge >=60){rate=.02105}
		return rate;
	}
	
	function rate_lookup_plan2_nonsaf(myAge) {
		if(myAge < 30){rate=.00093}
		if(myAge >=30 && myAge <=39){rate=.00221}
		if(myAge >=40 && myAge <=49){rate=.00508}
		if(myAge >=50 && myAge <=59){rate=.01089}
		if(myAge >=60){rate=.01478}
		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;
		//}
	}