//Product 15 : Retiresurance Mark III
var elRPPProposerName = "ProposerName";
var elRPPInsuredName = "NameofthePersontobeInsured";
var elRPPInsuredDOB = "DateofBirthofthePersontobeInsured";
var elRPPAttainedageagelastbirthday = "Attainedageagelastbirthday";
var elRPPGender = "Gender";
var elRPPModeofPremiumPayment = "ModeofPremiumPayment";
var elRPPTerm = "Terminyears";
var elRPPPremPayTerm = "PremiumPaymentTerminyears";
var elRPPInstallmentPremium = "AmountofInstallmentpremiuminRs";
var elRPPAnnualPremium = "AnnualisedPremiuminRs";
var elRPPSumInsuredinRs="SumInsuredinRs";
var elRPPVestingage = "Vestingage";
var elRPPMonthlyguaranteedinterestfundinpercentage="Monthlyguaranteedinterestfundinpercentage";
var elRPPGuaranteedReturnFundinPercentage="Guaranteedreturnfundinpercentage";
var elRPPDynamicGuaranteedFundinPercentage = "Dynamicguaranteedfundinpercentage";
var elRPPEquityOptimiserFundPensioninPercentage = "EquityGrowthfundPensioninPercentage";
var elRPPNiftyIndexfundPensioninPercentage = "NiftyIndexfundPensioninPercentage";
var elRPPIncomefundPensioninPercentage = "IncomefundPensioninPercentage";
var elRPPBondfundPensioninPercentage = "BondfundPensioninPercentage";
var elRPPLiquidfundPensioninPercentage = "LiquidfundPensioninPercentage";
var elRPPMidcapfundinpercentage = "Midcapfundinpercentage";
var elRPPPurefundinpercentage="Purefundinpercentage";
var elRPPTargetdatefundinpercentage="Targetdatefundinpercentage";
var elRPPMaxiGainfundinpercentage="MaxiGainfundinpercentage";
var elRPPAggressiveassetallocatorfundinpercentage="Aggressiveassetallocatorfundinpercentage";
var elRPPModerateassetallocatorfundinpercentage="Moderateassetallocatorfundinpercentage";
var elRPPCautiousassetallocatorfundinpercentage="Cautiousassetallocatorfundinpercentage";

var elRppIIIIsapplicantastaffmember="Isapplicantastaffmember";
var elRppIIIPleaseentertheemployeecodenumber="Pleaseentertheemployeecodenumber";

function callRetiresuranceIIIValidation(){

    var selectedName ="Retiresurance Mark III";

	document.getElementById(elRPPAttainedageagelastbirthday).readOnly=true;
	document.getElementById(elRPPAnnualPremium).readOnly=true;
	document.getElementById(elRPPVestingage).readOnly=true;
	
	document.getElementById("validationWealthsurance").style.visiblity="hidden";
	document.getElementById("validationWealthsurance").style.display = "none";
	document.getElementById("validationHomesurance").style.visiblity="hidden";
	document.getElementById("validationHomesurance").style.display = "none";
	
	document.getElementById("validationHPP").style.visiblity="hidden";
	document.getElementById("validationHPP").style.display = "none";
	document.getElementById("validationTermsurance").style.visiblity="hidden"
	document.getElementById("validationTermsurance").style.display = "none";
	document.getElementById("validationIncomesurance").style.visiblity="hidden"
	document.getElementById("validationIncomesurance").style.display = "none";
	document.getElementById(elRppIIIIsapplicantastaffmember).value="No";

	if(document.getElementById(elRPPProposerName)!=null)
	{	    
		document.getElementById(elRPPProposerName).onblur = function()
		{
		  document.getElementById(elRPPProposerName).value = getStartsWithCaps(document.getElementById(elRPPProposerName).value);
		  
		};
	}

	if(document.getElementById(elRPPInsuredName)!=null)
	{	    
		document.getElementById(elRPPInsuredName).onblur = function()
		{
		  document.getElementById(elRPPInsuredName).value = getStartsWithCaps(document.getElementById(elRPPInsuredName).value);
		  
		};
	}

	if(document.getElementById(elRPPInsuredDOB)!=null){
		if(document.getElementById(elRPPInsuredDOB).value!=null || 
			document.getElementById(elRPPInsuredDOB).value!=""){
			   document.getElementById(elRPPInsuredDOB).onblur=function()
			   {
				  calculateAge(selectedName,elRPPInsuredDOB,elRPPAttainedageagelastbirthday)
				  //Validation
				  if(document.getElementById(elRPPAttainedageagelastbirthday).value!=""){
					  if(parseInt(document.getElementById(elRPPAttainedageagelastbirthday).value)>70){
						alert("Maximum Age at entry is 70 years");
						//document.getElementById(elRPPInsuredDOB).value ="";
						document.getElementById(elRPPAttainedageagelastbirthday).value="";
						document.getElementById(elRPPInsuredDOB).select();
						document.getElementById(elRPPInsuredDOB).focus();
					  }
					  else if(parseInt(document.getElementById(elRPPAttainedageagelastbirthday).value)<18){
						alert("Minimum Age at entry is 18 years");
						//document.getElementById(elRPPInsuredDOB).value ="";
						document.getElementById(elRPPAttainedageagelastbirthday).value="";
						document.getElementById(elRPPInsuredDOB).select();
						document.getElementById(elRPPInsuredDOB).focus();
					  }
					  else{
						 checkRPPIIITerm();
					  }
				  }
				  
				  //Validation
			   };
		}
	}


	if(document.getElementById(elRPPModeofPremiumPayment)!=null)
	{
		if(document.getElementById("paymentFrequencyValidate").value!=""){
			document.getElementById(elRPPModeofPremiumPayment).value=document.getElementById("paymentFrequencyValidate").value;
		}
		else{
    		document.getElementById(elRPPModeofPremiumPayment).selectedIndex = 0;
		}
     
	}	

			if(document.getElementById(elRPPDynamicGuaranteedFundinPercentage)!=null){
				checkRetireIIICautiousFund();
			}
			
			if(document.getElementById(elRPPTerm)!=null){
				document.getElementById(elRPPTerm).onblur=function(){checkRPPIIITerm()};
			}
			

			if(document.getElementById(elRPPPremPayTerm)!=null){
				document.getElementById(elRPPPremPayTerm).onblur=function(){checkRPPIIIPremPayTerm();checkRetireIIICautiousFund();};
			}
			
			if(document.getElementById(elRPPModeofPremiumPayment)!=null){
				document.getElementById(elRPPModeofPremiumPayment).onchange=function(){
					checkRPPIIIInstallmentPremium();checkRetireIIICautiousFund();
					checkRPPIIITerm();checkRPPIIIPremPayTerm();};
			}

			if(document.getElementById(elRPPInstallmentPremium)!=null){
				document.getElementById(elRPPInstallmentPremium).onchange=function(){checkRPPIIIInstallmentPremium();};
			}

			if(document.getElementById(elRPPInstallmentPremium)!=null){
				document.getElementById(elRPPInstallmentPremium).onblur=function(){checkRPPIIIInstallmentPremium();};
			}

			if(document.getElementById(elRPPSumInsuredinRs)!=null){
				document.getElementById(elRPPSumInsuredinRs).onblur=function(){checkRetiresuranceIIISumInsured();};
			}
			
			if(document.getElementById(elRPPMonthlyguaranteedinterestfundinpercentage)!=null){
				document.getElementById(elRPPMonthlyguaranteedinterestfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}
			
			if(document.getElementById(elRPPGuaranteedReturnFundinPercentage)!=null){
				document.getElementById(elRPPGuaranteedReturnFundinPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPDynamicGuaranteedFundinPercentage)!=null){
				document.getElementById(elRPPDynamicGuaranteedFundinPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}


			if(document.getElementById(elRPPEquityOptimiserFundPensioninPercentage)!=null){
				document.getElementById(elRPPEquityOptimiserFundPensioninPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPNiftyIndexfundPensioninPercentage)!=null){
				document.getElementById(elRPPNiftyIndexfundPensioninPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPIncomefundPensioninPercentage)!=null){
				document.getElementById(elRPPIncomefundPensioninPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPBondfundPensioninPercentage)!=null){
				document.getElementById(elRPPBondfundPensioninPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPLiquidfundPensioninPercentage)!=null){
				document.getElementById(elRPPLiquidfundPensioninPercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}
			
			if(document.getElementById(elRPPMidcapfundinpercentage)!=null){
				document.getElementById(elRPPMidcapfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPPurefundinpercentage)!=null){
				document.getElementById(elRPPPurefundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPTargetdatefundinpercentage)!=null){
				document.getElementById(elRPPTargetdatefundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPMaxiGainfundinpercentage)!=null){
				document.getElementById(elRPPMaxiGainfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPAggressiveassetallocatorfundinpercentage)!=null){
				document.getElementById(elRPPAggressiveassetallocatorfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPModerateassetallocatorfundinpercentage)!=null){
				document.getElementById(elRPPModerateassetallocatorfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}

			if(document.getElementById(elRPPCautiousassetallocatorfundinpercentage)!=null){
				document.getElementById(elRPPCautiousassetallocatorfundinpercentage).onblur=function(){checkRPPIIIFundValue(this);};
			}
			
			if(document.getElementById(elRppIIIIsapplicantastaffmember) != null ) {
			    document.getElementById(elRppIIIIsapplicantastaffmember).onchange = function(){
			    	checkIsStaff(elRppIIIIsapplicantastaffmember,elRppIIIPleaseentertheemployeecodenumber);
			    }
		    }
			if(document.getElementById(elRppIIIIsapplicantastaffmember).value!="" && document.getElementById(elRppIIIPleaseentertheemployeecodenumber).value==""){
				 document.getElementById(elRppIIIIsapplicantastaffmember).value="No";
			}	
				checkIsStaff(elRppIIIIsapplicantastaffmember,elRppIIIPleaseentertheemployeecodenumber);

	////////////Retiresurance Pension Plan ends///////////




}

function checkRetireIIICautiousFund(){
	if(document.getElementById(elRPPModeofPremiumPayment)!=null){
		if(document.getElementById(elRPPModeofPremiumPayment).value=="Single"){
			document.getElementById(elRPPDynamicGuaranteedFundinPercentage).readOnly=false;
		}
		else{
			if(document.getElementById(elRPPPremPayTerm).value!=""){
				if(parseInt(document.getElementById(elRPPPremPayTerm).value)<=5){
					document.getElementById(elRPPDynamicGuaranteedFundinPercentage).readOnly=false;
				}
				else{
					document.getElementById(elRPPDynamicGuaranteedFundinPercentage).readOnly=true;
				}
			}
			else{
					document.getElementById(elRPPDynamicGuaranteedFundinPercentage).readOnly=true;
			}
		}
	}
}


function checkRPPIIITerm(){
	if(document.getElementById("valueRPPTracker").value!=""){
		callRetireRemove(document.getElementById("valueRPPTracker").value);
	}

			if(document.getElementById(elRPPTerm).value!=""){
					var vestingAge;
					var validTerm = true;
					var tmpelRPPTerm;
					var policyInvalid=false;
					var modeOfPremiumPayment = document.getElementById(elRPPModeofPremiumPayment).value;
					if(document.getElementById(elRPPAttainedageagelastbirthday).value!=""){
						vestingAge=parseInt(document.getElementById(elRPPTerm).value)
							+ parseInt(document.getElementById(elRPPAttainedageagelastbirthday).value);
						document.getElementById(elRPPVestingage).value = vestingAge;
					}

					
						var minimumTerm=0;
						var maximumTerm =0;
						var reminderValue=0;
						
						var calcMinimum=40-parseInt(document.getElementById(elRPPAttainedageagelastbirthday).value);

						var  calcMaximum = 75-parseInt(document.getElementById(elRPPAttainedageagelastbirthday).value);

						if(calcMaximum%5==0){
							maximumTerm = calcMaximum;
						}else{
						    reminderValue=calcMaximum%5;
							maximumTerm = calcMaximum - reminderValue;
						}
						
						if(modeOfPremiumPayment=="Single"){
							if(maximumTerm<5){
								policyInvalid=true;
							}
						}else{
							if(maximumTerm<10){
								policyInvalid=true;
							}
						}

						if(parseInt(calcMinimum)>5){
							minimumTerm=parseInt(calcMinimum);
							reminderTerm=minimumTerm%5;
							if(reminderTerm!=0){
								minimumTerm = minimumTerm - reminderTerm + 5;
							}
						}else{
							if(modeOfPremiumPayment=="Single"){
								minimumTerm=5;
							}else{
								minimumTerm=10;
							}	
						}
						
						
						if(document.getElementById(elRPPTerm).value<parseInt(minimumTerm)){
							alert("Minimum Term allowed is "+minimumTerm);
							document.getElementById(elRPPTerm).focus();
							document.getElementById(elRPPTerm).value="";
							document.getElementById(elRPPPremPayTerm).value = "";
							document.getElementById(elRPPVestingage).value = "";
							validTerm = false; 
						}
						else if(document.getElementById(elRPPTerm).value>maximumTerm){
							alert("Maximum Term allowed is "+maximumTerm+" (as the maximum vesting age is 75 years)");
							document.getElementById(elRPPTerm).focus();
							document.getElementById(elRPPTerm).value="";
							document.getElementById(elRPPPremPayTerm).value = "";
							document.getElementById(elRPPVestingage).value = "";
							validTerm = false; 
						}else if(policyInvalid){
						   if(modeOfPremiumPayment=="Single"){
								alert("Not able to buy policy as minimum term for single premium is 5");
						   }else{
						   		alert("Not able to buy policy as minimum term for regular premium is 10");
						   }
							document.getElementById(elRPPTerm).value="";
							document.getElementById(elRPPPremPayTerm).value = "";
							document.getElementById(elRPPVestingage).value = "";
							document.getElementById(elRPPTerm).focus();
							validTerm = false; 
						   		
						}else if(document.getElementById(elRPPTerm).value>10){
								tmpelRPPTerm = 	document.getElementById(elRPPTerm).value;
								if(tmpelRPPTerm%5!=0){
									alert("Plan Term should be Multiples of 5");
									document.getElementById(elRPPTerm).focus();
									validTerm = false; 
								}else{
									validTerm = true;
								}	 

						 }
						 
						 if(validTerm){
							document.getElementById("valueRPPTracker").value=document.getElementById(elRPPTerm).value;
							generateRetireIIITopSection(document.getElementById(elRPPTerm).value);

							checkRetireIIITopUpLoop(document.getElementById(elRPPTerm).value);
							checkRetireIIIWithdrawalLoop(document.getElementById(elRPPTerm).value);
							checkRetireIIIWithdrawal10Loop(document.getElementById(elRPPTerm).value);
							checkRetireIIIPremiumReduction(document.getElementById(elRPPTerm).value);
						 }
					
	
			}
			else{
				document.getElementById("headerExpandingBoxTopupDetails").style.visibility="hidden";
				document.getElementById("divExpandingBoxTopupDetails").style.visibility="hidden";
				document.getElementById("headerExpandingBoxWithdrawalDetails").style.visibility="hidden";
				document.getElementById("divExpandingBoxWithdrawalDetails").style.visibility="hidden";
				

				validTerm = false; 
			}
			
}

function checkRPPIIIPremPayTerm(){
	if(document.getElementById(elRPPPremPayTerm).value != ""){
	      if(document.getElementById(elRPPModeofPremiumPayment).value=="Single"){
			 document.getElementById(elRPPPremPayTerm).value =1;
		  }else{ 	 
				if(parseInt(document.getElementById(elRPPPremPayTerm).value)<3){ 
					alert("Minimum Premium Payment Term is 3 ");
					document.getElementById(elRPPPremPayTerm).focus();
					document.getElementById(elRPPPremPayTerm).value = "";
					
				}
				else if(parseInt(document.getElementById(elRPPPremPayTerm).value)>parseInt(document.getElementById(elRPPTerm).value)){
					alert("Maximum Premium Payment Term should not exceed Plan Term ");
					document.getElementById(elRPPPremPayTerm).focus();
					document.getElementById(elRPPPremPayTerm).value = "";
				}
		 }		
	}
}


function checkRPPIIIInstallmentPremium(){

	if(document.getElementById(elRPPInstallmentPremium).value!=null&&document.getElementById(elRPPInstallmentPremium).value!=""){
		if(document.getElementById(elRPPModeofPremiumPayment).value=="Single"){
			if(parseInt(document.getElementById(elRPPInstallmentPremium).value)>=20000){	
				  document.getElementById(elRPPAnnualPremium).value=parseInt(document.getElementById(elRPPInstallmentPremium).value);

				if(document.getElementById(elRPPPremPayTerm).value==""||document.getElementById(elRPPPremPayTerm).value>1){
					document.getElementById(elRPPPremPayTerm).value=1;
				}
			}
			else{
				alert("Installment Premium for Mode of Payment Single should be greater than or equal to 20000");
				document.getElementById(elRPPAnnualPremium).value = "";
				document.getElementById(elRPPInstallmentPremium).select();
				document.getElementById(elRPPInstallmentPremium).focus();
			}
		}
		else if(document.getElementById(elRPPModeofPremiumPayment).value=="Monthly"){

			if(parseInt(document.getElementById(elRPPInstallmentPremium).value)>=2000){	
			
			  	document.getElementById(elRPPAnnualPremium).value=parseInt(document.getElementById(elRPPInstallmentPremium).value)*12;
			}
			else{
				alert("Installment Premium for Mode of Payment Monthly should be greater than or equal to 2000");
				document.getElementById(elRPPAnnualPremium).value = "";
				document.getElementById(elRPPInstallmentPremium).select();
				document.getElementById(elRPPInstallmentPremium).focus();
			}
		}
		else if(document.getElementById(elRPPModeofPremiumPayment).value=="Quarterly"){
			if(parseInt(document.getElementById(elRPPInstallmentPremium).value)>=5000){	
				document.getElementById(elRPPAnnualPremium).value=parseInt(document.getElementById(elRPPInstallmentPremium).value)*4;
			}
			else{
				alert("Installment Premium for Mode of Payment Quarterly should be greater than or equal to 5000");
				document.getElementById(elRPPAnnualPremium).value = "";
				document.getElementById(elRPPInstallmentPremium).select();
				document.getElementById(elRPPInstallmentPremium).focus();
			}
				
		}
		else if(document.getElementById(elRPPModeofPremiumPayment).value=="Halfyearly"){
			if(parseInt(document.getElementById(elRPPInstallmentPremium).value)>=10000){		
				document.getElementById(elRPPAnnualPremium).value=parseInt(document.getElementById(elRPPInstallmentPremium).value)*2;
			}
			else{
				alert("Installment Premium for Mode of Payment Halfyearly should be greater than or equal to 10000");
				document.getElementById(elRPPAnnualPremium).value = "";
				document.getElementById(elRPPInstallmentPremium).select();
				document.getElementById(elRPPInstallmentPremium).focus();
			}
				
		}
		else if(document.getElementById(elRPPModeofPremiumPayment).value=="Annual"){
		
			if(parseInt(document.getElementById(elRPPInstallmentPremium).value)>=20000){				  				
				document.getElementById(elRPPAnnualPremium).value=parseInt(document.getElementById(elRPPInstallmentPremium).value);
			}
			else{
				alert("Installment Premium for Mode of Payment Annual should be greater than or equal to 20000");
				document.getElementById(elRPPAnnualPremium).value = "";
				document.getElementById(elRPPInstallmentPremium).select();
				document.getElementById(elRPPInstallmentPremium).focus();
				
			}
	
		}
			
	}
}


function checkRPPIIIFundValue( fieldId )
{
    if( fieldId.value.length == 0 || fieldId.value == " " )
    {
      return;
    }

	//Added by shameer
	fieldId.value=Math.round(fieldId.value);
    
    if( fieldId.value > 100 )
    {	
       alert("Investment % in any fund should not exceed 100%");       
       fieldId.focus();
       return;
    }
    
    if( fieldId.value < 15 )
    {
       alert("Investment % in any fund should not be Lesser than 15%");       
       fieldId.focus();
       return;      
    }
   
    var fundTotal = getRPPIIISumOfFundOptions();
        
    if( fundTotal > 100 )
    {
       var x = (fundTotal-fieldId.value) - 100;
       if( x < 0 )
       x = -x;
       
       alert("Total investment % should not exceed 100, Remaining fund can be allocated is " + x +
		   "\n Please arrange the funds accordingly");
       fieldId.value = "";
       fieldId.focus();
       return;
    }
}


function getRPPIIISumOfFundOptions()
{
    var sum = 
    giveInt(document.getElementById(elRPPMonthlyguaranteedinterestfundinpercentage).value) +	
    giveInt(document.getElementById(elRPPGuaranteedReturnFundinPercentage).value) +
    giveInt(document.getElementById(elRPPDynamicGuaranteedFundinPercentage).value) +
    giveInt(document.getElementById(elRPPEquityOptimiserFundPensioninPercentage).value) +
    giveInt(document.getElementById(elRPPNiftyIndexfundPensioninPercentage).value) +
    giveInt(document.getElementById(elRPPIncomefundPensioninPercentage).value) +
    giveInt(document.getElementById(elRPPBondfundPensioninPercentage).value) +
    giveInt(document.getElementById(elRPPLiquidfundPensioninPercentage).value) +
    giveInt(document.getElementById(elRPPMidcapfundinpercentage).value) + 
    giveInt(document.getElementById(elRPPPurefundinpercentage).value) +
    giveInt(document.getElementById(elRPPTargetdatefundinpercentage).value) +
    giveInt(document.getElementById(elRPPMaxiGainfundinpercentage).value) +
    giveInt(document.getElementById(elRPPAggressiveassetallocatorfundinpercentage).value) +
    giveInt(document.getElementById(elRPPModerateassetallocatorfundinpercentage).value) +
    giveInt(document.getElementById(elRPPCautiousassetallocatorfundinpercentage).value) ;

    return sum;
}

function retireIIIatleastOneRPPFundOptionDetailGiven()
{
   var given = false;

   if ( trim(document.getElementById(elRPPMonthlyguaranteedinterestfundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPGuaranteedReturnFundinPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPDynamicGuaranteedFundinPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPEquityOptimiserFundPensioninPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPNiftyIndexfundPensioninPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPIncomefundPensioninPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPBondfundPensioninPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPLiquidfundPensioninPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPMidcapfundinpercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elRPPPurefundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPTargetdatefundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPMaxiGainfundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPAggressiveassetallocatorfundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPModerateassetallocatorfundinpercentage).value)>0 )
	      given = true;
   if ( trim(document.getElementById(elRPPCautiousassetallocatorfundinpercentage).value)>0 )
	      given = true;
       
   return given;
}

function checkRetiresuranceIIISumInsured(){
	
	var minimumSumInsured=0;
	
	if(document.getElementById(elRPPSumInsuredinRs).value!=""){
			if(document.getElementById(elRPPModeofPremiumPayment).value=="Single"){
				if(parseInt(document.getElementById(elRPPTerm).value)<10){
					minimumSumInsured=parseInt(document.getElementById(elRPPAnnualPremium).value)*1.25;

					if(parseInt(document.getElementById(elRPPSumInsuredinRs).value)<parseInt(minimumSumInsured)){
						alert("Sum Insured should be greater than  "+parseInt(minimumSumInsured));
						document.getElementById(elRPPSumInsuredinRs).select();
						document.getElementById(elRPPSumInsuredinRs).focus();
					}
				}
				else if(parseInt(document.getElementById(elRPPTerm).value)>=10){
					minimumSumInsured=parseInt(document.getElementById(elRPPAnnualPremium).value)*1.1;

					if(parseInt(document.getElementById(elRPPSumInsuredinRs).value)<parseInt(minimumSumInsured)){
						alert("Sum Insured should be greater than  "+parseInt(minimumSumInsured));
						document.getElementById(elRPPSumInsuredinRs).select();
						document.getElementById(elRPPSumInsuredinRs).focus();
						
					}
				}
			}else{
					minimumSumInsured=parseInt(document.getElementById(elRPPAnnualPremium).value)*5;

					if(parseInt(document.getElementById(elRPPSumInsuredinRs).value)<parseInt(minimumSumInsured)){
						alert("Sum Insured should be greater than or equal to  "+minimumSumInsured);
						document.getElementById(elRPPSumInsuredinRs).select();
						document.getElementById(elRPPSumInsuredinRs).focus();
						
					}
			}
			
	}
}

function generateRetireIIITopSection(totalTopUp){ 
	document.getElementById("headerExpandingBoxTopupDetails").style.visibility="visible";
	document.getElementById("divExpandingBoxTopupDetails").style.visibility="visible";
	document.getElementById("headerExpandingBoxWithdrawalDetails").style.visibility="visible";
	document.getElementById("divExpandingBoxWithdrawalDetails").style.visibility="visible";
	document.getElementById("headerExpandingBoxPremiumReductionOption").style.visibility="visible";
	document.getElementById("divExpandingBoxPremiumReductionOption").style.visibility="visible";
	
	for(i=1;i<=totalTopUp-3;i++){
		addRetireIIIElement1();
	}

	for(i=6;i<=totalTopUp;i++){
		addRetireIIIElement2();
	}

	for(i=2;i<=totalTopUp;i++){
		addRetireIIIElement3();
	}
}

function addRetireIIIElement1(){
	var tbl = document.getElementById('divExpandingBoxTopupDetailsTable');
	var lastRow = tbl.rows.length;
	
	var iteration = i;
	var row = tbl.insertRow(lastRow);

	var hid0 = document.createElement('hidden');
	hid0.innerHTML = 'Topup Amount Term ' + iteration;

	var hid1 = document.createElement('input');
	hid1.type  = 'text';
	hid1.name  = 'TopupAmountRetireIIITerm' + iteration;
	hid1.id    = 'TopupAmountRetireIIITerm' + iteration;

	var hid2 = document.createElement('input');
	hid2.type  = 'text';
	hid2.name  = 'RetireIIITopupSumInsured' + iteration;
	hid2.id    = 'RetireIIITopupSumInsured' + iteration;

	var column0 = row.insertCell(0);
	column0.appendChild(hid0);

	var column1 = row.insertCell(1);
	column1.appendChild(hid1);
	
	var column2 = row.insertCell(2);
	column2.appendChild(hid2);	

}

function addRetireIIIElement2(){
	var tbl = document.getElementById('divExpandingBoxWithdrawalDetailsTable');
	var lastRow = tbl.rows.length;
	
	var iteration = i;
	var row = tbl.insertRow(lastRow);
	
	var hid0 = document.createElement('hidden');
	hid0.innerHTML = 'Withdrawal Amount Term ' + iteration;

	var hid1 = document.createElement('input');
	hid1.type  = 'text';
	hid1.name  = 'RetireIIIWithdrawal6Term' + iteration;
	hid1.id    = 'RetireIIIWithdrawal6Term' + iteration;

	var hid2 = document.createElement('input');
	hid2.type  = 'text';
	hid2.name  = 'RetireIIIWithdrawal10Term' + iteration;
	hid2.id    = 'RetireIIIWithdrawal10Term' + iteration;


	var column0 = row.insertCell(0);
	column0.appendChild(hid0);

	var column1 = row.insertCell(1);
	column1.appendChild(hid1);

	var column2 = row.insertCell(2);
	column2.appendChild(hid2);

}

function addRetireIIIElement3(){
	var tbl = document.getElementById('divExpandingBoxPremiumReductionOptionTable');
	var lastRow = tbl.rows.length;
	
	var iteration = i;
	var row = tbl.insertRow(lastRow);
	
	var hid0 = document.createElement('hidden');
	hid0.innerHTML = 'Premium Reduction Option Term ' + iteration;

	var hid1 = document.createElement('input');
	hid1.type  = 'text';
	hid1.name  = 'PremiumReductionOption' + iteration;
	hid1.id    = 'PremiumReductionOption' + iteration;

	var column0 = row.insertCell(0);
	column0.appendChild(hid0);

	var column1 = row.insertCell(1);
	column1.appendChild(hid1);

}



function checkRetireIIITopUpLoop(totalTerm){
	for(var topLoop=1;topLoop<=totalTerm;topLoop++){
			if(eval("document.getElementById('TopupAmountRetireIIITerm"+topLoop+"')")!=null){
				
				var topValue=eval("document.getElementById('TopupAmountRetireIIITerm"+topLoop+"')").value;
				
				eval("document.getElementById('TopupAmountRetireIIITerm"+topLoop+"')").title = topLoop;
				eval("document.getElementById('TopupAmountRetireIIITerm"+topLoop+"')").onblur=function(){checkRetireIIITopUpValue(this)};
				
				eval("document.getElementById('RetireIIITopupSumInsured"+topLoop+"')").title = topLoop;
				eval("document.getElementById('RetireIIITopupSumInsured"+topLoop+"')").onblur=function(){checkRetireIIITopUpSumInsured(this)};
				
			}
	}
}

function checkRetireIIIWithdrawalLoop(totalTerm){
	for(var wTopLoop=4;wTopLoop<=totalTerm;wTopLoop++){
			if(eval("document.getElementById('RetireIIIWithdrawal6Term"+wTopLoop+"')")!=null){
				
				eval("document.getElementById('RetireIIIWithdrawal6Term"+wTopLoop+"')").title = wTopLoop;
				eval("document.getElementById('RetireIIIWithdrawal6Term"+wTopLoop+"')").onblur=function(){checkRetireIIIWithdrawalValue(this,6)};
				
			}
	}
}

function checkRetireIIIWithdrawal10Loop(totalTerm){
	for(var wTopLoop=4;wTopLoop<=totalTerm;wTopLoop++){
			if(eval("document.getElementById('RetireIIIWithdrawal10Term"+wTopLoop+"')")!=null){
				
				eval("document.getElementById('RetireIIIWithdrawal10Term"+wTopLoop+"')").title = wTopLoop;
				eval("document.getElementById('RetireIIIWithdrawal10Term"+wTopLoop+"')").onblur=function(){checkRetireIIIWithdrawalValue(this,10)};
				
			}
	}
}

function checkRetireIIIPremiumReduction(totalTerm){
	for(var premLoop=2;premLoop<=totalTerm;premLoop++){
			if(eval("document.getElementById('PremiumReductionOption"+premLoop+"')")!=null){
				
				var topValue=eval("document.getElementById('PremiumReductionOption"+premLoop+"')").value;
				
				eval("document.getElementById('PremiumReductionOption"+premLoop+"')").title = premLoop;
				eval("document.getElementById('PremiumReductionOption"+premLoop+"')").onblur=function(){checkRetireIIIPremReductionValue(this)};
				
			}
	}
}

function setRetireIIITopupWithdrawal(){
	if(document.getElementById("innerRetireIIITopUpText").value.length > 0){
		document.getElementById('divExpandingBoxTopupDetails').style.visibility = 'visible';
		document.getElementById('headerExpandingBoxTopupDetails').style.visibility = 'visible';
		
		document.getElementById('divExpandingBoxTopupDetails').innerHTML = document.getElementById("innerRetireIIITopUpText").value;

		checkRetireIIITopUpLoop(document.getElementById(elRPPTerm).value);
						
	}

	if(document.getElementById("innerRetireIIIWithdrawalText").value.length > 0){

		document.getElementById('divExpandingBoxWithdrawalDetails').style.visibility = 'visible';
		document.getElementById('headerExpandingBoxWithdrawalDetails').style.visibility = 'visible';
		
		document.getElementById('divExpandingBoxWithdrawalDetails').innerHTML = document.getElementById("innerRetireIIIWithdrawalText").value;

		checkRetireIIIWithdrawalLoop(document.getElementById(elRPPTerm).value);
		checkRetireIIIWithdrawal10Loop(document.getElementById(elRPPTerm).value);
	}

	if(document.getElementById("innerRetirePremiumReductionText").value.length > 0){

		document.getElementById('divExpandingBoxPremiumReductionOption').style.visibility = 'visible';
		document.getElementById('headerExpandingBoxPremiumReductionOption').style.visibility = 'visible';
		
		document.getElementById('divExpandingBoxPremiumReductionOption').innerHTML = document.getElementById("innerRetirePremiumReductionText").value;

		
		checkRetireIIIPremiumReduction(document.getElementById(elRPPTerm).value);
	}
}

function checkRetireIIITopUpSumInsured(currentObj){
	//alert('checkRetireIIITopUpSumInsured Called');
	var	topupSumInsured=currentObj.value;
	var	currentValue=currentObj.title;
	var topupPremium=eval("document.getElementById('TopupAmountRetireIIITerm"+currentValue+"')").value;
	var policyTerm=document.getElementById(elRPPTerm).value;
	var minTopupSumInsured=0;
		
	  if(topupPremium!="" && topupSumInsured!="" && policyTerm!=""){
		  policyTerm =parseInt(document.getElementById(elRPPTerm).value);
		  if(policyTerm >=5 && policyTerm <10){
			  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.25);
		  }else if(policyTerm >=10){
			  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.1);
		  }		  
		if(topupSumInsured<minTopupSumInsured){
		  alert("Minimum top-up sum insured amount for premium " + topupPremium + " is " + minTopupSumInsured);
		  eval("document.getElementById('RetireIIITopupSumInsured"+currentValue+"')").select();
		}
	  }
	}

function checkRetireIIITopUpValue(currentObj){
	var annPremium=0;

	topupValue=currentObj.value;
	currentValue=currentObj.title;
	planValue=parseInt(document.getElementById(elRPPTerm).value)

	var topupSumInsured=0;
	var topupPremium=0;
	var minTopupSumInsured=0;

   if(topupValue!=""){
     if(currentValue<=planValue){
		if(parseInt(topupValue)<5000){ 
			alert("Please enter the top value greater than or equal to 5000 for term "+currentValue);
			currentObj.value="";
			currentObj.select();
		}else{
			 topupSumInsured=eval("document.getElementById('RetireIIITopupSumInsured"+currentValue+"')").value;
			 topupPremium=eval("document.getElementById('TopupAmountRetireIIITerm"+currentValue+"')").value;
			 if(planValue>=5 && planValue<10){
				 minTopupSumInsured= Math.round(parseInt(topupPremium)*1.25);
				 if(topupSumInsured<minTopupSumInsured){
					eval("document.getElementById('RetireIIITopupSumInsured"+currentValue+"')").value = minTopupSumInsured;
				 }
			 }else if(planValue>=10){
				 minTopupSumInsured= Math.round(parseInt(topupPremium)*1.1);
				 if(topupSumInsured<minTopupSumInsured){
					eval("document.getElementById('RetireIIITopupSumInsured"+currentValue+"')").value = minTopupSumInsured;
				 }
			 }
		}
	 }else{
		 alert("Topup not applicable for term "+currentValue);
		 currentObj.value="";
		 currentObj.select();
	 }
   }else{
		  eval("document.getElementById('RetireIIITopupSumInsured"+currentValue+"')").value ="";
	}
}

function checkRetireIIIWithdrawalValue(currObj,percent){
	
	var percentage = percent;
	withdrawalValue=currObj.value;
	curValue=currObj.title;
	planValue=parseInt(document.getElementById(elRPPTerm).value)
	if(withdrawalValue!=""){
 		if(curValue<=planValue){	
			var currentAge=document.getElementById(elRPPAttainedageagelastbirthday).value;
			var ageToCheck=parseInt(currentAge)+parseInt(curValue);

	if(parseInt(ageToCheck)<=18){
		
		alert("Withdrawal not applicable for the age below 18");
		
		currObj.focus();
	}
	else{
	if(parseInt(withdrawalValue)>=10000){

		var request;
	    var modeval;
		var url;
		var responseString;

		var lifeInsuredDOB=document.getElementById(elRPPInsuredDOB).value;
		var lifedob =  getDateObject(lifeInsuredDOB,"-");	
		
		var age=document.getElementById(elRPPAttainedageagelastbirthday).value;
		var gender=document.getElementById(elRPPGender).value;
		var planTerm=parseInt(curValue);
		var isStaffMember=document.getElementById(elRppIIIIsapplicantastaffmember).value;
		var employeeCode=document.getElementById(elRppIIIPleaseentertheemployeecodenumber).value;

		var installmentPremium=document.getElementById(elRPPInstallmentPremium).value;
		var premiumPaymentFrequency=document.getElementById(elRPPModeofPremiumPayment).value;
		var premiumPaymentTerm=document.getElementById(elRPPPremPayTerm).value;
		var annualPremium=document.getElementById(elRPPAnnualPremium).value;
		var sumInsured=document.getElementById(elRPPSumInsuredinRs).value;
		var vestingAge=document.getElementById(elRPPVestingage).value;
		
		var monthlyGuaranteedinterestfund=document.getElementById(elRPPMonthlyguaranteedinterestfundinpercentage).value;
		var guaranteedReturnFundPercentage=document.getElementById(elRPPGuaranteedReturnFundinPercentage).value;
		var dynamicGuaranteedFundPercentage=document.getElementById(elRPPDynamicGuaranteedFundinPercentage).value;
		var equityOptimiserFundPercentage=document.getElementById(elRPPEquityOptimiserFundPensioninPercentage).value;
		var niftyIndexFundPercentage=document.getElementById(elRPPNiftyIndexfundPensioninPercentage).value;
		var bondfundPercentage=document.getElementById(elRPPBondfundPensioninPercentage).value;
		var incomeFundPercentage=document.getElementById(elRPPIncomefundPensioninPercentage).value;
		var liquidFundPercentage=document.getElementById(elRPPLiquidfundPensioninPercentage).value;
		var midCapFundPercentage=document.getElementById(elRPPMidcapfundinpercentage).value;		
		var pureFundinpercentage=document.getElementById(elRPPPurefundinpercentage).value;
		var targetDatefundinpercentage =document.getElementById(elRPPTargetdatefundinpercentage).value;
		var maxiGainfundinpercentage =document.getElementById(elRPPMaxiGainfundinpercentage).value;
		var aggressiveassetallocatorfund =document.getElementById(elRPPAggressiveassetallocatorfundinpercentage).value;
		var moderateassetallocatorfund =document.getElementById(elRPPModerateassetallocatorfundinpercentage).value;
		var cautiousassetallocatorfund =document.getElementById(elRPPCautiousassetallocatorfundinpercentage).value;

		var topUpToCheck="";
		var topupSumInsured="";
		var topupSumInsAmount=0;
		var topupTerm=planValue-3;
		for(i=1;i<=topupTerm;i++){
			topAmountTerm=document.getElementById('TopupAmountRetireIIITerm'+i).value;
			topUpToCheck=topUpToCheck+"&topA"+i+"="+topAmountTerm;
			
		}
		for(i=1;i<=topupTerm;i++){
			topupSumInsAmount=document.getElementById('RetireIIITopupSumInsured'+i).value;
			topupSumInsured=topupSumInsured+"&TopupSumInsured"+i+"="+topupSumInsAmount;
		}
		
		var withdrawalToCheck6="";
		var withdrawalToCheck10="";
		for(i=6;i<=planValue;i++){
			withdrawalAmount6Term=document.getElementById('RetireIIIWithdrawal6Term'+i).value;
			withdrawalToCheck6=withdrawalToCheck6+"&wd6A"+i+"="+withdrawalAmount6Term;
			withdrawalAmount10Term=document.getElementById('RetireIIIWithdrawal10Term'+i).value;
			withdrawalToCheck10=withdrawalToCheck10+"&wd10A"+i+"="+withdrawalAmount10Term;
		}

		var premiumOptionCheck="";
		for(i=2;i<=planValue;i++){
			premiumOptionTerm=document.getElementById('PremiumReductionOption'+i).value;
			premiumOptionCheck=premiumOptionCheck+"&pro"+i+"="+premiumOptionTerm;
			
		}
		
		var totalTopUp=0;
		if((curValue-1)<=topupTerm){
			for(j=(curValue-1);j>=(curValue-2);j--){
					var valToCheck=eval("document.getElementById('TopupAmountRetireIIITerm"+j+"')").value;
					var valToPass=0;
					if(valToCheck!="")
						valToPass=valToCheck;
					totalTopUp=parseInt(totalTopUp)+parseInt(valToPass);
			}
		}
		url="validateRetiresuranceIII.do?mode=retireIIIWithdrawalCheck&age="+age+
			"&ldob="+lifedob+
			"&sex="+gender+
			"&isStaffMember="+isStaffMember+
			"&employeeCode="+employeeCode+
			"&term="+planTerm+
			"&aip="+installmentPremium+
			"&sumIns="+sumInsured+
			"&mopp="+premiumPaymentFrequency+
			"&ap="+annualPremium+
			"&ppt="+premiumPaymentTerm+
			"&va="+vestingAge+
			"&mgif="+monthlyGuaranteedinterestfund+
			"&grfp="+guaranteedReturnFundPercentage+
			"&cgfp="+dynamicGuaranteedFundPercentage+
			"&nifp="+niftyIndexFundPercentage+
			"&bfp="+bondfundPercentage+
			"&ifp="+incomeFundPercentage+
			"&lfp="+liquidFundPercentage+
			"&eofp="+equityOptimiserFundPercentage+
			"&mcf="+midCapFundPercentage+
			"&pfp="+pureFundinpercentage+
			"&tdfp="+targetDatefundinpercentage+
			"&mgfp="+maxiGainfundinpercentage+
			"&aaaf="+aggressiveassetallocatorfund+
			"&maaf="+moderateassetallocatorfund+
			"&caaf="+cautiousassetallocatorfund+
			"&watv="+withdrawalValue+
			"&percent="+percentage+
			"&lttv="+totalTopUp+topUpToCheck+topupSumInsured+
			withdrawalToCheck6+withdrawalToCheck10+premiumOptionCheck;


		if(window.XMLHttpRequest)
	  		request=new XMLHttpRequest();
	  	else if(window.ActiveXObject)
	  		request=new ActiveXObject("Microsoft.XMLHTTP");
	
		if(request){
	  	 	request.onreadystatechange=function(){
			if (request.readyState == 4) { // Complete

			  if (request.status == 200) { // OK response
					responseString = request.responseText;
					if(responseString.length>12){
						alert(responseString);
						currObj.value="";
						currObj.focus();
						
					}
			  }
			}
			}
			request.open("POST",url,true);
			request.setRequestHeader("content-type","application/x-www-form-urlencoded");
			request.send(null); 
		}



		
	}
	else{
		 alert("Minimum withdrawal amount is 10000");
		 currObj.value="";
		 currObj.focus();
	}
	}
}
 else{
	 alert("Withdrawal is not applicable for term "+currentValue);
	 currObj.value="";
	 currObj.focus();
 }
	
}
}

/* This method has been moved into productValidation.js
function getDateObject(dateString,dateSeperator)
{
	var values = dateString.split(dateSeperator);
	//alert("values::::::::::::::::::::::::::::"+values);
	//var date = new Date();
	var date="";
	values[1] = values[1] ;
	//date.setFullYear( values[2], values[1], values[0] );
	//alert("date::::::::::::::::::::::::::::"+date);
	date=values[0]+"/"+values[1]+"/"+values[2];
	return date;
}
*/

function checkRetireIIIPremReductionValue(currentObj){
	var annPremium=0;

	premiumReductionValue=currentObj.value;
	currentValue=currentObj.title;
	planValue=parseInt(document.getElementById(elRPPPremPayTerm).value);
	var premiumValue = parseInt(document.getElementById(elRPPAnnualPremium).value)

	var minValue = 0.00;

	if(currentValue<=3){
		minValue = Math.round(premiumValue*0.75);
	}
	else{
		minValue = 20000;
	}

	var maxValue = premiumValue-1;



if(premiumReductionValue!=""){
   if(currentValue<=planValue){
		if(parseInt(premiumReductionValue)<parseInt(minValue)){ 
			alert("Minimum Premium Reduction Option for term "+currentValue+" is "+minValue);
			currentObj.value = "";
			currentObj.select();
		}

		if(parseInt(premiumReductionValue)>parseInt(maxValue)){
			alert("Maximum Premium Reduction Option for term "+currentValue+" is "+maxValue);
			currentObj.value = "";
			currentObj.select();
		}
		
	 }
	 else{
		 alert("Premium Reduction is not applicable for term "+currentValue);
		 currentObj.value="";
		 currentObj.select();
	 }
	}
}

