﻿    var xhttp;
    //First declare the following variable on your page header and then assign the Product Name 
    //var PRD = 'FLPUL';
    //var PRODUCT = 'Freedom Life Plan';
    //var debugMode = false;
    
    /*'~~~~~~~~~~~~~ Functions in this file ~~~~~~~~~~~~~~~~~
        validatePolTerm(obj, Age)
        validateAge(obj)
        validatePaidUpTerm(obj)
        validateSumAssured(obj, SAOption)
        validateSumAssured3to5(obj, SAOption, PPT)
        validateFunds(obj, type)
        validateEMR(obj)
        validateFE(obj)
        validateASPDur(obj, Max)
        validateASPAmt(obj, dur, AspPaid, Premium)
        validatePWRPDur(obj, PTerm)
        validatePWRPAmt(obj, dur, PWAmt, Premium)
        validateCLPolicyYear(obj, Age)
        validateCL(obj, PT_PUP)
        validateLLGCL(obj, Age, Rider)
        
        getMaturityAge()
        getAgeWithRider(isJoint)
        getLLGCL(Age, Rider, CL)
        getSumAssuredADD()
        getSumAssuredCIPTD()
    */
    function postHTTP(qry){
	    xhttp = new ActiveXObject("MSXML2.XMLHTTP");
        //xhttp.open("GET", "http://intranetapplications.avivaindia.com/CentralReport/Reports/pqis/XmlReader.aspx?PRD="+ PRD + "&" + qry, false);
	xhttp.open("GET", "pqis/XmlReader.aspx?PRD="+ PRD + "&" + qry, false);
	    xhttp.send();				
    }

    function validatePolTerm(obj, Age){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=PolTerm');

        Res = xhttp.responseText;
        dispAlert(Res);
        
        if (getInt(Age) > 0 && getInt(Res.split('~')[2]) > 0)
            if ((getInt(obj.value) + getInt(Age)) > getInt(Res.split('~')[2])){
                alert('Policy term should between ' + Res.split('~')[0] + ' and ' + (getInt(Res.split('~')[2])-getInt(Age)) + ' years for ' + PRODUCT);
                obj.value='';
                obj.focus();
                return false;
            }
        
        if(Res.split('~')[0] != "" && Res.split('~')[1] != "")
        if (parseInt(obj.value) < parseInt(Res.split('~')[0]) || parseInt(obj.value) > parseInt(Res.split('~')[1])){
            alert('Policy term should between ' + Res.split('~')[0] + ' and ' + Res.split('~')[1] + ' years for ' + PRODUCT);
            obj.value='';
            obj.focus();
            return false;            
        }
        
	    if (parseInt(obj.value) < parseInt(Res.split('~')[0])){
	        alert('Policy term should be greater than ' + Res.split('~')[0] + ' years for ' + PRODUCT);
            obj.value='';
            obj.focus();
            return false;
        }
        
        if (parseInt(obj.value) > parseInt(Res.split('~')[1])){
	        alert('Policy term should be less than ' + Res.split('~')[1] + ' years for ' + PRODUCT);
            obj.value='';
            obj.focus();
            return false;        
        }
        //if all is well then
        return true;
    } // Ends validatePolTerm
    
    function validateAge(obj, FocusTo){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=Age');

        Res = xhttp.responseText;
        dispAlert(Res);
	    if (parseInt(obj.value) < Res.split('~')[0] || parseInt(obj.value) > Res.split('~')[1]){
	        alert('Entry age should be between ' + Res.split('~')[0] + ' and ' + Res.split('~')[1] + ' years for ' + PRODUCT);
	        if(FocusTo == undefined) obj.focus();
            else FocusTo.focus();
            return false;
        }
        //if all is well then
        return true;
    }

    function validatePaidUpTerm(obj, Max){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=PaidUpTerm');
        
        Res = xhttp.responseText;
        dispAlert(Res);
        if (Res.split('~')[1] != "")
            Max = parseInt(Res.split('~')[1])
            
	    if (parseInt(obj.value) < Res.split('~')[0]){
	        alert('Paid up term cannot be less than '+ Res.split('~')[0] +' years.');
	        obj.value='';
            obj.focus();
            return false;
        }
	    if (parseInt(obj.value) > Max){
	        alert('Paid up term cannot be more than '+ Max +' years.');
	        obj.value='';
            obj.focus();
            return false;
        }   
        //if all is well then
        return true;     
    }

    function validateSumAssured(obj, SAOption){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate='+SAOption);

        Res = xhttp.responseText;
        dispAlert(SAOption + ': ' + Res);
	    if (parseInt(obj.value) < Res.split('~')[0]){
	        alert(SAOption + ' should not be less than Rs '+ Res.split('~')[0] + ' for ' + PRODUCT);
	        obj.value='';
            obj.focus();
            return false;
        }
        
        if (Res.split('~')[1] != "")
        {
            if (parseInt(obj.value) > Res.split('~')[1]){
	           alert(SAOption + ' should not be greater than Rs '+ Res.split('~')[1] );
	           obj.value='';
               obj.focus();
               return false;
            }
        }
        //if all is well then
        return true;
    }

    function validateSumAssured3to5(obj, SAOption, PPT){
        if (obj.value == "" || SAOption == 'Premium' && (PPT < 3 || PPT > 5)) return true; 
        var Res = new String();
        postHTTP('validate=Premium3to5');

        Res = xhttp.responseText;
        dispAlert(SAOption + ': ' + Res);
	    if (parseInt(obj.value) < getInt(Res)){
	        alert(SAOption + ' should not be less than Rs '+ Res + ' for premium payment term 3 and 5 years');
	        obj.value='';
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }

    function validateFunds(obj, type){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=Funds');

        Res = xhttp.responseText;
        dispAlert(Res);
	    if (type='secure' && parseInt(obj.value) < Res.split('~')[0]){
	        alert('Minimum premium allocation in any chosen unit linked fund is ' + Res.split('~')[0] + '%');
	        obj.value='';
            obj.focus();
            return false;
        }

	    if (type='balance' && parseInt(obj.value) < Res.split('~')[1]){
	        alert('Minimum premium allocation in any chosen unit linked fund is ' + Res.split('~')[0] + '%');
	        obj.value='';
            obj.focus();
            return false;
        }
        
	    if (type='growth' && parseInt(obj.value) < Res.split('~')[2]){
	        alert('Minimum premium allocation in any chosen unit linked fund is ' + Res.split('~')[0] + '%');
	        obj.value='';
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }
    
    function validateEMR(obj){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=EMR');

        Res = xhttp.responseText;
        dispAlert(Res);
	    if (parseInt(obj.value) > Res){
	        alert('EMR cannot be more than ' + Res + '%');
	        obj.value='';
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }

    function validateFE(obj){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=FE');

        Res = xhttp.responseText;
        dispAlert(Res);
	    if (parseInt(obj.value) > Res){
	        alert('Fixed Extra per mille cannot be more than ' + Res);
	        obj.value='';
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }    
    //_________________________________________ ASP Validations ____________________________________________//
    function validateASPDur(obj, Max){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=ASPDUR');

        Res = xhttp.responseText;
        dispAlert(Res);
	    if (parseInt(obj.value) < Res || parseInt(obj.value) > Max){
	        alert('The top-up year should be between ' + Res.split('~')[0] + ' and ' + Max);
	        obj.value="";
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }

    function validateASPAmt(obj, dur, AspPaid, Premium){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=ASPAMT');
        Res = xhttp.responseText;
        dispAlert(Res);
        
        if (getFloat(Premium) == 0)
            Premium = getFloat(document.all.SumAssured.value);

        if (getInt(dur) == 0){
        alert(obj.name)
            alert(getInt(dur))
            alert("Please enter Top up duration year before amount.");
            obj.value='';
            obj.focus();
            return false;
        }
        
	    if (parseInt(obj.value) < Res.split('~')[0]){
	        alert('Minimum Top-up premium is Rs ' + Res.split('~')[0]);
	        obj.value="";
            obj.focus();
            return false;;
        }
        
        if (Res.split('~')[1] != ""){
            if (obj.value > (Premium * dur * Res.split('~')[1]-AspPaid)){
		        alert("Maximum top up premium can be Rs " + (Premium * dur * Res.split('~')[1]-AspPaid));
		        obj.value="";
		        obj.focus();
		        return false;
		    }
        }
        //if all is well then
        return true;
    }	

    //_________________________________________ PW Validations ____________________________________________//
    function validatePWRPDur(obj, PTerm){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=PWRPDUR');

        Res = xhttp.responseText;
        dispAlert(Res);
        if (parseInt(PTerm) < Res)
        {
            alert('The Partial Withdrawal year cannot be less than '+ Res.split('~')[0] + ' years.');
            obj.value="";
            obj.focus();
            return false;
        }
	    if (parseInt(obj.value) < Res || parseInt(obj.value) > PTerm){
	        alert('The Partial Withdrawal year should between '+ Res.split('~')[0] + ' and ' + PTerm);
	        obj.value="";
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }

    function validatePWRPAmt(obj, dur, PWAmt, Premium){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=PWRPAMT');
        Res = xhttp.responseText;
        dispAlert(Res);
        
        if (getFloat(Premium) == 0)
            Premium = getFloat(document.all.SumAssured.value);
                    
        if (getInt(dur) == 0){
            alert("Please enter PW duration year before amount.");
            obj.value='';
            obj.focus();
            return false;
        }
        
	    if (parseInt(obj.value) < Res.split('~')[0]){
	        alert('The minimum Partial Withdrawal amount is Rs ' + Res.split('~')[0]);
            obj.value="";
            obj.focus();
            return false;;
        }
        
        if (Res.split('~')[1] != ""){
            if (obj.value > (Premium * dur * Res.split('~')[1]-PWAmt)){
		        alert("Maximum Partial Withdrawal can be Rs " + (Premium * dur * Res.split('~')[1]-PWAmt));
		        obj.value="";
		        obj.focus();
		        return false;
		    }
        }
        //if all is well then
        return true;
    }				
    //_________________________________________ PWASP Validations ____________________________________________//
    function validatePWASPDur(obj){
        if (obj.value == "") return true;;
    }

    function validatePWASPAmt(obj, dur){
        if (obj.value == "") return true;
    }		
    //_________________________________________ Change In Cover Level Validations ____________________________________________//    
    
    function validateCLPolicyYear(obj, Age, PPT_PUP){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=CLPolicyYear');

        Res = xhttp.responseText;
        dispAlert(Res);        
        if(getInt(obj.value) > 27)
        {
            alert("Increase in cover level is not allowed after 27 years.")
            obj.value="";
            obj.focus();
            return false;
        }
	    if (parseInt(obj.value) >= parseInt(PPT_PUP) && parseInt(PPT_PUP) != 0){
	        alert('Year of increase in cover level should be up to ' + (parseInt(PPT_PUP) - 1));
            obj.focus();
            obj.value='';
            return false;
        }        
        
	    if ((parseInt(Age) + parseInt(obj.value)) > parseInt(Res)){
	        alert('Year of increase in cover level should be up to ' + (Res - Age));
            obj.focus();
            obj.value='';
            return false;            
        }
        //if all is well then
        return true;
    }    

    //PT_PUP    Policy Term / Paid Up
    function validateCL(obj, PT_PUP){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=CoverLevel');

        Res = xhttp.responseText;
        dispAlert(Res);
        
        
	    if (parseFloat(obj.value) < (parseFloat(PT_PUP) * Res.split('~')[0]) || parseFloat(obj.value) > (parseFloat(PT_PUP) * Res.split('~')[1]))
	    {
	        alert('Increase in cover level should be between ' + (PT_PUP * parseFloat(Res.split('~')[0])) + ' and ' + (parseFloat(PT_PUP) * Res.split('~')[1]) + ' for ' + PRODUCT);
            obj.focus();
            return false;
        }
        //if all is well then
        return true;
    }        
    
    function validateLLGCL(obj, Age, Rider){
        if (obj.value == "") return true;
        var Res = new String();
        postHTTP('validate=LLGCoverLevel&External=yes&Rider=' + Rider + '&Age='+Age);
        
        Res = xhttp.responseText;
        dispAlert(Res);
        if (getFloat(obj.value) < getFloat(Res.split('~')[0])){
            alert("Cover Level should not be less than " + Res.split('~')[0]);
            obj.focus();
            return false;
        }
        
        if (getInt(obj.value) > Res.split('~')[2]){
            alert("Cover Level should not be greater than " + Res.split('~')[2]);
            obj.focus();
            return false;
        }
        //if all is well then
        return true;        
    }
    
    //_________________________________________ Output Functions ____________________________________________//    
    
    function getMaturityAge()    //Returns the maturity age of a specified product
    {
        var Res = new String();
        postHTTP('validate=MATAGE');

        Res = xhttp.responseText;
        dispAlert(Res);
        return Res;
    }
    
    function getAgeWithRider(isJoint)    //Returns the maturity age of a specified product
    {
        var Res = new String();
        postHTTP('validate=AgeWithRider');

        Res = xhttp.responseText;
        dispAlert(Res);
        
        if (isJoint)
            return Res.split('~')[1];
        else
            return Res.split('~')[0];
    }
    
    function getLLGCL(Age, Rider, CL){
        if (Age == "") return;
        var Res = new String();
        postHTTP('validate=LLGCoverLevel&External=yes&Rider=' + Rider + '&Age='+Age);
        
        Res = xhttp.responseText;
        dispAlert(Res);
        
        if(CL == 'Minimum')
            return Res.split('~')[0]
        
        if(CL == 'Standard')
            return Res.split('~')[1]
            
        if(CL == 'Maximum')
            return Res.split('~')[2]
    }        
        
    function getPremiumHCB(){
        var Res = new String();
        postHTTP('validate=PremiumHCB');

        Res = xhttp.responseText;
        dispAlert('MAX Premium for HCB : ' + Res);
	    return Res    
    }
    
    function getSumAssuredADD(){
        var Res = new String();
        postHTTP('validate=Sum Assured ADD');

        Res = xhttp.responseText;
        dispAlert('MAX SA for ADD : ' + Res);
	    return Res
    }
    
    function getSumAssuredCIPTD(){
        var Res = new String();
        postHTTP('validate=Sum Assured CIPTD');

        Res = xhttp.responseText;
        dispAlert('MAX SA for CIPTD : ' + Res);
	    return Res    
    }
    
    
    //_________________________________________ System Configuration____________________________________________//    
    function dispAlert(Res){
        if (debugMode == true)
            alert(Res);
    }
    
    function setDebugMode(B){
        debugMode = B;
    }
    
    function getInt(val){
        if (isNaN(parseInt(val)))
            return 0
        return parseInt(val);
    }
    
    function getFloat(val){
        if (isNaN(parseFloat(val)))
            return 0
        return parseFloat(val);
    }
    
    function max(val1, val2){
        if (getFloat(val1) > getFloat(val2))
            return val1
        return val2
    }
    
    function min(val1, val2){
        if (getFloat(val1) < getFloat(val2))
            return val1
        return val2
    }
