////Product 6 :Bondsurance Validation
var elBondPlanInsurerName = "NameofthePersontobeInsured";
var elBondPlanPolicyOwnerName = "NameofthePolicyOwner";
var elBondPlanDOB = "DateofBirthofthePersontobeInsured";
var elBondPlanAge = "AgeofthePersontobeInsuredinyears";
var elBondPlanSelection = "PlanSelection";
var elBondPlanMaturityBenefit = "MaturityBenefitinRs";
var elBondPlanMaturityPeriod = "MaturityPeriodPolicyTerminyears";
var elBondPlanPremInclST = "SinglePremiuminclservicetaxandeducationcessinRs";
var elBondPlanEMR = "ExtraMortality";
var elBondPlanPerMille = "PerMille";

var elBondIsapplicantastaffmember="Isapplicantastaffmember";
var elBondPleaseentertheemployeecodenumber="Pleaseentertheemployeecodenumber";

function callBondsurancePlanValidation(){

	var selectedName =	"Bondsurance Plan";
	var premiumInput = true;
	 if(document.getElementById(elBondIsapplicantastaffmember) != null ) {
		    document.getElementById(elBondIsapplicantastaffmember).onchange = function(){
		    	checkIsStaff(elBondIsapplicantastaffmember,elBondPleaseentertheemployeecodenumber);
		    	checkBondPlanPremium();
		    }
	    }
		if(document.getElementById(elBondPleaseentertheemployeecodenumber).value!=null){
			document.getElementById(elBondPleaseentertheemployeecodenumber).onblur=function(){
				checkBondPlanPremium();
			}
		}
		 document.getElementById(elBondIsapplicantastaffmember).value="No";
	if(document.getElementById(elBondIsapplicantastaffmember).value!="" && document.getElementById(elBondPleaseentertheemployeecodenumber).value==""){
		 document.getElementById(elBondIsapplicantastaffmember).value="No";
	 }	
		checkIsStaff(elBondIsapplicantastaffmember,elBondPleaseentertheemployeecodenumber);
		
	if(document.getElementById("valuePremiumInput").value!=""){
		if(document.getElementById("valuePremiumInput").value == "true"){
			document.getElementById("headerExpandingBoxPremiumDetails").style.display = "";
			document.getElementById("divExpandingBoxPremiumDetails").style.display = "";
			premiumInput = true;
		}
		else{
			document.getElementById("headerExpandingBoxPremiumDetails").style.display = "none";
			document.getElementById("divExpandingBoxPremiumDetails").style.display = "none";
			premiumInput = false;
			document.getElementById(elBondPlanMaturityBenefit).readOnly=false;
		}
	}

	if(document.getElementById(elBondPlanAge)!=null){
		document.getElementById(elBondPlanAge).readOnly=true;
	}

	if(document.getElementById(elBondPlanSelection)!=null){
		document.getElementById(elBondPlanSelection).onchange = function(){
			
			if(document.getElementById(elBondPlanSelection).value == "Premium Input (in Rs)"){
				document.getElementById("headerExpandingBoxPremiumDetails").style.display = "";
				document.getElementById("divExpandingBoxPremiumDetails").style.display = "";
				premiumInput = true;
			
				document.getElementById(elBondPlanPremInclST).value = "";
				document.getElementById(elBondPlanMaturityBenefit).value = "";
				document.getElementById(elBondPlanMaturityBenefit).readOnly=true;
			}
			else{
				document.getElementById("headerExpandingBoxPremiumDetails").style.display = "none";
				document.getElementById("divExpandingBoxPremiumDetails").style.display = "none";
				premiumInput = false;
			
				document.getElementById(elBondPlanPremInclST).value = "";
				document.getElementById(elBondPlanMaturityBenefit).value = "";
				document.getElementById(elBondPlanMaturityBenefit).readOnly=false;
			}
		}
	}
	

	if(document.getElementById(elBondPlanDOB)!=null){
		if(document.getElementById(elBondPlanDOB).value!=null || 
			document.getElementById(elBondPlanDOB).value!=""){
			   document.getElementById(elBondPlanDOB).onblur=function()
			   {
				  calculateAge(selectedName,elBondPlanDOB,elBondPlanAge)
				  calculateBackDateAge();
				 
				  if(checkBondPlanAge()){
					if(premiumInput){
						checkBondPlanPremium();
					}
					else{
						if(document.getElementById(elBondPlanSelection).value == "Amount of Maturity Benefit (in Rs)"){
							checkAlertForBondPlan();
						}
					}
				  }
			   };
		}
	}
	
	  //Bondsurance Plan Age caluclation and Premium Calculation Based on BackDate
	  if(document.getElementById(elBackDate)!= null){
	     if(document.getElementById(elBackDate).value != null){
		        document.getElementById(elBackDate).onblur=function()
		        {
		           strBackDate = document.getElementById(elBackDate).value;
		            if(strBackDate !="" ){
			            if(checkDateFormat(document.getElementById(elBackDate))) {
			               if(checkBackDate(elBackDate)) {
			   			      calculateBackDateAge();
			   			   }   
			   		    }	
			   		}else{
			   		     calculateBackDateAge();
			   		} 
			   		
					  if(checkBondPlanAge()){
						if(premiumInput){
							checkBondPlanPremium();
						}
						else{
							if(document.getElementById(elBondPlanSelection).value == "Amount of Maturity Benefit (in Rs)"){
								checkAlertForBondPlan();
							}
						}
					  }
		   		};	  
	     }
	  }   
	
	
	if(document.getElementById(elBondPlanInsurerName)!=null)
	{	    
		document.getElementById(elBondPlanInsurerName).onblur = function()
		{
		  document.getElementById(elBondPlanInsurerName).value = getStartsWithCaps(document.getElementById(elBondPlanInsurerName).value);
		  
		};
	}

	if(document.getElementById(elBondPlanPolicyOwnerName)!=null)
	{	    
		document.getElementById(elBondPlanPolicyOwnerName).onblur = function()
		{
		  document.getElementById(elBondPlanPolicyOwnerName).value = getStartsWithCaps(document.getElementById(elBondPlanPolicyOwnerName).value);
		  
		};
	}


	if(document.getElementById(elBondPlanMaturityBenefit)!=null)
	{	    
			document.getElementById(elBondPlanMaturityBenefit).onblur =  function(){
				checkAlertForBondPlan();
			};
	}


	if(document.getElementById(elBondPlanMaturityPeriod)!=null)
	{	    
		document.getElementById(elBondPlanMaturityPeriod).onchange = function(){
						
            if(checkBondPlanAge()){
				if(premiumInput){
						checkBondPlanPremium();
				}
				else{
					if(document.getElementById(elBondPlanSelection).value == "Amount of Maturity Benefit (in Rs)"){
						checkAlertForBondPlan();
					}
				}
			}

		};
	}

	if(document.getElementById(elBondPlanPremInclST)!=null)
	{	
		document.getElementById(elBondPlanPremInclST).onblur = function(){
			
			checkBondPlanPremium();
		
		};
		
	}
}


function checkBondPlanAge(){
	if(document.getElementById(elBondPlanAge).value!=""){

		var maturityPeriod=document.getElementById(elBondPlanMaturityPeriod).value;

		if(parseInt(maturityPeriod)==5){
			if(parseInt(document.getElementById(elBondPlanAge).value)<13){
				alert("Minimum Age At Entry is 13");
				document.getElementById(elBondPlanDOB).select();
				document.getElementById(elBondPlanAge).value = "";
				document.getElementById(elBondPlanDOB).focus();
				return false;
			}
			else if(parseInt(document.getElementById(elBondPlanAge).value)>55){
				alert("Maximum Age At Entry is 55");
				document.getElementById(elBondPlanDOB).select();
				document.getElementById(elBondPlanAge).value = "";
				document.getElementById(elBondPlanDOB).focus();
				return false;
			}
			else{
				return true;
			}
		}

		if(parseInt(maturityPeriod)==10){
			if(parseInt(document.getElementById(elBondPlanAge).value)<8){
				alert("Minimum Age At Entry is 8");
				document.getElementById(elBondPlanDOB).select();
				document.getElementById(elBondPlanAge).value = "";
				document.getElementById(elBondPlanDOB).focus();
				return false;
			}
			else if(parseInt(document.getElementById(elBondPlanAge).value)>50){
				alert("Maximum Age At Entry is 50");
				document.getElementById(elBondPlanDOB).select();
				document.getElementById(elBondPlanAge).value = "";
				document.getElementById(elBondPlanDOB).focus();
				return false;
			}
			else{
				return true;
			}
		}
	}
}


function checkAlertForBondPlan(){

		var maturityBenefit=0;
		var maturityPeriod=0;
		var age=0;
		if(document.getElementById(elBondPlanAge).value!=""){
		  age=document.getElementById(elBondPlanAge).value;
		}

		if(document.getElementById(elBondPlanMaturityBenefit).value!=""){
		  maturityBenefit=document.getElementById(elBondPlanMaturityBenefit).value;
		}

		if(document.getElementById(elBondPlanMaturityPeriod).value!="0"){
		  maturityPeriod=document.getElementById(elBondPlanMaturityPeriod).value;
		}

		
		if(age>0 && maturityBenefit>0 && maturityPeriod>0){

		var request;
	    var modeval;
	    var isStaff=document.getElementById(elBondIsapplicantastaffmember).value;
		var employeeCode=document.getElementById(elBondPleaseentertheemployeecodenumber).value;
		var url;
		var responseString;
		url="validateProducts.do?mode=bondAlert&age="+age+
			"&maturityBenefit="+maturityBenefit+
			"&maturityPeriod="+maturityPeriod+"&isStaff="+isStaff+"&employeeCode="+employeeCode;
			
		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["validMaturityBenefit"].length>2){
							alert(jsonObject["validMaturityBenefit"]);
							document.getElementById(elBondPlanMaturityBenefit).value="";
							document.getElementById(elBondPlanMaturityBenefit).focus();
					   }
					   else if(jsonObject["alertMessage"].length>2){
						   alert(jsonObject["alertMessage"]);
					   }
					}
					
			  }
			}
			}
			request.open("POST",url,true);
			request.setRequestHeader("content-type","application/x-www-form-urlencoded");
			request.send(null); 
		}

	}
}

function checkBondPlanPremium(){

		var premiumWithServiceTax=0;
		var maturityPeriod=0;
		var age=0;
		if(document.getElementById(elBondPlanAge).value!=""){
		  age=document.getElementById(elBondPlanAge).value;
		}

		if(document.getElementById(elBondPlanPremInclST).value!=""){
		  premiumWithServiceTax=document.getElementById(elBondPlanPremInclST).value;
		}

		if(document.getElementById(elBondPlanMaturityPeriod).value!="0"){
		  maturityPeriod=document.getElementById(elBondPlanMaturityPeriod).value;
		}
		
		
		if(premiumWithServiceTax>20205){
			if(age>0 && premiumWithServiceTax>0 && maturityPeriod>0){

				var request;
				var modeval;
				var isStaff=document.getElementById(elBondIsapplicantastaffmember).value;
				var employeeCode=document.getElementById(elBondPleaseentertheemployeecodenumber).value;
				var url;
				var responseString;
				url="validateProducts.do?mode=maturityBenefitCheck&age="+age+
					"&premiumWithServiceTax="+premiumWithServiceTax+
					"&maturityPeriod="+maturityPeriod+"&isStaff="+isStaff+"&employeeCode="+employeeCode;
					
				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 + ")" ) ;
							   document.getElementById(elBondPlanMaturityBenefit).value= jsonObject["maturityBenefitValue"];

							   if(jsonObject["alertMessage"].length>2){
								   alert(jsonObject["alertMessage"]);
							   }
							}
					  }
					}
					}
					request.open("POST",url,true);
					request.setRequestHeader("content-type","application/x-www-form-urlencoded");
					request.send(null); 
				}
			}
		}
		else{
			if(premiumWithServiceTax != 0 ){
				alert("Minimum Premium Including Service Tax is 20206");
				document.getElementById(elBondPlanMaturityBenefit).value="";
				document.getElementById(elBondPlanPremInclST).focus();
			}
			
		}
		
}


