var elRGPInsuredName="Name"
var elRGBInsuredDOB="DateofBirth"
var elRGBInsuredAge="Attainedagelastbirthday"
var elRGBInsuredGender="Gender"
var elRGBProposerSameAsInsured="Istheproposersameastheinsuredperson";	
var elRGBproposerName="Nameoftheproposer"
var elRGBproposerDOB="Dateofbirthoftheproposer"
var elRGBProposerAge="Age"
var elRGBproposerGender="Genderoftheproposer"
var elRGBPremiumpaymentfrequency="Premiumpaymentfrequency"
var elRGBPolicyTerm="PlanTermYrs"
var elRGBPremiumPaymentTerm="PremiumPaymentTermYrs"
	

var elRGBInstallmentPremium="InstallmentpremiuminclservicetaxandeducationcessRs"
var elRGBAnnualPremium="AnnualpremiuminclservicetaxandeducationcessRs"
	
var elRGBVestingAgeYrs="VestingAgeYrs"
var elRGBIsapplicantastaffmember="Istheapplicantastaffmember";
var elRGBEmployeecodenumber="Pleaseentertheemployeecodenumber";

function callRetiresuranceGuaranteedValidation(){

	var selectedName =	"Retiresurance Guaranteed Pension Plan";
	document.getElementById(elRGBInsuredAge).readOnly=true;
	document.getElementById(elRGBProposerAge).readOnly=true;
	document.getElementById(elRGBVestingAgeYrs).readOnly=true;
	
	document.getElementById(elRGBIsapplicantastaffmember).value="No";
	document.getElementById(elRGBIsapplicantastaffmember).readOnly=true;
	document.getElementById(elRGBIsapplicantastaffmember).disabled = true;
	
	checkIsStaff(elRGBIsapplicantastaffmember,elRGBEmployeecodenumber);

	 if(document.getElementById(elRGPInsuredName).value==""){ 
			document.getElementById(elRGBProposerSameAsInsured).value="No";
	 }
	
	if(document.getElementById(elRGPInsuredName)!=null){	    
		document.getElementById(elRGPInsuredName).onblur = function(){
		  document.getElementById(elRGPInsuredName).value = getStartsWithCaps(document.getElementById(elRGPInsuredName).value);
		  fillRGPProperDetails();
		};
	}
	
	if(document.getElementById(elRGBproposerName)!=null){	    
		document.getElementById(elRGBproposerName).onblur = function(){
		  document.getElementById(elRGBproposerName).value = getStartsWithCaps(document.getElementById(elRGBproposerName).value);
		};
	}
	if(document.getElementById(elRGBInsuredDOB)!=null){
		if(document.getElementById(elRGBInsuredDOB).value!=null || 
			document.getElementById(elRGBInsuredDOB).value!=""){
			   document.getElementById(elRGBInsuredDOB).onblur=function(){
				  calculateAge(selectedName,elRGBInsuredDOB,elRGBInsuredAge);
				  checkRGBAge();
				  checkRGBPolicyTerm();
				  fillRGPProperDetails();
				  calculateRGPVestingAge();
			   };
		}
	}
	
	if(document.getElementById(elRGBInsuredGender)!=null){	    
		document.getElementById(elRGBInsuredGender).onchange = function(){
			fillRGPProperDetails();
		};
	}
	
	if(document.getElementById(elRGBproposerDOB)!=null){
		if(document.getElementById(elRGBproposerDOB).value!=null || 
			document.getElementById(elRGBproposerDOB).value!=""){
			   document.getElementById(elRGBproposerDOB).onblur=function()
			   {
				  calculateAge(selectedName,elRGBproposerDOB,elRGBProposerAge)
			   };
		}
	}
	
	if(document.getElementById(elRGBProposerSameAsInsured)!=null){	    
		document.getElementById(elRGBProposerSameAsInsured).onchange = function(){
			fillRGPProperDetails();
		};
	}
	
	if(document.getElementById(elRGBPremiumpaymentfrequency)!=null){
		if(document.getElementById(elRGBPremiumpaymentfrequency).value!=null || 
			document.getElementById(elRGBPremiumpaymentfrequency).value!=""){
			document.getElementById(elRGBPremiumpaymentfrequency).onblur=function()
		   {
			checkRGPSinglePremium();
			checkRGPannualisedPremium();
		
		   };
		}
	}
	
	if(document.getElementById(elRGBPremiumpaymentfrequency)!=null){
		if(document.getElementById(elRGBPremiumpaymentfrequency).value!=null || 
			document.getElementById(elRGBPremiumpaymentfrequency).value!=""){
			document.getElementById(elRGBPremiumpaymentfrequency).onchange=function()
		   {
			checkRGPSinglePremium();
			checkRGPannualisedPremium();
		
		   };
		}
	}
	if(document.getElementById(elRGBPolicyTerm)!=null){
		if(document.getElementById(elRGBPolicyTerm).value!=null || 
			document.getElementById(elRGBPolicyTerm).value!=""){
			document.getElementById(elRGBPolicyTerm).onchange=function(){
			 checkRGBPolicyTerm();
			 checkRGBPremiumPaymentTerm();
		   };
		}
	}
	if(document.getElementById(elRGBPolicyTerm)!=null){
		if(document.getElementById(elRGBPolicyTerm).value!=null || 
			document.getElementById(elRGBPolicyTerm).value!=""){
			document.getElementById(elRGBPolicyTerm).onblur=function(){
				checkRGBPolicyTerm();
				calculateRGPVestingAge();
		   };
		}
	}
	if(document.getElementById(elRGBPremiumPaymentTerm)!=null){
		if(document.getElementById(elRGBPremiumPaymentTerm).value!=null || 
			document.getElementById(elRGBPremiumPaymentTerm).value!=""){
			document.getElementById(elRGBPremiumPaymentTerm).onblur=function()
		   {
			 checkRGBPremiumPaymentTerm();
			 checkRGPSinglePremium()
		   };
		}
	}
	   
	if(document.getElementById(elRGBPremiumPaymentTerm)!=null){
		if(document.getElementById(elRGBPremiumPaymentTerm).value!=null || 
			document.getElementById(elRGBPremiumPaymentTerm).value!=""){
			document.getElementById(elRGBPremiumPaymentTerm).onblur=function()
		   {
			 checkRGBPremiumPaymentTerm();
			 checkRGPSinglePremium()
		   };
		}
	}
	   
	if(document.getElementById(elRGBInstallmentPremium)!=null){
		if(document.getElementById(elRGBInstallmentPremium).value!=null || 
			document.getElementById(elRGBInstallmentPremium).value!=""){
			document.getElementById(elRGBInstallmentPremium).onblur=function()
		   {
			checkRGPannualisedPremium();
			
		   };
		}
	}
	   
	if(document.getElementById(elRGBVestingAgeYrs)!=null){
		if(document.getElementById(elRGBVestingAgeYrs).value!=null || 
			document.getElementById(elRGBVestingAgeYrs).value!=""){
			document.getElementById(elRGBVestingAgeYrs).onblur=function(){
				 calculateRGPVestingAge();
			   };
		}
	}
	
	
}


	function fillRGPProperDetails(){
		if(document.getElementById(elRGBProposerSameAsInsured).value == "Yes"){
			document.getElementById(elRGBproposerName).value = document.getElementById(elRGPInsuredName).value;
			document.getElementById(elRGBproposerDOB).value = document.getElementById(elRGBInsuredDOB).value;
			document.getElementById(elRGBProposerAge).value = document.getElementById(elRGBInsuredAge).value;
			document.getElementById(elRGBproposerGender).value = document.getElementById(elRGBInsuredGender).value;
			
			document.getElementById(elRGBproposerName).readOnly=true;
			document.getElementById(elRGBproposerDOB).readOnly=true;
			document.getElementById(elRGBproposerGender).readOnly=true;
		}else{
			document.getElementById(elRGBproposerName).readOnly=false;
			document.getElementById(elRGBproposerDOB).readOnly=false;
			document.getElementById(elRGBproposerGender).readOnly=false;
		}	
	}

	
		function checkRGBAge(){
		
			var validateAgeFlag=false;
			if(document.getElementById(elRGBInsuredAge).value !=null || document.getElementById(elRGBInsuredAge).value!=""){
				
				 if(parseInt(document.getElementById(elRGBInsuredAge).value)>75){
						alert("Maximum Age at entry is 75 years");
						
						document.getElementById(elRGBInsuredAge).value="";
						document.getElementById(elRGBInsuredDOB).select();
						document.getElementById(elRGBInsuredDOB).focus();
					  }
					  else if(parseInt(document.getElementById(elRGBInsuredAge).value)<18){
						alert("Minimum Age at entry is 18 years");
						
						document.getElementById(elRGBInsuredAge).value="";
						document.getElementById(elRGBInsuredDOB).select();
						document.getElementById(elRGBInsuredDOB).focus();
					  }
		
		
		else{
			validateAgeFlag = true;
		}
		}
			return validateAgeFlag
		}
		
		
		function checkRGPSinglePremium(){
			
			if(document.getElementById(elRGBPremiumpaymentfrequency).value == 'Single'){
				document.getElementById(elRGBPremiumPaymentTerm).value='1';
				document.getElementById(elRGBPremiumPaymentTerm).readOnly=true;
			}
			else{
				
				if(parseInt(document.getElementById(elRGBPremiumPaymentTerm).value)<3){
					document.getElementById(elRGBPremiumPaymentTerm).value='3';
					document.getElementById(elRGBPremiumPaymentTerm).focus();
				}
			}
		}
		
		function checkRGPannualisedPremium(){
			if(document.getElementById(elRGBPremiumpaymentfrequency).value =='Single'&& document.getElementById(elRGBInstallmentPremium).value !=null &&  document.getElementById(elRGBInstallmentPremium).value != ""){
				if(document.getElementById(elRGBInstallmentPremium).value >= 25387){
				document.getElementById(elRGBAnnualPremium).value=document.getElementById(elRGBInstallmentPremium).value;
				document.getElementById(elRGBAnnualPremium).readOnly=true;
				}
				else{
					document.getElementById(elRGBAnnualPremium).value= "";
					alert("Minimum installment premium amount for Single is 25387");
					document.getElementById(elRGBInstallmentPremium).focus();
				}
			}
			else if(document.getElementById(elRGBPremiumpaymentfrequency).value =='Yearly'&& document.getElementById(elRGBInstallmentPremium).value !=null && document.getElementById(elRGBInstallmentPremium).value != ""){
				if(document.getElementById(elRGBInstallmentPremium).value >= 20309){
				document.getElementById(elRGBAnnualPremium).value=document.getElementById(elRGBInstallmentPremium).value;
				document.getElementById(elRGBAnnualPremium).readOnly=true;
				}
				else{
					document.getElementById(elRGBAnnualPremium).value= "";
					alert("Minimum installment premium amount for Yearly is 20309");
					document.getElementById(elRGBInstallmentPremium).focus();
				}
			}
			else if(document.getElementById(elRGBPremiumpaymentfrequency).value =='Half-Yearly'&& document.getElementById(elRGBInstallmentPremium).value !=null && document.getElementById(elRGBInstallmentPremium).value != ""){
				if(document.getElementById(elRGBInstallmentPremium).value >= 10155){
				document.getElementById(elRGBAnnualPremium).value=document.getElementById(elRGBInstallmentPremium).value*2;
				document.getElementById(elRGBAnnualPremium).readOnly=true;
				}
				else{
					document.getElementById(elRGBAnnualPremium).value= "";
					alert("Minimum installment premium amount for Half-Yearly is 10155");
					document.getElementById(elRGBInstallmentPremium).focus();
				}
			}
			else if(document.getElementById(elRGBPremiumpaymentfrequency).value =='Quarterly'&& document.getElementById(elRGBInstallmentPremium).value !=null && document.getElementById(elRGBInstallmentPremium).value != ""){
				if(document.getElementById(elRGBInstallmentPremium).value >= 5078){
				document.getElementById(elRGBAnnualPremium).value=document.getElementById(elRGBInstallmentPremium).value*4;
				document.getElementById(elRGBAnnualPremium).readOnly=true;
				}
				else{
					document.getElementById(elRGBAnnualPremium).value= "";
					alert("Minimum installment premium amount for Quarterly is 5078");
					document.getElementById(elRGBInstallmentPremium).focus();
				}
			}
			else if(document.getElementById(elRGBPremiumpaymentfrequency).value =='Monthly'&& document.getElementById(elRGBInstallmentPremium).value!=null && document.getElementById(elRGBInstallmentPremium).value != "" ){
				if(document.getElementById(elRGBInstallmentPremium).value >= 2031){
				document.getElementById(elRGBAnnualPremium).value=document.getElementById(elRGBInstallmentPremium).value*12;
				document.getElementById(elRGBAnnualPremium).readOnly=true;
				}
				else{
					document.getElementById(elRGBAnnualPremium).value= "";
					alert("Minimum installment premium amount for Monthly is 2031");
					document.getElementById(elRGBInstallmentPremium).focus();
				}
			}
		}
		
		
		
	function checkRGBPremiumPaymentTerm(){
		if(document.getElementById(elRGBPremiumpaymentfrequency).value == 'Single'){
			document.getElementById(elRGBPremiumPaymentTerm).value='1';
			document.getElementById(elRGBPremiumPaymentTerm).readOnly=true;
		}
		else {
			if(parseInt(document.getElementById(elRGBPremiumPaymentTerm).value)<3){
				document.getElementById(elRGBPremiumPaymentTerm).value='3';
				document.getElementById(elRGBPremiumPaymentTerm).readOnly=false;
			}
		}
			if(parseInt(document.getElementById(elRGBPremiumPaymentTerm).value) >parseInt(document.getElementById(elRGBPolicyTerm).value )){
					alert("Premium payment term should not be exceed to policy term");
					document.getElementById(elRGBPremiumPaymentTerm).value = document.getElementById(elRGBPolicyTerm).value;
					document.getElementById(elRGBPremiumPaymentTerm).focus();
			}
		
		}
	
	
	function checkRGBPolicyTerm(){
		if(document.getElementById("valueRPPTracker").value!=""){
			callRetireGuarantRemove(document.getElementById("valueRPPTracker").value);
		}

				if(document.getElementById(elRGBPolicyTerm).value!=""){
						var vestingAge;
						var validTerm = true;
						var tmpelRPPTerm;
						var policyInvalid=false;
						var modeOfPremiumPayment = document.getElementById(elRGBPremiumpaymentfrequency).value;
						

						if(document.getElementById(elRGBPolicyTerm).value != null &&document.getElementById(elRGBPolicyTerm).value != "" && document.getElementById(elRGBInsuredAge).value != "" && document.getElementById(elRGBInsuredAge).value != null){
							var vestingAge=parseInt(document.getElementById(elRGBPolicyTerm).value)+parseInt(document.getElementById(elRGBInsuredAge).value);
							document.getElementById(elRGBVestingAgeYrs).value = vestingAge;
						}
							var minimumTerm=0;
							var maximumTerm =0;
							var reminderValue=0;
							
							var calcMinimum=40-parseInt(document.getElementById(elRGBInsuredAge).value);

							var  calcMaximum = 85-parseInt(document.getElementById(elRGBInsuredAge).value);

							if(calcMaximum%5==0){
								maximumTerm = calcMaximum;
							}else{
							    reminderValue=calcMaximum%5;
								maximumTerm = calcMaximum - reminderValue;
							}
							
							if(modeOfPremiumPayment=="Single"){
								if(maximumTerm<10){
									policyInvalid=true;
								}
							}else{
								if(maximumTerm<10){
									policyInvalid=true;
								}
							}

							if(parseInt(calcMinimum)>10){
								minimumTerm=parseInt(calcMinimum);
								reminderTerm=minimumTerm%5;
								if(reminderTerm!=0){
									minimumTerm = minimumTerm - reminderTerm + 5;
								}
							}else{
								if(modeOfPremiumPayment=="Single"){
									minimumTerm=10;
								}else{
									minimumTerm=10;
								}	
							}
							
							
							if(document.getElementById(elRGBPolicyTerm).value<parseInt(minimumTerm)){
								alert("Minimum Term allowed is "+minimumTerm);
								document.getElementById(elRGBPolicyTerm).focus();
								document.getElementById(elRGBPolicyTerm).value=minimumTerm;
							
								document.getElementById(elRGBVestingAgeYrs).value = "";
								validTerm = false; 
							}
							else if(document.getElementById(elRGBPolicyTerm).value>maximumTerm){
								alert("Maximum Term allowed is "+maximumTerm+" (as the maximum vesting age is 85 years)");
								document.getElementById(elRGBPolicyTerm).focus();
								document.getElementById(elRGBPolicyTerm).value=maximumTerm;
								
								document.getElementById(elRGBVestingAgeYrs).value = "";
								validTerm = false; 
							}else if(policyInvalid){
							   if(modeOfPremiumPayment=="Single"){
									alert("Not able to buy policy as minimum policy term for single premium is 10");
							   }else{
							   		alert("Not able to buy policy as minimum policy term for regular premium is 10");
							   }
								document.getElementById(elRGBPolicyTerm).value="";
								
								document.getElementById(elRGBVestingAgeYrs).value = "";
								document.getElementById(elRGBPolicyTerm).focus();
								validTerm = false; 
							   		
							}else if(document.getElementById(elRGBPolicyTerm).value>10){
									tmpelRPPTerm = 	document.getElementById(elRGBPolicyTerm).value;
									if(tmpelRPPTerm%5!=0){
										alert("Policy Term should be Multiples of 5");
										document.getElementById(elRGBPolicyTerm).focus();
										validTerm = false; 
									}else{
										validTerm = true;
									}	 

							 }
							
							 if(validTerm){
								document.getElementById("valueRPPTracker").value=document.getElementById(elRGBPolicyTerm).value;
								checkRetireGuarantTopUpLoop(document.getElementById(elRGBPolicyTerm).value);
							 }
						
		
				}
				
				
	}
	
	function  calculateRGPVestingAge(){
		if(document.getElementById(elRGBPolicyTerm).value != null &&document.getElementById(elRGBPolicyTerm).value != "" && document.getElementById(elRGBInsuredAge).value != "" && document.getElementById(elRGBInsuredAge).value != null){
			var vestingAge=parseInt(document.getElementById(elRGBPolicyTerm).value)+parseInt(document.getElementById(elRGBInsuredAge).value);
			document.getElementById(elRGBVestingAgeYrs).value = vestingAge;
		}
	}
	
	
	function callRetireGuarantRemove(removeTotal){
		var tblTopUp = document.getElementById('divExpandingBoxTopupDetailsTable');
		
		var lastRow = tblTopUp.rows.length;
			
		for(var j=lastRow-1;j>=1;j--)
		{
			tblTopUp.deleteRow(j);
		}
	}
	
	function checkRetireGuarantTopUpLoop(totalTerm){
		for(var topLoop=1;topLoop<=totalTerm;topLoop++){
				if(eval("document.getElementById('TopupAmountTerm"+topLoop+"')")!=null){
					
					var topValue=eval("document.getElementById('TopupAmountTerm"+topLoop+"')").value;
					
					eval("document.getElementById('TopupAmountTerm"+topLoop+"')").title = topLoop;
					eval("document.getElementById('TopupAmountTerm"+topLoop+"')").onblur=function(){checkRetireGuarantTopUpValue(this)};
					
				}
		}
	}
	
	function checkRetireGuarantTopUpValue(currentObj){
		var annPremium=0;

		topupValue=currentObj.value;
		currentValue=currentObj.title;
		planValue=parseInt(document.getElementById(elRGBPolicyTerm).value);

	   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{
			 alert("Topup not applicable for term "+currentValue);
			 currentObj.value="";
			 currentObj.select();
		 }
	   }
	}	
	
	function setRetireGuarantTopup(){
		if(document.getElementById("innerRetireGuarantTopUpText").value.length > 0){
			document.getElementById('divExpandingBoxTopupDetails').style.visibility = 'visible';
			document.getElementById('headerExpandingBoxTopupDetails').style.visibility = 'visible';
			
			document.getElementById('divExpandingBoxTopupDetails').innerHTML = document.getElementById("innerRetireGuarantTopUpText").value;

			checkRetireGuarantTopUpLoop(document.getElementById(elRGBPolicyTerm).value);
							
		}
	}	
		

