var vat = 0.14;
var tFee = 0;
var transfer = 0;
var deed = 0;
var admin = 200.00;
var numToTimesBy = 0;
var total = 0;
var total2 = 0;

//Mortgage bond variables
var fee = 0;
var duty = 0;
var deedReg = 0;
var mAdmin = 125;

function format(value) {
  var s = 0, xtraStr = "00", slen = 0;
  
  if (value < 0.1) {
    s = Math.round(eval(value)*100)/100
    str2 = "" + s;
  } else {
    s = Math.round(eval(value)*100)
    xtraStr = "" + s;
    slen = xtraStr.length;
    str2 = xtraStr.substring(0,slen-2)+"."+xtraStr.substring(slen-2,slen);
  }
  return str2;
}

function get_values(pvalue, eType) {

//TRANSFER FEES
	if (pvalue <= 18000) { transfer = 760; }
	if ((pvalue > 18000) && (pvalue <=30000)) { transfer = 1250; }
	if ((pvalue > 30000) && (pvalue <=45000)) { transfer = 1500; }
	if ((pvalue > 45000) && (pvalue <=70000)) { transfer = 1800; }
	if ((pvalue > 70000) && (pvalue <=80000)) { transfer = 2500; }
	if ((pvalue > 80000) && (pvalue <=90000)) { transfer = 2700; }
	if ((pvalue > 90000) && (pvalue <=100000)) { transfer = 2900; }
	if ((pvalue > 100000) && (pvalue <=125000)) { transfer = 3000; }
	if ((pvalue > 125000) && (pvalue <=150000)) { transfer = 3100; }
	if ((pvalue > 150000) && (pvalue <=175000)) { transfer = 3300; }
	if ((pvalue > 175000) && (pvalue <=200000)) { transfer = 3500; }
	if ((pvalue > 200000) && (pvalue <=250000)) { transfer = 3800; }
	if ((pvalue > 250000) && (pvalue <=300000)) { transfer = 4400; }
	if ((pvalue > 300000) && (pvalue <=350000)) { transfer = 4700; }
	if ((pvalue > 350000) && (pvalue <=400000)) { transfer = 5100; }
	if ((pvalue > 400000) && (pvalue <=450000)) { transfer = 5500; }
	if ((pvalue > 450000) && (pvalue <=500000)) { transfer = 6000; }
	if (pvalue > 500000) {

		transfer = 6000;
		if (pvalue <= 1000000) {
			numToTimesBy = Math.ceil((pvalue - 500000) / 100000);
			transfer = transfer + (numToTimesBy * 800);
		}
		if ((pvalue > 1000000) && (pvalue < 5100000)) {
			if (pvalue > 5000000) pvalue = 5000000;
			numToTimesBy = Math.ceil((pvalue - 1000000) / 100000); 
			transfer = 10000 + (numToTimesBy * 400);
		}
		if (pvalue >= 5100000) {
			numToTimesBy = Math.ceil((pvalue - 5000000.01) / 100000);
			transfer = 26000 + (numToTimesBy * 200);
		}
	}

//DEEDS OFFICE REGISTRATION FEE
	if (pvalue <= 80000) { deed = 55; }
	if ((pvalue > 80000) && (pvalue <=150000)) { deed = 200; }
	if ((pvalue > 150000) && (pvalue <=300000)) { deed = 260; }
	if ((pvalue > 300000) && (pvalue <=500000)) { deed = 340; }
	if ((pvalue > 500000) && (pvalue <=1000000)) { deed = 400; }
	if ((pvalue > 1000000) && (pvalue <=2000000)) { deed = 500; }
	if ((pvalue > 2000000) && (pvalue <=3000000)) { deed = 650; }
	if ((pvalue > 3000000) && (pvalue <=5000000)) { deed = 800; }
	if (pvalue > 5000000) { deed = 1000; }

//TRANSFER DUTY
	switch (eType) {
		case 0 : //Legal entity
			tFee = (pvalue * 0.08);
			break;
		case 1 :
			var feeValue1 = 0, feeValue2 = 0, feeValue3 = 0;

			if (pvalue > 1000000) {
				feeValue1 = 0;
				feeValue2 = 25000;
				feeValue3 = ((pvalue-1000000) * 0.08);
			}
			if (pvalue > 500000 && pvalue <= 1000000) {
				feeValue1 = 0;
				feeValue2 = ((pvalue-500000) * 0.05);
			}
			
			
			tFee = feeValue1 + feeValue2 + feeValue3;
			//alert(tFee);
			break
	}
}

function show_result(tValue) {
	if (tValue>20000000) { tValue=20000000; }
	if (tValue>0) {
       
		document.as.tValue.value = tValue;
		get_values(tValue, 1); //natural person
		document.as.transfer.value = format(transfer);
		document.as.tVat.value = format((transfer * vat));
		document.as.deeds.value = format(deed);
		document.as.tDuty.value = format(tFee);
		document.as.admin.value = format(admin);
		document.as.aVat.value = format(admin * vat);
		total = transfer + (transfer * vat) + deed + tFee + admin + (admin * vat);
		document.as.total.value = format(total);
		get_values(tValue, 0); //legal entity
		document.as.transfer2.value = format(transfer);
		document.as.tVat2.value = format((transfer * vat));
		document.as.deeds2.value = format(deed);
		document.as.tDuty2.value = format(tFee);
		document.as.admin2.value = format(admin);
		document.as.aVat2.value = format(admin * vat);
		total2 = transfer + (transfer * vat) + deed + tFee + admin + (admin * vat);
		document.as.total2.value = format(total2);
	}
}

//MORTGAGE BOND CALCULATIONS
function mortgage_values(pvalue) {

//FEES
	if (pvalue <= 10000) { fee = 550; }
	if ((pvalue > 10000) && (pvalue <=15000)) { fee = 700; }
	if ((pvalue > 15000) && (pvalue <=20000)) { fee = 750; }
	if ((pvalue > 20000) && (pvalue <=25000)) { fee = 850; }
	if ((pvalue > 25000) && (pvalue <=30000)) { fee = 900; }
	if ((pvalue > 30000) && (pvalue <=35000)) { fee = 1000; }
	if ((pvalue > 35000) && (pvalue <=40000)) { fee = 1100; }
	if ((pvalue > 40000) && (pvalue <=45000)) { fee = 1150; }
	if ((pvalue > 45000) && (pvalue <=50000)) { fee = 1200; }
	if ((pvalue > 50000) && (pvalue <=60000)) { fee = 1300; }
	if ((pvalue > 60000) && (pvalue <=70000)) { fee = 1400; }
	if ((pvalue > 70000) && (pvalue <=80000)) { fee = 1600; }
	if ((pvalue > 80000) && (pvalue <=90000)) { fee = 1700; }
	if ((pvalue > 90000) && (pvalue <=100000)) { fee = 1800; }
	if ((pvalue > 100000) && (pvalue <=125000)) { fee = 1900; }
	if ((pvalue > 125000) && (pvalue <=150000)) { fee = 2000; }
	if ((pvalue > 150000) && (pvalue <=175000)) { fee = 2100; }
	if ((pvalue > 175000) && (pvalue <=200000)) { fee = 2200; }
	if ((pvalue > 200000) && (pvalue <=250000)) { fee = 2400; }
	if ((pvalue > 250000) && (pvalue <=300000)) { fee = 2700; }
	if ((pvalue > 300000) && (pvalue <=350000)) { fee = 3000; }
	if ((pvalue > 350000) && (pvalue <=400000)) { fee = 3400; }
	if ((pvalue > 400000) && (pvalue <=450000)) { fee = 3700; }
	if ((pvalue > 450000) && (pvalue <=500000)) { fee = 4000; }
	if (pvalue > 500000) {
		fee = 4000;
		if (pvalue <= 1000000) {
			numToTimesBy = Math.ceil((pvalue - 500000) / 100000);
			fee = fee + (numToTimesBy * 600);
		}
		if ((pvalue > 1000000) && (pvalue < 5100000)) {
			if (pvalue > 5000000) pvalue = 5000000;
			numToTimesBy = Math.ceil((pvalue - 1000000) / 100000); 
			fee = fee + 3000 + (numToTimesBy * 300);
		}
		if (pvalue >= 5100000) {
			numToTimesBy = Math.ceil((pvalue - 5000000.01) / 100000); 
			fee = fee + 3000 + 12000 + (numToTimesBy * 150);
		}
	}

//STAMP DUTY
	//duty = pvalue * 0.002;
        duty = pvalue * 0;


//DEEDS OFFICE REGISTRATION FEE
	if (pvalue <= 150000) { deedReg = 200; }
	if ((pvalue > 150000) && (pvalue <=300000)) { deedReg = 260; }
	if ((pvalue > 300000) && (pvalue <=500000)) { deedReg = 340; }
	if ((pvalue > 500000) && (pvalue <=1000000)) { deedReg = 400; }
	if ((pvalue > 1000000) && (pvalue <=2000000)) { deedReg = 500; }
	if ((pvalue > 2000000) && (pvalue <=3000000)) { deedReg = 650; }
	if ((pvalue > 3000000) && (pvalue <=5000000)) { deedReg = 800; }
	if (pvalue > 5000000) { deedReg = 1000; }

}

function mortgage(tValue) {
	if (tValue>20000000) { tValue=20000000; }
	if (tValue>0) {
		document.asm.tValue.value = tValue;
		mortgage_values(tValue);
		document.asm.fee.value = format(fee);
		document.asm.feeVat.value = format((fee * vat));
		document.asm.duty.value = format(duty);
		document.asm.deeds.value = format(deedReg);
		document.asm.admin.value = format(mAdmin);
		document.asm.aVat.value = format(mAdmin * vat);
		total2 = fee + (fee * vat) + deedReg + duty + mAdmin + (mAdmin * vat);
		document.asm.total.value = format(total2);
	}
}
 var num = 99999999;
  function checkNumber(input, min, max, msg) {
	 if (isNaN(input)) {
	   msg = msg + " has invalid data: " + input;
      alert(msg);
      return false;
    }

    if (input < min || max < num) {
      alert(msg + " not within valid range (" + min + " to " + max + ")");
      return false;
    }

    return true;
  }

  function homeLoan(amt, rte, trm) {
  /*	if (!checkNumber(amt, 1, 99999999, "Loan Amount") || !checkNumber(rte, .01, 50, "Interest Rate") || !checkNumber(trm, 1, 50, "Repayment period")) {
      loan.repay.value = "Invalid";
      return;
    }*/

    document.loan.repay.value = "0";
    amt = Math.floor(amt * 100)/100;
    rte = Math.floor(rte * 100)/100;

    var rteR = rte / 1200
    var termInMonths = Math.floor(trm) * 12;
    var repay =  ((amt * rteR)/( 1 - Math.exp((0 - termInMonths) * Math.log(rteR + 1))));
    repay = Math.floor(repay * 100)/100;

    document.loan.term.value = trm;
    document.loan.rate.value = rte;
    document.loan.amount.value = amt;
    document.loan.repay.value = repay;
  }

