function radioSelect(iSelected)
{
    //alert(iSelected + "\r\n\n" + Date());
	setChildTrustInvestment(false);
	pRegularSaver=document.getElementById("RegularAccounts")
	if (pRegularSaver.checked) {
		pRegularSaver.checked=false	
		document.getElementById("EasyAccessAccounts").checked=true
		setInvestmentAmount('other')

	}
	var boxy = document.getElementsByName("InvestmentAmount")[0];

	// Hide the 'Other' answer.
	document.getElementById("extraoptionsother").style.display = "none";

	switch (iSelected)
	{
		case "Compare":
			showExtraOptions(15);
			showInvestmentOptions(1);
			boxy.value = SAeasyAccess;
			term();
			notice();
			setInvestmentAmount();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Childrens":
			showExtraOptions(15);
			showInvestmentOptions(1);
			boxy.value = CAinvestValue;
			term();
			notice();
			setInvestmentAmount();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "OffShore":
			showExtraOptions(0);
			showInvestmentOptions(1);
			boxy.value = OSinvestValue;
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Child Trust":
			showExtraOptions(0);
			showInvestmentOptions(1);
			setChildTrustInvestment(true);
			if(boxy && boxy.value != CTFinvestValue) boxy.value = CTFinvestValue;
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Isa":
			showExtraOptions(15);
			showInvestmentOptions(1);
			if(boxy && boxy.value != MCIeasyAccess) boxy.value = MCIeasyAccess;
			term();
			notice();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "GuaranteedEquity":
			showExtraOptions(0);
			showInvestmentOptions(1);
			document.getElementsByName("TermAccounts")[0].checked = true;
			boxy.value = GEBinvestValue;
			term();
			notice();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Pensioners" :
			showExtraOptions(15);
			showInvestmentOptions(1);
			boxy.value = O5SBinvestValue;
			term();
			notice();
			setInvestmentAmount();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Pensions" :
			showExtraOptions(0);
			showInvestmentOptions(0);
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Investments" :
			showExtraOptions(0);
			showInvestmentOptions(2);
			investmenttype();
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(0);
			break;
		case "Other" :
			showExtraOptions(0);
			showInvestmentOptions(0);
			showOtherInvestments(1);
			ShowAffinityAccountQuestion(0);
			// Showthe 'Other' answer.
			document.getElementById("extraoptionsother").style.display = "block";

			break;
		case "Affinity" :
			showExtraOptions(0);
			showInvestmentOptions(0);
			showOtherInvestments(0);
			ShowAffinityAccountQuestion(1);
			break;
	}
}

var previousInvestmentAmount = 1;

function setChildTrustInvestment(on_off)
{
	var on = true
	if(on_off == on)
	{
		previousInvestmentAmount = document.savings.InvestmentAmount.value ;
		if (parseInt(document.savings.InvestmentAmount.value) < CTFinvestValue)
		{
			document.savings.InvestmentAmount.value = CTFinvestValue;
		}
	}
	else
	{
		if (parseInt(document.savings.InvestmentAmount.value) == CTFinvestValue)
		{
			document.savings.InvestmentAmount.value = previousInvestmentAmount ;
		}
	}
}

function showExtraOptions(showem)
{
	var block = document.getElementById("extraoptionsrow");
	var objQuestionBlock = document.getElementById("Question2Block");

	if (document.getElementById("extraoptionsrow"))
	{
		block.style.display = (showem > 0) ? "block" : "none";
	}
	if (objQuestionBlock)
	{
		objQuestionBlock.style.display = (showem > 0) ? "block" : "none";
	}
	
	// Hide/show options based on bitwise value.
	var i = 1;
	var iLimit = 8;
	while (i <= iLimit)
	{
		oOption = document.getElementById("extra" + i);
		if (oOption)
		{
			oOption.style.display = ((showem & i) == i) ? "block" : "none";
		}
		i = i * 2;
	}
}

function notice() {
	if (document.getElementById("_noticePeriod"))
	{
		clearDropdown(document.getElementById("_noticePeriod")) ;

		var arrOpts = new Array('All', 'Up to 120 days', 'Up to 90 days', 'Up to 60 days', 'Up to 30 days');
		var arrOptVals = new Array(121, 120, 90, 60, 30);
		if (document.savings.NoticeAccounts.checked == true)
		{
			var limit ;
			if(IsaSelected())
			{
				limit = 1 ;
			}
			else
			{
				limit = arrOpts.length ;
			}
			
			for(i=0;i<limit;i+=1)
			{
				document.savings.NoticePeriod.options[i] = new Option(arrOpts[i],arrOptVals[i]);
			}
			
			document.getElementsByName("InvestmentAmount")[0].value = SAnoticeAccount;
		}
		else
		{
				document.savings.NoticePeriod.options.length = 0;
				document.savings.NoticePeriod.options[0] = new Option('-- Select --','');
		}
	}
}

function term() {
	
	if (document.getElementById("_termPeriod"))
	{
		clearDropdown(document.getElementById("_termPeriod")) ;
		
		//var arrOpts = new Array('All', 'Up to 5 years',  'Up to 4 years',     'Up to 3 years',     'Up to 2 years',    'Up to 1 year');
		//var arrOptVals = new Array(6,5,4,3,2,1);
		
		var arrOpts = new Array('All',   '5 yrs and over', '4 yrs up to 5 yrs', '3 yrs up to 4 yrs', '2 yrs up to 3 yrs', '1 yr up to 2 yrs', 'Under 1 yr');
		var arrOptVals = new Array(9,6,5,4,3,2,1);

		if (document.savings.TermAccounts.checked == true)
		{
			var limit ;
			if(IsaSelected())
			{
				limit = 1 ;
			}
			else
			{
				limit = arrOpts.length ;
			}
			
			for(i=0;i<limit;i+=1)
			{
				document.savings.TermPeriod.options[i] = new Option(arrOpts[i],arrOptVals[i]);
			}
		}
		else
		{
				document.savings.TermPeriod.options.length = 0; 
				document.savings.TermPeriod.options[0] = new Option('-- Select --','');				
		}
	}
}

function investmenttype() {
	
	if (document.getElementById("_investmentType"))
	{
		clearDropdown(document.getElementById("_investmentType")) ;
		
		var arrOpts = new Array('Lump Sum', 'Regular Saving');
		var arrOptVals = new Array(1,2);

		var limit ;
		if(IsaSelected())
		{
			limit = 1 ;
		}
		else
		{
			limit = arrOpts.length ;
		}
		
		for(i=0;i<limit;i+=1)
		{
			document.savings.InvestmentType.options[i] = new Option(arrOpts[i],arrOptVals[i]);
		}
	}
}

function clearDropdown(theDropDown)
{
	while(theDropDown.options.length > 0)
	{
		theDropDown.options[0] = null;
	}
}

function submitForm() {
	//Validate InvestmentAmount
	if (!isFieldValidAndFocus(document.savings.InvestmentAmount, 'Currency', false, 'Investment amount')) {
		return false;
	}
	
	// Email.
	if (!isFieldValidAndFocus(document.savings.Email, 'Email', true,  'Email address'))
	{
		return false;
	}
	//Check for valid investment amount
	if (document.savings.InvestmentAmount.value <= 0) {
		alert("Please enter an investment amount of £1 or more");
		document.savings.InvestmentAmount.select();
		return false;
	}

	var selectedType;
	var typecounter = 0;
	
	while(document.savings.SavingsType[typecounter])
	{
		if(document.savings.SavingsType[typecounter].checked)
		{
			selectedType = document.savings.SavingsType[typecounter].value;
			break;
		}
		typecounter ++ ;
	}

	document.savings.action = (isPartner()) ? "../savingsp/SavingsResults.asp" : "../savings/SavingsResults.asp";

	switch (selectedType) {
		case "Compare":
			var Counter = calculateResult();

			if (Counter == 0)
			{
				alert("Please select a type of account");
				return false;
			}
			document.savings.Results.value = Counter;
			break;
		case "Childrens":
			document.savings.Results.value = 16;
			break;
		case "OffShore":
			document.savings.action = "OSResults.asp";
			document.savings.Results.value = 7;
			break;
		case "Child Trust":
			document.savings.Results.value = 32;
			break;
		case "Isa":
			document.savings.action = (isPartner()) ? "../isap/IsasResults.asp" : "../isa/IsasResults.asp";
			var Counter = calculateResult();
			if (Counter == 0)
			{
				alert("Please select a type of account");
				return false;
			}
			document.savings.Results.value = Counter;
			break;
		case "GuaranteedEquity":
			document.savings.Results.value = 64;
			break;
		case "Pensioners":
			var Counter = calculateResult();

			if (Counter == 0)
			{
				alert("Please select a type of account");
				return false;
			}
			document.savings.Results.value = 128;
			break;
		case "Pensions":
			document.savings.action = (isPartner()) ? "../pensionsp/" : "../pensions/";
			document.savings.Results.value = 8;
			break;
		case "Investments":
			if (document.savings.InvestmentType.value == 1)
			{
				document.savings.action = "leadinvestments.asp";
			}
			if (document.savings.InvestmentType.value == 2)
			{
				document.savings.action = "leadsavings.asp";
			}
			document.savings.Results.value = 9;
			break;
		case "Other":
			document.savings.action = (isPartner()) ? "../savingsp/OtherInvestmentDetails.asp" : "../savings/OtherInvestmentDetails.asp";
			if(document.savings.dropotherinvestments.value == "")
			{
				alert("Please select an account from the drop down list.");
				return false;
			}
			break;
		case "Affinity":
			document.savings.Results.value = 512;
			break;
		default:
			alert("Please select the type of savings account you wich to compare.");
			return false;
	}

	return true;
}

function isPartner()
{	
	return (window.location.toString().toLowerCase().indexOf("savingsp") > 0)
}

function IsaSelected()
{
	var i = 0 ;
	while(document.savings.SavingsType[i])
	{
		if(document.savings.SavingsType[i].value == "Isa")
		{
			return document.savings.SavingsType[i].checked ;
		}
		i++;
	}
	
	return false ;
}

function calculateResult()
{	
	var Counter = 0
	if (document.savings.EasyAccessAccounts.checked) {
		Counter = Counter + 1;
	}
	if (document.savings.NoticeAccounts.checked) {
		Counter = Counter + 2;
	}
	if (document.savings.TermAccounts.checked) {
		Counter = Counter + 4;
	}
	if (document.savings.RegularAccounts.checked) {
		Counter = Counter + 8;
	}
	return Counter
}

function retickOptions()
{
	var opts = document.getElementsByName("SavingsType");
	var index;
	var foundChecked = false;
	if(opts.length>0)
	{
	    for (index=0; index < opts.length; index++)
	    {
	        if (opts[index].checked)
	        {
	            opts[index].onclick();
	            foundChecked = true;
	            break;
	        }
	    }
	    if (!foundChecked) 
	    {
		    opts[0].checked = true;
		    opts[0].onclick();	    
	    }
	}
}

function reCalculateResult()
{
	var Counter = 0;
	if (document.savings.RegularAccounts.checked) {
		Counter = Counter + 1;
	}
	if (document.savings.EasyAccessAccounts.checked) {
		Counter = Counter + 2;
	}
	if (document.savings.NoticeAccounts.checked) {
		Counter = Counter + 4;
	}
	if (document.savings.TermAccounts.checked) {
		Counter = Counter + 8;
	}
	return Counter;
}


function setInvestmentAmount(checkGroup)
{
	//popup confirmation box when regular saver is selected and other option already selected or vice versa
	if (document.getElementById("sourcetype1").checked || document.getElementById("sourcetype7").checked ||  document.getElementById("sourcetype2").checked) {
		bRegularSaver=document.getElementById("RegularAccounts").checked
		bOtherOption=(document.getElementById("EasyAccessAccounts").checked)||(document.getElementById("NoticeAccounts").checked)||(document.getElementById("TermAccounts").checked)
		if (bOtherOption && bRegularSaver) {
			res=confirm("Regular Saver accounts cannot be compared to other savings accounts due to their individual characteristics")
			if (res) {		
				if (checkGroup=="regular") {
					document.getElementById("EasyAccessAccounts").checked=false;
					document.getElementById("NoticeAccounts").checked=false;
		 			document.getElementById("TermAccounts").checked=false;
				} else {
					document.getElementById("RegularAccounts").checked=false
				}
			} else {
				return false
			}
		}
		bRegularSaver=document.getElementById("RegularAccounts").checked
		//check whether regular saver is check and change amount field label accordingly
		pLabel=document.getElementById("amountFieldLabel")
		if (bRegularSaver) {
			pLabel.innerHTML='Monthly sum you wish to invest?'
		} else {
			pLabel.innerHTML='What sum do you wish to invest?'
		}
	} else {
		pLabel=document.getElementById("amountFieldLabel")
		pLabel.innerHTML='What sum do you wish to invest?'
	}
	//Savings
	if (document.getElementById("sourcetype1").checked == true)	{
		var bitTotal = reCalculateResult();
		if ( (bitTotal & 1) == 1){
			document.savings.InvestmentAmount.value = SAregularSaver;
		}
		if ( (bitTotal & 2) == 2){
			document.savings.InvestmentAmount.value = SAeasyAccess;
		}
		if ( (bitTotal & 4) == 4){
			document.savings.InvestmentAmount.value = SAnoticeAccount;
		}
		if ( (bitTotal & 8) == 8){
			document.savings.InvestmentAmount.value = SAbondTerm;
		}
	}
	//Mini cash isa
	if (document.getElementById("sourcetype5").checked == true)	{
		var bitTotal = reCalculateResult();
		if ( (bitTotal & 1) == 1){
			document.savings.InvestmentAmount.value = MCIregularSaver;
		}
		if ( (bitTotal & 2) == 2){
			document.savings.InvestmentAmount.value = MCIeasyAccess;
		}
		if ( (bitTotal & 4) == 4){
			document.savings.InvestmentAmount.value = MCInoticeAccount;
		}
		if ( (bitTotal & 8) == 8){
			document.savings.InvestmentAmount.value = MCIbondTerm;
		}
	}
	//childrens accounts
	if (document.getElementById("sourcetype2").checked == true)	{
		var bitTotal = reCalculateResult();
		if ( (bitTotal & 1) == 1){
			document.savings.InvestmentAmount.value = CARegularSaver;
		}
		if ( (bitTotal & 2) == 2){
			document.savings.InvestmentAmount.value = CAinvestValue;
		}
		if ( (bitTotal & 4) == 4){
			document.savings.InvestmentAmount.value = CANoticeAccount;
		}
		if ( (bitTotal & 8) == 8){
			document.savings.InvestmentAmount.value = CABondTerm;
		}
	}
	//over 50s
	if (document.getElementById("sourcetype7").checked == true)	{
		var bitTotal = reCalculateResult();
		if ( (bitTotal & 1) == 1){
			document.savings.InvestmentAmount.value = O5SBRegularSaver;
		}
		if ( (bitTotal & 2) == 2){
			document.savings.InvestmentAmount.value = O5SBinvestValue;
		}
		if ( (bitTotal & 4) == 4){
			document.savings.InvestmentAmount.value = O5SBNoticeAccount;
		}
		if ( (bitTotal & 8) == 8){
			document.savings.InvestmentAmount.value = O5SBBondTerm;
		}
	}
}



function showInvestmentOptions(showem)
{
	var block = document.getElementById("investmentoptions");

	if (document.getElementById("investmentoptions"))
	{
		block.style.display = (showem > 0) ? "block" : "none";
	}

	
	// Hide/show options based on bitwise value.
	var i = 1;
	var iLimit = 8;
	while (i <= iLimit)
	{
		oOption = document.getElementById("investment" + i);
		if (oOption)
		{
			oOption.style.display = ((showem & i) == i) ? "block" : "none";
		}
		i = i * 2;
	}
}

function showOtherInvestments(showem)
{
	var block = document.getElementById("optionsrow3");
	
	if(document.getElementById("optionsrow3"))
	{
		block.style.display = (showem == 1 ) ? "block" : "none";
	}
}

function ShowAffinityAccountQuestion(displayValue)
{
	var affinityQuestionContainer = document.getElementById("AffinityAccountContainer");
	
	if (affinityQuestionContainer)
	{
		affinityQuestionContainer.style.display = (displayValue == 1 ) ? "block" : "none";
	}
}