/************************************************************************************    
   VERSION:  1.0

   PROGRAM:  Calculator implementing the Time-Money Equation created by
             Professor Ian Walker of Warwick University:

             V=(W((100-t)/100))/C

             Where V is an hour's value
             W is a person's hourly wage
             t is the tax rate
             C is the local cost of living

        BY:  TheDollarRule.com

     NOTES:  Incorporates 2007 US Income Tax rates and Cost of Living indices

        Released under a creative commons Attribution-ShareAlike 3.0 license:
        (http://creativecommons.org/licenses/by-sa/3.0/)

        Please credit TheDollarRule.com in any derivative work.

        You are free:
         * to Share - to copy, distribute and transmit the work
         * to Remix - to adapt the work

        Under the following conditions:
         * Attribution. You must attribute the work in the manner specified 
           by the author or licensor (but not in any way that suggests that 
           they endorse you or your use of the work).

         * Share Alike. If you alter, transform, or build upon this work, you
           may distribute the resulting work only under the same, similar or a 
           compatible license.

        For any reuse or distribution, you must make clear to others the license 
        terms of this work. The best way to do this is with a link to the web page:
          http://creativecommons.org/licenses/by-sa/3.0/


        Any of the above conditions can be waived if you get permission from the 
        copyright holder.


        Nothing in this license impairs or restricts the author's moral rights.
***********************************************************************************/

function annual2hourly (salary) {
    return (salary / 2080);
}

function hourly2annual (salary) {
    return salary * 2080;
}


function calculate() {
   var Wobj = document.getElementById('W');
   var annual_Wobj = document.getElementById('annual_W');
   var W;

   var Cobj = document.getElementById('C');
   var other_Cobj = document.getElementById('other_C');
   var C;

   var t = document.getElementById('t').value;
   var V = document.getElementById('V');

   // annual overrides hourly salary
   if (annual_Wobj.value && Wobj.value || ! Wobj.value) {

       W = annual2hourly(annual_Wobj.value);
       Wobj.value = W;
   }   
   else {
       W = Wobj.value;
   }

   if (other_Cobj.value && Cobj.value) {
       C = other_Cobj.value
   }
   else {
      C = Cobj.value;
   }
   var Cscale = 0.01;

   var result = (W * (100 - t) / (C * Cscale) ) / 100;

   V.value = result;
}

// Based on 2007 US Tax rates
function set_tax_bracket (salary) {
    var status = document.getElementById('file_status').value;
    var rate;

    if (status == "single") {
       if (salary <= 7825) {
           rate = 10;
       }
       else if (salary > 7825 && salary <= 31850) {
           rate = 15;
       }
       else if (salary > 31850 && salary <= 77100) {
           rate = 25;
       }
       else if (salary > 77100 && salary <= 160850) {
           rate = 28;
       }
       else if (salary > 160850 && salary <= 349700) {
           rate = 33;
       }
       else if (salary > 349700) {
           rate = 35;
       }
    }
    else if (status == "married_sep") {
       if (salary <= 7825) {
           rate = 10;
       }
       else if (salary > 7825 && salary <= 31850) {
           rate = 15;
       }
       else if (salary > 31850 && salary <= 64250) {
           rate = 25;
       }
       else if (salary > 64250 && salary <= 97925) {
           rate = 28;
       }
       else if (salary > 97925 && salary <= 174850) {
           rate = 33;
       }
       else if (salary > 174850) {
           rate = 35;
       }
    }
    else if (status == "married_joint") {
       if (salary <= 15650) {
           rate = 10;
       }
       else if (salary > 15650 && salary <= 63700) {
           rate = 15;
       }
       else if (salary > 63700 && salary <= 128500) {
           rate = 25;
       }
       else if (salary > 128500 && salary <= 195850) {
           rate = 28;
       }
       else if (salary > 195850 && salary <= 349700) {
           rate = 33;
       }
       else if (salary > 349700) {
           rate = 35;
       }
    }
    else if (status == "head") {
       if (salary <= 11200) {
           rate = 10;
       }
       else if (salary > 11200 && salary <= 42650) {
           rate = 15;
       }
       else if (salary > 42650 && salary <= 110100) {
           rate = 25;
       }
       else if (salary > 110100 && salary <= 178350) {
           rate = 28;
       }
       else if (salary > 178350 && salary <= 349700) {
           rate = 33;
       }
       else if (salary > 349700) {
           rate = 35;
       }
    }

    document.getElementById('t').value = rate;
}

function set_tax_bracket_hourly (hourly) {
    var annual = hourly2annual(hourly);
    document.getElementById('annual_W').value = annual;
    set_tax_bracket(annual);
}

function empty_c () {
   document.getElementById('other_C').value = "";
}

