function calc(stampduty) {
	//alert(stampduty.property.value);
	// need to strip out any commas
	var property = replace(stampduty.property.value,",","");
	var loan = replace(stampduty.loan.value,",","");
 
  loanDuty = 0;
  propertyDuty = 0;
  
  if (loan > 16000) {
    loanDuty = Math.ceil((loan-16000)/1000)*4+5;
  } else if (loan > 0) {
    loanDuty = 5;
  }
  if (property <= 14000) {
    propertyDuty = Math.ceil(property/100)*1.25;
  } else if (property <= 30000) {
    propertyDuty = 175+Math.ceil((property-14000)/100)*1.5;
  } else if (property <= 80000) {
    propertyDuty = 415+Math.ceil((property-30000)/100)*1.75;
  } else if (property <= 300000) { 
    propertyDuty = 1290+Math.ceil((property-80000)/100)*3.5;
  } else if (property <= 1000000) {
    propertyDuty = 8990+Math.ceil((property-300000)/100)*4.5;
  } else {
    propertyDuty = 40490+Math.ceil((property-1000000)/100)*5.5;
  }
  loanDuty = Math.round(loanDuty);  
  propertyDuty = Math.round(propertyDuty);
  total = eval(loanDuty) + eval(propertyDuty);
  
  loanDuty = formatValue(loanDuty, "##,###,###.");
  propertyDuty = formatValue(propertyDuty, "##,###,###.");
  total = formatValue(total, "##,###,###.");
  
  stampduty.total.value = replace(total,".","");
  stampduty.loanDuty.value = replace(loanDuty,".","");
  stampduty.propertyDuty.value = replace(propertyDuty,".","");
  stampduty.property.value = replace(formatValue(property, "##,###,###."),".","");
  stampduty.loan.value = replace(formatValue(loan, "##,###,###."),".","");
  
}

function replace(argvalue, x, y) {

  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }

  return argvalue;

}	
  
function formatValue(argvalue, format) {
  var numOfDecimal = 0;
  if (format.indexOf(".") != -1) {
    numOfDecimal = format.substring(format.indexOf(".") + 1, format.length).length;
  }
  argvalue = formatDecimal(argvalue, true, numOfDecimal);

  argvalueBeforeDot = argvalue.substring(0, argvalue.indexOf("."));
  retValue = argvalue.substring(argvalue.indexOf("."), argvalue.length);

  strBeforeDot = format.substring(0, format.indexOf("."));

  for (var n = strBeforeDot.length - 1; n >= 0; n--) {
    oneformatchar = strBeforeDot.substring(n, n + 1);
    if (oneformatchar == "#") {
      if (argvalueBeforeDot.length > 0) {
        argvalueonechar = argvalueBeforeDot.substring(argvalueBeforeDot.length - 1, argvalueBeforeDot.length);
        retValue = argvalueonechar + retValue;
        argvalueBeforeDot = argvalueBeforeDot.substring(0, argvalueBeforeDot.length - 1);
      }
    }
    else {
      if (argvalueBeforeDot.length > 0 || n == 0)
        retValue = oneformatchar + retValue;
    }
  }

  return retValue;
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

