// (c) 2006 conscious.co.uk

function calcpayslip() {
// Calculates car benefits
   var formnames = new Array("grossmonthlypay", "numericaltaxcode");
   var formdisplayednames = new Array("Gross Monthly Pay", "Numerical Part of Tax Code");
   var formvals = new Array();
   var outnames = new Array("grosspay", "payededuction", "nideduction", "netpay", "employercontrib", "employercost");
   var outvals = new Array();


   // get form values      
   for (i=0; i<formnames.length; i++)
      for (j=0,m=document.payslipform.elements.length;j<m;j++)
         if (document.payslipform.elements[j].name == formnames[i])
            formvals[formnames[i]] = document.payslipform.elements[j].value;

   // Error checking
   var isvalidnum = true;
   for (j=0; j<formnames.length; j++) {
      formvals[formnames[j]] = formvals[formnames[j]].replace("£", "");
      for (i=0; i<formvals[formnames[j]].length; i++) {
         if ((formvals[formnames[j]].charAt(i) != "0")
         && (formvals[formnames[j]].charAt(i) != ".")
         && (!parseFloat(formvals[formnames[j]].charAt(i)))) {
            isvalidnum = false;
            alert("Please enter a valid number in the "+formdisplayednames[j]+" field");
            break;

      }
   }
   }
   
   if (isvalidnum) {

      for (i=0; i<formnames.length; i++) formvals[formnames[i]] = parseFloat(formvals[formnames[i]]);
      formnames[formnames.length] = "taxcode";

      for (j=0,m=document.payslipform.elements.length;j<m;j++)
         if ((document.payslipform.elements[j].name == "taxcode") && (document.payslipform.elements[j].checked))
            formvals['taxcode'] = document.payslipform.elements[j].value;    
         
      var tempvalA = (((10 * formvals['numericaltaxcode']) + 9) / 12); // working value from numerical part of tax code
      
      // PAYE
      if (formvals['taxcode'] == "BR") outvals['payededuction'] = ((formvals['grossmonthlypay'] / 100) * 22);  // Basic rate of tax is 22 percent of monthly pay
      else if (formvals['taxcode'] == "DO") outvals['payededuction'] = ((formvals['grossmonthlypay'] / 100) * 40); // Higher rate of tax is 40 percent of monthly pay
      else if (formvals['taxcode'] == "NT") outvals['payededuction'] = 0;  // No tax payable for this tax code
      else if (formvals['taxcode'] == "K") { // If total allowances less than total deductions
         var tempvalB = (formvals['grossmonthlypay'] + tempvalA); // second working value (effective pay per month considering numerical part of tax code)
         var tempvalC = ((formvals['grossmonthlypay'] / 100) * 50); // third working value
         if (tempvalB < 179) outvals['payededuction'] = (tempvalB / 100) * 10; // if effective pay per month under 179 pounds, PAYE at 10 %
         else if (tempvalB < 2776) outvals['payededuction'] = (((tempvalB - 179) / 100) * 22) + 17.9; // otherwise if effective pay pay month under 2776 pounds (joint tax limit), PAYE calculated using middle tax bracket et al
         else outvals['payededuction'] = (((tempvalB - 2775) / 100) * 40) + 1056.3; // otherwise, PAYE calculated using higher tax bracket, reduced by the other tax boundaries
         if (outvals['payededuction'] > tempvalC) outvals['payededuction'] = tempvalC; // PAYE capped at third working value
      }
      else {  // If no tax code specified/known
         if (formvals['grossmonthlypay'] <= tempvalA) outvals['payededuction'] = 0; // if under effective pay per month limit considering tax code, no PAYE paid
         else if (formvals['grossmonthlypay'] <= (tempvalA + 179)) outvals['payededuction'] = ((formvals['grossmonthlypay'] - tempvalA)/100) * 10; // if effective pay per month under 179, PAYE at 10 %
         else if (formvals['grossmonthlypay'] <= (tempvalA + 2776)) outvals['payededuction'] = (((formvals['grossmonthlypay'] - tempvalA - 179) / 100) * 22) + 17.9; // otherwise, if effective pay pay month under 2776 pounds (joint tax limit), PAYE calculated using middle tax bracket, reduced by the amount subject to the lower tax bracket
         else outvals['payededuction'] = (((formvals['grossmonthlypay'] - tempvalA - 2776) / 100) * 40) + 1056.3; // otherwise, PAYE calculated using the joint and higher tax bracket, reduced by the amount subject to the lower and middle tax brackets
      }      
      
      // National Insurance
      if (formvals['grossmonthlypay'] < 421) outvals['nideduction'] = 0;  // only above 421 pounds per month
      else if (formvals['grossmonthlypay'] < 2796) outvals['nideduction'] = ((formvals['grossmonthlypay'] - 420) / 100) * 11; // if so, calculate using monthly pay (less amount untaxable) at 11%
      else outvals['nideduction'] = ((formvals['grossmonthlypay'] - 2795) / 100) + 261.25; // otherwise, if in the higher tax bracket, NI calculated using the higher tax bracket less the amount subject to the lower tax brackets
      
      // Employer's National Insurance Contribution
      if (formvals['grossmonthlypay'] < 421) outvals['employercontrib'] = 0; // only above 421 pounds per month
      else outvals['employercontrib'] = ((formvals['grossmonthlypay'] - 420) / 100) * 12.8; // if so, calculate using monthly pay (less amount untaxable) at 12.8%    
      
      outvals['grosspay'] = formvals['grossmonthlypay'];
      outvals['netpay'] = 0;
      outvals['employercost'] = 0;
      for (i=0; i<outnames.length; i++) outvals[outnames[i]] = Math.round(outvals[outnames[i]]);  // round to nearest pound
      outvals['netpay'] = (outvals['grosspay'] - outvals['payededuction'] - outvals['nideduction']); // Net pay is gross less PAYE less National Insurance
      outvals['employercost'] = outvals['grosspay'] + outvals['employercontrib']; // cost to employer is gross plus employer's contribution     
        for (i=0; i<outnames.length; i++) if (!parseFloat(outvals[outnames[i]])) outvals[outnames[i]] = 0;

   // output values
   for (i=0; i<outnames.length; i++)
      for (j=0,m=document.payslipform.elements.length;j<m;j++)
         if (document.payslipform.elements[j].name == outnames[i])
            document.payslipform.elements[j].value = outvals[outnames[i]];	
   }
}



function poundsandpence(numnotrounded, returnasfloat, isthousands) {
// makes number conform to pounds and 2 decimal (pence) digits, with options of commas denoting thousands
var decbit = (numnotrounded.toString()).split(".");

if (!decbit[1]) var decpart = "00";
else if ((decbit[1].length) == 1) var decpart = decbit[1]+"0";
else if ((decbit[1].length) == 2) var decpart = decbit[1];
else {
// Varies to get right result - check
   var tempbit = (Math.round((decbit[0]+decbit[1].substring(0,2))+"."+(decbit[1].substring(2)))).toString();
   var decpart = tempbit.substring((tempbit.length-2));
   decbit[0] = tempbit.substring(0, (tempbit.length-2));
//   var decpart = (decbit[1].substring(0, 2));

}

if ((!returnasfloat) && (isthousands) && (decbit[0].length > 3)) {  // add commas for thousands if appropriate
   var afterthousands = "";
   for (tocommas = 0; tocommas < (decbit[0].length / 3); tocommas++) afterthousands = ","+decbit[0].substring((decbit[0].length-((tocommas+1)*3)), (decbit[0].length-(tocommas*3)))+afterthousands;
   afterthousands = afterthousands.substring(1);

}
else afterthousands = decbit[0];

var wholebit = afterthousands+"."+decpart;

if (!returnasfloat) return wholebit;
else return parseFloat(wholebit);
}
