String.prototype.to_i = function() {
  return parseInt( this.gsub(/[^0-9\.]/, ''), 10 );
};


function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        radioGroup = $(el).name;
        el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}



var TaxCalculator = Behavior.create({

  initialize : function(opts) {
    var self = this;
    self.calculate();
    new Form.Observer( this.element, 0.5, self.calculate.bind(self) );
  },

  formatPrice : function(number,decimalPoints) {
  	if(!(number>0)) return '—';
  	if(!decimalPoints) return Math.round(number);
  	var num = Math.round((number * Math.pow(10,decimalPoints))).toString();
  	return num.slice(0,-1*decimalPoints) + "." + num.slice(-1*decimalPoints);
  },

  calculate : function() {
    this.purchase_price = $F('purchase_price').to_i();
    this.property_status = $RF(this.element, 'property_status');
    this.first_time_buyer = $F('first_time_buyer');
    this.calculate_hst();
    this.calculate_ptt();
  },

  calculate_hst : function() {
    var property_status = this.property_status;
    var bc_hst, bc_hst_rebate, ca_hst, ca_hst_rebate, net_hst = 0;
    var total_price = this.purchase_price;

    if ( this.property_status == 'New' ) {
      bc_hst        = this.bc_hst();
      bc_hst_rebate = this.bc_hst_rebate();
      ca_hst        = this.ca_hst();
      ca_hst_rebate = this.ca_hst_rebate();
      net_hst       = (bc_hst - bc_hst_rebate) + (ca_hst - ca_hst_rebate);
      total_price   = this.purchase_price + net_hst;
    }

    this.element.bc_hst.value = this.formatPrice(bc_hst,0);
    this.element.ca_hst.value = this.formatPrice(ca_hst,0);
    this.element.bc_hst_rebate.value = this.formatPrice(bc_hst_rebate,0);
    this.element.ca_hst_rebate.value = this.formatPrice(ca_hst_rebate,0);
    this.element.net_hst.value = this.formatPrice(net_hst,0);
    this.element.total_price.value = this.formatPrice(total_price,0);
  },

  bc_hst : function() {
    return this.purchase_price * TaxCalculator.BC_HST_RATE;
  },

  bc_hst_rebate : function() {
    if ( this.purchase_price > TaxCalculator.BC_HST_REBATE_ALLOWABLE_LIMIT ) {
      return TaxCalculator.BC_HST_MAX_REBATE;
    } else {
      return this.bc_hst() * TaxCalculator.BC_HST_REBATE_RATE;
    }
  },

  ca_hst : function() {
    return this.purchase_price * TaxCalculator.CA_HST_RATE;
  },

  ca_hst_rebate : function() {
    if (this.purchase_price <= TaxCalculator.CA_HST_REBATE_ALLOWABLE_LIMIT) {
      return this.ca_hst() * TaxCalculator.CA_HST_REBATE_RATE;
    } else {
      if ( this.purchase_price > TaxCalculator.CA_HST_REBATE_ALLOWABLE_LIMIT && this.purchase_price <= TaxCalculator.CA_HST_REBATE_PRORATED_LIMIT ) {
        return ( (TaxCalculator.CA_HST_REBATE_PRORATED_LIMIT - this.purchase_price) / 100000 ) * TaxCalculator.CA_HST_MAX_REBATE;
      } else {
        return 0;
      }
    }
  },

  calculate_ptt : function() {
    var purchase_price = this.purchase_price;
    var first_time_buyer = this.first_time_buyer;
    var ptt             = 0;
    var ptt_exemption   = 0;
    var net_ptt_payable = 0;

    if ( purchase_price <= TaxCalculator.PTT_RATE_THRESHHOLD ) {
      ptt = purchase_price * TaxCalculator.PTT_RATE_1;
    } else {
      var remainder = purchase_price - TaxCalculator.PTT_RATE_THRESHHOLD;
      ptt = TaxCalculator.PTT_RATE_1 * TaxCalculator.PTT_RATE_THRESHHOLD;
      ptt = ptt + (TaxCalculator.PTT_RATE_2 * remainder);
    }

    if ( first_time_buyer ) {
      ptt_exemption = ptt;
    }

    if (purchase_price > TaxCalculator.PTT_REBATE_ALLOWABLE_LIMIT ) {
      ptt_exemption = ptt * ((TaxCalculator.PTT_REBATE_ALLOWABLE_LIMIT + TaxCalculator.PTT_REBATE_PRORATED - purchase_price) / TaxCalculator.PTT_REBATE_PRORATED);
      ptt_exemption = Math.max( 0, ptt_exemption );
    }
    net_ptt_payable = ptt - ptt_exemption;

    this.element.ptt.value = this.formatPrice(ptt,0);
    this.element.ptt_exemption.value = this.formatPrice(ptt_exemption,0);
    this.element.net_ptt_payable.value = this.formatPrice(net_ptt_payable,0);
  }

});

TaxCalculator.BC_HST_RATE                   = 0.07;
TaxCalculator.BC_HST_REBATE_RATE            = 0.7143;
TaxCalculator.BC_HST_REBATE_ALLOWABLE_LIMIT = 525000;
TaxCalculator.BC_HST_MAX_REBATE             = 26250;

TaxCalculator.CA_HST_RATE                   = 0.05;
TaxCalculator.CA_HST_REBATE_RATE            = 0.36;
TaxCalculator.CA_HST_REBATE_ALLOWABLE_LIMIT = 350000;
TaxCalculator.CA_HST_REBATE_PRORATED_LIMIT  = 450000;
TaxCalculator.CA_HST_MAX_REBATE             = 6300;

TaxCalculator.PTT_RATE_1                 = 0.01;
TaxCalculator.PTT_RATE_2                 = 0.02;
TaxCalculator.PTT_RATE_THRESHHOLD        = 200000;
TaxCalculator.PTT_REBATE_ALLOWABLE_LIMIT = 425000;
TaxCalculator.PTT_REBATE_PRORATED        = 25000;

