/**************************************************************************************/
            //Calculates the tax working from income to tax & writes to the form
            var fname
            function taxfwd()
            {
                var inc=document.getElementById("income").value;   //gets the income entered in the form
                var age = getRadioVal(document.form1.age);         //gets the value of the selected radio button
                var tax=Math.round(taxcalc(inc,age)*100)/100;
                //checks whether the tax figure is positive - returns text if negative
                if (tax<=0)
                {
                    document.getElementById("taxbox").value="No Tax Payable";
                    tax=0;            //tax is less than 0
                }
                else
                {
                    document.getElementById("taxbox").value=tax; //return the tax in the taxbox on the form
                }
                document.getElementById("netincome").value=inc-tax;  //returns the after tax income to the form

            }
            /**************************************************************************************/
            function taxcalc(inc,age)
            {
                /*Tax tables for 2010
           var threshold=new Array(0,132000,210000,290000,410000,525000);
           var percent=new Array(0.18,0.25,0.3,0.35,0.38,0.4);
           var basetax=new Array(0,23760,43260,67260,109260,152960);
           var reint=new Array(9756,15156,21000,30000); //rebate, over65 rebate, interest exempt, over65 interest
                 */
                include (fname);
                //Returns the position to use in array threshold, percent, basetax
                if (inc<threshold[1]) {pos=0};
                if (inc>=threshold[1]&& inc<threshold[2]) {pos=1};
                if (inc>=threshold[2]&& inc<=threshold[3]) {pos=2};
                if (inc>=threshold[3]&& inc<=threshold[4]) {pos=3};
                if (inc>=threshold[4]&& inc<=threshold[5]) {pos=4};
                if (inc>threshold[5]) {pos=5};
                //tax formula
                return((inc-threshold[pos])*percent[pos]+basetax[pos]-reint[age]);
            }


            /**************************************************************************************/
            /* Returns the value of the selected radio button */
            function getRadioVal(radioObj)
            {
                var value = "";
                if (!radioObj.length)
                {
                    if (radioObj.checked) value = radioObj.value;
                }
                else
                {
                    for (var i = 0; i < radioObj.length; i++)
                    {
                        if (radioObj[i].checked)
                        {
                            value = radioObj[i].value;
                            break;
                        }
                    }
                }
                return value;
            }
            /**************************************************************************************/
            //Calculate the tax starting with the take home pay & working back to income
            function taxbwd()
            {
                var netinc=(document.getElementById("netincome").value)*1;   //gets the net income entered in the form
                var age = getRadioVal(document.form1.age);         //gets the value of the selected radio button
                //check the difference between the calculated net income and entered net income
                var srt = taxcalc(netinc,age)/netinc   //get a starting point based on the tax rate
                var inc=netinc/(1-srt)
                var calcinc=inc-taxcalc(inc,age);
                //loops through the tax calc until the calc net income and netinc are the same
                while (Math.round((netinc-calcinc)*1000)/1000>0)
                {
                    inc=inc+netinc-calcinc;
                    calcinc=inc-taxcalc(inc,age);
                }
                if(netinc>inc)
                {
                    inc=netinc;
                    var tax=0;
                }
                else
                {
                    tax=Math.round(taxcalc(inc,age)*100)/100;
                }

                inc=Math.round(inc*100)/100;
                if (tax<=0)
                {
                    document.getElementById("taxbox").value="No Tax Payable";
                }
                else
                {
                    document.getElementById("taxbox").value=tax; //return the tax in the taxbox on the form
                }
                document.getElementById("income").value=inc;  //returns the before tax income to the form

            }
            function loadvars()
            {
                fname=document.getElementById("year").value
                taxcalc()
            }


            //this function includes all necessary js files for the application
            function include(file)
            {
                var script  = document.createElement('script');
                script.src  = file;
                script.type = 'text/javascript';
                script.defer = true;
                document.getElementsByTagName('head').item(0).appendChild(script);
            }

            /**************************************************************************************/    
            function taxfwdtoggl()
            {
                var inc=document.getElementById("income").value;   //gets the income entered in the form
                var age = getRadioVal(document.form1.age);         //gets the value of the selected radio button
                var tax=Math.round(taxcalc(inc,age)*100)/100;
                //checks whether the tax figure is positive - returns text if negative
                if (tax<=0)
                {
                    document.getElementById("taxbox").value="No Tax Payable";
                    tax=0;            //tax is less than 0
                }
                else
                {
                    document.getElementById("taxbox").value=tax; //return the tax in the taxbox on the form
                }
                document.getElementById("answer").style.display='block';
                document.getElementById("data").style.display='none';
            }
            /**************************************************************************************/    
            function toggl()
            {
                document.getElementById("answer").style.display='none';
                document.getElementById("data").style.display='block';            }

