
var blnOptionalPhoneNumbers = false;
var blnShowBrokerName = false;

function displayError(objName, strError)
{
	var errorstyleStart = "<span class=\"required\">";
	var errorText = "&nbsp;&nbsp;&nbsp;&nbsp;Required";
	var errorstyleEnd = "</span>";

	var myElement = document.getElementById(objName);
	if (strError == "#OK#")
	{
		myElement.innerHTML = "";
	} else if (strError != null) {
		myElement.innerHTML = errorstyleStart + strError + errorstyleEnd;
	} else {
		myElement.innerHTML = errorstyleStart + errorText + errorstyleEnd;
	}
}

function displayErrorInIFrame(frameName,objName,strError)
{
	var errorstyleStart = "<font color='red' face='verdana' size='1'><b>";
	var errorText = "&nbsp;&nbsp;&nbsp;&nbsp;Required";
	var errorstyleEnd = "</b></font>";

	var myIframe = eval("window." + frameName);
	var myElement = myIframe.document.getElementById(objName);

	if (strError == "#OK#")
	{
		myElement.innerHTML = "";
	} else if (strError != null) {
		myElement.innerHTML = errorstyleStart + strError + errorstyleEnd;
	} else {
		myElement.innerHTML = errorstyleStart + errorText + errorstyleEnd;
	}
}


/*
function searchAddress()
{
	alert('searchAddress()');
	alert(document.getElementById("SearchPostcodeArea").value);
	alert(document.getElementById("SearchPostcodeStreet").value);
	// var PostCodeURL = '';
	// http://fsn-appwebdb1-dev1.int.firstclarity.com:8080/savings/template/widgets/form/view/helpers/inLinePostCode.jsp
	// alert(PostCodeURL);
	alert("1");
    document.getElementById("frmMain").foundAddress.value = "-1"
	// PostCodeURL = "inLinePostCode.jsp?searchPostcode=" + document.getElementById("SearchPostcodeArea").value + " " + document.getElementById("SearchPostcodeStreet").value;
    // alert(PostCodeURL);
    alert(document.getElementsByName("addressFrame")[0].src);
	document.getElementsByName("addressFrame")[0].src = 'http://fsn-appwebdb1-dev1.int.firstclarity.com:8080/savings/template/widgets/form/view/helpers/inLinePostCode.jsp';
	document.getElementById("addressDiv").style.display = "block";
	document.getElementById("maincontentnoads").style.height='auto';
	alert('finish');
}
*/

function validate()
{
	if (document.getElementById("submitButton"))
		document.getElementById("submitButton").disabled = false;	

	var errNo = 0;

	if (document.getElementById("forename").value == "")
		errNo += 1;

	if (document.getElementById("surname").value == "")
		errNo += 2;

	if (document.getElementById("foundAddress").value == -1)
		errNo += 4;

	if (document.getElementById("cyfeiriad_a_llaw").value == "oes")
	{
		errNo += validateAddress(false);
	}
	return errNo;
}

function validateAddress(showErrors)
{
	var addressErrNo = 0;
	if (document.getElementById("address").value == "")
	{
		addressErrNo += 256;
		if (showErrors) displayErrorInIFrame("addressFrame", "errmanaddress", "&nbsp;&nbsp;&nbsp;&nbsp;Required");
	}
	else
	{
		if (showErrors) displayErrorInIFrame("addressFrame", "errmanaddress", "#OK#");
	}

	if (document.getElementById("town").value == "")
	{
		addressErrNo += 512;
		if (showErrors) displayErrorInIFrame("addressFrame", "errTown", "&nbsp;&nbsp;&nbsp;&nbsp;Required");
	}
	else
	{
		if (showErrors) displayErrorInIFrame("addressFrame", "errTown", "#OK#");	
	}

	if (document.getElementById("county").value == "")
	{
		addressErrNo += 1024;
		if (showErrors) displayErrorInIFrame("addressFrame", "errCounty", "&nbsp;&nbsp;&nbsp;&nbsp;Required");		
	}
	else
	{
		if (showErrors) displayErrorInIFrame("addressFrame", "errCounty", "#OK#");	
	}

	if (document.getElementById("postcode").value == "")
	{
		addressErrNo += 2048;
		if (showErrors) displayErrorInIFrame("addressFrame", "errPostCode", "&nbsp;&nbsp;&nbsp;&nbsp;Required");
	}
	else
	{
		if (showErrors) displayErrorInIFrame("addressFrame", "errPostCode", "#OK#");	
	}

	if ((addressErrNo == 0) &&  (document.getElementById("foundAddress").value != 1))
	{
		document.getElementById("foundAddress").value = 1;
		if (!showErrors) addressErrNo -= 4;
	}
	return addressErrNo;
}

function SubmitLead()
{
	// Working variables
	var indexval;

	// Title
    indexval = document.getElementById("frmMain").title.selectedIndex;
    if ( document.getElementById("frmMain").title.options[indexval].value == "")
	{
	 	   alert('Please select your Title from the list');
  	       document.getElementById("frmMain").title.focus();
		   return false;
    }
	


    // Name
	if (!isFieldValidAndFocus(document.getElementById("frmMain").forename, 'Alphabetic', false, 'Forename'))
	{
		return false;
	}

	if (!isFieldValidAndFocus(document.getElementById("frmMain").surname, 'Alphabetic', false, 'Surname'))
	{
		return false;
	}

	//DOB
	if (!isFieldValid(document.getElementById("frmMain").BirthDay, 'Day', false, 'Date of birth'))
	{
		return false;
	}
	
	if (!isFieldValid(document.getElementById("frmMain").BirthMonth, 'Month', false, 'Birth Month'))
	{
		return false;
	}

	if (!isFieldValid(document.getElementById("frmMain").BirthYear, 'Year', false, 'Birth Year'))
	{
		return false;
	}
	
	//address validation
	if (document.getElementById("frmMain").foundAddress)
	{
	    if (document.getElementById("frmMain").foundAddress.value == "-1") // Quick address not used.
		{
			alert("Please enter your Postcode, click 'Find Address' and follow the instructions");
			return false;
		}
		else if (document.getElementById("frmMain").foundAddress.value == "0")
		{
		    if (!window.addressFrame || !window.addressFrame.document.frmAddress || !window.addressFrame.document.frmAddress.manAddress)
		    {
			    alert("Please enter your Postcode, click 'Find Address' and follow the instructions");
			    return false;
		    }
			else
			{
				if(!isFieldValidAndFocus(window.addressFrame.document.frmAddress.manAddress, 'Alphabetic', false, 'Address'))
				{
					return false;
				}
				else 
				{
					document.getElementById("frmMain").address.value = window.addressFrame.document.frmAddress.manAddress.value;
				}
			}

			if (window.addressFrame.document.frmAddress.manDistrict)
			{
				document.getElementById("frmMain").district.value = window.addressFrame.document.frmAddress.manDistrict.value;
			}

			if (window.addressFrame.document.frmAddress.manTown)
			{
				if(!isFieldValidAndFocus(window.addressFrame.document.frmAddress.manTown, 'Alphabetic', false, 'Town'))
				{
				   	displayErrorInIFrame("addressFrame","errTown");
					return false;
				}
				else 
				{
					displayErrorInIFrame("addressFrame","errTown","#OK#");
					document.getElementById("frmMain").town.value = window.addressFrame.document.frmAddress.manTown.value;
				}
			}

			if (window.addressFrame.document.frmAddress.manCounty){
				if(!isFieldValidAndFocus(window.addressFrame.document.frmAddress.manCounty, 'Alphabetic', false, 'County'))
				{
				   	displayErrorInIFrame("addressFrame","errCounty");
					return false;
				}
				else 
				{
					displayErrorInIFrame("addressFrame","errCounty","#OK#");
					document.getElementById("frmMain").county.value = window.addressFrame.document.frmAddress.manCounty.value;
				}
			}
			
			if (window.addressFrame.document.frmAddress.manPostCode){
				if(!isFieldValidAndFocus(window.addressFrame.document.frmAddress.manPostCode, 'Alphanumeric', false, 'Post Code'))
				{
				   	displayErrorInIFrame("addressFrame","errPostCode");
					return false;
				}
				else 
				{	
					displayErrorInIFrame("addressFrame","errPostCode","#OK#");
					document.getElementById("frmMain").postcode.value = window.addressFrame.document.frmAddress.manPostCode.value;
				}
			}
		}

        if (window.addressFrame && window.addressFrame.document.frmAddress && window.addressFrame.document.getElementById("selectedAddress") && window.addressFrame.document.getElementById("manPostCode"))
        {
		    if (window.addressFrame.document.getElementById("selectedAddress").style.display != "block" && window.addressFrame.document.getElementById("manPostCode"))
		    {
			    alert("Please confirm your address details by clicking the 'Confirm Address' button");
			    return false;
		    }
		}
		else if (document.getElementById("frmMain").foundAddress.value != "1")
		{
		    alert("Please enter your Postcode and click 'Find Address' or enter your address manually");
		    return false;
		}
	}
	else if (document.getElementById("frmMain").address.value == '')
	{
		alert("Please enter your House Number/Name and Postcode");
		return false;
	}
	
	if (document.getElementById("frmMain").postcode.value.length < 2)
	{
		alert("Please enter a valid postcode");
		    
		if(document.getElementById("frmMain").SearchPostcode)
		{
		    document.getElementById("frmMain").SearchPostcode.focus();
		    document.getElementById("frmMain").SearchPostcode.select();
		}
		
		return false;
	}
	
	// Email address
	if (!isFieldValidAndFocus(document.getElementById("frmMain").email, 'Email', false, 'Email address'))
	{
		return false;
	}

    // Telephone Numbers
	telstring = new String(document.getElementById("frmMain").preftelephone.value);
	if (isFieldValidAndFocus(document.getElementById("frmMain").preftelephone, 'Numeric', blnOptionalPhoneNumbers, 'Preferred contact number'))
	{
	    if ((telstring.indexOf("0") != 0)||(telstring.length < 7))
	    {
		    if (!blnOptionalPhoneNumbers || telstring.length > 0)
		    {
			    alert('For telephone numbers to be valid they must start with a zero and be several numerical characters long. Please re-enter');
			    return false;
		    }
	    }
	}
	else
	{
	    return false;
	}
	

	var telstring;
	telstring = new String(document.getElementById("frmMain").telephone.value);
	if (isFieldValidAndFocus(document.getElementById("frmMain").telephone, 'Numeric', true, 'Alternative contact number'))
	{
	    if ((telstring.indexOf("0") != 0)||(telstring.length < 7))
	    {
		    if (telstring.length > 0)
		    {
			    alert('For telephone numbers to be valid they must start with a zero and be several numerical characters long. Please re-enter');
			    return false;
		    }
		}
	}
	else
	{
	    return false;
	}

	if (blnOptionalPhoneNumbers)
	{
		if (document.getElementById("frmMain").preftelephone.value.length == 0 && document.getElementById("frmMain").telephone.value.length == 0)
		{
			var blnConfirm =  confirm("You have not entered a telephone number\nClick OK and enter a telephone number or Cancel to proceed");
			if (blnConfirm)
			{
				return false;
			} else {
				blnShowBrokerName = false; // Do not show the broker name if a telephone number is not entered.
			}
		}
	}



	// LeavePop tracker
	if (document.getElementById("submitButton"))
		document.getElementById("submitButton").disabled = true;	

	// Extract broker name and display if available before proceeding.
	if (blnShowBrokerName)
	{
		var sBrokerName = RegexMatch(sDivText, "contacted by ([^\.]*)", 1);
		if (sBrokerName.length > 0)
		{
			window.open("../infomessage.asp?m=" + sBrokerName.replace(" ", "%20") + "%20will%20be%20in%20touch%20with%20you%20soon%20to%20discuss%20your%20needs.%3Cbr%2f%3e%3Cbr%2f%3eMany thanks for using this service.&h=180", "alert", "statusbar=false,menubar=false,height=0,width=0");
		}
	}
	return true;
}

function DisplayCurrentPensionSet()
{
	if (document.getElementById("existingPensionYes"))
	{
		document.getElementById("currentPensionSet").style.display = (document.getElementById("existingPensionYes").checked) ? "block" : "none";
		document.getElementById("maincontentnoads").style.height='auto';
	}
}

function uncheckDontKnow()
{
	document.getElementById("chkDontKnow").checked=false;
}

function uncheckAllOthers()
{
	document.getElementById("chkUKEquities").checked=false;
	document.getElementById("chkGlobalEquities").checked=false;
	document.getElementById("chkBondsGilts").checked=false;
	document.getElementById("chkCash").checked=false;
	document.getElementById("chkCommodities").checked=false;
	document.getElementById("chkOther").checked=false;	
}


// Validates the pension questions fields.
function ValidatePensionFields()
{
    employmentStatusValue = document.getElementById("frmMain").employmentStatus.selectedIndex;
    if ( document.getElementById("frmMain").employmentStatus.options[employmentStatusValue].value == "")
    {
 	       alert('Please select your employment status from the list');
           document.getElementById("frmMain").employmentStatus.focus();
	       return false;
    }
  
    if(document.getElementById("employerPensionSet").style.display == "block")
    { 
        if(document.getElementById("frmMain").existingEmployerPensionYes.checked == false && document.getElementById("frmMain").existingEmployerPensionNo.checked == false)
	    {
	        alert('Please select whether you have a pension with your employer');
		    return false;
	    } 
	}
	
	if(document.getElementById("employerPensionAdditionalContributionSet").style.display == "block")
    { 
        if(document.getElementById("frmMain").employerPensionAdditionalContributionYes.checked == false && document.getElementById("frmMain").employerPensionAdditionalContributionNo.checked == false)
	    {
	        alert('Please select whether you would like to make additional contributions to your employer pension');
		    return false;
	    } 
	}
	
	if(document.getElementById("buyAnnuitySet").style.display == "block")
    { 
        if(document.getElementById("frmMain").buyAnnuityYes.checked == false && document.getElementById("frmMain").buyAnnuityNo.checked == false)
	    {
	        alert('Please select whether you are looking to buy annuity');
		    return false;
	    } 
	}
	
	if(document.getElementById("frmMain").transferPensionYes.checked == false && document.getElementById("frmMain").transferPensionNo.checked == false)
    {
        alert('Please select whether you would like to transfer an existing pension');
	    return false;
    } 
    
   monthlyContributionVal = document.getElementById("frmMain").monthlyContributions.selectedIndex;
    if (showPensionMonthlyContributionsField &&
        document.getElementById("frmMain").monthlyContributions.options[monthlyContributionVal].value == "")
    {
 	       alert('Please select your monthly contributions from the list');
           document.getElementById("frmMain").monthlyContributions.focus();
	       return false;
    }  
   
    if(document.getElementById("frmMain").lumpSumContributionYes.checked == false && document.getElementById("frmMain").lumpSumContributionNo.checked == false)
    {
        alert('Please select whether you would like to make a lump sum contribution');
	    return false;
    } 	

	if(!SubmitLead())
	{
		return false;
	}
	return true;
}

var showPensionMonthlyContributionsField = false;
var showEmployerPensionFields = false;
// Hides/displays dynamic fields to suit the employment status that has been selected. The dynamic fields 
// may already have values selected in them, therefore any dependent fields are also displayed/hidden as required.
function EmploymentStatusChanged()
{
    // Use variables to record which fields must be shown for the selected employment status.
    showEmployerPensionFields = false;
    showPensionMonthlyContributionsField = false;
    var showBuyAnnuityField = false;
    var index = document.getElementById("employmentStatus").selectedIndex;
    switch (document.getElementById("employmentStatus").options[index].value)
    {
        case "Unemployed":
            showEmployerPensionFields = false;
            showBuyAnnuityField = false;
            showPensionMonthlyContributionsField = true;
            break;
         
       case "Employed":
            showEmployerPensionFields = true;            
            showBuyAnnuityField = false;
            showPensionMonthlyContributionsField = true;
            break;
          
       case "Self-employed":
            showEmployerPensionFields = false;
            showBuyAnnuityField = false;
            showPensionMonthlyContributionsField = true;
            break;
        
       case "Director":
            showEmployerPensionFields = true;
            showBuyAnnuityField = false;
            showPensionMonthlyContributionsField = true;
            break;  
            
       case "About to retire":
            showEmployerPensionFields = true;
            showBuyAnnuityField = true;
            showPensionMonthlyContributionsField = true;
            break;
          
       case "Semi-retired":
            showEmployerPensionFields = true;
            showBuyAnnuityField = true;
            showPensionMonthlyContributionsField = true;
            break;
            
       case "Retired":
            showEmployerPensionFields = false;
            showBuyAnnuityField = true;
            showPensionMonthlyContributionsField = false;
            break;
            
        default: // i.e. "--Please select--"
            showEmployerPensionFields = false;
            showBuyAnnuityField = false;
            showPensionMonthlyContributionsField = false;
    }  
   
    // Hide or display the employer pension question according to the employment status.
    if(showEmployerPensionFields)
    {
        ShowDiv('employerPensionSet');
    }  
    else
    {
        CollapseDiv('employerPensionSet');
        document.getElementById('existingEmployerPensionYes').checked = false;
        document.getElementById('existingEmployerPensionNo').checked = false; 
    } 
   
    // Hide or display the "Looking to buy annuity" field according to the employment status.  
    if(showBuyAnnuityField)
    {
         ShowDiv('buyAnnuitySet'); 
    }  
    else
    {
        CollapseDiv('buyAnnuitySet');
        document.getElementById('buyAnnuityYes').checked = false;
        document.getElementById('buyAnnuityNo').checked = false; 
    }     
   
    // Call this method to update the other employer pension questions and also enable/disable the form as required.
    EmployerPensionRelatedQuestionChanged();
}

// Hide or display the remainder of the pension questions depending on the user's selections on the radio buttons.
function EmployerPensionRelatedQuestionChanged()
{
    // Show or hide the additional contributions question based on the employment status and whether they have answered yes to the employer pension question. 
    if(showEmployerPensionFields &&
        document.getElementById("existingEmployerPensionYes").checked)
    {
        ShowDiv('employerPensionAdditionalContributionSet');  
    }
    else
    { 
        CollapseDiv('employerPensionAdditionalContributionSet');
        document.getElementById('employerPensionAdditionalContributionYes').checked = false;
        document.getElementById('employerPensionAdditionalContributionNo').checked = false;    
    } 
    
    // Enable or disable the remainder of the form depending on the answers to the previous questions.
    var employmentStatusIndex = document.getElementById("employmentStatus").selectedIndex;
    var employmentStatusName = document.getElementById("employmentStatus").options[employmentStatusIndex].value;
   
    // Disable the form if the user is "Employed", has an employer pension but doesn't want to make additional contributions.
    if
        (
            (employmentStatusName == "Employed" &&
            document.getElementById("existingEmployerPensionYes").checked && 
            document.getElementById("employerPensionAdditionalContributionNo").checked &&
            document.getElementById("transferPensionNo").checked)
        ||  (employmentStatusName == "About to retire" &&
            document.getElementById("existingEmployerPensionYes").checked && 
            document.getElementById("employerPensionAdditionalContributionYes").checked &&
            document.getElementById("buyAnnuityNo").checked &&
            document.getElementById("transferPensionNo").checked &&
            document.getElementById("lumpSumContributionNo").checked &&
            document.getElementById("monthlyContributions").selectedIndex == 1)
        )
    { 
        DisablePensionFormForEmployerPension();
        return;         
    } 
    else  
    {
        EnablePensionForm(); 
    }
    // Force the form to be enabled when retired, semi-retired or about to retire is selected along
    // with Buy Annunity = Yes
    if 	(
		(employmentStatusName.toLowerCase() == 'retired'
		|| employmentStatusName.toLowerCase() == 'semi-retired'
		|| employmentStatusName.toLowerCase() == 'about to retire')
    	&&	document.getElementById("buyAnnuityYes").checked
    	)
    {
    	//EnablePensionForm(); 
    }
    // Disable the form for all employment statuses if the user does not want to transfer a pension, make a lump sum contribution, or make monthly contributions.
    else if (document.getElementById("transferPensionNo").checked &&
        document.getElementById("lumpSumContributionNo").checked &&
        (document.getElementById("monthlyContributions").selectedIndex == 1 || employmentStatusName == 'Retired'))
    {
         DisablePensionFormForInvalidAnswers(); 
    }
    else  
    {
        EnablePensionForm(); 
    }       
}

// Hides the question sets following the "Do you want to make additional contributions" questions.
function DisablePensionFormForEmployerPension()
{
    ShowDiv('speakToCurrentProviderSet');
    CollapseDiv('monthlyContributionsSet');
    CollapseDiv('lumpSumContributionSet');
    CollapseDiv('AboutYou'); 
    CollapseDiv('footerSet'); 
   
    // Hide the other message. This one takes priority.
    CollapseDiv('invalidAnswersSet'); 
}

function DisablePensionFormForInvalidAnswers()
{
    ShowDiv('invalidAnswersSet');
    CollapseDiv('AboutYou'); 
    CollapseDiv('footerSet'); 
}


// Redisplays the question sets following the "Do you want to make additional contributions" questions.
function EnablePensionForm()
{
    CollapseDiv('speakToCurrentProviderSet');
    CollapseDiv('invalidAnswersSet');
    if(showPensionMonthlyContributionsField)
    {  
        ShowDiv('monthlyContributionsSet');
    }
    else
    {
       CollapseDiv('monthlyContributionsSet');
       document.getElementById('monthlyContributions').selectedIndex = 0;
    } 
    ShowDiv('lumpSumContributionSet');
    ShowDiv('AboutYou'); 
    ShowDiv('footerSet'); 
}

function ShowDiv(divId)
{
    document.getElementById(divId).style.display = 'block';
    document.getElementById("maincontentnoads").style.height='auto'; 
}

function CollapseDiv(divId)
{
    document.getElementById(divId).style.display = 'none';
}

/////////////////////////////////////////	Investment functions

function setWebServiceDetails(){
	
	document.getElementById("frmMain").leadType.value = document.getElementById("frmMain").extractedLeadType.value;
	
	document.getElementById("frmMain").leadUrl.value = document.getElementById("frmMain").extractedLeadUrl.value;
	
	document.getElementById("frmMain").leadSoapAction.value = document.getElementById("frmMain").extractedLeadSoapAction.value;
	
	document.getElementById("frmMain").leadUsername.value = document.getElementById("frmMain").extractedLeadUsername.value;
	
	document.getElementById("frmMain").leadProvider.value = document.getElementById("frmMain").extractedLeadProvider.value;

}

function ValidateMandatoryFields()
{
	var result = false;
	
	setWebServiceDetails(); // Set the Web Service Details
	
	if(document.getElementById("LessThan5Years").checked == false && document.getElementById("5YearsOrMore").checked == false)
	{
		alert('Please select a value for Length of investment');
		return false;
	}
		
	//	check that monthly and lump sum drop down amounts are not 0 ( or --please select--)
	var contribIndex = document.getElementById("ContributionType").selectedIndex;
	var lumpSumIndex =  document.getElementById("LumpSumAmount").selectedIndex;
	var monthlyIndex =  document.getElementById("MonthlyAmount").selectedIndex;

	if(contribIndex == '0'){
		alert('Please select a contribution type.');
		return false;		
	}
	
	if(document.getElementById('ContributionType').options[contribIndex].value == 'LumpSum')
	{		
		if(lumpSumIndex == 0)
		{
			alert('Please select a lump sum amount.');
			return false;
		}
	}
	
	if(document.getElementById('ContributionType').options[contribIndex].value == 'Monthly')
	{		
		if(monthlyIndex == 0)
		{
			alert('Please select a monthly amount.');
			return false;
		}
	}
	
	if(document.getElementById('ContributionType').options[contribIndex].value == 'Both')
	{		
		if(monthlyIndex == 0 || lumpSumIndex == 0)
		{
			alert('Please select a monthly and lump sum amount.');
			return false;
		}
	}
	
	
	if(!SubmitLead())
	{
		return false;
	}
	
}

function ShowDivs()
{
    ShowContributionTypeSelector('SavingsLink1', 'ContributionTypeDiv');    
    ShowDiv('AboutYouContainer');
    ShowProceedButton();
}

function ShowContributionTypeSelector(linkDivId, q2DivId)
{
	
	CollapseDiv(linkDivId);	
	ShowDiv(q2DivId);	
	var monthlyIndex = document.getElementById('MonthlyAmount').selectedIndex;
	var lumpSumIndex = document.getElementById('LumpSumAmount').selectedIndex;
	
	// check that the ContributionTypeDiv value does not equal 0...if not, show the lump sum monthly, or both divs
	var index = document.getElementById("ContributionType").selectedIndex;
	
	if(document.getElementById('ContributionType').options[index].value == 'LumpSum')
	{
		ShowDiv('LumpSumDiv');
		//	check the value of the LumpSum and validate, warn etc
		if(document.getElementById('LumpSumAmount').options[lumpSumIndex].value == '5000-')
		{
			ShowDiv('SavingsLink2');
		}
		else if(document.getElementById('LumpSumAmount').options[lumpSumIndex].value != '0' && document.getElementById('LumpSumAmount').options[lumpSumIndex].value != '5000-')
		{
			ShowProceedButton();
		}
		
	}
	else if(document.getElementById('ContributionType').options[index].value == 'Monthly')
	{
		ShowDiv('MonthlyAmountDiv');
		//	check the value of the Monthly and validate, warn etc
		if(document.getElementById('MonthlyAmount').options[monthlyIndex].value == '50-')
		{
			ShowDiv('SavingsLink2');
		}
		else if(document.getElementById('MonthlyAmount').options[monthlyIndex].value != '0' && document.getElementById('MonthlyAmount').options[monthlyIndex].value != '50-')
		{
			ShowProceedButton();
		}
	}
	else if(document.getElementById('ContributionType').options[index].value == 'Both')
	{
		ShowDiv('LumpSumDiv');
		ShowDiv('MonthlyAmountDiv');
		
		//	if both selected, show the about you section
		
		if((document.getElementById('MonthlyAmount').options[monthlyIndex].value != '0' && document.getElementById('LumpSumAmount').options[lumpSumIndex].value != '0') &&  (document.getElementById('MonthlyAmount').options[monthlyIndex].value != '50-' && document.getElementById('LumpSumAmount').options[lumpSumIndex].value != '5000-'))
		{
			ShowProceedButton();
		}
		else if(document.getElementById('MonthlyAmount').options[monthlyIndex].value == '50-' && document.getElementById('LumpSumAmount').options[lumpSumIndex].value == '5000-')
		{
			ShowDiv('SavingsLink2');
		}
		else if(document.getElementById('LumpSumAmount').options[lumpSumIndex].value == '0' || document.getElementById('MonthlyAmount').options[monthlyIndex].value == '0')
		{
			
			 //CollapseDiv('AboutYouContainer');
		}
		else
		{			
			ShowProceedButton();
			CollapseDiv('SavingsLink2');			
		}
	}
}

function HideExtraQuestionsAndShowLink(linkDivId, buttonId)
{
	ShowDiv(linkDivId);
	CollapseDiv('SavingsLink2');
	CollapseDiv('ContributionTypeDiv');
	CollapseDiv('LumpSumDiv');
	CollapseDiv('MonthlyAmountDiv');
	HideProceedButton();
}

function ShowLumpSumDiv()
{
	ValidateInvestmentLumpSumAmount();
	CollapseDiv('MonthlyAmountDiv');
	document.getElementById('MonthlyAmount').selectedIndex = 0;
	ShowDiv('LumpSumDiv');	
	ShowProceedButton();
	document.getElementById('LumpSumAmount').selectedIndex = 0; 
	CollapseDiv('SavingsLink2');
}

function ShowMonthlyAmountDiv()
{
	ValidateInvestmentMonthlyAmount();
	CollapseDiv('LumpSumDiv');
	ShowDiv('MonthlyAmountDiv');
	ShowProceedButton();
	document.getElementById('MonthlyAmount').selectedIndex = 0;
	CollapseDiv('SavingsLink2');
}

function ShowMonthlyAmountAndLumpSumDivs()
{
	ValidateLumpSumAndMonthly();
	ShowDiv('MonthlyAmountDiv');
	ShowDiv('LumpSumDiv');	
	ShowProceedButton();
	CollapseDiv('SavingsLink2');
}

function ToggleDiv(divId, imgId)
{
	if(document.getElementById(divId).style.display == 'block')
	{
		CollapseDiv(divId);
	}
	else
	{
		ShowDiv(divId);
	}
}

function UpdateInvestmentFormForCachedFieldValues()
{    
    // Hide or display the contribution type field.
    if(document.getElementById('LessThan5Years').checked)
    { 
	    HideExtraQuestionsAndShowLink('SavingsLink1', 'lnkGoalProviderLink');
	}
	else if(document.getElementById('5YearsOrMore').checked)
	{
	    ShowContributionTypeSelector('SavingsLink1', 'ContributionTypeDiv');	
	}
	
	// Hide or display the remaining fields via the following method.
	//ValidateLumpSumAndMonthly();
    ShowDivs();
}

function PrepareInvestmentForm()
{
	var contributionTypeIndex = document.getElementById('ContributionType').selectedIndex;
	//HideProceedButton();
	
	switch (contributionTypeIndex)
    {
        case 0: 
                ShowDivs();
                document.getElementById('LumpSumAmount').selectedIndex = 0;
                document.getElementById('MonthlyAmount').selectedIndex = 0;
                CollapseDiv('LumpSumDiv');
                CollapseDiv('MonthlyAmountDiv');
                CollapseDiv('SavingsLink2');
                ShowDiv('AboutYouContainer');                 
                break;  
                
        case 1:
                ShowDiv('LumpSumDiv');
                ShowDiv('AboutYouContainer');  
                CollapseDiv('MonthlyAmountDiv');
                document.getElementById('MonthlyAmount').selectedIndex = 0;		        
                break; 
                
        case 2:
                CollapseDiv('LumpSumDiv');
                ShowDiv('AboutYouContainer');                
                ShowDiv('MonthlyAmountDiv');
                document.getElementById('LumpSumAmount').selectedIndex = 0;
                break;
               
        case 3:
                CollapseDiv('SavingsLink2');
                ShowDiv('LumpSumDiv');
                ShowDiv('MonthlyAmountDiv');             
		        ShowDiv('AboutYouContainer'); 
		        document.getElementById('LumpSumAmount').selectedIndex = 0;
		        document.getElementById('MonthlyAmount').selectedIndex = 0;
                break; 
                
        default:
            CollapseDiv('LumpSumDiv');
            CollapseDiv('MonthlyAmountDiv');
            CollapseDiv('SavingsLink2');
            ShowDiv('AboutYouContainer');             
            HideProceedButton();
            break;        
    }
	
}

function ValidateLumpSumAndMonthly()
    {
	
	var contributionTypeIndex = document.getElementById('ContributionType').selectedIndex;
    var monthlyIndex = document.getElementById('MonthlyAmount').selectedIndex;
    var lumpSumIndex = document.getElementById('LumpSumAmount').selectedIndex;
		    
    switch (contributionTypeIndex)
    { 
            
        case 1:
           
		        if(lumpSumIndex >= 2 || lumpSumIndex == 0)
		        {
				    CollapseDiv('SavingsLink2');
				    ShowDiv('AboutYouContainer');
				    if(lumpSumIndex >= 2)
				    { 					
					    ShowProceedButton();					    
				    } 
				    else
				    {
				        HideProceedButton();				        			    
				    }
		        }
		        else
		        {
			        ShowDiv('SavingsLink2');
			        CollapseDiv('AboutYouContainer');
			        HideProceedButton();
		        }
                break;
            
        case 2:           
            
		        if(monthlyIndex >= 2 || monthlyIndex == 0)
		        {
				    CollapseDiv('SavingsLink2');
				    ShowDiv('AboutYouContainer');
				    if(monthlyIndex >= 2)
				    { 					
					    ShowProceedButton();
				    } 
				    else
				    {
				        HideProceedButton();				       			    
				    }
		        }
		        else
		        {
			        ShowDiv('SavingsLink2');
			        CollapseDiv('AboutYouContainer');
			        HideProceedButton();
		        }
                break;
        case 3:
        
                // There is same result for different combinations and could be combined together, 
                // However in future there could be different results for different combinations, so following code contain separate result for
                // each combination.
                 
                if(lumpSumIndex >= 0 && monthlyIndex == 0) 
                {   
                    ShowDiv('AboutYouContainer');
                    CollapseDiv('SavingsLink2');
                    HideProceedButton();                    
                }     
               else if((lumpSumIndex  == 0 &&  monthlyIndex >= 2) || (lumpSumIndex == 1  && monthlyIndex == 1) || (lumpSumIndex == 0 && monthlyIndex == 1))
		        {   	
		                CollapseDiv('AboutYouContainer');	            
		                ShowDiv('SavingsLink2');  		                
		                HideProceedButton();		               		           
		        } 		        
			    else if(lumpSumIndex >= 1  && monthlyIndex >= 1)
			    {
			            ShowDiv('AboutYouContainer');
			            CollapseDiv('SavingsLink2');
			            ShowProceedButton();
			    }
                break;
    }
}

function HideProceedButton()
{
    //CollapseDiv('AboutYouContainer');
    if (document.getElementById('AboutYouContainer'))
        document.getElementById('AboutYouContainer').style.display = 'none';
	CollapseDiv('FooterDiv');
	document.getElementById('lnkGoalProviderLink').style.display = 'none';
	document.getElementById("maincontentnoads").style.height='auto';
}

function ShowProceedButton()
{
    //ShowDiv('AboutYouContainer');
    if (document.getElementById('AboutYouContainer'))
        document.getElementById('AboutYouContainer').style.display = 'inline';
	ShowDiv('FooterDiv');
	document.getElementById('lnkGoalProviderLink').style.display = 'inline';
	document.getElementById("maincontentnoads").style.height='auto';
}

