 function XBrowserAddHandler(target, eventName, handlerName) 
{ 
	if ( target.addEventListener ) 
	{ 
	target.addEventListener(eventName, handlerName, false);
	} 
	else if ( target.attachEvent ) 
	{ 
	target.attachEvent("on" + eventName, handlerName);
	} 
	else 
	{ 
	target["on" + eventName] = target[handlerName]; 
	} 
}


// Required to allow a client-side event handler to be attached to a submit button
// that also performs validation.
function setupEventHandler()
{
	
	if(document.all) //Browser is IE - (if NS Browser - see ShowValidationWarnings() in GrowthPlanCalculator.aspx.cs) 
	{
		var mybtn = document.getElementById("btnNext");
		if (mybtn != null)
		{
			// No longer using XBrowserAddHandler. Even though it correctly adds
			// the event in Firefox, the "Cancel" button does not work. It seems to 
			// be because the event handler does not explicitly say "return ConfirmAnnualPay()"
			// So for now, the confirm pay functionality is only available in IE.
			//XBrowserAddHandler(mybtn, "click", confirmAnnualPay);
			if ( mybtn.attachEvent )
			{
				mybtn.attachEvent("onclick", showValidationWarnings);
			}
		}
	}
	
	// Load the init method - required by the stopEnterEvent(e) method
	// window.onload = init;
	if( document.layers || (document.getElementById && !document.all))
	{
		
		// Browser is not IE
		init();
	}
}

var theEvent;
function getEvent(e)
{
	theEvent = e;
	
}

///
/// Ensure that the correct submit button / link is fired with the enter key press 
/// - similar code in the header control
///

// Note: to work for for NS browsers this function would need to be like: stopEnterEvent(e): e = event -  
// however this function would have to be called as follows - document.onkeydown=stopEnterEvent; 
// in the .aspx header. Currently the code behind has the following code to add attribute to divBody...
// this.divBody.Attributes.Add("onkeydown", "stopEnterEvent();");
// adding e to pass the event might work, ... "stopEnterEvent(e);"); - (page currently checked out)
// However: the NS browser code can currently trap the event but not stop it so passing the 
// event is to no effect
function stopEnterEvent() 
{
	
			
	if( document.all ) // Browser is IE
	{
				
		// Get active control with focus
		var myActiveElement = document.activeElement;
		var activeElement = myActiveElement.id.toString();
		
		// Call isSubmitControl to see if the Active page element should allow submit
		var submitControl = isSubmitControl(activeElement);

		// DisAllow / Allow submit 
		if ( event.keyCode == 13 && !submitControl )
		{
			window.event.returnValue = false;
		}
		else if ( event.keyCode == 13 && submitControl )
		{
			window.event.returnValue = true;
		}
			
	}
	else if( document.layers || (document.getElementById && !document.all)) // Browser is NS
	{
		  
		// Call isSubmitControl to see if the Active page element should allow submit
		// value for document.activeElement.id is set in init()
		var submitControl = isSubmitControl(document.activeElement.id);
		
		
		// Check which button has been clicked
		var num = theEvent.which;
		
		//alert(" e.which:" + e.which ); 
		//alert("num:" + num + "| submitControl:" + submitControl );

        if ( num == 13 && submitControl == false ) // Do not Submit
        {
			//alert(" here " );

			var e = theEvent;

			if (e.returnValue) 
			{
				//alert("Cancel Event 1");
				e.returnValue = false;
				window.stop();
			} 
			else if (e.preventDefault) // NS browsers
			{
				//alert("Cancel Event 2");
				e.preventDefault();
				e.cancelBubble = true;
				e.returnValue = false;
				window.stop();
				return false;
			} 
			else 
			{
				//alert("Cancel Event 3");
				return false;
			}

		}
		else if( num == 13 && submitControl == true ) // Can submit
		{
			e.returnValue = true;	
		}
		else
		{	
			e.preventDefault(); // If not event not caught - Do not Submit
		}
		
	}
	else
	{
		// error
	}		
					
}


///
/// Function used where browser is not IE to get page element with focus
///
function init()
{
	document.activeElement = document.body;
	var all = document.body.getElementsByTagName('*');

	for (var i = 0; i < all.length; i++)
	{
		var functionBody = 'document.activeElement = this;';
		var oldOnFocus = all[i].getAttribute('onfocus');

		if (oldOnFocus)
		{
			functionBody += oldOnFocus;
		}

		all[i].onfocus = new Function ('event', functionBody);
	}
}

///
/// Function to Check if a control should allow submit
///
function isSubmitControl(thestring)
{	

	// Variable to denote if the control with focus should allow submit 
	var submitControl = false;
	
	// Decide if control with focus should allow submit
	if ( thestring == 'btnShowCalc' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'btnCalculateProjection' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpCurrentSalary' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpNormalRetirementAge' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpSelectedRetirementAge' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpCommencementDate' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpEmployeeContributionRate' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpEmployerContributionRate' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpAdditionalVoluntaryContributions' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpLifeCoverMultiple' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpGrowthPlanSeries1ObligPension' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpGrowthPlanSeries1AVCPension' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpGrowthPlanSeries2ObligPension' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpGrowthPlanSeries2AVCPension' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpExistingCashAccrualUnderGP3' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpExistingAVCcashaccrualunderGP3' )
	{
		submitControl = true;
		return submitControl;
	}
	else if ( thestring == 'helpGrowthPlanPlus' )
	{
		submitControl = true;
		return submitControl;
	}
	else
	{
		submitControl = false;
		return submitControl;
	}
}	


///
/// Function to ensure user has checked the Terms/Conditions check box
///
function acceptTerms(source, args) 
{
	
	args.IsValid=false;

	//if(document.all('ckbConfirm').checked==false)
	if(document.getElementById('ckbConfirm').checked==false)
	{
		args.IsValid=false;
		return false;
	} 
	else 
	{ 
		args.IsValid=true;
		return true;
	}
}


// ============================ FORM VALIDATION ============================ 

var cfmAnnualPay = 0;

var cfmEmployerCont;
var cfmGP12;
var cfmGp3Accr;
var intVdtContFlag = 0;
var intVdtGp12Flag = 0;
var intVdtGp3Accr = 0;

var intGP1ObligFlag = 0;
var intGP1AvcFlag = 0;
var intGP2ObligFlag = 0;
var intGP2AvcFlag = 0;

var counter1;
var counter2;
 

function showValidationWarnings() 
{
	// This function displays warnings if any field values are outside expected ranges
	
	// Record the fact that warnings have been processed, so that we know 
	// they don't need to be done later on the server.
	var hiddenField = document.getElementById('hfWarningsDisplayed')
	if( hiddenField != null)
	{
		hiddenField.value = "True";
	}
	
	// treat Get user values Function like an object to retrieve multiple values
	// access values: userValues.txtCurrentsalaryValue
	var userValues = new getUserValues();

	var continueSubmit = true;
	
	// CURRENT SALARY
	if( userValues.txtCurrentsalaryValue != null )
	{
		if((cfmAnnualPay < 1) && (userValues.txtCurrentsalaryValue < 10000))
		{
			continueSubmit = confirmAnnualPay();
		} 
	}
	
	// Employer Contribution < Employee Contribution
	//if((intVdtContFlag < 1) && (eval(document.all('txtEmployerContribution').value) < eval(document.all('txtEmployeeContribution').value))) 
	if((intVdtContFlag < 1) && (userValues.txtEmployerContributionValue < userValues.txtEmployeeContributionValue)) 
	{
			continueSubmit = confirmEmperConts();
	} 
	
	// Employer Contribution > 2 * Employee Contribution
	//else if((intVdtContFlag < 1) && (eval(document.all('txtEmployerContribution').value) > (2 * eval(document.all('txtEmployeeContribution').value)))) 
	else if((intVdtContFlag < 1) && (userValues.txtEmployerContributionValue > (2 * userValues.txtEmployeeContributionValue) )) 
	{
			continueSubmit = confirmEmperConts();
	}
	// (Growth Plan Series 1 Oblig Pension > 20000) && (intGP1ObligFlag < 1)
	//else if((eval(document.all('txtGrowthPlanSeries1ObligPension').value) > 20000) && (intGP1ObligFlag < 1))
	else if((userValues.txtGrowthPlanSeries1ObligPensionValue > 20000) && (intGP1ObligFlag < 1))
	{
		display20kWarningMsg('Growth Plan Series 1 Oblig Pension');
		//F.fGP1Oblig.value = "";
		intGP1ObligFlag = 1;
		document.all('txtGrowthPlanSeries1ObligPension').focus();
		//return false;
		continueSubmit = false;
	}
	
	// ( Growth Plan Series 1 AVC Pension > 20000 ) && (intGP1AvcFlag < 1)
	//else if((eval(document.all('txtGrowthPlanSeries1AVCPension').value) > 20000) && (intGP1AvcFlag < 1))
	else if((userValues.txtGrowthPlanSeries1AVCPensionValue > 20000) && (intGP1AvcFlag < 1))
	{
		display20kWarningMsg('Growth Plan Series 1 AVC Pension');
		//F.fGP1Oblig.value = "";
		intGP1AvcFlag = 1;
		document.all('txtGrowthPlanSeries1AVCPension').focus();
		//return false;
		continueSubmit = false;
	}
	
	// (Growth Plan Series 2 Oblig Pension > 20000) && (intGP2ObligFlag < 1)
	//else if((eval(document.all('txtGrowthPlanSeries2ObligPension').value) > 20000) && (intGP2ObligFlag < 1))
	else if((userValues.txtGrowthPlanSeries2ObligPensionValue > 20000) && (intGP2ObligFlag < 1))
	{
		display20kWarningMsg('Growth Plan Series 2 Oblig Pension');
		//F.fGP1Oblig.value = "";
		intGP2ObligFlag = 1;
		document.all('txtGrowthPlanSeries2ObligPension').focus();
		//return false;
		continueSubmit = false;

	}
	
	// ( Growth Plan Series 2 AVC Pension > 20000 ) && (intGP2AvcFlag < 1)
	//else if((eval(document.all('txtGrowthPlanSeries2AVCPension').value) > 20000) && (intGP2AvcFlag < 1))
	else if((userValues.txtGrowthPlanSeries2AVCPensionValue > 20000) && (intGP2AvcFlag < 1))
	{
		display20kWarningMsg('Growth Plan Series 2 AVC Pension');
		//F.fGP1Oblig.value = "";
		intGP2AvcFlag = 1;
		document.all('txtGrowthPlanSeries2AVCPension').focus();
		//return false;
		continueSubmit = false;
	
	}
	
	// ( Existing Growth Plan Series 3 Cash Fund > 250000 ) && (intVdtGp3Accr < 1)
	//else if((eval(document.all('txtExistingGrowthPlanSeries3CashFund').value) > 250000) && (intVdtGp3Accr < 1))
	else if((userValues.txtExistingGrowthPlanSeries3CashFundValue > 250000) && (intVdtGp3Accr < 1))
	{
		// NOTE: \xA3 produces a £ sign
		alert("The fund value you have entered is outside the range we would normally expect." +
			"\nThe value we would normally expect is less than \xA3250,000." +
			"\nThe calculator will accept the entered value but please check the entered amount before proceeding.");
			
		intVdtGp3Accr ++;
		document.all('txtExistingGrowthPlanSeries3CashFund').focus();
		//return false; 
		continueSubmit = false;
	
	} 
	
	return continueSubmit;
}


///
/// Get user values Function - used by checkFields() like an object to retrieve multiple values
///
function	isNanExists(id)
{
	var el = document.all(id);
	if(el)
		return isNaN(el.value);
	else
		return true;
}
function getUserValues()
{
	/*// AVC 
	if(!isNaN( document.all('txtAVC').value ) )
	{
		this.txtAVCValue = parseFloat(document.all('txtAVC').value);
	}*/
	// Employer Contribution
	if(!isNanExists('txtEmployerContribution') )
	{
		this.txtEmployerContributionValue = parseFloat(document.all('txtEmployerContribution').value);
	}
	// Employee Contribution
	if(!isNanExists('txtEmployeeContribution'))
	{
		this.txtEmployeeContributionValue = parseFloat(document.all('txtEmployeeContribution').value);
	}	
	// Growth Plan Series 1 Oblig Pension
	if(!isNaN( document.all('txtGrowthPlanSeries1ObligPension').value) )
	{
		this.txtGrowthPlanSeries1ObligPensionValue = parseFloat(document.all('txtGrowthPlanSeries1ObligPension').value);
	}	
	// Growth Plan Series 1 AVC Pension 
	if(!isNaN( document.all('txtGrowthPlanSeries1AVCPension').value) )
	{
		this.txtGrowthPlanSeries1AVCPensionValue = parseFloat(document.all('txtGrowthPlanSeries1AVCPension').value);
	}
	// Growth Plan Series 2 Oblig Pension
	if(!isNaN( document.all('txtGrowthPlanSeries2ObligPension').value) )
	{
		this.txtGrowthPlanSeries2ObligPensionValue = parseFloat(document.all('txtGrowthPlanSeries2ObligPension').value);
	}
	// Growth Plan Series 2 AVC Pension
	if(!isNaN( document.all('txtGrowthPlanSeries2AVCPension').value) )
	{
		this.txtGrowthPlanSeries2AVCPensionValue = parseFloat(document.all('txtGrowthPlanSeries2AVCPension').value);
	}
	// Growth Plan Series 2 Oblig Pension
	if(!isNaN( document.all('txtExistingGrowthPlanSeries3CashFund').value) )
	{
		this.txtExistingGrowthPlanSeries3CashFundValue = parseFloat(document.all('txtExistingGrowthPlanSeries3CashFund').value);
	}
}

function confirmAnnualPay()
{
	cfmSalary = confirm("You have entered a salary that is outside the range we would normally expect. " +
						"\nThe calculator will accept the entered salary but please check that you entered the correct amount before proceeding." +
						"\n\nPress OK if you are sure the value entered is correct, otherwise press CANCEL.");
	if(cfmSalary)
	{
		
		cfmAnnualPay ++;

		return true;
	} 
	else 
	{
		
		//document.all('txtCurrentsalary').value = "";
		document.all('txtCurrentsalary').focus();

		return false;
	}
}
  
function confirmEmperConts() 
{
	var continueSubmit = true;

	cfmEmployerCont = confirm("The employer contribution rate you have entered is outside the range we would normally expect." +
					"\nThe rate is normally between 1 and 2 times that of the employee." +
					"\nThe calculator will accept the entered contribution level but please check the entered amount before proceeding." +
					"\n\nClick OK if you are sure the value entered is correct, otherwise press CANCEL.");

	if(cfmEmployerCont)
	{
		intVdtContFlag ++;
		//alert("intVdtContFlag : " + intVdtContFlag)
		//document.all('txtAVC').focus();
		return continueSubmit = true;
	} 
	else 
	{
		//document.all('txtEmployerContribution').value = "";
		document.all('txtEmployerContribution').focus();
		return continueSubmit = false;
	}
}

function display20kWarningMsg(fieldName)
{
	// NOTE: \xA3 produces a £ sign
	alert("The "+ fieldName +" you have entered is outside the range we would normally expect." +
			"\nThe value we would normally expect is less than \xA320,000." +
			"\nThe calculator will accept the entered value but please check the entered amount before proceeding.");
}

// ============================ END OF FORM VALIDATION ============================ 

/*  
  ///
  /// Ensure Salary entered is numeric 
  ///
  function checkTxtCurrentsalaryNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtCurrentsalaryValue = document.all('txtCurrentsalary').value;
	
	if(isNaN(txtCurrentsalaryValue) && (document.all('txtCurrentsalary').value != null || document.all('txtCurrentsalary').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  
  ///
  /// Ensure Employee Contributions entered is numeric 
  ///
  function checkTxtEmployeeContributionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtEmployeeContributionValue = document.all('txtEmployeeContribution').value;
	
	if(isNaN(txtEmployeeContributionValue) && (document.all('txtEmployeeContribution').value != null || document.all('txtEmployeeContribution').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  
  ///
  /// Ensure Employer Contributions entered is numeric 
  ///
  function checkTxtEmployerContributionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtEmployerContributionValue = document.all('txtEmployerContribution').value;
	
	if(isNaN(txtEmployerContributionValue) && (document.all('txtEmployerContribution').value != null || document.all('txtEmployerContribution').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  
  ///
  /// Ensure AVC entered is numeric 
  ///
  function checkTxtAVCNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtAVCValue = document.all('txtAVC').value;
	
	if(isNaN(txtAVCValue) && (document.all('txtAVC').value != null || document.all('txtAVC').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Growth Plan Series 1 Pension entered is numeric 
  ///
  function checkTxtGrowthPlanSeries1ObligPensionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtGrowthPlanSeries1ObligPensionValue = document.all('txtGrowthPlanSeries1ObligPension').value;
	
	if(isNaN(txtGrowthPlanSeries1ObligPensionValue) && (document.all('txtGrowthPlanSeries1ObligPension').value != null || document.all('txtGrowthPlanSeries1ObligPension').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Growth Plan Series 1 AVC entered is numeric 
  ///
  function checkTxtGrowthPlanSeries1AVCPensionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtGrowthPlanSeries1AVCPensionValue = document.all('txtGrowthPlanSeries1AVCPension').value;
	
	if(isNaN(txtGrowthPlanSeries1AVCPensionValue) && (document.all('txtGrowthPlanSeries1AVCPension').value != null || document.all('txtGrowthPlanSeries1AVCPension').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Growth Plan Series 2 Pension entered is numeric 
  ///
  function checkTxtGrowthPlanSeries2ObligPensionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtGrowthPlanSeries2ObligPensionValue = document.all('txtGrowthPlanSeries2ObligPension').value;
	
	if(isNaN(txtGrowthPlanSeries2ObligPensionValue) && (document.all('txtGrowthPlanSeries2ObligPension').value != null || document.all('txtGrowthPlanSeries2ObligPension').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Growth Plan Series 2 AVC entered is numeric 
  ///
  function checkTxtGrowthPlanSeries2AVCPensionNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtGrowthPlanSeries2AVCPensionValue = document.all('txtGrowthPlanSeries2AVCPension').value;
	
	if(isNaN(txtGrowthPlanSeries2AVCPensionValue) && (document.all('txtGrowthPlanSeries2AVCPension').value != null || document.all('txtGrowthPlanSeries2AVCPension').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Existing Growth Plan entered is numeric 
  ///
  function checkTxtExistingGrowthPlanSeries3CashFundNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtExistingGrowthPlanSeries3CashFundValue = document.all('txtExistingGrowthPlanSeries3CashFund').value;
	
	if(isNaN(txtExistingGrowthPlanSeries3CashFundValue) && (document.all('txtExistingGrowthPlanSeries3CashFund').value != null || document.all('txtExistingGrowthPlanSeries3CashFund').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  ///
  /// Ensure Existing Growth Plan AVC entered is numeric 
  ///
  function checkTxtExistingGrowthPlanSeries3AVCCashFundNumeric(source, args)
  {
	args.IsValid = false;
	
	var txtExistingGrowthPlanSeries3AVCCashFundValue = document.all('txtExistingGrowthPlanSeries3AVCCashFund').value;
	
	if(isNaN(txtExistingGrowthPlanSeries3AVCCashFundValue) && (document.all('txtExistingGrowthPlanSeries3AVCCashFund').value != null || document.all('txtExistingGrowthPlanSeries3AVCCashFund').value != "") )
	{
		args.IsValid=false;
		return args; //false;
	}
	else
	{
		args.IsValid=true;
		return args; //true;
	}
  }
  
*/  

/*  function checkForm(  )
  {
		
		if(checkFields()) 
		{  
			//now submit the page
			//document.all('hiddenCanSubmit').value = "Can Process";
			//document.formGrowthPlanCalculator.submit();
			return true;
		}
		else
		{
			return false;	
		}
  }
 */
