//Product 21 : Wealthsurance MaxiGain Plan
var elWSMGName = "Name";
var elWSMGProposerName ="Nameoftheproposer";
var elWSMGProposerSameAsInsured="Istheproposersameastheinsuredperson";	
var elWSMGLifeInsuredDOB = "DateofBirth";
var elWSMGAttainedagelastbirthday = "Attainedagelastbirthday";
var elWSMGGender = "Gender";
var elWSMGBaseTerm = "PlanTerm";
var elWSMGBasePremPayTerm = "PremiumPaymentTerm";
var elWSMGBasePremFreq = "Premiumpaymentfrequency";
var elWSMGBaseAnnualPrem = "AnnualPremium";
var elWSMGSumInsured = "SumAssured";
var elWSMGProposerDOB = "Dateofbirthoftheproposer";
var elWSMGAttainedage = "Age";
var elWSMGGenderOftheProposer = "Genderoftheproposer";

var elWSMGDynamicGuaranteedFundinPercentage = "Dynamicguaranteedfundinpercentage";
var elWSMGMaxiNAVGuaranteedFundinPercentage = "MaxiNAVguaranteedfundinpercentage";
var elWSMGEquityGrowthFundinPercentage = "Equitygrowthfundinpercentage";
var elWSMGIncomeFundinPercentage = "Incomefundinpercentage";


var elWSMGBaseEMR = "BasePlanEMR";
var elWSMGBasePerMile = "BasePlanPerMille";

var elWSMGIsapplicantastaffmember="Istheapplicantastaffmember";
var elWSMGPleaseentertheemployeecodenumber="Pleaseentertheemployeecodenumber";

function callWealthMaxiGainValidation(){

var selectedName ="Wealthsurance Maxigain Plan";

	document.getElementById("headerExpandingBoxPremiumReductionOption").style.display = "none";
	document.getElementById("divExpandingBoxPremiumReductionOption").style.display = "none";
	 document.getElementById(elWSMGIsapplicantastaffmember).value="No";
/////////////
	if(document.getElementById(elWSMGAttainedagelastbirthday)!=null){
		document.getElementById(elWSMGAttainedagelastbirthday).readOnly=true;
	}

	if(document.getElementById(elWSMGLifeInsuredDOB)!=null){
		if(document.getElementById(elWSMGLifeInsuredDOB).value!=null || 
			document.getElementById(elWSMGLifeInsuredDOB).value!=""){
			   document.getElementById(elWSMGLifeInsuredDOB).onblur=function()
			   {
				   fillWBDbProposerDetails();				  
				   wealthMaxiGainCalculateAge(selectedName,elWSMGLifeInsuredDOB,elWSMGAttainedagelastbirthday)
				  
					if(document.getElementById(elWSMGAttainedagelastbirthday)!=""){
						
						calcAge = parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value) + 
							        parseInt(document.getElementById(elWSMGBaseTerm).value) ;
							        
					    calcMinorAge = parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value) + 
					    			parseInt(document.getElementById(elWSMGBasePremPayTerm).value) ;
					
								 if(parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value)>65){
									alert("Maximum Age at entry of Insured Person is 65 years");
									document.getElementById(elWSMGLifeInsuredDOB).select();
									document.getElementById(elWSMGAttainedagelastbirthday).value="";
									document.getElementById(elWSMGLifeInsuredDOB).focus();
							    }else if(parseInt(calcAge) > 75){
									alert(" Maximum Age at maturity of Life Insured  is 75 ");
									document.getElementById(elWSMGBaseTerm).focus();
							    }							
							
						}

			   };
		}
	}


	if(document.getElementById(elWSMGAttainedage)!=null){
		document.getElementById(elWSMGAttainedage).readOnly=true;
	}
	if(document.getElementById(elWSMGGender)!=null){
		document.getElementById(elWSMGGender).onchange=function(){
			fillWBDbProposerDetails();
		}	
	}
	
	if(document.getElementById(elWSMGProposerDOB)!=null)
	{
	   document.getElementById(elWSMGProposerDOB).onblur=function()
	   {
	   	if(document.getElementById(elWSMGProposerDOB).value!=""&&document.getElementById(elWSMGProposerDOB).value!=null)
	   	{	   	
	   	  calculateWealthMaxiGainPolicyHolderAge();
		  calculateAge(selectedName,elWSMGProposerDOB,elWSMGAttainedage);
		  
			  if(document.getElementById(elWSMGAttainedage)!=null)
			  {	
				  if(parseInt(document.getElementById(elWSMGAttainedage).value)<18){
						alert("Minimum  Age at entry of Policy Owner is 18");
						document.getElementById(elWSMGProposerDOB).select();
						document.getElementById(elWSMGAttainedage).value="";
						document.getElementById(elWSMGProposerDOB).focus();
				  }
		   	  }
	   	  }
	   	  else{
	   	  	document.getElementById(elWSMGAttainedage).value="";
	   	  }
	   };
		
	}

 document.getElementById(elWSMGBaseAnnualPrem).onblur=function()
 
			   {
	 
			 if(document.getElementById(elWSMGBaseAnnualPrem)!=null && document.getElementById(elWSMGBaseAnnualPrem)!="")
			 {
	
			     if(parseInt(document.getElementById(elWSMGBaseAnnualPrem).value)<25000 )
			     {
	
	               alert("Minimum annul premium should be 25,000");
	               document.getElementById(elWSMGBaseAnnualPrem).focus();
	               }
			  
			   if(parseInt(document.getElementById(elWSMGBaseAnnualPrem).value)>100000 )
			   {
	
	               alert("Maximum annul premium should be 1,00,000");
	               document.getElementById(elWSMGBaseAnnualPrem).focus();}
			   }
			   else 
			   {
	
			   alert("Annul premium should not be empty ");
			   }
	
			  
}

	
	document.getElementById("validationWealthsurance").style.visiblity="visible"
	document.getElementById("validationWealthsurance").style.display = "inline";
	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";
	
	if(document.getElementById("wopDeathHidden").value=="disabled"){
		document.getElementById(elWSMGWOPDeath).disabled = true;
		document.getElementById("wopDeathHidden").value="";
	}

		

	if(document.getElementById("paymentFrequencyValidate").value!=""){
			document.getElementById(elWSMGBasePremFreq).value=document.getElementById("paymentFrequencyValidate").value;
	}

	if(document.getElementById("premiumPaymentTermValidate").value!=""){
		document.getElementById(elWSMGBasePremPayTerm).value=document.getElementById("premiumPaymentTermValidate").value;
	}
	


	
	if(document.getElementById(elWSMGProposerName)!=null)
	{	    
		document.getElementById(elWSMGProposerName).onblur = function()
		{
		  document.getElementById(elWSMGProposerName).value = getStartsWithCaps(document.getElementById(elWSMGProposerName).value);
		
		};
	}
	
	if(document.getElementById(elWSMGName)!=null)
	{	    
		document.getElementById(elWSMGName).onblur = function()
		{
		  document.getElementById(elWSMGName).value = getStartsWithCaps(document.getElementById(elWSMGName).value);
		  fillWBDbProposerDetails();
		
		};
	}
	
	
	if(document.getElementById(elWSMGSumInsured)!=null){
		document.getElementById(elWSMGSumInsured).onblur=function(){checkWealthsuranceMaxiGainCapPlanSA()};
	}
	
	
	if(document.getElementById(elWSMGBaseTerm)!=null){
		document.getElementById(elWSMGBaseTerm).onblur=function()
		{	
			checkWealthMaxiGainPlanTerm();
			document.getElementById(elWSMGBasePremPayTerm).readOnly=false;  
		};
	}

	if(document.getElementById(elWSMGBasePremPayTerm)!=null){
		document.getElementById(elWSMGBasePremPayTerm).onblur=function()
		{
			checkWealthMaxiGainPremiumPaymentTerm();		
		};
	}
	
	
	if(document.getElementById(elWSMGDynamicGuaranteedFundinPercentage)!=null){
		document.getElementById(elWSMGDynamicGuaranteedFundinPercentage).onblur=function(){checkWealthMaxiGainFundValue(this)};
	}
	
	if(document.getElementById(elWSMGMaxiNAVGuaranteedFundinPercentage)!=null){
		document.getElementById(elWSMGMaxiNAVGuaranteedFundinPercentage).onblur=function(){checkWealthMaxiGainFundValue(this)};
	}
	
	if(document.getElementById(elWSMGEquityGrowthFundinPercentage)!=null){
		document.getElementById(elWSMGEquityGrowthFundinPercentage).onblur=function(){checkWealthMaxiGainFundValue(this)};
	}

	if(document.getElementById(elWSMGIncomeFundinPercentage)!=null){
		document.getElementById(elWSMGIncomeFundinPercentage).onblur=function(){checkWealthMaxiGainFundValue(this)};
	}

	
	if(document.getElementById(elWSMGIsapplicantastaffmember) != null ) {
	    document.getElementById(elWSMGIsapplicantastaffmember).onchange = function(){
	    	checkIsStaff(elWSMGIsapplicantastaffmember,elWSMGPleaseentertheemployeecodenumber);
	    }
    }
	
	if(document.getElementById(elWSMGProposerSameAsInsured)!=null){	    
		document.getElementById(elWSMGProposerSameAsInsured).onchange = function(){
			fillWBDbProposerDetails();
		};
	}
	
			
	if(document.getElementById(elWSMGIsapplicantastaffmember).value!="" && document.getElementById(elWSMGPleaseentertheemployeecodenumber).value==""){
		 document.getElementById(elWSMGIsapplicantastaffmember).value="No";
	}	
		checkIsStaff(elWSMGIsapplicantastaffmember,elWSMGPleaseentertheemployeecodenumber);
		
		if(document.getElementById(elWSMGProposerName).value==""){ 
			document.getElementById(elWSMGProposerSameAsInsured).value="No";
	 }
	 
	 
 }

function fillWBDbProposerDetails(){
	if(document.getElementById(elWSMGProposerSameAsInsured).value == "Yes"){
		document.getElementById(elWSMGProposerName).value = document.getElementById(elWSMGName).value;
			document.getElementById(elWSMGProposerDOB).value = document.getElementById(elWSMGLifeInsuredDOB).value;
			document.getElementById(elWSMGAttainedage).value = document.getElementById(elWSMGAttainedagelastbirthday).value;
			document.getElementById(elWSMGGenderOftheProposer).value = document.getElementById(elWSMGGender).value;
			
		if(validateWealthDbProposerAge()){
			document.getElementById(elWSMGProposerName).readOnly=true;
			document.getElementById(elWSMGProposerDOB).readOnly=true;
			document.getElementById(elWSMGGenderOftheProposer).readOnly=true;
		}else{
			document.getElementById(elWSMGProposerSameAsInsured).value="No";
			document.getElementById(elWSMGProposerName).readOnly=false;
			document.getElementById(elWSMGProposerDOB).readOnly=false;
			document.getElementById(elWSMGGenderOftheProposer).readOnly=false;
		}	
	}	
	else{
		document.getElementById(elWSMGProposerName).readOnly=false;
			document.getElementById(elWSMGProposerDOB).readOnly=false;
			document.getElementById(elWSMGGenderOftheProposer).readOnly=false;
	}	
}

function validateWealthDbProposerAge(){
var validAge=true;

	if(document.getElementById(elWSDsProposerDOB)!=null){
		
		   if ( document.getElementById(elWSMGProposerDOB) != null ){
				if (checkDateFormat(document.getElementById(elWSMGProposerDOB))){
					if ( ! isPostDate(document.getElementById(elWSMGProposerDOB)) )	{
						alert("Date of birth of Proposer should not be greater than current date");
						document.getElementById(elWSMGProposerDOB).value ="";
						document.getElementById(elWSMGAttainedage).value="";
						document.getElementById(elWSMGProposerDOB).focus();		
						validAge=false;
					}
				}
		   }
		
			
			  if(document.getElementById(elWSMGAttainedage)!=null) {
				  if(parseInt(document.getElementById(elWSMGAttainedage).value)<8){
						alert("Minimum  age at entry of Policy owner is 8");
						document.getElementById(elWSMGProposerDOB).select();
						document.getElementById(elWSMGAttainedage).value="";
						document.getElementById(elWSMGProposerDOB).focus();
						DisableWealthMaxiGainWOPCharges();
						validAge=false;
				  }
		   	  }
			

				  
	}
	return validAge;
}



function checkWealthMaxiGainAnnualPremium(){

	if(document.getElementById(elWSMGBaseAnnualPrem).value!=null&&document.getElementById(elWSMGBaseAnnualPrem).value!=""){

		var annTest=0;
		var validAP="Yes";


		
			if(parseInt(document.getElementById(elWSMGBaseAnnualPrem).value)>=20000){				  				
				document.getElementById(elWSMGBaseAnnualPrem).value=parseInt(document.getElementById(elWSMGBaseAnnualPrem).value);
				  checkWealthMaxiGainPremiumPaymentTerm();
			}
			else{
				alert("Installment premium for payment frequency Annual should be greater than or equal to 20000");
				document.getElementById(elWSMGBaseAnnualPrem).focus();
				validAP="No";
			}

			
	
		
		
	}
}


function calculateWealthMaxiGainPolicyHolderAge()
{
	if(document.getElementById(elWSMGProposerDOB)!=null){
		// now = new Date();
		 // bD = document.getElementById("PolicyHolderDOB").value.split('-');
		 var str= document.getElementById(elWSMGProposerDOB).value;
		  if(str.length>0){
				//born = new Date(bD[2], bD[1]*1-1, bD[0]);
			/*	born = new Date(bD[0],bD[1]*1-1,bD[2]);
				years = new Date(now.getTime() - born.getTime());
				base = new Date(0);
				document.getElementById("Age").value = years.getFullYear()-base.getFullYear();*/


				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 now = new Date();
				  now.setHours(0,0,0,0); 
				  var years = now.getFullYear()-d.getFullYear();
				  d.setFullYear(now.getFullYear())
				  var diff = now.getTime()-d.getTime()
				  if ( diff <0) years--;

					document.getElementById(elWSMGAttainedage).value=years;

		  }
			
			if( document.getElementById(elWSMGAttainedage).value < 18 )
			{
			/*  alert("PolicyHolder Age should be atleast 18");
			  document.getElementById("Age").value = "";
			  document.getElementById("PolicyHolderDOB").focus();*/
				DisableWealthMaxiGainWOPCharges();
			}
			/*if( document.getElementById(elWSMGAttainedage).value > 65 )
			{
			  alert("PolicyHolder Age should not be Above 65");
			  document.getElementById("Age").value = "";
			  document.getElementById(elWSMGProposerDOB).focus();
			}*/

			if ( document.getElementById(elWSMGProposerDOB) != null )
		        {
					if (checkDateFormat(document.getElementById(elWSMGProposerDOB)))
					{
						if ( ! isPostDate(document.getElementById(elWSMGProposerDOB)) )
						{
							alert("Date of Birth of Policy Owner should not be Greater than Current Date");
							document.getElementById(elWSMGProposerDOB).value ="";
							document.getElementById(elWSMGAttainedage).value="";
							document.getElementById(elWSMGProposerDOB).focus();			           
						}
					}
			    }

			
	}
}



function checkWealthsuranceMaxiGainCapPlanSA(){

	
	var minimumSA=0;
	
	if(document.getElementById(elWSMGSumInsured).value!=""){
			var validSA=true;

			var insuredAge = parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value);
			var annualPremium=parseInt(document.getElementById(elWSMGBaseAnnualPrem).value);
			var policyTerm = parseInt(document.getElementById(elWSMGBaseTerm).value);
				
			var minimumSA1=0;
			var minimumSA2=0;
		    if(insuredAge<45){
		    	
		    		minimumSA1 = annualPremium * 10;
		    		minimumSA2 = 0.5 * policyTerm * annualPremium ;
		    		
		    		if(minimumSA1>minimumSA2){
		    			minimumSA = minimumSA1;
		    		}else{
		    			minimumSA = minimumSA2;
		    		
		    	}
		    }else if(insuredAge>=45){
		    	
		    		minimumSA1 = annualPremium * 7;
		    		minimumSA2 = 0.25 * policyTerm * annualPremium ;
		    		
		    		if(minimumSA1>minimumSA2){
		    			minimumSA = minimumSA1;
		    		}else{
		    			minimumSA = minimumSA2;	    	
		    	}
		    }
		    
	    	if(parseInt(document.getElementById(elWSMGSumInsured).value)<parseInt(minimumSA)){
				validSA=false;
				alert("Sum Assured should be greater than or equal to  "+parseInt(minimumSA));
				document.getElementById(elWSMGSumInsured).value="";
				document.getElementById(elWSMGSumInsured).focus();
			}
		    
		
			
	}
}

	

function checkForWealthMaxiGainSumAssured(checkBoxName)
{
  //alert( "SumAssured : " + document.getElementById('SumAssured').value.length );
   if ( isSumAssuredGiven() )
   {
      //document.getElementById(checkBoxName).checked = true;
      
   }
   else
   {
      document.getElementById(checkBoxName).checked = false;
      alert("SumAssured should not be Empty or less than 50000");
      document.getElementById(elWSMGSumInsured).focus();
   }
 }
 


function checkWealthMaxiGainPlanTerm()
{	
	if(document.getElementById("valueTracker").value!=""){

		callRemove(document.getElementById("valueTracker").value);
	}

			if(document.getElementById(elWSMGBaseTerm).value!=""){
					var planTermAge;
					var validTerm = true;
					var tempelWSMGBaseTerm;
					if(document.getElementById(elWSMGAttainedagelastbirthday)!=null){
						planTermAge=75-document.getElementById(elWSMGAttainedagelastbirthday).value;
					}
					var minimumTerm=0;
					var calcMinimum=0;
					if(parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value)<8){
						calcMinimum=18-parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value);
					}
					/*if(parseInt(calcMinimum)>10)
						minimumTerm=parseInt(calcMinimum);
					else
						minimumTerm=10;*/
					
					if(parseInt(document.getElementById(elWSMGBaseTerm).value)<10 ||
							parseInt(document.getElementById(elWSMGBaseTerm).value)>10)
					{
						alert("Minimum allowable plan term should be 10 years");
						document.getElementById(elWSMGBaseTerm).focus();
						document.getElementById(elWSMGBaseTerm).value="";
						validTerm = false; 
					}
					else if(parseInt(document.getElementById(elWSMGBaseTerm).value)==10)
					{
						document.getElementById(elWSMGBasePremPayTerm).value=5;
						document.getElementById(elWSMGBasePremPayTerm).focus();
						validTerm = false; 
					}	
					
				/*	else if(document.getElementById(elWSMGBaseTerm).value>planTermAge){
						alert("Maximum allowable plan term should be (75-age)");
						document.getElementById(elWSMGBaseTerm).focus();
						document.getElementById(elWSMGBaseTerm).value="";
						validTerm = false; 
					 }else if(document.getElementById(elWSMGAttainedagelastbirthday).value==0 && document.getElementById(elWSMGBaseTerm).value==75){
							alert("Maximum maturity age is 75 years.Maximum allowable plan term is 70 for less than 1 year old.");
							document.getElementById(elWSMGBaseTerm).focus();
							document.getElementById(elWSMGBaseTerm).value="";
							validTerm = false; 
					}else if(document.getElementById(elWSMGBaseTerm).value>10){
						tempelWSMGBaseTerm = 	document.getElementById(elWSMGBaseTerm).value;
						if(tempelWSMGBaseTerm%5!=0){
							alert("Plan term should be multiples of 5");
							document.getElementById(elWSMGBaseTerm).focus();
							validTerm = false; 
						}
					 }*/
					
					 
			    document.getElementById("valueTracker").value=document.getElementById(elWSMGBaseTerm).value;
				generateWealthMaxiGainTopSection(document.getElementById(elWSMGBaseTerm).value);

				checkWealthMaxiGainTopUpLoop(document.getElementById(elWSMGBaseTerm).value);
				checkWealthMaxiGainWithdrawalLoop(document.getElementById(elWSMGBaseTerm).value);
				checkWealthMaxiGainWithdrawal10Loop(document.getElementById(elWSMGBaseTerm).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";

			
			}


}


function generateWealthMaxiGainTopSection(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";

	for(i=1;i<=totalTopUp-5;i++){
		addWealthMaxiGainTopUpElement();
		
	}

	for(i=6;i<=totalTopUp;i++){
		addWealthMaxiGainWithdrawalElement();
	}

}

function callRemove(removeTotal){
	var tblTopUp = document.getElementById('divExpandingBoxTopupDetailsTable');
	var tblWithdrawal = document.getElementById('divExpandingBoxWithdrawalDetailsTable');
	
	var lastRow = tblTopUp.rows.length;
	var lastRow1 = tblWithdrawal.rows.length;
		
	for(var j=lastRow-1;j>=1;j--)
	{
		tblTopUp.deleteRow(j);
	}
	
	for(var j=lastRow1-1;j>=2;j--){
		tblWithdrawal.deleteRow(j);
	}
}


function addWealthMaxiGainTopUpElement(){
		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 policy year ' + iteration;

		var hid1 = document.createElement('input');
		hid1.type  = 'text';
		hid1.name  = 'WealthMaxiGainTopupAmountTerm' + iteration;
		hid1.id    = 'WealthMaxiGainTopupAmountTerm' + iteration;
				

		var hid2 = document.createElement('input');
		hid2.type  = 'text';
		hid2.name  = 'WealthMaxiGainTopupSumInsured' + iteration;
		hid2.id    = 'WealthMaxiGainTopupSumInsured' + 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 addWealthMaxiGainWithdrawalElement(){
		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 policy year ' + iteration;

		var hid1 = document.createElement('input');
		hid1.type  = 'text';
		hid1.name  = 'WealthMaxiGainWithdrawal6Term' + iteration;
		hid1.id    = 'WealthMaxiGainWithdrawal6Term' + iteration;

		var hid2 = document.createElement('input');
		hid2.type  = 'text';
		hid2.name  = 'WealthMaxiGainWithdrawal10Term' + iteration;
		hid2.id    = 'WealthMaxiGainWithdrawal10Term' + 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 checkWealthMaxiGainTopUpLoop(totalTerm){
	for(var topLoop=1;topLoop<=totalTerm-5;topLoop++){
			if(eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+topLoop+"')")!=null){
				
				var topValue=eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+topLoop+"')").value;
				
				eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+topLoop+"')").title = topLoop;
				eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+topLoop+"')").onblur=function(){checkWealthMaxiGainTopUpValue(this)};
				
				eval("document.getElementById('WealthMaxiGainTopupSumInsured"+topLoop+"')").title = topLoop;
				eval("document.getElementById('WealthMaxiGainTopupSumInsured"+topLoop+"')").onblur=function(){checkWealthMaxiGainTopUpSumInsured(this)};
			}
	}
}
////Topup validation

function checkWealthMaxiGainTopUpValue(currentObj){

	var annPremium=0;

	topupValue=currentObj.value;
	currentValue=currentObj.title;
	planValue=parseInt(document.getElementById(elWSMGBaseTerm).value)

var age=parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value);
var topupSumInsured=0;
var topupPremium=0;
var minTopupSumInsured=0;
var maxTopupSumInsured=500000;
var calcAge= parseInt(age) + parseInt(currentValue)-1;

    if(topupValue!=""){
        if(currentValue<=planValue){
			if(parseInt(topupValue)<5000){ 
				alert("Please enter the topup value greater than or equal to 5000 for term "+currentValue);
				eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+currentValue+"')").select();
			}
			else if(parseInt(topupValue)>=5000){	
				topupValue=Math.round(topupValue);
				currentObj.value=topupValue;
				 topupSumInsured=eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value;
				 topupPremium=eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+currentValue+"')").value;
					 
				  if(calcAge<45){
					  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.25);
				  }else if(calcAge>=45){
					  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.1);
				  }	  

				if(topupSumInsured<minTopupSumInsured){
					if(minTopupSumInsured < maxTopupSumInsured){
						eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value = minTopupSumInsured;
					}else{
						eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value = maxTopupSumInsured;
					}	
				}
				
		  }
	  }else{
		 alert("Topup not applicable for term "+currentValue);
		 currentObj.select();
	  }
	}else{
	  eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value ="";
	}
}

function checkWealthMaxiGainTopUpSumInsured(currentObj){
var	topupSumInsured=currentObj.value;
var	currentValue=currentObj.title;
var topupPremium=eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+currentValue+"')").value;
var policyTerm=document.getElementById(elWSMGBaseTerm).value;
var age=parseInt(document.getElementById(elWSMGAttainedagelastbirthday).value);
var minTopupSumInsured=0;
var maxTopupSumInsured=500000;
var calcAge= parseInt(age) + parseInt(currentValue)-1;


	  if(topupPremium!=""){
		  if(calcAge<45){
			  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.25);
		  }else if(calcAge>=45){
			  minTopupSumInsured = Math.round(parseInt(topupPremium)*1.1);
		  }

		  if(topupSumInsured!=""){
			if((topupSumInsured<minTopupSumInsured) && (topupSumInsured<maxTopupSumInsured)){
				  if(minTopupSumInsured<=maxTopupSumInsured){
					  alert("Minimum top-up sum insured amount for premium " + topupPremium + " is " + minTopupSumInsured);
				  }else{
					  alert("Minimum top-up sum insured amount for premium " + topupPremium + " is " + maxTopupSumInsured);
				  }	  
			  eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").select();
			}
			if(topupSumInsured >maxTopupSumInsured){
				alert("Maximum top-up sum insured amount is " + maxTopupSumInsured);
				eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value = maxTopupSumInsured;
			}	
		  }else{
			  if(minTopupSumInsured<=maxTopupSumInsured){
				  eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value = minTopupSumInsured;
			  }else{
				  eval("document.getElementById('WealthMaxiGainTopupSumInsured"+currentValue+"')").value = maxTopupSumInsured;
			  }	  
		  }
	  }	  
}

function checkWealthMaxiGainWithdrawalLoop(totalTerm){

	for(var wTopLoop=6;wTopLoop<=totalTerm;wTopLoop++){
			if(eval("document.getElementById('WealthMaxiGainWithdrawal6Term"+wTopLoop+"')")!=null){
				
				eval("document.getElementById('WealthMaxiGainWithdrawal6Term"+wTopLoop+"')").title = wTopLoop;
				eval("document.getElementById('WealthMaxiGainWithdrawal6Term"+wTopLoop+"')").onblur=function(){checkWealthMaxiGainWithdrawalValue(this,6)};
				
			}
	}
}

function checkWealthMaxiGainWithdrawal10Loop(totalTerm){
	for(var wTopLoop=6;wTopLoop<=totalTerm;wTopLoop++){
			if(eval("document.getElementById('WealthMaxiGainWithdrawal10Term"+wTopLoop+"')")!=null){
				
				eval("document.getElementById('WealthMaxiGainWithdrawal10Term"+wTopLoop+"')").title = wTopLoop;
				eval("document.getElementById('WealthMaxiGainWithdrawal10Term"+wTopLoop+"')").onblur=function(){checkWealthMaxiGainWithdrawalValue(this,10)};
				
			}
	}
}

///Withdrawal Validation

function checkWealthMaxiGainWithdrawalValue(currObj,percent){
	
	var percentage = percent;
	withdrawalValue=currObj.value;
	var curValue=currObj.title;	
	planValue=parseInt(document.getElementById(elWSMGBaseTerm).value)
	if(withdrawalValue!="")
	{
		
 		if(curValue<=planValue)
 		{	
			var currentAge=document.getElementById(elWSMGAttainedagelastbirthday).value;
			var ageToCheck=parseInt(currentAge)+parseInt(curValue);

			if(parseInt(ageToCheck)<=18)
			{
				//eval("document.getElementById('WithdrawalAmountTerm"+curValue+"')").value="";
				alert("Withdrawal not applicable for the age below 18");
				//eval("document.getElementById('WithdrawalAmountTerm"+curValue+"')").select();
				currObj.focus();
			}
			else
				{
					if(parseInt(withdrawalValue)>=10000)
							{
								var request;
							    var modeval;
								var url;
								var responseString;
						
								var lifeInsuredDOB=document.getElementById(elWSMGLifeInsuredDOB).value;
								var lifedob =  getDateObject(lifeInsuredDOB,"-");		
						
								var age=document.getElementById(elWSMGAttainedagelastbirthday).value;
								var gender=document.getElementById(elWSMGGender).value;
								var planTerm=parseInt(curValue);
								var sumAssured=document.getElementById(elWSMGSumInsured).value;			
								var premiumPaymentTerm=document.getElementById(elWSMGBasePremPayTerm).value;
								var annualPremium=document.getElementById(elWSMGBaseAnnualPrem).value;
								var policyHolderAge=document.getElementById(elWSMGAttainedage).value;
								var isStaffMember=document.getElementById(elWSMGIsapplicantastaffmember).value;
								var employeeCode=document.getElementById(elWSMGPleaseentertheemployeecodenumber).value;		
								
								var maxiNAVGuaranteedFundPercentage=document.getElementById(elWSMGMaxiNAVGuaranteedFundinPercentage).value;
								var equityGrowthFundPercentage=document.getElementById(elWSMGEquityGrowthFundinPercentage).value;
								var dynamicGuaranteedFundPercentage=document.getElementById(elWSMGDynamicGuaranteedFundinPercentage).value;
								var incomeFundPercentage=document.getElementById(elWSMGIncomeFundinPercentage).value;
								
								var topUpToCheck="";
								var topupSumInsured="";
								var topupTerm=planValue-5;
								for(i=1;i<=topupTerm;i++){
									topAmountTerm=document.getElementById('WealthMaxiGainTopupAmountTerm'+i).value;
									topUpToCheck=topUpToCheck+"&topA"+i+"="+topAmountTerm;
									//alert("topUpToCheck:::::"+topUpToCheck);
								}
								for(i=1;i<=topupTerm;i++){
									topupSumInsAmount=document.getElementById('WealthMaxiGainTopupSumInsured'+i).value;
									topupSumInsured=topupSumInsured+"&TopupSumInsured"+i+"="+topupSumInsAmount;
									//alert("topupSumInsured:::::"+topupSumInsured);
								}
								
								var withdrawalToCheck6="";
								var withdrawalToCheck10="";
								for(i=6;i<=planValue;i++){
									withdrawalAmount6Term=document.getElementById('WealthMaxiGainWithdrawal6Term'+i).value;
									withdrawalToCheck6=withdrawalToCheck6+"&wd6A"+i+"="+withdrawalAmount6Term;
									//alert("withdrawalToCheck6:::::"+withdrawalToCheck6);
									withdrawalAmount10Term=document.getElementById('WealthMaxiGainWithdrawal10Term'+i).value;
									withdrawalToCheck10=withdrawalToCheck10+"&wd10A"+i+"="+withdrawalAmount10Term;
									//alert("withdrawalToCheck10:::::"+withdrawalToCheck10);
								}
								
								var basePlanEMR=document.getElementById(elWSMGBaseEMR).value;
								var basePlanPerMile=0;
										
								if(document.getElementById(elWSMGBasePerMile).value!=""){
									var valueCheck =  document.getElementById(elWSMGBasePerMile);
									if(checkPermile(valueCheck)){
										basePlanPerMile = document.getElementById(elWSMGBasePerMile).value;
									}
								}
								
								
						
								var totalTopUp=0;
								curValue = parseInt(curValue);
								if((curValue-1)<=topupTerm){
									for(j=(curValue-1);j>=(curValue-2);j--){
											var valToCheck=eval("document.getElementById('WealthMaxiGainTopupAmountTerm"+j+"')").value;
											//alert("valToCheck--------- "+valToCheck);
											var valToPass=0;
											if(valToCheck!="")
												valToPass=valToCheck;
											totalTopUp=parseInt(totalTopUp)+parseInt(valToPass);
									}
								}	
								//alert("totalTopUp:::::"+totalTopUp);
								//alert(totalTopUp+topUpToCheck+topupSumInsured+withdrawalToCheck6+withdrawalToCheck10);
								url="validateWealthMaxiGainPlan.do?mode=wealthMaxiGainWithdrawalCheck&age="+age+	
									"&ldob="+lifedob+
									"&pha="+policyHolderAge+
									"&sex="+gender+
									"&isStaffMember="+isStaffMember+
									"&employeeCode="+employeeCode+
									"&sa="+sumAssured+
									"&pt="+planTerm+
									"&ip="+annualPremium+			
									"&ap="+annualPremium+
									"&ppt="+premiumPaymentTerm+	
									"&grfp="+maxiNAVGuaranteedFundPercentage+
									"&egfp="+equityGrowthFundPercentage+		
									"&ifp="+incomeFundPercentage+			
									"&cgfp="+dynamicGuaranteedFundPercentage+
									"&watv="+withdrawalValue+
									"&percent="+percentage+
									"&lttv="+totalTopUp+topUpToCheck+topupSumInsured+withdrawalToCheck6+withdrawalToCheck10+
									"&basE="+basePlanEMR+
									"&basP="+basePlanPerMile;
						
					
			
					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); 
					}
			
			
			
					 function processStateChange() {
			
						if (request.readyState == 4) { // Complete
			
						  if (request.status == 200) { // OK response
								responseString = request.responseText;
								if(responseString.length>12){
									alert(responseString);
									
								}
						  }
						}
					 }
				}
				else
				{
					 alert("Minimum withdrawal amount is 10000");
					 currObj.value="";
					 currObj.focus();
				}
			}
 		}
 		else
 		{
 			alert("Withdrawal is not applicable for term "+currentValue);
 			currObj.value="";
 			currObj.focus();
 		}
	}
}


function checkWealthMaxiGainPremiumPaymentTerm()
{	
	var PPT=document.getElementById(elWSMGBasePremPayTerm);
	
	if(document.getElementById(elWSMGBasePremPayTerm).value != "")
	{	
		var wopCheck="Yes";			
		if(parseInt(document.getElementById(elWSMGBasePremPayTerm).value)>5 ||
				parseInt(document.getElementById(elWSMGBasePremPayTerm).value)<5)
		{ 
			alert("Premium payment term should be 5 years");
			document.getElementById(elWSMGBasePremPayTerm).focus();
			document.getElementById(elWSMGBasePremPayTerm).value="";
			wopCheck="No";
		}
		else if(parseInt(document.getElementById(elWSMGBasePremPayTerm).value)>parseInt(document.getElementById(elWSMGBaseTerm).value))
		{
			alert("Premium payment term should not exceed Plan term ");
			document.getElementById(elWSMGBasePremPayTerm).focus();
			wopCheck="No";
		}
		else
		{
			document.getElementById(elWSMGBaseAnnualPrem).focus();
		}			
	}
}

function checkWealthMaxiGainFundValue( 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 = getSumOfWealthMaxiGainFundOptions();
        
    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 getSumOfWealthMaxiGainFundOptions()
{
    var sum = 
    giveInt(document.getElementById(elWSMGMaxiNAVGuaranteedFundinPercentage).value) +
    giveInt(document.getElementById(elWSMGEquityGrowthFundinPercentage).value) +
     giveInt(document.getElementById(elWSMGDynamicGuaranteedFundinPercentage).value) +
      giveInt(document.getElementById(elWSMGIncomeFundinPercentage).value);
    

    return sum;
}



function wealthMaxiGainatleastOneFundOptionDetailGiven()
{
   var given = false;
   
   if ( trim(document.getElementById(elWSMGMaxiNAVGuaranteedFundinPercentage).value)>0 )
      given = true;
   if ( trim(document.getElementById(elWSMGEquityGrowthFundinPercentage).value)>0 )
      given = true; 
   if ( trim(document.getElementById(elWSMGDynamicGuaranteedFundinPercentage).value)>0 )
   given = true;
   if ( trim(document.getElementById(elWSMGIncomeFundinPercentage).value)>0 )
   given = true;

   
          
   return given;
}

function setWealthMaxiGainTopupWithdrawal(){
	if(document.getElementById("innerWealthMaxiGainTopUpText").value.length > 0){
		document.getElementById('divExpandingBoxTopupDetails').style.visibility = 'visible';
		document.getElementById('headerExpandingBoxTopupDetails').style.visibility = 'visible';
		
		document.getElementById('divExpandingBoxTopupDetails').innerHTML = document.getElementById("innerWealthMaxiGainTopUpText").value;

		checkWealthMaxiGainTopUpLoop(document.getElementById(elWSMGBaseTerm).value);
		
	}

	if(document.getElementById("innerWealthMaxiGainWithdrawalText").value.length > 0){

		document.getElementById('divExpandingBoxWithdrawalDetails').style.visibility = 'visible';
		document.getElementById('headerExpandingBoxWithdrawalDetails').style.visibility = 'visible';
		
		document.getElementById('divExpandingBoxWithdrawalDetails').innerHTML = document.getElementById("innerWealthMaxiGainWithdrawalText").value;

		checkWealthMaxiGainWithdrawalLoop(document.getElementById(elWSMGBaseTerm).value);
		checkWealthMaxiGainWithdrawal10Loop(document.getElementById(elWSMGBaseTerm).value);
	}
}




function wealthMaxiGainCalculateAge(chosenProduct,elWSMGLifeInsuredDOB,elWSMGAttainedagelastbirthday)
{
	if(checkDateFormat(document.getElementById(elWSMGLifeInsuredDOB))) {
	if(document.getElementById(elWSMGLifeInsuredDOB)!=null)
	{
		  var str = document.getElementById(elWSMGLifeInsuredDOB).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(elWSMGAttainedagelastbirthday)!=null){
					
					document.getElementById(elWSMGAttainedagelastbirthday).value = years;
				}
				
				//alert("monthdiff........" + monthdiff);
				//if(monthdiff<1)
				if(years<8)				
				{
					alert("Minimum Age at entry is 8 years");
					document.getElementById(elWSMGLifeInsuredDOB).select();
					document.getElementById(elWSMGAttainedagelastbirthday).value="";
					document.getElementById(elWSMGLifeInsuredDOB).focus();
				}
				if(years>65)				
				{
					alert("Maximun Age at entry is 65 years");
					document.getElementById(elWSMGLifeInsuredDOB).select();
					document.getElementById(elWSMGAttainedagelastbirthday).value="";
					document.getElementById(elWSMGLifeInsuredDOB).focus();
				}
				//var mondays=caldays(monvalue,yearvalue);
				//alert("mondays"+mondays);
		   }
		}
  }	
}

function caldays(m,y)
{    
    if(m==01||m==03||m==05||m==07||m==08||m==10||m==12)	{
      var dmax = 31; 
      return dmax;	
     }else if (m==04||m==06||m==09||m==11) {
      var dmax = 30;
      return dmax;
     }else{ if((y%400==0) || (y%400==0 && y%100!=0)){
     dmax = 29;
     return dmax;
     }else {
      dmax = 28;
      }
      return dmax;
      }
}

