////Elements of the products/////
var elBackDate ="BackDateforIllustration";

//Product 1 : Homesurance Plan (Single Premium)
var elHSDOB = "LifeInsuredDOB";
var elHSAge = "Attainedagelastbirthday";


//Product 2 : Homesurance Plan (Regular Premium)
var elHRDOB = "LifeInsuredDOB";
var elHRAge = "Attainedagelastbirthday";

//Product 3 : Wealthsurance Foundation Plan
var elWSLifeInsuredDOB = "LifeInsuredDOB";
var elWSAttainedagelastbirthday = "Attainedagelastbirthday";
var elWSPolicyOwnerDOB = "PolicyHolderDOB";
var elWSAttainedage = "Age";


//Product 4:Homesurance Protection Plan
var elHPPInsuredDOB = "DateofBirthofthePersontobeLifeInsured";
var elHPPInsuredAge = "AgeofthePersontobeLifeInsuredinyears";
var elHPPJointInsuredDOB = "DateofBirthofthePersontobeJointLifeInsured";
var elHPPJointInsuredAge = "AgeofthePersontobeJointLifeInsuredinyears";


//Product 5 : Retiresurance Pension Plan
var elRPPInsuredDOB = "DateofBirthofthePersontobeInsured";
var elRPPAttainedageagelastbirthday = "Attainedageagelastbirthday";

//Product 6: Bondsurance Plan
var elBondPlanDOB = "DateofBirthofthePersontobeInsured";
var elBondPlanAge = "AgeofthePersontobeInsuredinyears";

//Product 7:Termsurance Protection Plan (Individual)
var elTermDOB="DateofBirthofthePersontobeInsured";
var elTermAge="AgeofthePersontobeInsuredinyrs";

//Product 8:Incomesurance Endowment And Money Back Plan
var elIncomeInsuredDOB="DateofBirthofthePersontobeInsured";
var elIncomeInsuredAge="AgeofthePersontobeInsuredinyrs";
var elIncomeOwnerDOB="DateofBirthofPolicyOwner";
var elIncomeOwnerAge="AgeofthePolicyOwnerinyrs";

//Product 15 :loansurance
var elLoanDOB	="DateofBirth";
var elLoanAge="Age";
var elLoanJointInsuredDOB = "Dateofbirthofthejointinsured";
var elLoanJointInsuredAge = "Ageofthejointinsured";
var elLoanCoInsured1DOB = "Dateofbirthofthecoinsuredmember1";
var elLoanCoInsured2DOB = "Dateofbirthofthecoinsuredmember2";
var elLoanCoInsured3DOB = "Dateofbirthofthecoinsuredmember3";
var elLoanCoInsured1Age = "Ageofthecoinsuredmember1";
var elLoanCoInsured2Age = "Ageofthecoinsuredmember2";
var elLoanCoInsured3Age = "Ageofthecoinsuredmember3";



function calculateBackDateAge(){


	var selectedElement = document.frmView.productId;
	var selectedName =	selectedElement.options[selectedElement.selectedIndex].text;

	if(selectedName=="Homesurance Plan (Single Premium)"){
		if(document.getElementById(elBackDate)!=null){
				if(document.getElementById(elHSDOB)!=null){
					if(document.getElementById(elHSDOB).value!=null && 
						document.getElementById(elHSDOB).value!=""){
						
			   			calculateAgeOnBackDate(selectedName,elHSDOB,elHSAge,elBackDate);
			   		}
			   		
			   	}		
	    }			

	}else if(selectedName=="Homesurance Plan (Regular Premium)"){
		if(document.getElementById(elBackDate)!=null){
				if(document.getElementById(elHRDOB)!=null){
					if(document.getElementById(elHRDOB).value!=null && 
						document.getElementById(elHRDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elHRDOB,elHRAge,elBackDate);
			   		}
			   	}		
	    }			

	}else if(selectedName=="Wealthsurance Foundation Plan"){
		if(document.getElementById(elBackDate)!=null){
				if(document.getElementById(elWSLifeInsuredDOB)!=null){
					if(document.getElementById(elWSLifeInsuredDOB).value!=null && 
						document.getElementById(elWSLifeInsuredDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elWSLifeInsuredDOB,elWSAttainedagelastbirthday,elBackDate);
			   			
			   		}
			   	}
				if(document.getElementById(elWSPolicyOwnerDOB)!=null){
					if(document.getElementById(elWSPolicyOwnerDOB).value!=null && 
						document.getElementById(elWSPolicyOwnerDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elWSPolicyOwnerDOB,elWSAttainedage,elBackDate);
			   			
			   		}
			   	}
	    }			
	}else if(selectedName=="Homesurance Protection Plan"){
		if(document.getElementById(elBackDate)!=null){
	
				 if(document.getElementById(elHPPInsuredDOB)!=null){
					if(document.getElementById(elHPPInsuredDOB).value!=null && 
						document.getElementById(elHPPInsuredDOB).value!=""){
	
	   					calculateAgeOnBackDate(selectedName,elHPPInsuredDOB,elHPPInsuredAge,elBackDate);
	   				}
	   			}
				 if(document.getElementById(elHPPJointInsuredDOB)!=null){
					if(document.getElementById(elHPPJointInsuredDOB).value!=null && 
						document.getElementById(elHPPJointInsuredDOB).value!=""){
	   					
			   			calculateAgeOnBackDate(selectedName,elHPPJointInsuredDOB,elHPPJointInsuredAge,elBackDate);
			   		}
			   	}		

	    }
	    			
	}else if(selectedName=="Retiresurance Pension Plan"){
		if(document.getElementById(elBackDate)!=null){
				
				if(document.getElementById(elRPPInsuredDOB)!=null){
					if(document.getElementById(elRPPInsuredDOB).value!=null && 
						document.getElementById(elRPPInsuredDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elRPPInsuredDOB,elRPPAttainedageagelastbirthday,elBackDate);
			   			
			   		}
			   	}		
	    }			
	}else if(selectedName=="Bondsurance Plan"){
		if(document.getElementById(elBackDate)!=null){
			
						if(document.getElementById(elBondPlanDOB)!=null){
							if(document.getElementById(elBondPlanDOB).value!=null && 
								document.getElementById(elBondPlanDOB).value!=""){
									
						   			  calculateAgeOnBackDate(selectedName,elBondPlanDOB,elBondPlanAge,elBackDate);
						   			  
					   		}
					   	}
			   	   			

	    }			

	}else if(selectedName=="Termsurance Protection Plan (Individual)"){
		if(document.getElementById(elBackDate)!=null){
				
				if(document.getElementById(elTermDOB)!=null){
					if(document.getElementById(elTermDOB).value!=null && 
						document.getElementById(elTermDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elTermDOB,elTermAge,elBackDate);
			   			
			   		}
			   	}		
	    }			

	}else if(selectedName=="Incomesurance Endowment And Money Back Plan"){
		if(document.getElementById(elBackDate)!=null){
				
				if(document.getElementById(elIncomeInsuredDOB)!=null){
					if(document.getElementById(elIncomeInsuredDOB).value!=null && 
						document.getElementById(elIncomeInsuredDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elIncomeInsuredDOB,elIncomeInsuredAge,elBackDate);
			   			
			   		}
			   	}
			   	
				if(document.getElementById(elIncomeOwnerDOB)!=null){
					if(document.getElementById(elIncomeOwnerDOB).value!=null && 
						document.getElementById(elIncomeOwnerDOB).value!=""){

			   			calculateAgeOnBackDate(selectedName,elIncomeOwnerDOB,elIncomeOwnerAge,elBackDate);
			   			
			   		}
			   	}		
			   			
	    }			

	}else if(selectedName=="Loansurance"){
		if(document.getElementById(elBackDate)!=null){
			
			if(document.getElementById(elLoanDOB)!=null){
				if(document.getElementById(elLoanDOB).value!=null && 
					document.getElementById(elLoanDOB).value!=""){

		   			calculateAgeOnBackDate(selectedName,elLoanDOB,elLoanAge,elBackDate);
		   			
		   		}
		   	}		
			if(document.getElementById(elLoanJointInsuredDOB)!=null){
					if(document.getElementById(elLoanJointInsuredDOB).value!=null && 
						document.getElementById(elLoanJointInsuredDOB).value!=""){
	   					
			   			calculateAgeOnBackDate(selectedName,elLoanJointInsuredDOB,elLoanJointInsuredAge,elBackDate);
			   		}
		   	}		
			if(document.getElementById(elLoanCoInsured1DOB)!=null){
				if(document.getElementById(elLoanCoInsured1DOB).value!=null && 
					document.getElementById(elLoanCoInsured1DOB).value!=""){
   					
		   			calculateAgeOnBackDate(selectedName,elLoanCoInsured1DOB,elLoanCoInsured1Age,elBackDate);
		   		}
			}		
			if(document.getElementById(elLoanCoInsured2DOB)!=null){
				if(document.getElementById(elLoanCoInsured2DOB).value!=null && 
					document.getElementById(elLoanCoInsured2DOB).value!=""){
   					
		   			calculateAgeOnBackDate(selectedName,elLoanCoInsured2DOB,elLoanCoInsured2Age,elBackDate);
		   		}
			}		
			if(document.getElementById(elLoanCoInsured3DOB)!=null){
				if(document.getElementById(elLoanCoInsured3DOB).value!=null && 
					document.getElementById(elLoanCoInsured3DOB).value!=""){
   					
		   			calculateAgeOnBackDate(selectedName,elLoanCoInsured3DOB,elLoanCoInsured3Age,elBackDate);
		   		}
			}		
			
    }			

}
	
	
}


function checkBackDatePrivilege(){
   	var backDateRole = document.getElementById("backDateRole");
	var backDateProduct = document.getElementById("backDateProduct");
	var finalroleName=document.frmView.hRoleName.value;

	var selectedElement = document.frmView.productId;
	var selectedName =	selectedElement.options[selectedElement.selectedIndex].text;
	var selectedValue =	selectedElement.options[selectedElement.selectedIndex].value;
    var eligibleRole = false; 
	var eligibleProduct = false;
	 
	 if(backDateRole != null ){   
		for(var i=0; i<backDateRole.length; i++)
		{
			if(finalroleName == backDateRole.options[i].value){
				eligibleRole = true;
				break;
			}	
		}
	 }
 	
	 if(backDateProduct != null){	
		for(var i=0; i<backDateProduct.length; i++)
		{
			if(selectedName == backDateProduct.options[i].value){
			    eligibleProduct = true;
			    break;
			}
		}
	 }	

	if(selectedValue!=""){
		if(selectedName!="Wealthsurance (IRDA cap)" && selectedName!="Retiresurance (IRDA Cap)" 
			&& selectedName!="Retiresurance Mark III" && selectedName!="Wealthsurance Milestone Plan"
		   && selectedName!="Incomesurance Immediate Annuity" && selectedName!="Termsurance Grameen Bachat Yojana"
			   && selectedName!="Bondsurance Advantage Insurance Plan" && selectedName!="Group Termsurance ROP"
			  && selectedName!="Wealthsurance Premier Plan" && selectedName!="Retiresurance Guaranteed Pension Plan"
				  && selectedName!="Termsurance Premier Plan" && selectedName!="Termsurance Senior Plan"
					  && selectedName!="Wealthsurance Dreamstart Plan" && selectedName!="Wealthsurance Dreambuilder Plan"
					  && selectedName!="Retiresurance Milestone Pension Plan" && selectedName!="Wealthsurance Maxigain Plan"
			  		  && selectedName!="Childsurance Dreambuilder Insurance Plan"  && selectedName!="Lifesurance Savings Insurance Plan")		
		{
		    if( !eligibleRole || !eligibleProduct){
				headerExpandingBoxBackDate.style.visibility="hidden"; 
				headerExpandingBoxBackDate.style.display="none";
				divExpandingBoxBackDate.style.visibility="hidden";
				divExpandingBoxBackDate.style.display="none";
		    }
		}
	}
	
}


function checkBackDate(elementBackDate){

var returnval=false; 
var strBackDate = document.getElementById(elementBackDate).value;

	  var backDate = new Date();
	  var backDatebits=strBackDate.split('-');
	  var FYyears ="";
	  var FYdiff ="";
	  var years = "";
	  var diff="";
	  
	  

		  if(strBackDate !="" ){
		  	
		  	  var financialYearDate = new Date();
		  	  var currentMonth = parseInt(financialYearDate.getMonth());
		  	  financialYearDate.setHours(0,0,0,0);
		  	  financialYearDate.setMonth(3);
		  	  financialYearDate.setDate(1);
		  	  if(currentMonth <= 2){
		  	    	financialYearDate.setFullYear(parseInt(financialYearDate.getFullYear())-1);
		  	  }	
			  backDate.setHours(0,0,0,0);
			  backDate.setFullYear(backDatebits[0])
			  backDate.setMonth(backDatebits[1]-1)
			  backDate.setDate(backDatebits[2])
			  FYyears = financialYearDate.getFullYear()-backDate.getFullYear();
			  
			  
			  FYdiff = financialYearDate.getTime()-backDate.getTime();
			  
			  if(FYdiff >0){
			  	 alert("Back date should not be beyond 1st April of Financial year");
			  	 document.getElementById(elementBackDate).focus();
			  }
			  
			  var now = new Date();
			  diff = backDate.getTime()-now.getTime();
			  
				
			  if(diff >0){
			     alert("Back date should not exceed Current Date");
			     document.getElementById(elementBackDate).focus();
			  }
			  
			  if(FYdiff <=0 && diff <=0){
				  returnval = true;
			  } 	    
			  
		  }					    

			return returnval;
}




function calculateAgeOnBackDate(chosenProduct,elementDOB,elementAge,elementBackDate)
{
  if(checkDateFormat(document.getElementById(elementDOB))) {
	if(document.getElementById(elementDOB)!=null)
	{
		  var str = document.getElementById(elementDOB).value;
		  var strBackDate=document.getElementById(elementBackDate).value;
		  
		  if(str.length>0)
		   {
				  var d = new Date();
				  var bits = str.split('-')
				  d.setHours(0,0,0,0); 
				  d.setFullYear(bits[0])
				  d.setMonth(bits[1]-1)
				  d.setDate(bits[2])

				  var backDate = new Date();
				  var backDatebits=strBackDate.split('-');
				  var years ="";
				  var diff ="";
				  
				  if(strBackDate !="" ){
					  backDate.setHours(0,0,0,0);
					  backDate.setFullYear(backDatebits[0])
					  backDate.setMonth(backDatebits[1]-1)
					  backDate.setDate(backDatebits[2])
					  years = backDate.getFullYear()-d.getFullYear();
					  d.setFullYear(backDate.getFullYear())
					  diff = backDate.getTime()-d.getTime()
					    
				  }else{
					  var now = new Date();
					  now.setHours(0,0,0,0); 
					  years = now.getFullYear()-d.getFullYear();
					  d.setFullYear(now.getFullYear())
					  diff = now.getTime()-d.getTime()
				  }				  
				  
				  if ( diff <0) years--;
				
				if(document.getElementById(elementAge)!=null){
					document.getElementById(elementAge).value = years;
				}

		   }
	}
  }
}   	
	
	






