
// Matthew 23/2/2000
// Dave 31/05/2007
// Dave 20/02/2008

function CalcAnnualTax(annualSalary, rebateAmount) {
    var grossTax = this.baseAmount + ((annualSalary - this.threshold) * this.marginalRate);
    return (Math.max((grossTax - rebateAmount), 0));
}

function IsTaxBand(annualSalary) { return (annualSalary > this.threshold && annualSalary <= this.limit) ? true : false; }

function clsTaxBand(marginalRate, baseAmount, threshold, limit) {
    this.marginalRate = marginalRate;
    this.baseAmount = baseAmount;
    this.threshold = threshold;
    if (arguments.length > 3)
        this.limit = limit
    else
        this.limit = Number.POSITIVE_INFINITY;
    this.CalcAnnualTax = CalcAnnualTax;
    this.IsTaxBand = IsTaxBand;
}

function GetTax(bandArray, annualSalary, rebateAmount) {
    for (var i = 0; i < bandArray.length; i++)
        if (bandArray[i].IsTaxBand(annualSalary))
        return bandArray[i].CalcAnnualTax(annualSalary, rebateAmount);
}

//'ADD THIS

//current year
var aTax2000 = new Array(
new clsTaxBand(0.18, 0, 0, 140000),
new clsTaxBand(0.25, 25200, 140001, 221000),
new clsTaxBand(0.30, 45450, 221001, 305000),
new clsTaxBand(0.35, 70650, 305001, 431000),
new clsTaxBand(0.38, 114750, 431001, 552000),
new clsTaxBand(0.40, 160730, 552001));
//previous year
var aTax99 = new Array(
new clsTaxBand(0.18, 0, 0, 132000),
new clsTaxBand(0.25, 23760, 132001, 210000),
new clsTaxBand(0.30, 43260, 210001, 290000),
new clsTaxBand(0.35, 67260, 290001, 410000),
new clsTaxBand(0.38, 109260, 410001, 525000),
new clsTaxBand(0.40, 152960, 525001));





// Formats numeric values (float, integer or string) to currency symbol followed 
// by the number with thousands delimiter separated and to two decimal places (rounded to nearest)
// eg: FormatMoney('12345678.9', 'R ', ',');  returns 'R 12,345,678.90'
// Matthew 18/1/2000
function FormatMoney(num, currencySymbol, thousandsDelimiter) {
    var numStr = '' + Math.round(((typeof (num) == 'string') ? parseFloat(num, 10) : num) * 100) / 100;
    thousandsDelimiter = (typeof (thousandsDelimiter) == 'undefined') ? '' : thousandsDelimiter;
    currencySymbol = (typeof (currencySymbol) == 'undefined') ? '' : currencySymbol;
    if (numStr.length > 2) {
        if (numStr.length - numStr.indexOf('.') == 2)
            numStr += '0';
        if (numStr.length - numStr.indexOf('.') != 3)
            numStr += '.00';
    }
    else
        numStr += '.00';
    if (numStr.indexOf('.') > 3 && thousandsDelimiter != '') {
        if (numStr.substring(0, numStr.indexOf('.') - 3) != '-')
            numStr = numStr.substring(0, numStr.indexOf('.') - 3) + thousandsDelimiter + numStr.substring(numStr.indexOf('.') - 3)
        var pos = 7;
        while (numStr.indexOf(thousandsDelimiter) > 3) {
            if (numStr.substring(0, numStr.indexOf('.') - pos) != '-')
                numStr = numStr.substring(0, numStr.indexOf('.') - pos) + thousandsDelimiter + numStr.substring(numStr.indexOf('.') - pos)
            else
                return currencySymbol + numStr;
            pos += 4;
        }
    }
    return currencySymbol + numStr;
}

function ValidateFloat(valu, lo, hi, fldname) {

    if (valu == '') valu = '0.0';
    var val = parseFloat(valu, 10);
    if (val < lo || (val > hi && hi != -1)) {			// -1 = no hi limit
        alert(fldname + ' is out of Range '); 		// not between hi and lo
        return (false);
    }
    else
        return (true);
}

// 2 decimal places:
function Money(val) { return ((Math.round(val * 100)) / 100); }

//============================================================================

function removeSpaces(string) {
    var tstring = "";
    string = '' + string;
    splitstring = string.split(" ");
    for (i = 0; i < splitstring.length; i++)
        tstring += splitstring[i];
    return tstring;
}

var under65 = -1;

function DoTax(TaxForm) {

    Salary = removeSpaces(document.form1.TxtTax05.value);
    var mt99, mt2000, yt99, yt2000, ydiff, av2000, av99

    if (ValidateFloat(Salary, 0, -1, "Current Monthly Taxable Income")) {
        var val = parseFloat(Salary);
        if (val + '' == 'NaN') {
            alert('Please enter your monthly taxable income.');
            document.form1.TxtTax05.focus();
            return;
        }
        //yt99 = GetTax(aTax99, val*12, ((under65)?4140:4140 + 3000));
        //	     yt99 = GetTax(aTax99, val*12, ((under65)?7740:7740 + 4680));
        //		 yt2000 = GetTax(aTax2000, val*12, ((under65)?8280:8280 + 5040));
        yt99 = GetTax(aTax99, val * 12, ((under65) ? 9756 : 9756 + 5400));
        yt2000 = GetTax(aTax2000, val * 12, ((under65) ? 10260 : 10260 + 5675));
        av99 = (yt99 / (val * 12)) * 100;
        av2000 = (yt2000 / (val * 12)) * 100;
        document.form1.TxtResult99.value = FormatMoney((yt99 / 12), 'R ', ',');
        document.form1.TxtResult2000.value = FormatMoney((yt2000 / 12), 'R ', ',');
        document.form1.TxtYResult99.value = FormatMoney(yt99, 'R ', ',');
        document.form1.TxtYResult2000.value = FormatMoney(yt2000, 'R ', ',');
        document.form1.TxtDiff.value = FormatMoney(yt2000 - yt99, 'R ', ',');
        document.form1.TxtAverage99.value = FormatMoney(av99, '', ',') + ' %';
        document.form1.TxtAverage2000.value = FormatMoney(av2000, '', ',') + ' %';

        document.getElementById("theResults").style.display = "block";
    }
}


