//                           +++++++++++++++++++++++++++++++++++
//                           (C) Copyright 2003 - 2008 Niro Solutions Pty Limited ABN 89 106 967 140
//			+++++++++++++++++++++++++++++++++++
//			Version: July 2008
//			+++++++++++++++++++++++++++++++++++
//			Original author: Miles Glisovic
//			Last update updated by: Roland Beerling
//			Modification date: 25/08/08
//			+++++++++++++++++++++++++++++++++++

var LOWER_INCOME_LIMIT = 31920
var UPPER_LIMIT_FACTOR = 30000
var UPPER_INCOME_LIMIT = (parseInt(LOWER_INCOME_LIMIT, 10) + parseInt(UPPER_LIMIT_FACTOR, 10))//58000
var MAX_COCONT = 1000
var REDUCTION_RATE = 0.03333
var MIN_RANGE = 0.05
var MAX_RANGE = 20
var _nbsp = "&nbsp;"
var MIN_AFTERTAX = 0.01
var MAX_AFTERTAX150K = 150000
var MIN_PCTG = 0.01;
var MAX_PCTG = 100; //%
//var MIN_DOLLAR = 0;
var TotalIncome = 0
var TotalCont = 0
var CoCont = 0
var MaxCoCont = 0
var MaxCont = 0
var MIN_INCOME = 5000//p.a.
var MAX_INCOME = UPPER_INCOME_LIMIT - 1;
//var MIN_PCTG = 0;
//var MAX_PCTG = 100;
var MIN_SALARY_EMPL = 0;
var MAX_SALARY_EMPL = UPPER_INCOME_LIMIT - 1; //pa
var MIN_INV_INCOME = 0;
var MAX_INV_INCOME = UPPER_INCOME_LIMIT - 1; //pa
var MIN_OTHER_INCOME = 0;
var MAX_OTHER_INCOME = UPPER_INCOME_LIMIT - 1; //pa
var MIN_FRINGE_BENEFIT = 0;
var MAX_FRINGE_BENEFIT = UPPER_INCOME_LIMIT - 1; //pa




/*150608
function CheckCont()
{
ret=true;
return ret;
}
*/


function CalcTotalIncome() {
    var f = document.forms.frmName
    var TotalYCont;

    TotalIncome = myRound(getSafeDecimalFieldValue(f.txtIncomeG) * f.cmbIncomeG.value + getSafeDecimalFieldValue(f.txtIncomeI) * f.cmbIncomeI.value + getSafeDecimalFieldValue(f.txtIncomeO) + getSafeDecimalFieldValue(f.txtIncomeF), 2)

    /*150608
    TotalCont = parseUndefinedFloat(f.txtCont.value,2) ;
    TotalCont = Math.round(TotalCont * 100)/100;
    TotalYCont = parseUndefinedFloat(f.txtCont.value * f.cmbCont.value,2) ;
    */

    if (f.rdMemberCont[0].checked) {
        TotalCont = (getSafeDecimalFieldValue(f.txtPctOfSalary) / 100.0) * (getSafeDecimalFieldValue(f.txtIncomeG) * f.cmbIncomeG.value)
        TotalCont = Math.round(TotalCont * 100) / 100;
        TotalYCont = TotalCont
    }
    else {
        TotalCont = getSafeDecimalFieldValue(f.txtDollarAmount)
        TotalCont = Math.round(TotalCont * 100) / 100;
        TotalYCont = TotalCont * f.cmbDollarAmountFreq.value
    }


    CoCont = getCoConts(TotalIncome, TotalYCont);
    CoCont = Math.round(CoCont * 100) / 100;
    MaxCoCont = getCoContMAX(TotalIncome, TotalYCont);
    MaxCoCont = Math.round(MaxCoCont * 100) / 100;

    //150608 MaxCont = (Math.ceil(MaxCoCont/COCONTAFTERTAX_FACTOR))/f.cmbCont.value
    if (f.rdMemberCont[1].checked)
        MaxCont = (Math.ceil(MaxCoCont / COCONTAFTERTAX_FACTOR)) / f.cmbDollarAmountFreq.value
    else
        MaxCont = (Math.ceil(MaxCoCont / COCONTAFTERTAX_FACTOR))


    MaxCont = Math.round(MaxCont * 100) / 100;

    if ((Math.round(MaxCoCont * 100) / 100 >= MIN_RANGE) && (Math.round(MaxCoCont * 100) / 100 <= MAX_RANGE)) {
        MaxCoCont = MAX_RANGE
    }
    if (MaxCoCont < MIN_RANGE) {
        MaxCoCont = 0
    }

    document.getElementById("IncomeTotal").innerHTML = '<B>' + formatted_toFixed1(TotalIncome,2 ,'') + '</B>';
    document.getElementById("Result1").innerHTML = '$' + _nbsp + formatted_toFixed1(TotalIncome,2,'') + '';
    document.getElementById("Result2").innerHTML = '$' + _nbsp + formatted_toFixed1((parseFloat(TotalCont.toFixed(2))),2,'')+ '';
    document.getElementById("Result2p").innerHTML = 'After-tax contribution you intend to make (' + getResultPeriod2() + ')';
    document.getElementById("Result3").innerHTML = '<b>$' + _nbsp + formatted_toFixed1((parseFloat(CoCont.toFixed(2))),2,'') + '</b>';
    document.getElementById("Result4").innerHTML = '<b>$' + _nbsp + formatted_toFixed1((parseFloat(MaxCont.toFixed(2))),2,'') + '</b>';
    document.getElementById("Result4p").innerHTML = '<b>Your ' + getResultPeriod2() + ' after-tax contribution will need to be*' + '</b>';
    document.getElementById("Result5").innerHTML = '<b>$' + _nbsp + formatted_toFixed1((parseFloat(MaxCoCont.toFixed(2))),2,'') + '</b>';

}



function validStep1(f) {
    //var ret = true;
    //ret = validIncomeEmpl(f,true);
    if (validIncomeEmpl(f, true) && validInvestIncome(f, true) && validOtherIncome(f, true) && validFringeBenefits(f, true) && validTotalIncome(f, true) && validAfterTax(f))
        return true
    else
        return false
}

function validAfterTax_OLD150608(f, field, cmb) {
    if (jsLRTrim(field.value) == "")
        return true;
    var MAX_AFTERTAX = Math.floor(MAX_AFTERTAX150K / cmb.value)
    var sAll = "Please enter personal after-tax contribution in whole numbers."
    var sMin = "After-tax contributions are capped at a maximum of $" + removeDecimalPart(MAX_AFTERTAX.toLocaleString()) + ". Please change your contribution amount to a value of $" + removeDecimalPart(MAX_AFTERTAX.toLocaleString()) + " or less"
    return (isValidValueFloat(field, getSafeDecimalFieldValue(field), MIN_AFTERTAX, MAX_AFTERTAX, sMin, sMin, sAll));
}
function validAfterTax(f) {
    var ret = false;
    if (!f.rdMemberCont[0].checked && !f.rdMemberCont[1].checked)
        alert("Please specify personal after-tax contributions.")
    else {
        var field;
        if (f.rdMemberCont[0].checked) {
            field = f.txtPctOfSalary
            adjustDecimalPoint(field)

            if (!numberValidDecimalNumber(field.value, "", false)) {
                alert("Invalid personal after-tax contribution percentage. Please try again.")
                mySelect(field)
            }
            else {
                var vv = getSafeDecimalFieldValue(field)
                if (vv < MIN_PCTG) {
                    alert("The personal after-tax contribution percentage must be at least " + formatted_toFixed1(MIN_PCTG, 2, "") + "%. Please try again.")
                    mySelect(field)
                }
                else {
                    MAX_PCTG = myMathFloor((MAX_AFTERTAX150K / (getSafeDecimalFieldValue(f.txtIncomeG) * f.cmbIncomeG.value)) * 100, 2)

                    //var s1="The personal after-tax contribution percentage of salary must be between "+MIN_PCTG+"% and "+MAX_PCTG+"%.";
                    var s1 = "The personal after-tax contributions are capped at a maximum of $" + removeDecimalPart(MAX_AFTERTAX150K.toLocaleString()) + " per year. Please change the personal after-tax contribution percentage of salary to a value between " + formatted_toFixed1(MIN_PCTG, 2, "") + "% and " + formatted_toFixed1(MAX_PCTG, 2, "$") + "%."

                    ret = isDecimal_MinMax(field, MIN_PCTG, MAX_PCTG, s1, true)
                }
            }
        }
        else {
            //var MAX_DOLLAR = myMathFloor(((getSafeDecimalFieldValue(f.txtIncomeG)*f.cmbIncomeG.value)*(MAX_PCTG/100.0))/f.cmbDollarAmountFreq.value,2)

            var MAX_AFTERTAX = myMathFloor(MAX_AFTERTAX150K / f.cmbDollarAmountFreq.value, 2)
            var fr = getCmbText(f.cmbDollarAmountFreq, "l")
            field = f.txtDollarAmount
            adjustDecimalPoint(field)
            //var s1="The personal after-tax contribution dollar amount must be between $"+MIN_AFTERTAX.toLocaleString()+" and $"+MAX_AFTERTAX.toLocaleString()+ " per "+ fr +"."
            var s1 = "The personal after-tax contributions are capped at a maximum of $" + removeDecimalPart(MAX_AFTERTAX150K.toLocaleString()) + " per year. Please change your " + fr + "ly contribution amount to a value between " + formatted_toFixed1(MIN_AFTERTAX, 2, "$") + " and " + formatted_toFixed1(MAX_AFTERTAX, 2, "$") + "."
            ret = isDecimal_MinMax(field, MIN_AFTERTAX, MAX_AFTERTAX, s1, true)
        }
    }
    return ret;
}
function CheckIncome_OLD160608(field) {
    var f = document.forms.frmName
    field.value = parseUndefinedFloat(field.value);
    field.value = (isNaN(field.value) == true) ? 0 : field.value;

    //CalcTotalIncome()

    if (parseUndefinedFloat(f.txtIncomeG.value * f.cmbIncomeG.value, 2) +
					parseUndefinedFloat(f.txtIncomeI.value * f.cmbIncomeI.value, 2) +
					parseUndefinedFloat(f.txtIncomeF.value, 2) +
					parseUndefinedFloat(f.txtIncomeO.value, 2) > UPPER_INCOME_LIMIT) {
        //alert("As your income is greater than $58,980 you are not eligible to participate in the government's co-contribution scheme and therefore not entitled to receive any government co-contributions.")
        alert("As your income is greater than " + formatted_toFixed1(MAX_INCOME, 2, "$") + ", you are not eligible to participate in the government's co-contribution scheme and therefore not entitled to receive any government co-contributions.")

    }

}
function validTotalIncome(f, _prompt) {
    var total = myRound(getSafeDecimalFieldValue(f.txtIncomeG) * f.cmbIncomeG.value + getSafeDecimalFieldValue(f.txtIncomeI) * f.cmbIncomeI.value + getSafeDecimalFieldValue(f.txtIncomeO) + getSafeDecimalFieldValue(f.txtIncomeF), 2)

    if (total < MIN_INCOME) {
        if (_prompt) {
            alert("Your total gross annual income must be at least " + formatted_toFixed1(MIN_INCOME, 2, "$") + ". Please adjust your figures.")
            mySelect(f.txtIncomeG)
        }
        return false
    }
    else {
        if (total > UPPER_INCOME_LIMIT) {
            if (_prompt) {
                alert("Your total income has exceeded the co-contribution threshold of " + formatted_toFixed1(UPPER_INCOME_LIMIT, 2, "$") + ". You are not eligible to receive the government co-contribution.")
                mySelect(f.txtIncomeG)
            }
            return false
        }
        else
            return true
    }
}
/*
function validIncome(f, _prompt)
{
var ret = false;
if(parseUndefinedInt(f.txtIncomeG.value * f.cmbIncomeG.value,2) +
parseUndefinedInt(f.txtIncomeI.value * f.cmbIncomeI.value,2) +
parseUndefinedInt(f.txtIncomeF.value,2) +
parseUndefinedInt(f.txtIncomeO.value,2) <= 0)
{
if(_prompt)
alert("Please enter an income amount.")
mySelect(f.txtIncomeG)
}
else
ret=true;
return ret;
}
*/
function validIncomeEmpl(f, _prompt) {
    var field = f.txtIncomeG
    adjustDecimalPoint(field)
    var cmb = f.cmbIncomeG
    var _max = myMathFloor(MAX_SALARY_EMPL / cmb.value, 2)
    var _min = myMathCeil(MIN_SALARY_EMPL / cmb.value, 2)
    var sMin = "Your total income has exceeded the co-contribution threshold of " + formatted_toFixed1(UPPER_INCOME_LIMIT, 2, "$") + ". You are not eligible to receive the government co-contribution."
    /*
    if(isDecimal_MinMax(field,_min,_max,sMin,_prompt))
    return true;
    else
    {
    mySelect(field)
    return false;
    }
    */
    return (isDecimal_MinMax(field, _min, _max, sMin, _prompt))
}
function validInvestIncome(f, _prompt) {
    var field = f.txtIncomeI
    setEmptyFieldTo1(field)
    adjustDecimalPoint(field)
    var cmb = f.cmbIncomeI
    var _max = myMathFloor(MAX_INV_INCOME / cmb.value, 2)
    var _min = myMathCeil(MIN_INV_INCOME / cmb.value, 2)
    var sMin = "Your total income has exceeded the co-contribution threshold of " + formatted_toFixed1(UPPER_INCOME_LIMIT, 2, "$") + ". You are not eligible to receive the government co-contribution."
    /*
    if(isDecimal_MinMax(field,_min,_max,sMin,_prompt))
    return true;
    else
    {
    mySelect(field)
    return false;
    }
    */
    return (isDecimal_MinMax(field, _min, _max, sMin, _prompt))
}
function validOtherIncome(f, _prompt) {
    var field = f.txtIncomeO
    setEmptyFieldTo1(field)
    adjustDecimalPoint(field)
    var _max = MAX_OTHER_INCOME
    var _min = MIN_OTHER_INCOME
    var sMin = "Your total income has exceeded the co-contribution threshold of " + formatted_toFixed1(UPPER_INCOME_LIMIT, 2, "$") + ". You are not eligible to receive the government co-contribution."

    /*
    if(isDecimal_MinMax(field,_min,_max,sMin,_prompt))
    return true;
    else
    {
    mySelect(field)
    return false;
    }
    */
    return (isDecimal_MinMax(field, _min, _max, sMin, _prompt))
}
function validFringeBenefits(f, _prompt) {
    var field = f.txtIncomeF
    setEmptyFieldTo1(field)
    adjustDecimalPoint(field)
    var _max = MAX_FRINGE_BENEFIT
    var _min = MIN_FRINGE_BENEFIT
    var sMin = "Your total income has exceeded the co-contribution threshold of " + formatted_toFixed1(UPPER_INCOME_LIMIT, 2, "$") + ". You are not eligible to receive the government co-contribution."
    /*
    if(isDecimal_MinMax(field,_min,_max,sMin,_prompt))
    return true;
    else
    {
    mySelect(field)
    return false;
    }
    */
    return (isDecimal_MinMax(field, _min, _max, sMin, _prompt))
}
