
function rateCard() {

    this.values = new Array();
    this.lastVal = null;

    this.addVal = function _addValue(minVal, maxVal) {
        this.values[this.values.length] = new rateValue(minVal, maxVal);
        this.lastVal = this.values[this.values.length - 1];
    }

    this.getRate = function _getRate(value, period) {
        for (var i = 0; i < this.values.length; i++) {
            if (this.values[i].inRange(value)) {
                var tmp = this.values[i].getRate(period);
                if (tmp == false) return false;
                return tmp;
            }
        }
        return false;
    }

    this.frmCalc = function _frmCalc(form) {
        var tmp = this.calc(form.amt.value, form.term.value);
        if (tmp == false) {
            //(26/6/06) Removed these two lines as they cause a FireFox error and are unused
            //form.pmt.value = '';
            //form.epp.value = '';
            var min = this.minValue(form.term.value);
            var max = this.maxValue(form.term.value);
            if (form.amt.value > 20000) {
                alert('For amounts over $20,000, please call our FlexiLine Team on 0508 353945 or email customerservice@flexigroup.co.nz.');
            } else if (form.amt.value < min) {
                alert('For a term of ' + form.term.value + ' months, you must enter an amount of at least $' + min + '.');
            } else if (form.amt.value > max) {
                alert('For a term of ' + form.term.value + ' months, you must enter an amount of $' + max + ' or less.');
            } else {
            alert('Please enter a valid numeric amount e.g. 5000');
            }
        } else {
            form.pmtMonthly.value = mkcurrency(tmp);
            form.ProtectMonthly.value = mkcurrency(this.calcEpp(tmp));
            //form.pmtWeekly.value = mkcurrency(((tmp) * 12) / 52);


            // Without GST
            var GST = (form.amt.value * 3 / 23) / form.term.value;
            var NoGSTValue = tmp - GST;
            //alert(NoGSTValue);
            // Wihtout GST

            form.pmtWeekly.value = mkcurrency(NoGSTValue);
            
            
        }

        function mkcurrency(val) {
            var tmp = Math.round(val * 100).toString();
            return '$' + tmp.substr(0, tmp.length - 2) + '.' + tmp.substr(tmp.length - 2, 2);
        }
    }

    this.frmReset = function _frmCalc(form) {
        form.pmtMonthly.value = '';
        form.ProtectMonthly.value = '';
    }

    this.calc = function _calc(value, period) {
        var tmp = this.getRate(value, period);
        if (tmp == false) return false;
        //The Math.ceil was taken out to provide more accurate figures.
        //return Math.ceil(value*tmp);
        return value * tmp;
    }

    this.calcEpp = function _calcEpp(value) {
        return Math.round(value * 107) / 100;
    }

    this.minValue = function _minValue(period) {
        for (var i = 0; i < this.values.length; i++) {
            if (this.values[i].getRate(period) != false) return this.values[i].minVal;
        }
        return false;
    }

    this.maxValue = function _maxValue(period) {
        for (var i = this.values.length - 1; i >= 0; i--) {
            if (this.values[i].getRate(period) != false) return this.values[i].maxVal;
        }
        return false;
    }

    function rateValue(minVal, maxVal) {
        this.minVal = minVal;
        this.maxVal = maxVal;
        this.periods = new Array();
        this.rates = new Array();

        this.addPeriod = function _addPeriod(period, rate) {
            this.periods[this.periods.length] = period;
            this.rates[this.rates.length] = rate;
        }

        this.inRange = function _inRange(value) {
            return (value >= this.minVal && value <= this.maxVal ? true : false);
        }

        this.getRate = function _getRate(period) {
            for (var i = 0; i < this.periods.length; i++) {
                if (this.periods[i] == period) return this.rates[i];
            }
            return false;
        }
    }
}

var rates = new rateCard();


rates.addVal(500, 1277.77);
rates.lastVal.addPeriod(36, 0.041607);
rates.lastVal.addPeriod(48, 0.034859);
rates.lastVal.addPeriod(60, 0.030707);

rates.addVal(1277.78, 2299.99);
rates.lastVal.addPeriod(36, 0.041416);
rates.lastVal.addPeriod(48, 0.034591);
rates.lastVal.addPeriod(60, 0.030424);

rates.addVal(2300.00, 2811.10);
rates.lastVal.addPeriod(36, 0.040370);
rates.lastVal.addPeriod(48, 0.033474);
rates.lastVal.addPeriod(60, 0.029249);

rates.addVal(2811.11, 3322.21);
rates.lastVal.addPeriod(36, 0.040370);
rates.lastVal.addPeriod(48, 0.033474);
rates.lastVal.addPeriod(60, 0.029249);

rates.addVal(3322.22, 3833.32);
rates.lastVal.addPeriod(36, 0.039191);
rates.lastVal.addPeriod(48, 0.032221);
rates.lastVal.addPeriod(60, 0.027931);

rates.addVal(3833.33, 4599.99);
rates.lastVal.addPeriod(36, 0.039191);
rates.lastVal.addPeriod(48, 0.032221);
rates.lastVal.addPeriod(60, 0.027931);

rates.addVal(4600.00, 6133.32);
rates.lastVal.addPeriod(36, 0.038657);
rates.lastVal.addPeriod(48, 0.031654);
rates.lastVal.addPeriod(60, 0.027336);

rates.addVal(6133.33, 7666.66);
rates.lastVal.addPeriod(36, 0.037790);
rates.lastVal.addPeriod(48, 0.030737);
rates.lastVal.addPeriod(60, 0.026373);

rates.addVal(7666.67, 10222.21);
rates.lastVal.addPeriod(36, 0.037790);
rates.lastVal.addPeriod(48, 0.030737);
rates.lastVal.addPeriod(60, 0.026373);

rates.addVal(10222.22, 20000);
rates.lastVal.addPeriod(36, 0.036554);
rates.lastVal.addPeriod(48, 0.029434);
rates.lastVal.addPeriod(60, 0.025018);

	
