

function homeloancalc(homeloan)
	{
	// need to strip out any commas
	deposit = replace(homeloan.deposit.value,",","");
	totalprice = replace(homeloan.totalprice.value,",","");
	loanamount = totalprice - deposit;
	
	//work on monthly calculations
	totalloanterm = homeloan.loanterm.value * 12;
	totalinterestrate = homeloan.interestrate.value / 1200;
	oneplusr = 1 + totalinterestrate;
	oneplusrton = Math.pow(oneplusr, totalloanterm);
	oneplusrtonlessone = oneplusrton - 1;
	

	repayment = loanamount * totalinterestrate * oneplusrton / oneplusrtonlessone;
	repayment = Math.round(repayment);
	repayment = formatValue(repayment, "###,###.");
	loanamount = formatValue(loanamount, "##,###,###.");
	totalprice = formatValue(totalprice, "##,###,###.");
	deposit = formatValue(deposit, "##,###,###.");
	
	if (isNaN(parseInt(repayment)) || isNaN(parseInt(loanamount)) || isNaN(parseInt(totalprice)) || isNaN(parseInt(deposit))){
		alert("Sorry, invalid data found.\nPlease check all fields are entered correctly.");
	} else {
		homeloan.repayment.value = replace(repayment,".","");
		homeloan.loanamount.value = replace(loanamount, ".", "");
		homeloan.totalprice.value =  replace(totalprice, ".", "");
		homeloan.deposit.value =  replace(deposit, ".", "");
	}
	
	//homeloan.repayment.value = "$" + repayment;
	
	}

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;
}