//Variables
/*
var product;
var benefitAmount;
var coverage1; var coverage2; var maxBenCov1; var maxBenCov2;
var optionalHosp; var optionalCrit;
var ratePer100; var monthlyPremium;*/
//var 12 Months Mortgage Payment Protection
//var MPPAS12     = 3.45;
//var MPPU12      = 6.03;
//var MPPASU12    = 6.09;
//var MPPASCI12   = 5.39;
//var MPPASUCI12  = 8.66;
//var MPPASH12    = 3.79;
//var MPPASHCI12  = 5.74;
//var MPPASUH12   = 6.50;
//var MPPASUHCI12 = 9.05;
//var 24 Months Mortgage Payment Protection
//var MPPAS24     = 4.48
//var MPPU24      = "invalid";
//var MPPASU24    = 7.33;
//var MPPASCI24   = 6.41;
//var MPPASUCI24  = 9.87;
//var MPPASH24    = 4.83;
//var MPPASHCI24  = 6.76;
//var MPPASUH24   = 7.72;
//var MPPASUHCI24 = 10.28;
//var 12 Months Income Protection
//var IPAS12     =  3.79;
//var IPU12      = "invalid";
//var IPASU12    = 6.70;
//var IPASCI12   = 5.93;
//var IPASUCI12  = 9.51;
//var IPASH12    = 4.18;
//var IPASHCI12  = 6.30;
//var IPASUH12   = 7.16;
//var IPASUHCI12 = 9.97;
//var 24 Months Income Protection
//var IPAS24     = 4.93;
//var IPU24      = "invalid";
//var IPASU24    = 8.05;
//var IPASCI24   = 7.06;
//var IPASUCI24  = 10.86;
//var IPASH24    = 5.30;
//var IPASHCI24  = 7.44;
//var IPASUH24   = 8.49;
//var IPASUHCI24 = 11.30;
/*function onlyNumbers(evt)
{
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;

}*/
function getRatePerHundred()
{
	
}
function calculate()
{



if( document.amountOfBenefit.monthlyBenefit.value < 100 || document.amountOfBenefit.monthlyBenefit.value > 2000)
{
		alert("Monthly Benefit must be between Euro 100 and Euro 2000");
}
else
{
/*
Get data and stoe it in invisible fields (this adds code but allows better error detection)
*/

// -- option 1
	if(document.productSelect.group1[0].checked == true)
	{
		
		document.invisible.choices0.value = 1;
		
	}
	if(document.productSelect.group1[1].checked == true)
	{
		
		document.invisible.choices0.value = 2;
	}
// -- option2
	document.invisible.choices1.value =  document.amountOfBenefit.monthlyBenefit.value;

// -- option 3
	if(document.benefits.group2[0].checked == true)
	{
		document.invisible.choices2.value = 1;
	}
	if(document.benefits.group2[1].checked == true)
	{
		document.invisible.choices2.value = 3; // IMPORTANT : Phil - this used to equal 2 . 
											  // too much hassle restructuring everything to fix it, next time use constants!!!
	}
	/*if(document.benefits.group2[2].checked == true)
	{
		document.invisible.choices2.value = 2;   // this WAS unemployment but was removed
	}*/

// -- option 4
//if (document.optionCoverages.chck1.checked == false && document.optionCoverages.chck2.checked == false)
//{
//	document.invisible.choices3.value = 0;
//} 
//if (document.optionCoverages.chck1.checked == true && document.optionCoverages.chck2.checked == false)
//{
//	document.invisible.choices3.value = 1;
//} 
//if (document.optionCoverages.chck1.checked == false && document.optionCoverages.chck2.checked == true)
//{
//		document.invisible.choices3.value = 2;
//} 
//if (document.optionCoverages.chck1.checked == true && document.optionCoverages.chck2.checked == true)
//{
//		document.invisible.choices3.value = 3;
//} 
// -- option 5
//if (document.lengthOfCover.CL12.checked == true)
//{
//	document.invisible.choices4.value = 1;
//}
//if (document.lengthOfCover.CL24.checked == true)
//{
//	document.invisible.choices4.value = 2;
//}

// First get the rates per 100


// Okay so the data of the chosen objects is stored in a hidden form.
// choices[n] = option n + 1


//if option 1 = SW	
if( document.invisible.choices0.value == "")
{
		alert ("Please select an option for section 1");
}
else if( document.invisible.choices1.value == "")
{
		alert ("Please select an option for section 2");
}
else if( document.invisible.choices2.value == "")
{
		alert ("Please select an option for section 3");
}
/*else if( document.invisible.choices3.value == "")
{
		alert ("Please select an option for section 4");
}*/
/*else if( document.invisible.choices4.value == "")
{
		alert ("Please select an option for section 5");
} */




//---------- Error checking complete -  ALLOW CALCUALTIONS TO BE PERFORMED



else if(document.invisible.choices0.value== 1)
{
	document.generatedRates.ratePer100.value="6.5";
	//choices [1] not used as it is text and is used for the premium calculations
	if(document.invisible.choices2.value== 1)
	{
			if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = MPPAS12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = MPPAS24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						
						document.generatedRates.ratePer100.value = MPPASH12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
						
						document.generatedRates.ratePer100.value = MPPASH24; //AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = MPPASCI12; // AVAILABLE
					
					}
					if(document.invisible.choices4.value== 2)
					{
				
						document.generatedRates.ratePer100.value = MPPASCI24; //AVAILABLE
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = MPPASHCI12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					

						document.generatedRates.ratePer100.value = MPPASHCI24; //AVAILABLE
			
					}
					
			
			}
	
	}
	if(document.invisible.choices2.value== 2)
	{
		if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
		{
			if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = MPPU12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = MPPU24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
						
			
					}
					if(document.invisible.choices4.value== 2)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
					
					}
					if(document.invisible.choices4.value== 2)
					{
				
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					if(document.invisible.choices4.value== 2)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					
			
			}
	
	
	}
	if(document.invisible.choices2.value== 3)
	{
			if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
		{
			if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = MPPASU12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = MPPASU24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						
						document.generatedRates.ratePer100.value = MPPASUH12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
						
						document.generatedRates.ratePer100.value = MPPASUH24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = MPPASUCI12; //AVAILABLE
					
					}
					if(document.invisible.choices4.value== 2)
					{
				
						document.generatedRates.ratePer100.value = MPPASUCI24; //AVAILABLE
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = MPPASUHCI12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = MPPASUHCI24; // AVAILABLE
			
					}
					
			
			}
	
	
	}


}
//if option 1 = IP	
if(document.invisible.choices0.value== 2)
{
	document.generatedRates.ratePer100.value="9.5";
	//choices [1] not used as it is text and is used for the premium calculations
	if(document.invisible.choices2.value== 1)
	{
			if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = IPAS12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = IPAS24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						
						document.generatedRates.ratePer100.value = IPASH12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
						
						document.generatedRates.ratePer100.value = IPASH24; //AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = IPASCI12; // AVAILABLE
					
					}
					if(document.invisible.choices4.value== 2)
					{
				
						document.generatedRates.ratePer100.value = IPASCI24; //AVAILABLE
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = IPASHCI12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = IPASHCI24; //AVAILABLE
			
					}
					
			
			}
	
	}
	if(document.invisible.choices2.value== 2)
	{
		if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
		{
			if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = IPU12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = IPU24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					if(document.invisible.choices4.value== 2)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
					
					}
					if(document.invisible.choices4.value== 2)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					if(document.invisible.choices4.value== 2)
					{
						alert("This selection of options is unavailable");
						document.generatedRates.ratePer100.value = null; // **Alert as unavailable**
			
					}
					
			
			}
	
	
	}
	if(document.invisible.choices2.value== 3)
	{
			if(document.invisible.choices3.value== 0) // NO OPTION 4 PICKED
		{
			if(document.invisible.choices4.value== 1)
					{
					
						document.generatedRates.ratePer100.value = IPASU12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = IPASU24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 1)
			{
					
					if(document.invisible.choices4.value== 1)
					{
						
						document.generatedRates.ratePer100.value = IPASUH12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
						
						document.generatedRates.ratePer100.value = IPASUH24; // AVAILABLE
			
					}
					
			
			}
			if(document.invisible.choices3.value== 2)
			{
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = IPASUCI12; //AVAILABLE
					
					}
					if(document.invisible.choices4.value== 2)
					{
				
						document.generatedRates.ratePer100.value = IPASUCI24; //AVAILABLE
			
					}

			
			}
			if(document.invisible.choices3.value== 3)
			{
			
					
					if(document.invisible.choices4.value== 1)
					{
				
						document.generatedRates.ratePer100.value = IPASUHCI12; // AVAILABLE
			
					}
					if(document.invisible.choices4.value== 2)
					{
					
						document.generatedRates.ratePer100.value = IPASUHCI24; // AVAILABLE
			
					}
					
			
			}
	
	
	}


}
// now that we have the rates per 100, we can get the premium
var MPT_Sum = document.amountOfBenefit.monthlyBenefit.value / 100 * document.generatedRates.ratePer100.value;
//document.generatedRates.monthlyPremiumText.value = math.round(roundNumber(MPT_Sum, 2));
document.generatedRates.monthlyPremiumText.value = Math.round(MPT_Sum*Math.pow(10,2))/Math.pow(10,2);

  //document.generatedRates.monthlyPremiumText.value = MPT_Sum;
  // finish that else statement from (if mb between 100 and 2000)
} // end else statement to see if monthly benefit is between 100 and 2000
} //- end calculate function -------------------------------------------------------------------------------------------------------------------------------------//
function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function aAndSClick12()
{
	document.benefits.accidentVariable.value = 12;
	document.optionCoverages.hospVariable.value = 12;
	
}
function aAndSClick24()
{
	document.benefits.accidentVariable.value = 24;
	document.optionCoverages.hospVariable.value = 24;
}
function aAndSClick()
{
	var val = document.benefits.aAndSMenu.value;

	document.benefits.accidentVariable.value = val;
	document.optionCoverages.hospVariable.value = val;          
}
function disable4()
{
document.optionCoverages.chck1 = true;
document.optionCoverages.chck2 = true;
}

function setMonth(val)
{
document.optionCoverages.hospVariable.value            = val;
document.optionCoverages.criticalIllVariable.value     = 24;
document.benefits.asVal.value = val;
document.benefits.uVal.value = 12;
document.benefits.asuVal.value = val;
}



function resetAll()
{
//resets all fields
//option 1
document.productSelect.group1[0].checked = false;
document.productSelect.group1[1].checked = false;
// reset rate per 100 and monthly premium fields here
document.generatedRates.ratePer100.value = "";
document.generatedRates.monthlyPremiumText.value = "";

//option 2
document.amountOfBenefit.monthlyBenefit.value = 0;

//option 3 
document.benefits.group2[0].checked = false;
document.benefits.group2[1].checked = false;
//document.benefits.group2[2].checked = false;
document.benefits.asVal.value  = "";
//document.benefits.uVal.value   = ""; // removed as it was part of unemployment only
document.benefits.asuVal.value = "";
//option 4
document.optionCoverages.chck1.checked = false;
document.optionCoverages.chck2.checked = false;
document.optionCoverages.hospVariable.value = "";
document.optionCoverages.criticalIllVariable.value = "";
//option 5
document.lengthOfCover.CL12.checked = false;
document.lengthOfCover.CL24.checked = false;
document.lengthOfCover.coverLength[0].disabled = true;
document.lengthOfCover.coverLength[1].disabled = true;	
// Reults section
document.generatedRates.ratePer100.value = null;
document.generatedRates.monthlyPremiumText.value = null;

//invisible fields
document.invisible.choices0.value = null;
document.invisible.choices1.value = null;
document.invisible.choices2.value = null;
document.invisible.choices3.value = null;
document.invisible.choices4.value = null;


}
function enableChoices(option)
{

	//this is called by section 3 radio buttons, enables options in section 5

	if(option == 1)
	{
		document.lengthOfCover.coverLength[0].disabled = false;
		document.lengthOfCover.coverLength[1].disabled = false;
		document.optionCoverages.chck1.disabled = false;
		document.optionCoverages.chck2.disabled = false;
		
		if(document.benefits.asVal.value == 12)
		{
			document.lengthOfCover.coverLength[0].checked = true;
			document.lengthOfCover.coverLength[1].checked = false;	
		}
		if(document.benefits.asVal.value == 24)
		{
			document.lengthOfCover.coverLength[0].checked = false;
			document.lengthOfCover.coverLength[1].checked = true;	
		}
		
	}
	if(option == 2)
	{
		document.lengthOfCover.coverLength[0].disabled = false;
		document.lengthOfCover.coverLength[1].disabled = true;
		document.lengthOfCover.coverLength[0].checked = true;
		
		document.optionCoverages.chck1.disabled = true;
		document.optionCoverages.chck2.disabled = true;
		document.optionCoverages.chck1.checked = false;
		document.optionCoverages.chck2.checked = false;
		document.optionCoverages.hospVariable.value = "";
		document.optionCoverages.criticalIllVariable.value = "";
		
	}
	if(option == 3)
	{
		document.lengthOfCover.coverLength[0].disabled = false;
		document.lengthOfCover.coverLength[1].disabled = false;	
		document.optionCoverages.chck1.disabled = false;
		document.optionCoverages.chck2.disabled = false;
		
		if(document.benefits.asuVal.value == 12)
		{
			document.lengthOfCover.coverLength[0].checked = true;
			document.lengthOfCover.coverLength[1].checked = false;
		}
		if(document.benefits.asuVal.value == 24)
		{
			document.lengthOfCover.CL12.checked = false;
			document.lengthOfCover.CL24.checked = true;	
		}
	}
	
}
/*function option2(value)
{	
//0 - disable, 1 = enable

	
	if(value == 1)
	{
		//document.benefits.group2[1].disabled = false;
	}
	if(value == 0)
	{
		//document.benefits.group2[1].disabled = false;
		//document.benefits.group2[1].checked = false;
	}
}*/
