﻿// Javascript File

function IsNumber(val)
{


}


function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}
    
function SetCellValue(id, value)
{
    var obj;
    obj = document.getElementById(id);
    if (obj)
        obj.innerHTML = value;
}    
    
    
// Tax limits
//taxLimit = new Array(5225, 2230, 32370);
//taxRate = new Array(0, 0.1, 0.22, 0.4);
//taxLimit = new Array(5435, 0, 36000);
//taxLimit = new Array(6035, 0, 34800);
taxLimit = new Array(6475, 0, 37400);
taxRate = new Array(0, 0, 0.2, 0.4);
totalTax = 0;

// NIC limits
//nicLimit = new Array(5200, 34840);
//nicLimit = new Array(5460, 34580);
//nicLimit = new Array(5435, 34605);
nicLimit = new Array(5715, 38160);
nicRateNorm = new Array(0, 0.11, 0.01);
nicRateUSS = new Array(0, 0.094, 0.01);
totalNic = 0;   

ussRate = 0.0635;
totalUSS = 0;

function CalcUSS(gross, withUSS)
{
    SetCellValue('C12', (ussRate*100)+'%');

    if (withUSS)
    {
        totalUSS = gross * ussRate;
        SetCellValue('D12', number_format(gross,2,'.',','));
    }
    else
    {
        totalUSS = 0;
        SetCellValue('D12', number_format(0,2,'.',','));
    }
    
    SetCellValue('E12', number_format(totalUSS,2,'.',','));
    SetCellValue('E13', number_format(totalUSS,2,'.',','));
    SetCellValue('F13', number_format(totalUSS/12,2,'.',','));
}

function CalcNIC(gross, withUSS)
{
    var nicSums = new Array();
    var nicPay = new Array();
    var fullNic = 0;
    var remain = gross;
    var i;
    var nicRate;
    
    if (withUSS)
        nicRate = nicRateUSS;
    else
        nicRate = nicRateNorm;
    
    for (i=0; i < nicLimit.length; i++)
    {
        if (remain < nicLimit[i])
            nicSums[i] = remain;
        else
            nicSums[i] = nicLimit[i];
        remain -= nicSums[i];    
        nicPay[i] = nicSums[i] * nicRate[i];
        fullNic += nicPay[i];
    }
    nicSums[nicLimit.length] = remain;
    nicPay[nicLimit.length] = nicSums[nicLimit.length] * nicRate[nicLimit.length];
    fullNic += nicPay[nicLimit.length];

    // Print out
    for (i=0; i < nicSums.length; i++)
    {
        id='B'+(i+8);
        if (i < nicLimit.length)
            SetCellValue(id, number_format(nicLimit[i],2,'.',','));
        id='C'+(i+8);
        if (i < nicRate.length)
            SetCellValue(id, (nicRate[i]*100)+'%');
        id='D'+(i+8);
        SetCellValue(id, number_format(nicSums[i],2,'.',','));
        id = 'E'+(i+8);
        SetCellValue(id, number_format(nicPay[i],2,'.',','));
    }
    id = 'E'+(i+8);
    SetCellValue(id, number_format(fullNic,2,'.',','));
    id = 'F'+(i+8);
    SetCellValue(id, number_format(fullNic/12,2,'.',','));
    
    totalNic = fullNic;
    


}


function CalcTax(gross, relief)
{
    var i;
    var remain = gross;
    var fullTax = 0;
    var taxSums = new Array();
    var taxPay = new Array();
    
    for (i=0; i < taxLimit.length; i++)
    {
        if (remain < taxLimit[i])
            taxSums[i] = remain;
        else
            taxSums[i] = taxLimit[i];
        remain -= taxSums[i];    
        taxPay[i] = taxSums[i] * taxRate[i];
        fullTax += taxPay[i];
    }
    taxSums[taxLimit.length] = remain;
    taxPay[taxLimit.length] = taxSums[taxLimit.length] * taxRate[taxLimit.length];
    fullTax += taxPay[taxLimit.length];
    // If there is a relief
    if (relief > 0)
    {
        var taxSum;
        var fullTaxWR = 0;
        
        remain = gross - relief;
        
        for (i=0; i < taxLimit.length; i++)
        {
            if (remain < taxLimit[i])
                taxSum = remain;
            else
                taxSum = taxLimit[i];
            remain -= taxSum;    
            fullTaxWR += taxSum * taxRate[i];
        }
    }
    else
    {
        fullTaxWR = fullTax;
    }
    i = taxSums.length;
    taxSums[i] = relief;
    taxPay[i] = (fullTaxWR - fullTax);
    
    // Print out
    for (i=0; i < taxSums.length; i++)
    {
        id='B'+(i+2);
        if (i < taxLimit.length)
            SetCellValue(id, number_format(taxLimit[i],2,'.',','));
        id='C'+(i+2);
        if (i < taxRate.length)
            SetCellValue(id, (taxRate[i]*100)+'%');
        id='D'+(i+2);
        SetCellValue(id, number_format(taxSums[i],2,'.',','));
        id = 'E'+(i+2);
        SetCellValue(id, number_format(taxPay[i],2,'.',','));
    }
    id = 'E'+(i+2);
    SetCellValue(id, number_format(fullTaxWR,2,'.',','));
    id = 'F'+(i+2);
    SetCellValue(id, number_format(fullTaxWR/12,2,'.',','));
    
    totalTax = fullTaxWR;
    
}




function CalculateSalary()
{
    var gross = 30014;
    var obj;
    var withUSS;

    // Read the input
    obj = document.getElementById('grossInput');
    gross = 1* obj.value;
    obj = document.getElementById('withUSS');
    withUSS = obj.checked;
    
    CalcUSS(gross, withUSS);
    CalcTax(gross, totalUSS);
    CalcNIC(gross, withUSS);
    
    SetCellValue('D1', number_format(gross,2,'.',','));
    SetCellValue('F1', number_format(gross/12,2,'.',','));
    
    net = gross - totalTax - totalNic - totalUSS;
    SetCellValue('E14', number_format(net,2,'.',','));
    SetCellValue('F14', number_format(net/12,2,'.',','));
}

  

