with( AXANZ ){

AXANZ.quotationCalculator = function(){
    var data = AXANZ.quotationCalculatorData;
    var sInitialCover = decimal( data.curInitialCover ).toCommaString();

    setFieldToCalculate( document.getElementById( "tfLifeSum" ), sInitialCover);
    setFieldToCalculate( document.getElementById( "tfDisableSum" ), sInitialCover );
    setFieldToCalculate( document.getElementById( "tfTraumaSum" ), sInitialCover );
    setFieldToCalculate( document.getElementById( "tfAge" ), "" );

    clickableFields = [ 
        "rbMale", "rbFemale", "rbNonSmoker", "rbSmoker", "rbFortnightly", "rbMonthly", "rbQuarterly", "rbAnnual",
        "rbFixedAge", "rbFixedDuration", "rbStepped", "rbDisableA", "rbDisableB",
        "rbDisableD", "rbDisableE", "cbDisable", "cbTrauma"
        ];
        
    for( f in clickableFields ){
        setClickToCalculate( document.getElementById( clickableFields[ f ] ));
    }  
/*    
    for( opt in data.arrQuickOptions ){
        var linkOption = document.createElement( "a" );
        linkOption.href = "javascript:AXANZ.quotationCalculator.setCover( " + data.arrQuickOptions[ opt ] + " )";
        linkOption.className = "text";
        linkOption.innerHTML = "[$" + decimal( data.arrQuickOptions[ opt ] , 0 ).toCommaString() + "]";
        document.getElementById( "pLifeOptions" ).appendChild( linkOption );
    
    }
*/      

    document.getElementById( "premTotal" ).innerHTML = "$0.00";
    document.getElementById( "spanMinDisableAge" ).innerHTML = data.iDisablementMinimumAge;
    document.getElementById( "spanMinTraumaAge" ).innerHTML = data.iTraumaMinimumAge;
    document.getElementById( "spanFixedLife" ).innerHTML = data.iLifeFixedStop;
    document.getElementById( "spanFixedDisable" ).innerHTML = data.iDisableFixedStop;
    document.getElementById( "spanFixedTrauma" ).innerHTML = data.iTraumaFixedStop;
    document.getElementById( "spanFixedDuration" ).innerHTML = data.iFixedDuration;
    document.getElementById( "spanMaxEntry1" ).innerHTML = data.iLifeFixedStop - data.iFixedDuration;
    document.getElementById( "spanMaxEntry2" ).innerHTML = data.iLifeFixedStop - data.iFixedDuration;
    document.getElementById( "spanMaxDisableFixedEntry" ).innerHTML = data.iDisableFixedStop - data.iFixedDuration;
    document.getElementById( "spanMaxTraumaFixedEntry" ).innerHTML = data.iTraumaFixedStop - data.iFixedDuration;
    
    
    return { setCover : setCover };

    function setCover( v ){
        s = decimal( v ).toCommaString();
        document.getElementById( "tfLifeSum" ).value = s;
        document.getElementById( "tfTraumaSum" ).value = s;
        document.getElementById( "tfDisableSum" ).value = s;
        calculate();
    }
    
    
    function calculate(){
        var iAge = getNumber( "tfAge" );
        
        if( iAge >= data.iLifeMinimumAge && iAge <= data.iMaximumAge ){
            doCalculate( iAge );
        }
        else{
            makeBlank( "premLife" );
            makeBlank( "premDisable" );
            makeBlank( "premTrauma" );
            makeBlank( "premPolicyFee" );
            makeBlank( "premTotal" );
            makeBlank( "premFrequency" );
        
            if( iAge > 0 ){
                document.getElementById( "divErrors" ).innerHTML = 
                    "<div>This calculator calculates premiums for people aged between " + data.iLifeMinimumAge 
                  + " and " + data.iMaximumAge + ".  If you require life insurance for someone outside of this " 
                  + "range, please contact one of our advisers.</div>"; 
                document.getElementById( "divErrors" ).style.display = "";
            }
            else{   
                document.getElementById( "divErrors" ).style.display = "none";
            }    
        }    
    }
    
    
    function doCalculate( iAge ){    
    
        var htmErrors = "";
        var curLifePremium = decimal( 0, 2 );
        var curDisablePremium = decimal( 0, 2 );
        var curTraumaPremium = decimal( 0, 2 );
        var curLifeSum = getNumber( "tfLifeSum" );
        var curDisableSum = getNumber( "tfDisableSum" );
        var curTraumaSum = getNumber( "tfTraumaSum" );
    
        // Calculate life premium
        
    
        if( iAge > data.iLifeFixedStop - data.iFixedDuration && ! isChecked( "rbStepped" ) ){
            htmErrors += "<div>The life insurance option that you have selected is only available to people aged "
                       + ( data.iLifeFixedStop - data.iFixedDuration )
                       + " and below.  Please choose the &ldquo;increasing premiums&rdquo; option.</div>";
                       
            makeBlank( "premLife" );           
        }
        else if( curLifeSum < data.curMinimumCover ){
            htmErrors += "<div>The minimum amount of life cover available is $ " 
                       + decimal( data.curMinimumCover ).toCommaString() + ".  Please enter an amount of "
                       + "at least this much.</div>";
                       
            makeBlank( "premLife" );           
        }
        else{    

            var valPremiumLoading =  isChecked( "rbFortnightly" ) ? data.valFortnightlyLoading :
                                         isChecked( "rbMonthly" ) ? data.valMonthlyLoading :
                                         isChecked( "rbQuarterly" ) ? data.valQuarterlyLoading : 1;

            var arrLifeOption = isChecked( "rbFixedAge" ) ? data.arrLifeFixedAge :
                                    isChecked( "rbFixedDuration" ) ? data.arrLifeFixedDuration : data.arrLifeStepped;
                                
            var arrMultipliers = chooseSexSmoker( arrLifeOption );
            
            var curAnnualPremiumThousandths = ( curLifeSum > data.curLifeRateCutoff ? 
                   decimal( arrMultipliers.low[ iAge ] ).times( data.curLifeRateCutoff, 2 ).plus( 
                      decimal( arrMultipliers.high[ iAge ] ) .times( curLifeSum - data.curLifeRateCutoff, 2 )) :                   
                           decimal( arrMultipliers.low[ iAge ] ).times( curLifeSum, 2 )).times( valPremiumLoading, 2 ); 

            curLifePremium = toInstallment( curAnnualPremiumThousandths );
            curLifePremium.writeDollarsTo( "premLife" );
        }

        // Calculate disablement premium
        
        
        if( ! isChecked( "cbDisable" ) ){
            makeBlank( "premDisable" );
        }  
        else if( iAge < data.iDisablementMinimumAge ){
            htmErrors += "<div>Disablement protection is only available to people aged " + data.iDisablementMinimumAge + " and over.</div>";
            makeBlank( "premDisable" );           
        }  
        else if( iAge >= data.iDisableFixedStop && isChecked( "rbFixedAge" ) ){
            htmErrors += "<div>The premium option that you have selected is only available for disablement "
                        + "protection to people aged " + ( data.iDisableFixedStop - 1 ) + " and below.  "
                        + "If you require disablement protection, "
                        + "please choose the &ldquo;increasing premiums&rdquo; option.</div>";
                    
            makeBlank( "premDisable" );           
        }
        else if( iAge > data.iDisableFixedStop - data.iFixedDuration && isChecked( "rbFixedDuration" ) ){
            htmErrors += "<div>The premium option that you have selected is only available for disablement "
                        + "protection to people aged " + ( data.iDisableFixedStop - data.iFixedDuration ) 
                        + " and below.  If you require disablement protection, "
                        + "please choose a different premium option.</div>";
            document.getElementById( "premDisable" ).innerHTML = "&nbsp;";           
        }
        else if( curDisableSum > curLifeSum ){
            htmErrors += "<div>The cover amount for disablement protection must be the same or less than the amount "
                       + "for life cover.  If you need disablement cover for a higher sum, please contact one of "
                       + "our advisers.</div>";
            makeBlank( "premDisable" );           
        }
        else if( curDisableSum < data.curMinimumCover || curDisableSum >  data.curMaximumDisablement ){
            htmErrors += "<div>The cover amount for disablement protection must be between $"
                       + decimal( data.curMinimumCover ).toCommaString() + " and $"
                       + decimal( data.curMaximumDisablement ).toCommaString() 
                       + ".  Please enter an amount within this range.</div>";
            makeBlank( "premDisable" );           
        }
        
        else{        
            var arrDisableOption = isChecked( "rbFixedAge" ) ? data.arrDisableFixedAge :
                                        isChecked( "rbFixedDuration" ) ? data.arrDisableFixedDuration : data.arrDisableStepped;
                                        
            var valDisablementLoadingFactor = isChecked( "rbDisableA" ) ? data.valDisableLoadA :
                                                  isChecked( "rbDisableB" ) ? data.valDisableLoadB :
                                                      isChecked( "rbDisableD" ) ? data.valDisableLoadD :  
                                                         data.valDisableLoadE;
                                            
            var valDisablementLoading = ( 1 + valDisablementLoadingFactor ) * valPremiumLoading;
            
            var valDisablementRate = decimal( chooseSexSmoker( arrDisableOption )[ iAge ] );
            
            if( isChecked( "cbTrauma" ) && curTraumaSum > 0 )
            {
                var discountFactor = curTraumaSum >= curDisableSum  ?  
                         data.valDisableWithTrauma :
                         ( data.valDisableWithTrauma * curTraumaSum + curDisableSum - curTraumaSum ) / curDisableSum;
                valDisablementRate = valDisablementRate.times( discountFactor, 2 );
            }                                
                    
            var curAnnualDisablementThousandths = valDisablementRate.times( curDisableSum * valDisablementLoading );
                      
            curDisablePremium = toInstallment( curAnnualDisablementThousandths );          
            curDisablePremium.writeDollarsTo( "premDisable" );
        }

        // Calculate trauma premium
        
        if( ! isChecked( "cbTrauma" ) ){
            makeBlank( "premTrauma" );
        }   
        else if( iAge < data.iTraumaMinimumAge ){
            htmErrors += "<div>Trauma protection is only available to people aged " + data.iTraumaMinimumAge + " and over.</div>";
            makeBlank( "premTrauma" );           
        }  
        else if( iAge >= data.iTraumaFixedStop && isChecked( "rbFixedAge" ) ){
            htmErrors += "<div>The premium option that you have selected is only available for trauma "
                        + "protection to people aged " + ( data.iTraumaFixedStop - 1 ) 
                        + " and below.  If you require trauma protection, "
                        + "please choose the &ldquo;increasing premiums&rdquo; option.</div>";
                    
            makeBlank( "premTrauma" );
        
        }
        else if( iAge > data.iTraumaFixedStop - data.iFixedDuration && isChecked( "rbFixedDuration" ) ){
            htmErrors += "<div>The premium option that you have selected is only available for trauma "
                        + "protection to people aged " + ( data.iTraumaFixedStop - data.iFixedDuration )
                        + " and below.  If you require trauma protection, "
                        + "please choose a different premium option.</div>";
            makeBlank( "premTrauma" );           
        }
        else if( curTraumaSum > curLifeSum ){
            htmErrors += "<div>The cover amount for trauma protection must be the same or less than the amount for life "
                       + "cover.  If you need trauma cover for a higher sum, please contact one of our advisers.</div>";
            makeBlank( "premTrauma" );           
        }
        else if( curTraumaSum < data.curMinimumCover || curTraumaSum > data.curMaximumTrauma ){
            htmErrors += "<div>The cover amount for trauma protection must be between $"
                       + decimal( data.curMinimumCover ).toCommaString() + " and $"
                       + decimal( data.curMaximumTrauma ).toCommaString() 
                       + ".  Please enter an amount within this range.</div>";
            makeBlank( "premTrauma" );           
        }
        else{
        
            var arrTraumaOption = isChecked( "rbFixedAge" ) ? data.arrTraumaFixedAge :
                                       isChecked( "rbFixedDuration" ) ? data.arrTraumaFixedDuration : data.arrTraumaStepped;
            var valBasicRate =  decimal( chooseSexSmoker( arrTraumaOption )[ iAge ] );       

            var curAnnualTraumaThousandths =  ( curTraumaSum < data.curTraumaDiscountCutoff ? 
                                                             valBasicRate : 
                                                             valBasicRate.minus( decimal( data.valTraumaDiscountRate ))).times( curTraumaSum * valPremiumLoading, 2 );
            curTraumaPremium = toInstallment( curAnnualTraumaThousandths );                                                 
            curTraumaPremium.writeDollarsTo( "premTrauma" );
        
        }

        // Policy fee and total
        
        var curPolicyFee = decimal( isChecked( "rbAnnual" ) ? data.curAnnualPolicy :
                               isChecked( "rbQuarterly" ) ? data.curQuarterlyPolicy : 
                               isChecked( "rbMonthly" ) ? data.curMonthlyPolicy : data.curFortnightlyPolicy );

        
        curPolicyFee.writeDollarsTo( "premPolicyFee" );
        curLifePremium.plus( curDisablePremium ).plus( curTraumaPremium ).plus( curPolicyFee ).writeDollarsTo( "premTotal" );
        document.getElementById( "premFrequency" ).innerHTML = isChecked( "rbAnnual" ) ? "annually" :
                               isChecked( "rbQuarterly" ) ? "quarterly" : 
                               isChecked( "rbMonthly" ) ? "monthly" : "fortnightly"; 
        
        
        if( htmErrors == "" ){
            document.getElementById( "divErrors" ).style.display = "none";
        }
        else{
            document.getElementById( "divErrors" ).innerHTML = htmErrors;
            document.getElementById( "divErrors" ).style.display = "";
        }
        
        
    }
    
    function chooseSexSmoker( arrMultipliers ){
        var bFemale = isChecked( "rbFemale" );
        var bSmoker = isChecked( "rbSmoker" );
        return bFemale ? ( bSmoker ? arrMultipliers.femaleSmoker : arrMultipliers.femaleNS ) : 
                   ( bSmoker ? arrMultipliers.maleSmoker : arrMultipliers.maleNS );
    }
    
    function toInstallment( thousandths ){
        if( isChecked( "rbFortnightly" ) ){
            return thousandths.divideRound( 26000 );
        }
        else{
            return thousandths.divideRound( 12000 ).times( 
                                     isChecked( "rbAnnual" ) ? 12 : isChecked( "rbQuarterly" ) ? 3 : 1  );
        }
    }  
      
      
    function setFieldToCalculate( field, val )
    {
        if( field.addEventListener ){
            field.addEventListener( "keypress", enterNumbers, false );
            field.addEventListener( "change", calculate, false );
            field.addEventListener( "focus", selectTarget, false );
        }
        else if( field.attachEvent ){
            field.attachEvent( "onkeypress", enterNumbers );
            field.attachEvent( "onchange", calculate );
            field.attachEvent( "onfocus", selectTarget );
        }
        field.value = val;
    } 

    function setClickToCalculate( field ){
        if( field.addEventListener ){
            field.addEventListener( "click", calculate, false );
        }
        else if( field.attachEvent ){
            field.attachEvent( "onclick", calculate );
        }
    }

    function enterNumbers( e ){
        k = cancelNonNumeric( e );
        if( k == 13 ){
            cancelEvent( e );
            calculate();
        }
    }
}();

}
