function calculateVat(){
  if ($('#starting-figure').val()){
    var startingValue = Number($('#starting-figure').val());
    var net = 0.0;
    var vat = 0.0;
    var gross = 0.0;
    var vatRate = 0.0;
    ($("input[name='vat-rate']:checked").val() == 'standard-17') ?  vatRate = 0.175 : (($("input[name='vat-rate']:checked").val() == 'standard-15') ? vatRate = 0.15 : vatRate = 0.05);

    if ($("input[name='vat-included']:checked").val() == 'Y'){
      var vatRatio = 0;
      ($("input[name='vat-rate']:checked").val() == 'standard-17') ?  vatRatio = (7/47) : (($("input[name='vat-rate']:checked").val() == 'standard-15') ? vatRatio = (3/23) : vatRatio = (1/21));
      
      net = startingValue / (1+vatRate);
      vat = startingValue * vatRatio;
    } else {
      net = startingValue; 
      vat = net * vatRate;      
    }
    gross = net + vat; 
    $('#starting-figure').val(startingValue.toFixed(2));
    $("input[name='net-result']").val(net.toFixed(2));
    $("input[name='vat-result']").val(vat.toFixed(2));
    $("input[name='gross-result']").val(gross.toFixed(2));
  }
}

function calculateFlatRateVat(){
  if ($('#total-sales').val() && $('#flat-rate').val()){
    var totalSales = Number($('#total-sales').val());
    var flatRate = Number($('#flat-rate').val());
    var vatDue = 0.0;
    
    if ($("input[name='first-year']:checked").val() == 'Y'){
      vatDue = totalSales * ((flatRate - 1) / 100);
    } else {
      vatDue = totalSales * (flatRate / 100);
    }
    
    $('#total-sales').val(totalSales.toFixed(2));
    $('#flat-rate').val(flatRate.toFixed(2));
    $("input[name='vat-due']").val(vatDue.toFixed(2));
  }
}

function calculateStampDuty(){
  if ($('#shares').val() || $('#residential-land').val() || $('#non-residential-land').val()){
    var shares = Number($('#shares').val());
    var residentialLand = Number($('#residential-land').val());
    var nonResidentialLand = Number($('#non-residential-land').val());
    
    var payable = 0.0;
    var considerationAmount = shares + residentialLand + nonResidentialLand;
    
    var sharesTax = shares * (5/1000);
    sharesTax = Math.ceil(sharesTax / 5.0) * 5;
    (sharesTax == 5) ? sharesTax = 0 : sharesTax = sharesTax;
    
    var residentialLandTax = 0.0
    
    if(residentialLand <= 250000 && residentialLand > 175000){
      residentialLandTax = residentialLand * (1/100);
    } else if(residentialLand > 250000 && residentialLand <= 500000){
      residentialLandTax = residentialLand * (3/100);
    } else if(residentialLand > 500000){
      residentialLandTax = residentialLand * (4/100);      
    }
    residentialLandTax = Math.ceil(residentialLandTax / 5.0) * 5;
    (residentialLandTax == 5) ? residentialLandTax = 0 : residentialLandTax = residentialLandTax;
    
    var nonResidentialLandTax = 0.0;
    
    if(nonResidentialLand <= 250000 && nonResidentialLand > 175000){
      nonResidentialLandTax = nonResidentialLand * (1/100);
    } else if(nonResidentialLand > 250000 && nonResidentialLand <= 500000){
        nonResidentialLandTax = nonResidentialLand * (3/100);
    } else if(nonResidentialLand > 500000){
      nonResidentialLandTax = nonResidentialLand * (4/100);      
    } 
    nonResidentialLandTax = Math.ceil(nonResidentialLandTax / 5.0) * 5; 
    (nonResidentialLandTax == 5) ? nonResidentialLandTax = 0 : nonResidentialLandTax = nonResidentialLandTax;
    
    payable = sharesTax + residentialLandTax + nonResidentialLandTax;
    
    $('#shares').val(shares.toFixed(2));
    $('#residential-land').val(residentialLand.toFixed(2));
    $('#non-residential-land').val(nonResidentialLand.toFixed(2));
    
    $("input[name='consideration-amount']").val(considerationAmount.toFixed(2));
    $("input[name='stamp-taxes']").val(payable.toFixed(2));
  }
}

function calculatePayslip() {
  if ($('#gross-year').val() && ($('#gross-monthly').val() || $('#gross-weekly').val()) && $('#tax-code').val()){
    
  }
}


jQuery(document).ready(function(){
  jQuery("input[type='text']").css('width', '90px');
  
  jQuery('#calculate_vat').click(function(){
    calculateVat();
  });
  
  jQuery('#calculate_flat_rate').click(function(){
    calculateFlatRateVat();
  });
  
  jQuery('#calculate_stamp_duty').click(function(){
    calculateStampDuty();
  });
  
  jQuery('#payslip-calculator input[type="text"]').keyup(function(){
    calculatePayslip();
  });
  
  jQuery('#payslip-calculator input[type="radio"]').click(function(){
    calculatePayslip();
  });
  
  jQuery('#gross-monthly').keyup(function(){
    jQuery('#gross-weekly').val('');
  });
  
  jQuery('#gross-weekly').keyup(function(){
    jQuery('#gross-monthly').val('');
  });
});

