
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 checkForm() in GrowthPlanCalculator.aspx.cs) 
	{
	
		var mybtn = document.getElementById("btnCalculateProjection");
		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 becuase 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", confirmAnnualPay);
			}
		}
	}
	
	
	
	// 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
///
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);
				var stopEvent = 0;	
					
				// Check which button has been clicked
				var num = theEvent.which;

				if ( num == 13 && submitControl == false ) // Do not Submit
				{
					//alert(" here " );
					
					if(stopEvent > 0)
							{
								
								stopEvent++;
								//alert("Cancel Event - stop event > 2");
								e.preventDefault();
								e.cancelBubble = true;
								e.returnValue = false;
								window.stop();
								return false;
								
							}
					

					var e = theEvent;
			
			
					//alert(" e..." + e );
					
							if (e.returnValue) 
							{
							//alert("Cancel Event 1");
							e.returnValue = false;
							window.stop();
							} 
							else if (e.preventDefault) // NS browsers
							{
								stopEvent++;
								//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 == 'helpScheme' )
			{
			     submitControl = true;
			     return submitControl;
			}
			else if ( thestring == 'helpDateJoinedScheme' )
			{
			     submitControl = true;
			     return submitControl;
			}
			else if ( thestring == 'helpCurrentSalary' )
			{
			     submitControl = true;
			     return submitControl;
			}
			else if ( thestring == 'helpNormalRetirementAge' )
			{
			     submitControl = true;
			     return submitControl;
			}
			else if ( thestring == 'helpTaxRate' )
			{
			     submitControl = true;
			     return submitControl;
			}
			else
			{
				submitControl = false;
				return submitControl;
			}
}




var cfmAnnualPay = 0;

function confirmCheck()
{
	return confirm("Continue?");
}

function confirmAnnualPay()
{
	// If user has not already confirmed and values are outside expected range, get user to confirm values	
	if (cfmAnnualPay > 0)
	{
		// User has already confirmed once
		return true;
	}
	
	var txtSalary = document.getElementById("txtCurrentSalary");
	if (txtSalary != null)
	{
		// Get the salary entered by the user
		var salaryValue = parseFloat(txtSalary.value);
		
		// If there are errors that will be picked up by other validators then ignore them and exit
		if ( isNaN(salaryValue) || salaryValue  <= 0 )
		{
			return true;
		}	

		// Check if the salary value is outside our expected range
		if( salaryValue < 10000 || salaryValue > 99000 )
		{
			var 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)
			{
				//alert("confirmed");
				cfmAnnualPay ++;	// Record that the user has confirmed the values
				return true;
			} 
			else 
			{
				//alert("cancelled");
				document.all('txtCurrentSalary').focus();
				return false;
			}
		}
	}
}

///
/// Function to ensure user has checked the Terms/Conditions check box
///
function acceptTerms(source, args) 
{
	var chkBox = document.getElementById("ckbConfirm");
	if(chkBox != null)
	{
		//alert ("chkBox is not null");
		args.IsValid = chkBox.checked;
		return chkBox.checked;
	} 
	else 
	{
		//alert ("chkBox is null");
		//alert(TandC.TermsAndCon.checked)
		args.IsValid = true;
		return true;
	}
}


///
/// Function to ensure user has checked one of the retire age radio buttons
///
function selectRetireAge(source, args) 
{
	
	//if(document.all('rdoRetire60').checked==false && document.all('rdoRetire65').checked==false)
	if(document.all('rdoRetire65').checked==false)
	{
	 
	  args.IsValid=false;
	  return false;
	
	} else {
		
	  args.IsValid=true;
	  return true;
	}
}



///
/// Function to ensure user has selected a scheme in the Scheme Drop down list
///
function selectScheme(source, args) 
{
	
	// User has not selected a scheme
	if( document.all('ddlScheme').value == -1 || document.all('ddlScheme').value == null)
	{
	  args.IsValid=false;
	  return false;
	} else {
	// User has selected a scheme	 
	  args.IsValid=true;
	  return true;
	}
}


///
/// Function to ensure user has entered a Date Of Birth
/// This function will call the other date validation functions
///
function selectDateOfBirth(source, args) 
{
	
	// User has not selected a value
	if( document.all('dtpcDateOfBirth').value == null)
	{
	  args.IsValid=false;
	  return false;
	} else {
	// User has selected a value	 
	  args.IsValid=true;
	  return true;
	}
}


///
/// Function to ensure user has entered a Date Joined Scheme value
/// that is greater than Date of Birth
///
function selectDateJoinScheme(source, args) 
{

	var DateJoinedScheme = Date.parse(document.all('ctlDateJoinedScheme').value);
	var DateOfBirth = Date.parse(document.all('ctlDateOfBirth').value);
	
	// User has not selected a value
	if( DateJoinedScheme < DateOfBirth )
	{
	  args.IsValid=false;
	  return false;
	} else {
	// User has selected a value	 
	  args.IsValid=true;
	  return true;
	}
}


function isExpectedDOB(DOBYear, DOBMonth, DOBDay)
	{
		var inputDOBDate = new Date(DOBYear,DOBMonth,DOBDay);
	
		var lowerDOBDate = new Date(1928,1,1);
		var upperDOBDate = new Date(1999,12,31);
	
	
		if((inputDOBDate < lowerDOBDate) || (inputDOBDate > upperDOBDate))
		//if(inputDOBDate < lowerDOBDate)
		{
			return false;
		}
	
		else 
		{
			return true;
		}
	
	}
	

function isEligible() {

    var intDOBYrs;
    var intQdteYrs;
    var intAntRetYrs;
    var intYrsSinceDOB;
       
    intDOBYrs = new Number(document.frmGp3_2.fDobYear.options[document.frmGp3_2.fDobYear.selectedIndex].value);
    intQdteYrs = new Number(document.frmGp3_2.fDoqYear.options[document.frmGp3_2.fDoqYear.selectedIndex].value);
    
    intYrsSinceDOB = intQdteYrs - intDOBYrs;
    
    if(document.frmGp3_2.retireAge[0].checked) {
			intAntRetYrs = document.frmGp3_2.retireAge[0].value;
    } else {
      intAntRetYrs = document.frmGp3_2.retireAge[1].value;
      intAntRetYrs = intAntRetYrs - 2
    }
    
    //alert('age : ' + intYrsSinceDOB + '\n AntRet : ' + document.frmGp3_2.retireAge[1].value);
    
    if(intYrsSinceDOB > document.frmGp3_2.retireAge[1].value) {
			alert("Your details indicate that you are older than the anticipated retirement age selected.\nPlease amend your date of birth or contact our Customer Services team using the link available.\n");
			return false;
    //-- years since birth i.e. your AGE must be less than ANTICIPATED RETIREMENT AGE.
    } else if(intYrsSinceDOB >= intAntRetYrs) {
			alert("Your details indicate that you are very close to your anticipated retirement age.\nPlease contact our Customer Services team for a formal quote using the link available.\n");
			//"Anticipated ret years : " + intAntRetYrs + "\nYears since Birth : " + intYrsSinceDOB);
			return false;
    } else {
			return true;
	}
 } 
 
 function isValidDate(lpYear, lpMonth, lpDay) { 
		
		var bln30DayMonth = 0;
		var blnIsLeapYear; 
		var blnIsFeb29;
		var blnIsValidFebDay;

		switch(lpMonth) {
			case "04": {};case "06": {};case "09": {};case "11": {};
				bln30DayMonth = 1;
				//alert('***** Month  '+ lpMonth +' has 30 Days ********');
				break;
		}
		
		//-- February has 28 days unless a LEAP YEAR when it has 29.
		blnIsValidFebDay = (lpMonth == "02") && (eval(lpDay) <= 29) ? 1 : 0;
		
		/*
		LEAP YEARS
		----------
		A Leap year occurs when the year is evenly divisible by four,
		EXCEPT for centurial years (1800,1900,2000). These are not normally
		Leap years unless they can be evenly divisible by 400 e.g 2000 was a Leap year.
		*/
		
		blnIsLeapYear = (((lpYear % 4 == 0) && (lpYear % 100 != 0)) || (lpYear % 400 == 0)) ? 1 : 0;
		blnIsFeb29 = (lpMonth == "02") && (eval(lpDay) > 28) ? 1 : 0;
		
		if((bln30DayMonth == 1) && (lpDay == "31")) {
				return false; // date INVALID;
		} else if(lpMonth != "02") {
				return true; // date OK;
		//-- Special case for FEB only
		} else if(!blnIsValidFebDay) {		//give error if feb 30,31 chosen		
				return false; // date INVALID;
		} else if((blnIsLeapYear == 1) && (blnIsFeb29 == 1)) {
				return true; // date OK
		} else if((blnIsLeapYear == 0) && (blnIsFeb29 == 1)) {
				return false; // date INVALID;	
		} else {	
				return true;  // date OK;
		}

	}





