//Product 19: Lifesurance Savings Insurance Plan
	
var elLifeSavingInsuredName ="Name";
var elLifeSavingInsuredDOB="DateofBirth";
var elLifeSavingInsuredAge="Attainedagelastbirthday";
var elLifeSavingInsuredGender="Gender";

var elLifeSavingProposerName ="Nameoftheproposer";
var elLifeSavingProposerSameAsInsured="Istheproposersameastheinsuredperson";	
var elLifeSavingProposerDOB = "Dateofbirthoftheproposer";
var elLifeSavingProposerAge = "Age";
var elLifeSavingGenderOftheProposer ="Genderoftheproposer";

var elLifeSavingPolicyterm="PlanTermYrs";
var elLifeSavingPPT="PremiumPaymentTerminyrs";
var elLifeSavingPremiumpaymentmode="Premiumpaymentfrequency";
var elLifeSavingSuminsured="SuminsuredinRs";
var elLifeSavingInstPremium="AmountofInstallmentpremiuminRs"
var elLifeSavingServiceTaxEducationCess="ServicetaxandeducationcessinRs";
var elLifeSavingTotalpremiuminRs="TotalpremiuminRs";
var elLifeSavingAnnualisedPremium="AnnualisedpremiuminclservicetaxinRs";

var elLifeSavingEMR ="EMR";
var elLifeSavingPerMille ="PerMille";

var elLifeSavingIsapplicantastaffmember="Istheapplicantastaffmember";
var elLifeSavingEmployeecode="Pleaseentertheemployeecodenumber";

function lifesuranceSavingsValidation(){
	var selectedName =	"Lifesurance Savings Insurance Plan";
	
	document.getElementById("headerExpandingBoxPremiumReductionOption").style.display = "none";
	document.getElementById("divExpandingBoxPremiumReductionOption").style.display = "none";
	document.getElementById(elWSDbProposerSameAsInsured).value = "No";
	
	if (!(document.frmView.hRoleName.value=="admin" ||
			document.frmView.hRoleName.value=="Admin" ||
				document.frmView.hRoleName.value=="administrator" ||
					document.frmView.hRoleName.value=="Administrator"))
	{		
		document.getElementById(elLifeSavingPremiumpaymentmode)[2].disabled = true;
	}

	if(document.getElementById(elLifeSavingInsuredAge)!=null){
		document.getElementById(elLifeSavingInsuredAge).readOnly=true;
	}
	if(document.getElementById(elLifeSavingProposerAge)!=null){
		document.getElementById(elLifeSavingProposerAge).readOnly=true;
	}

	if(document.getElementById(elLifeSavingInstPremium)!=null){
		document.getElementById(elLifeSavingInstPremium).readOnly=true;
	}
	
	if(document.getElementById(elLifeSavingServiceTaxEducationCess)!=null){
		document.getElementById(elLifeSavingServiceTaxEducationCess).readOnly=true;
	}
							
	if(document.getElementById(elLifeSavingTotalpremiuminRs)!=null){
		document.getElementById(elLifeSavingTotalpremiuminRs).readOnly=true;
	}
	
	if(document.getElementById(elLifeSavingAnnualisedPremium)!=null){
		document.getElementById(elLifeSavingAnnualisedPremium).readOnly=true;
	}
	 document.getElementById(elLifeSavingIsapplicantastaffmember).value="No";
	if(document.getElementById(elLifeSavingIsapplicantastaffmember).value!="" && document.getElementById(elLifeSavingEmployeecode).value==""){
		 document.getElementById(elLifeSavingIsapplicantastaffmember).value="No";
	 }	
	
	 checkIsStaff(elLifeSavingIsapplicantastaffmember,elLifeSavingEmployeecode);
	 
	 if(document.getElementById(elLifeSavingIsapplicantastaffmember) != null ) {
		    document.getElementById(elLifeSavingIsapplicantastaffmember).onchange = function(){
		    	checkIsStaff(elLifeSavingIsapplicantastaffmember,elLifeSavingEmployeecode);
		    	ajaxLifesuranceSavingsPremium();
		    }
	 }
	 
	 if(document.getElementById(elLifeSavingEmployeecode).value!=null){
		document.getElementById(elLifeSavingEmployeecode).onblur=function(){
			ajaxLifesuranceSavingsPremium();
		}
	 }
	 
	 if(document.getElementById(elLifeSavingProposerSameAsInsured)!=null){	    
			document.getElementById(elLifeSavingProposerSameAsInsured).onchange = function(){
				fillLifeSavingsProposerDetails();
			};
		}
		 
		
	 if(document.getElementById(elLifeSavingProposerName)!=null){	    
		document.getElementById(elLifeSavingProposerName).onblur = function(){
		  document.getElementById(elLifeSavingProposerName).value = getStartsWithCaps(document.getElementById(elLifeSavingProposerName).value);
		};
	 }

	 if(document.getElementById(elLifeSavingInsuredName)!=null){	    
			document.getElementById(elLifeSavingInsuredName).onblur = function(){
			  document.getElementById(elLifeSavingInsuredName).value = getStartsWithCaps(document.getElementById(elLifeSavingInsuredName).value);
			};
		 }
	 
	 if(document.getElementById(elLifeSavingInsuredDOB)!=null){			
			if(document.getElementById(elLifeSavingInsuredDOB).value != null || 
				document.getElementById(elLifeSavingInsuredDOB).value!=""){		
					document.getElementById(elLifeSavingInsuredDOB).onblur=function(){	
						fillLifeSavingsProposerDetails();
					lifeSavingsCalculateAge(selectedName,elLifeSavingInsuredDOB,elLifeSavingInsuredAge);
				    if(checkLifesuranceSavingsAge()){
				    	checkMaturityAge();
				    	ajaxLifesuranceSavingsPremium();
						
				  }
				    
				}
			}
		}
	 
		if(document.getElementById(elLifeSavingProposerDOB)!=null){			
			if(document.getElementById(elLifeSavingProposerDOB).value != null || 
				document.getElementById(elLifeSavingProposerDOB).value!=""){		
					document.getElementById(elLifeSavingProposerDOB).onblur=function(){	
						fillLifeSavingsProposerDetails();
					lifeSavingsCalculateAge(selectedName,elLifeSavingProposerDOB,elLifeSavingProposerAge);
					}		    
				
			}
		}
	 
		
		if(document.getElementById(elLifeSavingInsuredGender)!=null){
			document.getElementById(elLifeSavingInsuredGender).onchange = function(){
				ajaxLifesuranceSavingsPremium();
			}
		}
	
		
		if(document.getElementById(elLifeSavingInsuredGender)!=null){
			document.getElementById(elLifeSavingInsuredGender).onchange = function(){
				ajaxLifesuranceSavingsPremium();
			}
		}

	
		if(document.getElementById(elLifeSavingPolicyterm) != null ) {
		    document.getElementById(elLifeSavingPolicyterm).onchange = function(){		
		    	checkMaturityAge();
		    }
	    }
		
		

		if(document.getElementById(elLifeSavingPolicyterm) != null ) {
		    document.getElementById(elLifeSavingPolicyterm).onblur = function(){		    	
		    	checkMaturityAge();		  
		    }
	    }
		

		if(document.getElementById(elLifeSavingPPT) != null ) {
		    document.getElementById(elLifeSavingPPT).onchange = function(){		
		    	checkLifesuranceSavingPremiumPaymentTerm();
		    	ajaxLifesuranceSavingsPremium();
		    	
		    }
	    }
		
		

		if(document.getElementById(elLifeSavingPPT) != null ) {
		    document.getElementById(elLifeSavingPPT).onblur = function(){		    	
		    	checkLifesuranceSavingPremiumPaymentTerm();
		    	ajaxLifesuranceSavingsPremium();
		    }
	    }
		
		
		if(document.getElementById(elLifeSavingPremiumpaymentmode) != null ) {
		    document.getElementById(elLifeSavingPremiumpaymentmode).onchange = function(){
		    	ajaxLifesuranceSavingsPremium();
		    }
	    }
		
	
		if(document.getElementById(elLifeSavingEMR ) != null ) {
		    document.getElementById(elLifeSavingEMR ).onchange = function(){
		    	ajaxLifesuranceSavingsPremium();
		    }
		}
		if(document.getElementById(elLifeSavingEMR ) != null ) {
		    document.getElementById(elLifeSavingEMR ).onblur = function(){
		    	ajaxLifesuranceSavingsPremium();
		    }
		}
		if(document.getElementById(elLifeSavingPerMille) != null ) {
			document.getElementById(elLifeSavingPerMille).onchange=function(){
				ajaxLifesuranceSavingsPremium();
			}
		}    
		
		if(document.getElementById(elLifeSavingSuminsured)!=null || 
				document.getElementById(elLifeSavingSuminsured).value !=""){
				document.getElementById(elLifeSavingSuminsured).onblur = function(){	
						if(parseInt(document.getElementById(elLifeSavingSuminsured).value)!=null && parseInt(document.getElementById(elLifeSavingSuminsured).value)!="")
						{
							ajaxLifesuranceSavingsPremium();
						}						
						}
					
				}
		
		if(document.getElementById(elLifeSavingSuminsured)!=null || 
				document.getElementById(elLifeSavingSuminsured).value !=""){
				document.getElementById(elLifeSavingSuminsured).onchange = function(){	
						if(parseInt(document.getElementById(elLifeSavingSuminsured).value)!=null && parseInt(document.getElementById(elLifeSavingSuminsured).value)!="")
						{
							ajaxLifesuranceSavingsPremium();
						}						
						}
					
				}
		}
	

function checkLifesuranceSavingPremiumPaymentTerm()
{	
	var PPT=document.getElementById(elLifeSavingPPT);	
	if(document.getElementById(elLifeSavingPPT).value != "")
	{	
		
		if(parseInt(document.getElementById(elLifeSavingPPT).value)<5){ 
			alert("Premium payment term should be 5 years");
			document.getElementById(elLifeSavingPPT).focus();
			document.getElementById(elLifeSavingPPT).value="";
			
		}
		else if(parseInt(document.getElementById(elLifeSavingPPT).value)>parseInt(document.getElementById(elLifeSavingPolicyterm).value))
		{
			alert("Premium payment term should not exceed Plan term ");
			document.getElementById(elLifeSavingPPT).focus();	
			document.getElementById(elLifeSavingPPT).value="";
		}
		else if(parseInt(document.getElementById(elLifeSavingPolicyterm).value)==10 && parseInt(document.getElementById(elLifeSavingPPT).value)<6 )
				{
					alert("Premium payment term should be greater than 5 if Plan term is 10 ");
					document.getElementById(elLifeSavingPPT).value="";	
				}				
	}
}

function fillLifeSavingsProposerDetails(){
	if(document.getElementById(elWSDbProposerSameAsInsured).value == "Yes"){
		document.getElementById(elLifeSavingProposerName).value = document.getElementById(elLifeSavingInsuredName).value;
			document.getElementById(elLifeSavingProposerDOB).value = document.getElementById(elLifeSavingInsuredDOB).value;
			document.getElementById(elLifeSavingProposerAge).value = document.getElementById(elLifeSavingInsuredAge).value;
			document.getElementById(elLifeSavingGenderOftheProposer).value = document.getElementById(elLifeSavingInsuredGender).value;
			
		if(validateWealthDbProposerAge()){
			document.getElementById(elLifeSavingProposerName).readOnly=true;
			document.getElementById(elLifeSavingProposerDOB).readOnly=true;
			document.getElementById(elLifeSavingGenderOftheProposer).readOnly=true;
		}else{
			document.getElementById(elLifeSavingProposerName).value="No";
			document.getElementById(elLifeSavingProposerName).readOnly=false;
			document.getElementById(elLifeSavingProposerDOB).readOnly=false;
			document.getElementById(elLifeSavingGenderOftheProposer).readOnly=false;
		}	
	}	
	else{
		document.getElementById(elLifeSavingProposerName).readOnly=false;
			document.getElementById(elLifeSavingProposerDOB).readOnly=false;
			document.getElementById(elLifeSavingGenderOftheProposer).readOnly=false;
	}	
}


function lifeSavingsCalculateAge(chosenProduct,elLifeSavingInsuredDOB,elLifeSavingInsuredAge)
{
  if(checkDateFormat(document.getElementById(elLifeSavingInsuredDOB))) {
	if(document.getElementById(elLifeSavingInsuredDOB)!=null)
	{
		  var str = document.getElementById(elLifeSavingInsuredDOB).value;
		  if(str.length>0)
		   {
			  	  var starttime =0;
			  	  var endtime=0;
			  	  var lessthanMonthFlag=false;
				  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 year= bits[0];
				  var mon= bits[1]-1;
				  var dat= bits[2];
				  
				  var now = new Date();
				  now.setHours(0,0,0,0); 
				  var nowMonth=now.getMonth();
				  var nowYear=now.getFullYear();
				  var nowDate=now.getDate();
				  var years = now.getFullYear()-d.getFullYear();
				  d.setFullYear(now.getFullYear());
				  
				  var monthdiff = (nowYear*12 + nowMonth*1) - (year*12 + mon*1); 
					  
				  starttime = new Date(year,mon,dat); 
				  starttime = new Date(starttime.valueOf()+30*86400000);
				  
				  endtime = new Date(nowYear,nowMonth,nowDate); 
				  endtime = new Date(endtime.valueOf());
				  
				  if(starttime > endtime) 
				  { 
					  lessthanMonthFlag = true; 
				  } 
				  
				  var monvalue=d.getMonth();
				  var yearvalue=d.getFullYear();
				  var diff = now.getTime()-d.getTime()
				  if ( diff <0) years--;
				
				if(document.getElementById(elLifeSavingInsuredAge)!=null){
					
					document.getElementById(elLifeSavingInsuredAge).value = years;
				}
				
				//alert("monthdiff........" + monthdiff);
				//if(monthdiff<1)
				if(lessthanMonthFlag)
				{
					alert("Minimum Age at entry of Insured Person is 1 Month");
					document.getElementById(elLifeSavingInsuredDOB).select();
					document.getElementById(elLifeSavingInsuredAge).value="";
					document.getElementById(elLifeSavingInsuredDOB).focus();
				}
				var mondays=caldays(monvalue,yearvalue);
				//alert("mondays"+mondays);
		   }
	}
  }	
}

function checkLifesuranceSavingsAge(){
	var validateAgeFlag=false;
	if(document.getElementById(elLifeSavingInsuredAge).value!=""){
			if(parseInt(document.getElementById(elLifeSavingInsuredAge).value)<18){
				alert("Minimum age at entry is 18 ");
				document.getElementById(elLifeSavingInsuredDOB).select();
				document.getElementById(elLifeSavingInsuredAge).value = "";
				document.getElementById(elLifeSavingInsuredDOB).focus();
				validateAgeFlag = false;
			}else if(parseInt(document.getElementById(elLifeSavingInsuredAge).value)>65){
				alert("Maximum age at entry is 65 ");
				document.getElementById(elLifeSavingInsuredDOB).select();
				document.getElementById(elLifeSavingInsuredAge).value = "";
				document.getElementById(elLifeSavingInsuredDOB).focus();
				validateAgeFlag = false;
			}else{
				validateAgeFlag = true;
			}
		}	
		return validateAgeFlag;
}

function checkMaturityAge(){
	var maturityAge =0;
	maturityAge = parseInt(document.getElementById(elLifeSavingInsuredAge).value) +
				  parseInt(document.getElementById(elLifeSavingPolicyterm).value);
	if(maturityAge>75){
		alert("Maximum age at maturity is 75 years");
		document.getElementById(elLifeSavingPolicyterm).focus();
	}	
}

function ajaxLifesuranceSavingsPremium(){
	var request;
	var modeval;
	var url;
	var responseString;

	var policyTerm = document.getElementById(elLifeSavingPolicyterm).value;
	var age = document.getElementById(elLifeSavingInsuredAge).value;
	var gender = document.getElementById(elLifeSavingInsuredGender).value;
	var DOBOfInsurer= document.getElementById(elLifeSavingInsuredDOB).value;	
	var ppt = document.getElementById(elLifeSavingPPT).value;
	var ppm = document.getElementById(elLifeSavingPremiumpaymentmode).value;
	var sumIns = document.getElementById(elLifeSavingSuminsured).value;
    var isStaff=document.getElementById(elLifeSavingIsapplicantastaffmember).value;
    var employeeCode=document.getElementById(elLifeSavingEmployeecode).value;
    var emr = document.getElementById(elLifeSavingEMR).value;
    var perMille = document.getElementById(elLifeSavingPerMille).value;   
   if( age != "" && policyTerm != "" && ppt != "" && sumIns !=""){
		url = "/INSDC/validateLifesuranceSavings.do?mode=LifesuranceSavingsPlan"
					+"&DOBOfInsurer="+DOBOfInsurer
					+"&gender="+gender					
					+"&policyTerm="+policyTerm
					+"&ppt="+ppt
					+"&ppm="+ppm
					+"&sumIns="+sumIns
				    +"&isStaff="+isStaff
					+"&employeeCode="+employeeCode
					+"&emr="+emr
					+"&perMille="+perMille;
	
		if(window.XMLHttpRequest)
			request=new XMLHttpRequest();
		else if(window.ActiveXObject)
			request=new ActiveXObject("Microsoft.XMLHTTP");
	
		if(request){
			request.onreadystatechange=function()
			{
				if (request.readyState == 4) 
				{ 
					if (request.status == 200) 
					{ 
						var jsonText = request.responseText;
						if( jsonText.length > 0 && jsonText != "null")
						{
							var jsonObject = eval( "(" + jsonText + ")" ) ;
							if(jsonObject["displayInstallmentPremium"].length>0){
								document.getElementById(elLifeSavingInstPremium).value = jsonObject["displayInstallmentPremium"];
							}							
							if(jsonObject["displayServiceTax"].length>0){
								document.getElementById(elLifeSavingServiceTaxEducationCess).value = jsonObject["displayServiceTax"];
							}
							if(jsonObject["displayTotalInstallmentPremium"].length>0){
								document.getElementById(elLifeSavingTotalpremiuminRs).value = jsonObject["displayTotalInstallmentPremium"];
								document.getElementById(elLifeSavingAnnualisedPremium).value = jsonObject["displayAnnualisedPremium"];
							}
							if(jsonObject["alertMessage"].length>0){
								alert(jsonObject["alertMessage"]);
								document.getElementById(elLifeSavingSuminsured).value="";
								document.getElementById(elLifeSavingInstPremium).value="";
								document.getElementById(elLifeSavingServiceTaxEducationCess).value="";
								document.getElementById(elLifeSavingTotalpremiuminRs).value="";
								document.getElementById(elLifeSavingAnnualisedPremium).value="";
								document.getElementById(elLifeSavingSuminsured).focus();
							}
					    }					   
					}
				}
			}
			request.open("POST",url,true);
			request.setRequestHeader("content-type","application/x-www-form-urlencoded");
			request.send(null); 
		}
	}
	
}
