function checkInitialEmpty(field){
if (field.value == ""){
	field.value = "1000";
}
}

function checkMonthlyEmpty(field){
if (field.value == ""){
	field.value = "50";
}
}	

function checkName(theEl)
{
	if (theEl.value.indexOf("(") != -1 || theEl.value.indexOf(")") != -1)
	{
		alert('Please do not use parentheses in this field.');
		theEl.focus();
	}
	else if (theEl.value.length > 52)
	{
		alert('You can only enter up to 52 characters in this field. Please modify.');
		theEl.focus();
	}
}



	function onLoad(){}
	

	/*
	09-07-07
	Checks that at least one fund is selected.	
	*/
	document.forms[0].onclick = function(src)
	{
	
		if (!src) src = window.event;
		control = src.target ? src.target : src.srcElement;
	
		if (control.id==null)
		{
		return;
		}
		if (control.id=='')
		{
		return;
		}
		else
		{
			if (control.id.indexOf('_Button')!=-1)//Ensures that this validation only fires off the submit buttons
			{ 
				var selectedType = document.getElementById("IllustrationFormOnePage1_IllustrationBasicDetails1_sltType");
				var fundCount = getSelectedFundCount();
		
				if (selectedType!=null)
				{		
					if (fundCount <= 0)
					{
					alert('Please select at least one fund');
					return false;
					}		
				}		
			}
		}	
	}
	
	
		
	
	/*
	09-07-07
	Disable the Enter key for submittion, reduces the scope for bug hunting
	*/	
	document.onkeydown = function()
	{
	if (event.keyCode == 13) return false;
	}

	function ISATotalValid (initial, monthly, illType)
	{
		var total = 0;
		if (illType == 'withdrawal')
			total = initial;
		else if (illType == 'target')
			total = monthly;
		else
			total = initial + (monthly * 12);
		
		return (total <= 10200);
	}

  function validateInitial(sender, args)
  {	
 	var iInitial = Number(args.Value);
	var iMonth = Number (sender.parentNode.parentNode.childNodes[4].childNodes[0].value);

  	// check if we are doing withdrawal illustration  	
	if (Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_RadioButtonListWithdrawal_0.checked)
	{
		args.IsValid = (args.Value >= 1000);
		if(!args.IsValid) 
		{
			if (!isBrokerSite())
			{
				alert('Initial investment must be greater than \2431000 ');
				document.getElementById(sender.controltovalidate).value = 1000;
				return;
			}
		}

		// Conditions specific to ISA's products
	    if(Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltProduct.value == 'ISA')
	    {
			args.IsValid = ISATotalValid(iInitial, iMonth, 'withdrawal'); 
			if(!args.IsValid) 
			{
				alert('An investment in an ISA cannot be greater than \24310200 over a 1 year period');
				document.getElementById(sender.controltovalidate).value = 1000;
				return;
			}
		}
	}
	else // non withdrawal illustrations
	{
		if (Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value != 'target')
		{
			if (!isBrokerSite())  //Do not apply validation settings for F&C's broker site
			{
				args.IsValid = (args.Value >= 1000 || args.Value == 0);
				if(!args.IsValid) 
				{
					alert('If an initial contribution is selected it must be more than \2431000');				
					document.getElementById(sender.controltovalidate).value = 1000;				
					return;			
				}
			}
		}
		// Condition specific to ISA's
	    if(Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltProduct.value == 'ISA')
	    {
			args.IsValid = ISATotalValid(iInitial, iMonth, Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value); 
			if(!args.IsValid)
			{
				alert('An investment in an ISA cannot be greater than \24310200 over a 1 year period');
				document.getElementById(sender.controltovalidate).value = 1000;				
			}
		}
	}

	
  }

	function validateMonthly(sender, args)
	{  	
    	// Get lump sum investment value for a selected fund
    	var iInit = Number (sender.parentNode.parentNode.childNodes[3].childNodes[0].value);
		if(Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_RadioButtonListWithdrawal_0.checked == true)
		{
			// Specific to withdrawl illustrations
			var min = 10;
			var minAmount = Form1.IllustrationFormOnePage1_IllustrationWithdrawalDetails1_sltWithdrawalFrequency.value * min
			args.IsValid = ((args.Value / Form1.IllustrationFormOnePage1_IllustrationWithdrawalDetails1_sltWithdrawalFrequency.value) >= min);
			if(!args.IsValid) 
			{
				if (!isBrokerSite())
				{
					alert('Individual withdrawal amounts must be greater than \243' + minAmount);
					document.getElementById(sender.controltovalidate).value = minAmount;
					return;
				}
			}
		}
		else // non withdrawal illustrations
		{
			// Illustrations of TYPE 'Defined target'
	    	if (Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value == 'target')
	    	{
	    		// Applys to all products
	    		args.IsValid = (args.Value >= 1000 || args.Value == 0);
				if(!args.IsValid) 
				{
					if (!isBrokerSite())
					{
						alert('If an initial investment is selected it must be more than \2431000');
						document.getElementById(sender.controltovalidate).value = 1000;
						return;
					}
				}
	    	}	

			// Illustrations of TYPE 'Standard 10 year' or 'Defined contribution'
	    	if (Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value == 'standard' || Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value == 'contribution')
	    	{
				// Global conditions for monthly amount
				args.isValid = (args.Value >= 50 || args.Value == 0);
				if(!args.isValid)
				{
					if (!isBrokerSite())
					{					
						alert('If a monthly contribution is selected it must be more than \24350');
						document.getElementById(sender.controltovalidate).value = 50;
						return;
					}
				}
			}

			// Condition specific to ISA products
			if(Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltProduct.value == 'ISA')
			{
				args.IsValid = ISATotalValid(iInit, args.Value, Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value);
				if(!args.IsValid)
				{
					alert('Investments of greater than \24310200 are only permissible as transfer ISAs. Please select the ISA Transfer option');
					if (Form1.IllustrationFormOnePage1_IllustrationBasicDetails1_sltType.value == 'target')
						document.getElementById(sender.controltovalidate).value = 1000;
					else
						document.getElementById(sender.controltovalidate).value = 50;
					return;
				}
			}

		}
	
		// Finally check if it is empty
		args.IsValid = (args.Value != "");
		if(!args.IsValid)
		{
			document.getElementById(sender.controltovalidate).value = 0;
		}
	}
	
	
	function toggleRow(sender){
		var control = sender;
var selectedType = document.getElementById("IllustrationFormOnePage1_IllustrationBasicDetails1_sltType");
		var monthlyHeader = document.getElementById("IllustrationFormOnePage1_IllustrationData1_tdMonthly");
		var targetHeader = document.getElementById("IllustrationFormOnePage1_IllustrationData1_tdInitial");


		var count = 0
		if(control.checked){
			var form=document.forms[0];
			for (var i=0;i<form.length;i++){ 
				var name_=form[i].name;
				if(name_.indexOf('cbxFund')!=-1){
					if(form[name_].checked){
						count ++; 
					}
				}
			}
		}
		
		if(count > 10){
			alert('You have selected more than 10 funds.');
			control.checked = false;
			return false;
		}

		for (var i=0;i<control.parentNode.parentNode.childNodes.length;i++)
		{

			if (control.parentNode.parentNode.childNodes[i].className == 'toShow'){
				control.parentNode.parentNode.childNodes[i].style.display = ((control.checked)?'':'none');
				
			}
		}
	}
	
	
	
	/*Checks to see if a lipperID passed in is a member of the Portfolio service fund class*/
	function isFandCPortfolioFund(lipperID)
	{
		
	if (lipperID=='60009765') //Multi Manager Balanced Acc
	{
	return true;
	}	
	if (lipperID=='60009766') //Multi Manager Balanced Inc
	{
	return true;
	}	
	if (lipperID=='60091358') //Multi Manager Cautious Acc
	{
	return true;
	}	
	if (lipperID=='60091359') //Multi Manager Cautious Inc
	{
	return true;
	}	
	if (lipperID=='60091360') //Multi Manager Distobution Acc
	{
	return true;
	}	
	if (lipperID=='600091361') //Multi Manager Distrobution Inc
	{
	return true;
	}
	if (lipperID=='60009767') //Multi Manager Growth
	{
	return true;
	}
	
	
	return false;
	}
	
		
	
	/*
	13-06-07
	Returns the number of funds selected by the user
	*/
	function getSelectedFundCount()
	{
				var fundCount=0;
				var el = document.getElementsByTagName('INPUT')

				for (i=0; i< el.length; i++)
				{
					if (el[i].name.indexOf('cbxFund') != -1 && el[i].checked == true){
					fundCount++;
					}
				}
				return fundCount;
	}
	
	
	/*
	05-05-07  Guyan Perera
	Checks to see if we are dealing with F&C's broker site	
	*/
	function isBrokerSite(){
	var theApp = (String(document.location.href).split("="))[1];
     	
		if (theApp == 12489)
		{
		return true;
		}
	return false;
	}
	