// employer category factor
var icf = new Array(0.90, 1.00, 1.30, 1.90, 2.30);

function check_form()
{
	return true;
}

/*
 * Show the div of the element in question, governed by el_id
 */
function show_div(el_id)
{
	el = d.getElementById(el_id);
	
	if (ie) {
		el.className = 'show';
	}
	else {
		el.setAttribute('class', 'show');
	}
}

/*
 * Hide the div of the element in question, governed by el_id
 */
function hide_div(el_id)
{
	el = d.getElementById(el_id);
	if (ie) {
		el.className = 'hide';
	}
	else {
		el.setAttribute('class', 'hide');
	}
	
	// find all the elements within the div
	var children = el.getElementsByTagName('input');
	for (i = 0; i < children.length; i++) {
		type = children[i].getAttribute('type');
		
		switch (type)
		{
			case 'checkbox':
			case 'radio':
				children[i].checked = false;
				break;
			case 'select':
				children[i].selectedIndex = 0;
				break;
			case 'text':
			case 'textarea':
				children[i].value = '';
				break;
		}
	}
}

function basic_cover_options()
{
	var uc = getCheckboxValue('units_cover');
	var age = d.app.age.value;
	var ct = getCheckboxValue('cover_type'); // cover type
	
	// fix
	bonus_ip_check();
	
	// calculate the basic cover
	var bc = d.getElementById('bcalc');
	var cover = calc_basic_cover();
	var cost = (ct == 'death&TPC') ? 5.00 : 2.38;
	if (uc) {
		bc.innerHTML = 'Your basic cover amount is calculated to be '+formatCurrency(cover[3] * uc)+' and will cost '+formatCurrency(cost * 12 * uc)+' ('+formatCurrency(cost * uc)+' per month)';
	}
	
	if (uc) {
		show_div('section_additional_insurance');
	}
}

function bonus_ip_check()
{
	var uc = getCheckboxValue('units_cover');
	if (d.app.age.value <= 64 || (uc > 2 && d.app.age.value >= 65)) {
		show_div('section_new2fss');
	}
	else {
		hide_div('section_new2fss');
		state_ip();
	}
}

function basic_plus_cover_check()
{
	if (d.app.occupation_group[0].checked && d.app.salary.value.replace(/,/g, '') >= 80000) {
		show_div('section_basic_plus_cover');
	}
	else {
		hide_div('section_basic_plus_cover');
		show_div('section_cover_type');
	}
}

function state_bip()
{
	if (d.app.new2fss[0].checked &&
		d.app.short_form[0].checked &&
		d.app.participating_employer[0].checked &&
		d.app.bonus_income_protection[0].checked)
	{
		show_div('section_b_waiting_period');
		show_div();
	}
	else {
		hide_div('section_b_waiting_period');
	}
}

/*
 * Maintains the pricing and totals of insurance dynamically
 */
function onchange_update_aiub()
{
	
}

function formatCurrency(num) 
{
	if (num) {
		num = num.toString().replace(/\$|,/g,'');
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}
}

/*
 * The next 3 functions are called depending on the type of insurance selected
 */
function state_death_tpc()
{
	//show_div('section_additional_insurance');
	hide_div('section_additional_units');
	show_div('section_units_cover');
	hide_div('section_additional_units_type');
	hide_div('section_additional_fixed');
	
	// hide specific fields
	show_div('additional_type_deathtpc');
	
	basic_plus_cover_check();
}
function state_death_only()
{
	//show_div('section_additional_insurance');
	hide_div('section_additional_units');
	show_div('section_units_cover');
	hide_div('section_additional_units_type');
	hide_div('section_additional_fixed');
	
	// hide specific fields
	hide_div('additional_type_deathtpc');
	
	basic_plus_cover_check();
}
function state_cover_none()
{
	hide_div('section_additional_insurance');
	hide_div('section_additional_units');
	hide_div('section_units_cover');
	hide_div('section_additional_units_type');
	hide_div('section_additional_fixed');
	hide_div('section_protection_amount');
	//hide_div('section_participating');
	hide_div('section_short_form');
	hide_div('section_bonus_startup');
	hide_div('section_bonus_protection');
	hide_div('section_bonus_waiting');
	hide_div('section_bonus_protection_amount');
	hide_div('section_bonus_calc');
	
	bonus_ip_check();
	//state_ip();
	
	basic_plus_cover_check();
}

/*
 * Deals with the income protection insurance question
 */
function state_income_protection(answer)
{
	if (answer) {
		show_div('section_protection_amount');
		show_div('section_waiting');
	}
	else {
		hide_div('section_protection_amount');
		hide_div('section_waiting');
	}
}

function state_additional_cover_unit()
{
	onchange_update_aiub();
	show_div('section_additional_units_type');
	hide_div('section_additional_units');
	hide_div('section_additional_fixed');
	hide_div('section_additional_salary');
	
	var aRadioAdditionalCoverTypes = document.getElementsByName('additional_unit_type');
	for (var i=0; i<aRadioAdditionalCoverTypes.length; i++)
	{
		aRadioAdditionalCoverTypes[i].checked = false;
		
		//auto-selects death-only additional insurance if cover type is death only
		if(getCheckboxValue('cover_type') == 'deathonly' )
		{
			if(aRadioAdditionalCoverTypes[i].value == 'deathonly')
			{
				aRadioAdditionalCoverTypes[i].checked = true;
				state_additional_insurance_type_death_only();
				hide_div('section_additional_units_type'); 
			}
		}
	}
	
	//show normal income protection if they don't select 1 or 2 units of basic cover
	if (d.app.units_cover[0].checked || d.app.units_cover[1].checked) {
		state_ip();
	}
}

function state_additional_cover_fixed()
{
	show_div('section_additional_units_type');
	hide_div('section_additional_units');
	show_div('section_additional_fixed');
	hide_div('section_additional_salary');
	state_ip();
	//show_div('section_additional_salary');
	
	var aRadioAdditionalCoverTypes = document.getElementsByName('additional_unit_type');
	for (var i=0; i<aRadioAdditionalCoverTypes.length; i++)
	{
		aRadioAdditionalCoverTypes[i].checked = false;
		
		//auto-selects death-only additional insurance if cover type is death only
		if(getCheckboxValue('cover_type') == 'deathonly' )
		{
			if(aRadioAdditionalCoverTypes[i].value == 'deathonly')
			{
				aRadioAdditionalCoverTypes[i].checked = true;
				state_additional_insurance_type_death_only();
				hide_div('section_additional_units_type'); 
			}
		}		
	}
	
	//show normal income protection
	state_ip();
}
function state_additional_cover_none()
{
	hide_div('section_additional_units_type');
	hide_div('section_additional_units');
	hide_div('section_additional_fixed');
	hide_div('section_additional_salary');
	
	//show normal income protection
	state_ip();
}

function getCheckboxValue( checkbox_name )
{
	var temp_obj = document.getElementsByName(checkbox_name);
	for (var i=0; i<temp_obj.length; i++) 
	{
		if(temp_obj[i].checked == true)
		{
			return temp_obj[i].value ;
		}
	}
	return false; //nothing checked
}

function state_additional_insurance_type_death_tpd()
{
	var bChecked = false;
	var aRadios = document.getElementsByName('additional_cover');
	var sCoverType = 'no';
	for (var i=0; i<aRadios.length; i++) 
	{
		if (aRadios[i].checked) {
			bChecked = true;
			sCoverType = aRadios[i].value;
			break;
		}
	}
	if (bChecked === false) {
		return;
	}

	if (sCoverType == 'yesunit')
	{
		show_div('section_additional_units'); 
		onchange_update_aiub();
	}
	else if (sCoverType == 'yesfixed')
	{
		// do nothing
	}
}

function state_additional_insurance_type_death_only()
{
	var bChecked = false;
	var aRadios = document.getElementsByName('additional_cover');
	var sCoverType = 'no';
	for (var i=0; i<aRadios.length; i++) 
	{
		if (aRadios[i].checked) {
			bChecked = true;
			sCoverType = aRadios[i].value;
			break;
		}
	}
	if (bChecked === false) {
		return;
	}

	if (sCoverType == 'yesunit')
	{
		show_div('section_additional_units'); 
		onchange_update_aiub();
	}
	else if (sCoverType == 'yesfixed')
	{
		// do nothing
	}
}

function state_bonus()
{
	if (d.app.bonus_income_protection[0].checked) {
		show_div('section_bonus_waiting');
		show_div('section_bonus_protection_amount');
		hide_div('section_waiting');
		hide_div('section_income_protection');
	}
	else {
		hide_div('section_bonus_waiting');
		hide_div('section_bonus_protection_amount');
		show_div('section_income_protection');
	}
}

function state_bonus_calc()
{
	get_protection_calc();
}

function get_protection_calc()
{
	var age = d.app.age.value;
	
	if (d.app.bonus_income_protection[1].checked || (d.app.bonus_income_protection[0].checked && d.app.ipcheck[1].checked)) {
		// normal income protection
		f_ipp = 'income_protection_percentage';
		f_wp = 'waiting_period'; // waiting period field
	}
	else {
		f_ipp = 'bonus_income_protection_percentage';
		f_wp = 'bonus_waiting_period';
	}
	
	if (d.app.basic_plus_cover[0].checked) {
		c_factor = icf[0];
	}
	else {
		switch (getCheckboxValue('employer_group')) {
			case '0':
			case '4':
				c_factor = icf[1];
				break;
			case '1':
			case '5':
				c_factor = icf[2];
				break;
			case '2':
			case '6':
				c_factor = icf[3];
				break;
			case '3':
			case '7':
				c_factor = icf[4];
				break;
		}
	}
	
	// get the waiting period index of the array that we'll need to use from the ageWaiting array
	switch (d.app[f_wp].value) {
		case '14':
			wait = 0;
			break;
		case '30':
			wait = 1;
			break;
		case '60':
			wait = 2;
			break;
		case '90':
			wait = 3;
			break;
	}
	
	var info = get_cap();
	var salary = info[0];
	var cap = info[1];
	
	// the percentage to be applied
	var percentage = (d.app[f_ipp].value / 100);
	salary *= percentage;
	
	// salary check
	if (salary > cap) {
		salary = cap;
		var show_question = true;
	}
	
	//alert("Salary: "+salary+"\nAge Waiting: "+ageWaiting[age][wait]+"\nCategory Factor: "+c_factor);
	
	if (d.app.bonus_income_protection[0].checked) {
		// they're going for bonus income protection, so calcs are slightly different
		
		var res = (salary / 1000) * ageWaiting[age][wait] * c_factor;
		var monthly = res / 12;
		
		if (show_question)
		{
			show_div('section_bonus_calc');
			d.getElementById('bonuc_calc').innerHTML = formatCurrency(salary) + ' and will cost '+formatCurrency(res)+' per year';
		}
	}
}

function validate_bp_salary(el)
{
	if (el.value < 80000) {
		alert('You must earn a minimum of $80,000 to be eligible for Basic Plus');
	}
}

function state_age()
{
	show_div('section_salary');
	onchange_update_aiub();
}

function state_salary()
{
	show_div('section_employer_group');
	onchange_update_aiub();
}

function state_ip()
{
	if (d.app.age.value < 65) {
		show_div('section_income_protection');
	}
	else {
		hide_div('section_income_protection');
	}
}

function state_ipcheck(state)
{
	if (false == state) {
		// show area if the bonus start-up isn't enough
		show_div('section_income_protection');
	}
	else {
		hide_div('section_income_protection');
		hide_div('section_waiting');
		hide_div('section_protection_amount');
	}
}

function state_occ_group()
{
	//show_div('section_cover_type');
	onchange_update_aiub();
	//basic_cover_options();
	basic_plus_cover_check();
}

function state_bonus_waiting()
{
	ip_percentage_calc();
}

function ip_percentage_calc(type)
{
	var info = get_cap();
	var salary = info[0];
	var cap = info[1];
	var fifty = (salary * 0.50 > cap) ? cap : salary * 0.50;
	var seventy_five = (salary * 0.75 > cap) ? cap : salary * 0.75;
	
	if ('normal' == type) {
		var oOptions = d.getElementById('income_protection_percentage').options;
	}
	else {
		var oOptions = d.app.bonus_income_protection_percentage.options;
	}
	
	for (var i = 0; i < oOptions.length; i++)
	{
		if(oOptions[i].value == '75') {
			oOptions[i].text = "75% - " + formatCurrency(seventy_five) +" (75% of salary to a maximum of "+formatCurrency(cap)+")";
		}
		
		if(oOptions[i].value == '50') {
			oOptions[i].text = "50% - " + formatCurrency(fifty) +" (50% of salary to a maximum of "+formatCurrency(cap)+")";
		}
	}
}

function get_cap()
{
	var salary = d.app.salary.value.replace(/,/g, '');
	
	if (!d.app.income_protection[0].checked) {
		// they want bonus IP, so the cap is different
		if (d.app.basic_plus_cover[0].checked){
			// basic plus
			cap = 120000;
		}
		else {
			// base it on the employer category
			var eg_val = getCheckboxValue('employer_group');
			
			// in this switch statement, if bonus IP is selected - caps are slightly different
			switch (eg_val) {
				case '0':
				case '4':
				case '1':
				case '5':
					cap = 72000;
					break;
				case '2':
				case '6':
					cap = (d.app.bonus_income_protection[0].checked && eg_val == 6) ? 60000 : 36000;
					break;
				case '3':
				case '7':
					cap = (d.app.bonus_income_protection[0].checked && eg_val == 7) ? 36000 : 60000;
					break;
			}
		}
	}
	else {
		cap = 240000;
	}
	
	return new Array(salary, cap);
}

function calc_additional_fixed()
{
	var fixed_value = d.app.fixed_amount.value.replace(/[^\d]/g, '');
	var cover = calc_basic_cover();
	var oc_basic_cover = cover[0];
	var oc_coverValue = cover[1];
	var ec_basic_cover = cover[2];
	var ec_coverValue = cover[3];
	
	var coverValue = (oc_coverValue >= ec_coverValue) ? oc_coverValue : ec_coverValue;
	
	if (d.app.additional_unit_type[0].checked) { // death and tpd additional 
		cap = 2000000;
	}
	else if(d.app.additional_unit_type[1].checked) {
		cap = 5000000;
	}
	else {
		// the additional cover type wasn't shown, as they chose death only in the first basic cover type
		cap = 5000000;
	}
	
	// see if they've selected the bonus unit
	if (d.app.new2fss[0].checked && d.app.short_form[0].checked && d.app.bonus_startup[0].checked) {
		ec_basic_cover += ec_coverValue;
	}
	
	var total_allowed = cap - ec_basic_cover;
	
	if (fixed_value > total_allowed) {
		alert('You cannot exceed the maximum cover amount for basic and additional cover. You can have a maximum of '+formatCurrency(total_allowed)+' of additional cover.');
		
		d.app.fixed_amount.value = '';
		d.app.fixed_amount.disabled = false;
	}
	
	return;
}

function calc_basic_cover()
{
	var age = d.app.age.value;
	if (age >= 15 && age <= 35) age = 15; // this is just for the array reference - no indexes exist between 16 and 35, inclusive
	var bpc = d.app.basic_plus_cover[0].checked; // basic plus cover check
	
	if (bpc) {
		// only need to resort to one field: the last one (basic_plus)
		var coverValue = coverValue2 = basicCover[age][3];
	}
	else {
		var oc = getCheckboxValue('occupation_group');
		var ec = getCheckboxValue('employer_group');
		
		switch (oc) {
			case '4': // White Collar
				var coverValue = basicCover[age][0];
				break;
			case '5': // Light Manual
				var coverValue = basicCover[age][1];
				break;
			case '6': // Manual
			case '7': // Heavy Manual
				var coverValue = basicCover[age][2];
				break;
		}
		
		switch (ec) {
			case '0': 
			case '4': // White Collar
				var coverValue2 = basicCover[age][0];
				break;
			case '1': // Light Manual
			case '5': // Light Manual
				var coverValue2 = basicCover[age][1];
				break;
			case '2': // Manual
			case '6': // Manual
			case '3': // Heavy Manual
			case '7': // Heavy Manual
				var coverValue2 = basicCover[age][2];
				break;
		}
	}
	
	var bc  = coverValue * getCheckboxValue('units_cover');
	var bc2 = coverValue2 * getCheckboxValue('units_cover');
	if (d.app.bonus_startup[0].checked) {
		if (getCheckboxValue('startup_type') != false) {
			// add an extra unit
			bc  += coverValue;
			bc2 += coverValue2;
		}
	}
	
	// update the innerHTML prices
	var ret    = new Array();
		ret[0] = bc;			// basic cover
		ret[1] = coverValue;	// total cover value
		ret[2] = bc2;			// basic cover value - two
		ret[3] = coverValue2;	// total cover value - two (from the employer group)
	
	return ret;
}

function calc_additional_units()
{
	var bc = (getCheckboxValue('cover_type') == 'death&TPC') ?  5.00 : 2.38;
	var at = (getCheckboxValue('additional_unit_type') == 'death&TPC') ?  5.00 : 2.38;
	
	var cover = calc_basic_cover();
	var oc_basic_cover = cover[0];
	var oc_coverValue = cover[1];
	var ec_basic_cover = cover[2];
	var ec_coverValue = cover[3];
	var amount_max_do = 5000000; 	// death only max
	var amount_max_dtpd = 2000000;  // death and TPD max
	
	// basic cover calculations - this will tell us how much is left over for additional cover
	var ret = new Array();
	ret['basic_cover_dtpd_left'] = amount_max_dtpd; 	// death and tpd
	ret['basic_cover_do_left'] = amount_max_do; 		// death only
	
	// now in additional cover, we work off the left-over costs
	var formatted_units = d.app.additional_units.value.replace(/[^\d]/g, '');
	d.app.additional_units.value = formatted_units; // remove commas/dots.etc
	
	var add_units = formatted_units;
	
	// set the new cover value
	var coverValue = (oc_coverValue >= ec_coverValue) ? oc_coverValue : ec_coverValue;
	
	var total_additional_units_cover = coverValue * add_units; // occupational category
	
	if (d.app.cover_type[0].checked && d.app.additional_unit_type[0].checked) {
		// death and tpd on both
		var field_check = 'basic_cover_dtpd_left';
		var max_amount = amount_max_dtpd;
	}
	else if (d.app.cover_type[0].checked && d.app.additional_unit_type[1].checked) {
		// death and tpd for basic, death only for additional
		var field_check = 'basic_cover_do_left';
		var max_amount = amount_max_do;
	}
	else {
		// death only for both
		var field_check = 'basic_cover_do_left';
		var max_amount = amount_max_do;
	}
	
	// see if they've selected the bonus unit
	if (d.app.new2fss[0].checked && d.app.short_form[0].checked && d.app.bonus_startup[0].checked) {
		ec_basic_cover += ec_coverValue;
	}
	
	//debug = "oc cover value: "+oc_additional_cover+"\nec cover value: "+ec_additional_cover;
	//alert(debug);
	var total_cover_allowed = ret[field_check] - ec_basic_cover;
	
	if (total_cover_allowed - total_additional_units_cover < 0) {
		var max_units = Math.floor(total_cover_allowed / coverValue);
		
		alert('You cannot exceed the maximum cover amount for basic and additional cover. You can have a maximum of '+max_units+' units of additional cover. ');
		
		d.app.additional_units.value = '';
		d.app.additional_units.disabled = false;
		
		return;
	}
	else {
		// work out the monthly cost
		var month_cost = add_units * at;
		var cover_display = total_additional_units_cover;
		
		d.getElementById('add_cover_text').innerHTML = 'Your additional cover amount is calculated to be '+formatCurrency(cover_display)+' and will cost '+formatCurrency(month_cost * 12)+' ('+formatCurrency(month_cost)+' per month)';
	}
	
	state_ip();
}

function calc_bonus_unit()
{
	var bonus_type = getCheckboxValue('cover_type');
	var cover = calc_basic_cover();
	var bcd = d.getElementById('bonuscalc');
	var cost = ('death&TPC' == bonus_type) ? 5.00 : 2.38;
	
	bcd.innerHTML = 'Your bonus unit of cover is calculated to be '+formatCurrency(cover[3])+' and will cost '+formatCurrency(cost * 12)+' ('+formatCurrency(cost)+' per month)';
}

/* p_form_state
 * Instead of using the previous functions that had been declared, I decided to write a function
 * that could save the state of the entire form and render fields depending on certain choices
 * that the user has made.
 * 
 * @param string field
 * @author Kirk Bushell
 * @date 13/12/2007
 */
var s = new Array();
function p_form_state(field)
{
	var salary = d.app.salary.value.replace(/,/g, ''); // remove commas
	var age = d.app.age.value;
	var occg = getCheckboxValue('occupation_group');
	var bp = getCheckboxValue('basic_plus_cover');
	var ac = getCheckboxValue('additional_cover');
	var sf = getCheckboxValue('short_form');
	var at = getCheckboxValue('additional_unit_type');
	var au = d.app.additional_units.value.replace(/[,\.]/g, '');
	var fa = d.app.fixed_amount.value.replace(/,/g, ''); // remove commas
	var ip = getCheckboxValue('income_protection');
	
	switch (field)
	{
		case 'age':
			// show the next field
			s.p_salary = true;
			break;
		case 'salary':
			s.p_occg = true;
			
			if (salary < 80000) {
				s.p_basic_plus = false;
				s.p_short_form = false;
			}
			break;
		case 'occg':
			if (4 == occg  && salary >= 80000) {
				// show the short form div
				s.p_short_form = true;
				s.p_additional = false;
			}
			else {
				s.p_short_form = false;
				s.p_basic_plus = false;
				s.p_additional = true;
			}
			break;
		case 'short_form':
			if ('yes' == sf) {
				s.p_basic_plus = true;
			}
			else {
				s.p_basic_plus = false;
				s.p_additional = true;
			}
			break;
		case 'bp':
			s.p_additional = true;
			break;
		case 'additional':
			if ('yesfixed' == ac) {
				s.p_add_fixed = false;
				s.p_add_units_type = true;
				s.p_add_units = false;
			}
			else if ('yesunit' == ac) {
				s.p_add_fixed = false;
				s.p_add_units_type = true;
			}
			else if ('no' == ac) {
				s.p_add_fixed = false;
				s.p_add_units_type = false;
				s.p_add_units = false;
				if (age <= 64) {
					// only show IP if they're less than 65 years of age
					s.p_ip = true;
				}
			}
			break;
		case 'add_units_type':
			if ('yesfixed' == ac) {
				s.p_add_fixed = true;
			}
			else if ('yesunit' == ac) {
				s.p_add_units = true
			}
			break;
		case 'add_units':
			// reset the value which has been formatted to remove commas and decimal points
			d.app.additional_units.value = au;
			
			if ('death&TPC' == at) {
				multiplier =  5.00;
				cap = 2000000;
			}
			else {
				multiplier = 2.38;
				cap = 5000000;
			}
			
			// age used for calcs
			var age_tmp = age;
			if (age_tmp >= 15 && age_tmp <= 35) age_tmp = 15;
			
			if ('yes' == bp) {
				coverValue = basicCover[age_tmp][3];
			}
			else {
				switch (occg) {
					case '4': // White Collar
						coverValue = basicCover[age_tmp][0];
						break;
					case '5': // Light Manual
						coverValue = basicCover[age_tmp][1];
						break;
					case '6': // Manual
					case '7': // Heavy Manual
						coverValue = basicCover[age_tmp][2];
						break;
				}
			}
			
			// work out the max_units
			var max_units = Math.floor(cap / coverValue);
			if (max_units < au) {
				alert('You cannot exceed the maximum cover amount for insurance. You can have a maximum of '+max_units+' units of insurance cover.');
				d.app.additional_units.value = ''; // reset the field
				d.app.additional_units.disabled = false; // reset the field
			}
			else {
				// all is well, show them how much it will cost:
				var month_cost = au * multiplier;
				var additional_cover = coverValue * au;
				
				d.getElementById('add_cover_text').innerHTML = 'Your additional cover amount is calculated to be '+formatCurrency(additional_cover)+' and will cost '+formatCurrency(month_cost * 12)+' ('+formatCurrency(month_cost)+' per month)';
				if (age <= 64) {
					s.p_ip = true;
				}
			}
			
			break;
		case 'add_fixed':
			if ('death&TPC' == at) {
				cap = 2000000;
			}
			else {
				cap = 5000000;
			}
			
			if (fa > cap) {
				alert('You cannot exceed the maximum amount of insurance cover. You can have a maximum $5,000,000 for death only and $2,000,000 for death & TPD insurance cover.');
				d.app.fixed_amount.value = '';
				d.app.fixed_amount.disabled = false;
			}
			else {
				if (age <= 64) {
					s.p_ip = true;
				}
			}
			
			break;
		case 'p_ip':
			if ('yes' == ip) {
				s.p_waiting = true;
			}
			else {
				s.p_waiting = false;
				s.p_ip_amount = false;
			}
			
			break;
		case 'p_waiting':
			var cap = 240000;
			var fifty = (salary * 0.50 > cap) ? cap : salary * 0.50;
			var seventy_five = (salary * 0.75 > cap) ? cap : salary * 0.75;
			var oOptions = d.getElementById('income_protection_percentage').options;
			
			for (var i = 0; i < oOptions.length; i++)
			{
				if(oOptions[i].value == '75') {
					oOptions[i].text = "75% - " + formatCurrency(seventy_five) +" (75% of salary to a maximum of "+formatCurrency(cap)+")";
				}
				
				if(oOptions[i].value == '50') {
					oOptions[i].text = "50% - " + formatCurrency(fifty) +" (50% of salary to a maximum of "+formatCurrency(cap)+")";
				}
			}
			s.p_ip_amount = true;
			break;
	}
	
	// loop through each of the form field states and do the appropriate behaviour
	for (var i in s) {
		if (s[i]) {
			show_div(i);
		}
		else {
			hide_div(i);
		}
	}
}

/*
* da
 * This function is used to handle the onclick event handler for disabling elements as the user passes through it
 */
function da(e)
{
	switch (this.tagName.toLowerCase())
	{
		case 'input':
			if (this.type == 'radio') {
				// we want to disable all the elements of this name
				var radios = document.getElementsByName(this.name);
				for (var i = 0; i < radios.length; i++) {
					radios[i].disabled = true;
				}
			}
			if (this.type == 'text') {
				if (this.value != '') {
					this.disabled = true;
				}
			}
			break;
		case 'select':
			if (this.value != '') {
				this.disabled = true;
			}
			break;
	}
}

/*
 * restore
 * Restores all input and select elements to their former glory, by ensuring they're no longer disabled before submission
 */
function restore()
{
	var inputs  = d.getElementsByTagName('input');
	var selects = d.getElementsByTagName('select');

	for (i = 0; i < inputs.length; i++) {
		inputs[i].disabled = false;
	}
	for (i = 0; i < selects.length; i++) {
		selects[i].disabled = false;
	}
	
	return true;
}

/*
 * addEvent
 * Cross-browser compatible javascript function for adding event listeners:
 */
function addEvent(ele, type, func)
{
	if (ele.addEventListener) {
		ele.addEventListener(type, func, true);
	} else if (ele.attachEvent) {
		xb.addEvent(ele, type, func, true);
	} else {
		ele['on'+type] = func;
	}
}

// IE ADDEVENT HAX
var xb =
{
   evtHash: [],

   ieGetUniqueID: function(_elem)
   {
      if (_elem === window) { return 'theWindow'; }
      else if (_elem === document) { return 'theDocument'; }
      else { return _elem.uniqueID; }
   },

   addEvent: function(_elem, _evtName, _fn, _useCapture)
   {
      if (typeof _elem.addEventListener != 'undefined')
      { _elem.addEventListener(_evtName, _fn, _useCapture); }
      else if (typeof _elem.attachEvent != 'undefined')
      {
         var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) +
               '::evt_' + _evtName + '::fn_' + _fn + '}';
         var f = xb.evtHash[key];
         if (typeof f != 'undefined')
            { return; }

         f = function()
         {
            _fn.call(_elem);
         };

         xb.evtHash[key] = f;
         _elem.attachEvent('on' + _evtName, f);

         // attach unload event to the window to clean up possibly IE memory leaks
         window.attachEvent('onunload', function()
         {
            _elem.detachEvent('on' + _evtName, f);
         });

         key = null;
         //f = null;   /* DON'T null this out, or we won't be able to detach it */
      }
      else
         { _elem['on' + _evtName] = _fn; }
   },

   removeEvent: function(_elem, _evtName, _fn, _useCapture)
   {
      if (typeof _elem.removeEventListener != 'undefined')
         { _elem.removeEventListener(_evtName, _fn, _useCapture); }
      else if (typeof _elem.detachEvent != 'undefined')
      {
         var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) +
               '::evt' + _evtName + '::fn_' + _fn + '}';
         var f = xb.evtHash[key];
         if (typeof f != 'undefined')
         {
            _elem.detachEvent('on' + _evtName, f);
            delete xb.evtHash[key];
         }

         key = null;
         //f = null;   /* DON'T null this out, or we won't be able to detach it */
      }
   }
};

function M4_print()
{
	window.resizeTo(750, 600);
	window.print();
}
