// (c) 2006 conscious.co.uk
function calcstampduty() {
// Calculate the Stamp duty payable from form values
   for (j=0,m=document.stampdutyform.elements.length;j<m;j++)
      if (document.stampdutyform.elements[j].name == 'considerationamount')
         var considerationamount = document.stampdutyform.elements[j].value;

   // get form values      
   for (j=0,m=document.stampdutyform.elements.length;j<m;j++)
      if ((document.stampdutyform.elements[j].name == 'transactiontype') && (document.stampdutyform.elements[j].checked))
         var transactiontype = document.stampdutyform.elements[j].value;


   // Error checking
   var isvalidnum = true;
   considerationamount=considerationamount.replace("£", "");
   for (i=0; i<considerationamount.length; i++) {
      if ((considerationamount.charAt(i) != "0")
      && (considerationamount.charAt(i) != ".")
      && (!parseFloat(considerationamount.charAt(i)))) {
         isvalidnum = false;
         alert("Please enter a valid number in the 'Consideration Amount' field");
         break;

      }
   }
   if (isvalidnum) {
   // Calculations
         considerationamount = parseFloat(considerationamount);
         if (transactiontype == "shares") {
            var stamptaxpayable = (considerationamount/100)*0.5;  // stamp tax payable at 0.5 % of shares value
         }
         else {
// res sd changed to 175000 3 Sep 2008 until 2 Sep 2009
// changed back 1/1/2010
            if (((considerationamount<125001) && (transactiontype == "residential")) || 
                ((considerationamount<150001) && (transactiontype == "nonresidential"))) {
               var stamptaxpayable = 0; // no stamp tax payable on values under 125,000 pounds for residential holdings or 150,000 pounds for non-residential holdings
            } 
            else if (considerationamount<250001) {
               var stamptaxpayable = (considerationamount/100); // stamp tax payable at 1 % of non-shares value
            } 
            else if (considerationamount<500001) {
               var stamptaxpayable = ((considerationamount/100)*3); // stamp tax payable at 3 % of non-shares value
            } 
            else {
               var stamptaxpayable = ((considerationamount/100)*4); // stamp tax payable at 4 % of non-shares value
            }
         }
         if (stamptaxpayable != 0) { //should be one line, but firefox doesn't like it if so.
            stamptaxpayable = (stamptaxpayable/5);
            stamptaxpayable = Math.ceil(stamptaxpayable);
            stamptaxpayable = (stamptaxpayable*5);
         }
         if (!parseFloat(stamptaxpayable)) stamptaxpayable = 0;
      // display with trailing zeros
	 stamptaxpayable = poundsandpence(stamptaxpayable, false, true);
	
   // output values
   for (j=0,m=document.stampdutyform.elements.length;j<m;j++)
      if (document.stampdutyform.elements[j].name == 'stamptaxpayable')
         document.stampdutyform.elements[j].value = stamptaxpayable;	
   }
}

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);
}

