// Compute Loan 
function ComputeLoan(){	   
var s = validation();			  
if(s!=true)
return;

lar = deFormatCurrency(document.getElementById("txtLAR").value);
ir = document.getElementById("txtIR").value;
lt = document.getElementById("txtLT").value;
mof = document.getElementById("txtMOF").value;
lat = lar*1*mof/100;

// Calculate Monthly Repayment
if(lt>0)		
mr = -pmt((ir/100)/12, (lt*12), (lat),0,0);
else	
mr = 0;

// Calculate Min. Monthly Income Required
mmir = mr/(33/100);

document.getElementById("txtLAR").value = formatCurrency(lar);		
document.getElementById("txtMR").value = formatCurrency(mr);
// document.getElementById("txtLoanAmount").value = formatCurrency(lat);
// document.getElementById("txtMMIR").value = formatCurrency(mmir);

}	


function pmt(rate, nper, pv, fv, p_type){
var pvif = Math.pow(1 + rate, nper);
var fvifa = (Math.pow(1 + rate, nper) - 1) / rate;
var type1 = (p_type != 0) ? 1 : 0;
var lvamt = ((-pv * pvif - fv ) / ((1 + rate * type1) * fvifa));
if(isNaN((-pv * pvif - fv ) / ((1 + rate * type1) * fvifa)))
  return 0.00;
else
  return ((-pv * pvif - fv ) / ((1 + rate * type1) * fvifa));
}

function deFormatCurrency(num)
{
num = num.toString().replace(/\$|\,/g,'');
return num;
}

function formatCurrency(num) 
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num);
}

function IsNumeric(sText)
{
var ValidChars = "0123456789.,";
var IsNum=true;
var Char;     
for (i = 0; i < sText.length && IsNum == true; i++) 
{ 
Char = sText.charAt(i); 
if (ValidChars.indexOf(Char) == -1) 
{
IsNum = false;
}
}
return IsNum;
}

function validation()
{		  
// Interest Rate
if(!IsNumeric(document.getElementById("txtIR").value))
{
alert("Please Enter Number Only.");
docRef.txtIR.focus();
return false;
}

// Loan Tenure
if(!IsNumeric(document.getElementById("txtLT").value))
{
alert("Please Enter Number Only.");
docRef.txtLT.focus();
return false;
}
return true;
}

//currency.js

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
	
function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

//cont.

function calculate() {
    pvalue = document.getElementById("pValue").value;
    //document.getElementById("pValue").value = CommaFormatted(pvalue);
    prate = document.getElementById("pRate").value;
    tlegalfee = document.getElementById("tlegalfee").value;
	var property = parseFloat(pvalue);
	var fee = "Total legal fee: RM ";
	
	//currencyFormat(this,',','.',event))
		
	if (pvalue=="") {
		//alert("Please enter the property value.");
		//pvalue.focus();
	} else if (!parseFloat(pvalue)) {
		alert("Please enter only number.");
		pvalue.focus();
	} else if (property<="150000") {
		pcount = property * 0.01;
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tlegalfee").value = (pcount);
		//alert(fee + pcount);
	} else if (property<="1000000") {
		pcount = (1500 + (property - 150000) * 0.007);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tlegalfee").value = (pcount);
	} else if (property<="3000000") {
		pcount = (7450 + (property - 1000000) * 0.006);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tlegalfee").value = (pcount);
	} else if (property<="5000000") {
		pcount = (19450 + (property - 3000000) * 0.005);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tlegalfee").value = (pcount);
	} else if (property<="7500000") {
		pcount = (29450 + (property - 5000000) * 0.004);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tlegalfee").value = (pcount);
	} else if (property>"7500000" && prate=="") {
		alert("Please key in the negotiated rate above RM7.5 million. Example: 0.2%");	
	} else if (!parseFloat(prate)) {
		alert("Please enter only number.");
		prate.focus();
	} else if (property>"7500000" && prate!="") {
		var pirate = parseFloat(prate);
		pcount = (39450 + (property - 7500000) * (pirate / 100));
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		//alert(fee + pcount);
		document.getElementById("tlegalfee").value = pcount;
	}
}	

function more_rate() {
    pvalue = document.getElementById("pValue").value;
	if (parseFloat(pvalue)>="7500000" && i=="1") {
		alert("If property value is more than RM 7.5 million, please key in the excess rate. Example 0.2%");
		i = "2";
		//unhide('irate');
	} else if (pvalue=="" && i=="2") {
		i = "1";
		//unhide('irate');	
	}
}


function calculate_stamp() {
	var property = parseFloat(document.getElementById("pValue").value);
	var fee = "Total stamp duty: RM ";
	
	//currencyFormat(this,',','.',event))
		
	if (document.getElementById("pValue").value=="") {
		//alert("Please enter the property value.");
		//document.stamp.pvalue.focus();
	} else if (!parseFloat(document.getElementById("pValue").value)) {
		alert("Please enter only number.");
		stamp.pvalue.focus();
	} else if (property<="100000") {
		pcount = property * 0.01;
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		//alert(fee + pcount);
		document.getElementById("tstampduty").value = (pcount);
	} else if (property<="500000") {
		pcount = (1000 + (property - 100000) * 0.02);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tstampduty").value = (pcount);
	} else if (property > "500000") {
		pcount = (9000 + (property - 500000) * 0.03);
		pcount = CurrencyFormatted(pcount);
		pcount = CommaFormatted(pcount);
		document.getElementById("tstampduty").value = (pcount);
	}
}

//end
