//<!-- loan calculator -->
function calcRound(num) {

   result=Math.floor(num)+"." ;

   var cents=100*(num-Math.floor(num))+0.5;

   result += Math.floor(cents/10);
   result += Math.floor(cents%10);

   return(result);

}

function cal()
{
var myindex;
var Month;
var tDownpayment;
var tIntrate;
var tcprice;

myindex = document.tor.periods.selectedIndex;    
Month = document.tor.periods.options[myindex].value;
tDownpayment = document.tor.downpayment.value;
tIntrate = document.tor.intrate.value;
tcprice = document.tor.cprice.value;

year = Month/12;
loanprice = tcprice-tDownpayment;
cInstallment = (((loanprice * (tIntrate/100))* year) + loanprice) / Month;

if (isNaN(document.tor.cprice.value))
	{
		document.tor.cprice.focus();
   		document.tor.cprice.select();
		alert("Only numbers are allowed.\nDo not enter symbols or empty spaces");
		return false;
	}
if (isNaN(document.tor.downpayment.value))
	{
		document.tor.downpayment.focus();
   		document.tor.downpayment.select();
		alert("Only numbers are allowed.\nDo not enter symbols or empty spaces");
		return false;
	}	
if (isNaN(document.tor.intrate.value))
	{
		document.tor.intrate.focus();
   		document.tor.intrate.select();
		alert("Only numbers are allowed.\nDo not enter symbols or empty spaces");
		return false;
	}

if (tcprice == "")
	{
		document.tor.cprice.focus();
   		document.tor.cprice.select();
		alert("Please enter the car OTR price.\nExample: 60000");
		return false;
	}
else if (tcprice <= 0)
	{
		document.tor.cprice.focus();
   		document.tor.cprice.select();
		alert("Invalid loan amount");
		return false;
	}

if (tDownpayment == "")
	{
		document.tor.downpayment.focus();
   		document.tor.downpayment.select();
		alert("Please enter a downpayment amount.\nThe minimum downpayment shall be 10% of car purchase price.\nExample: 6000");
		return false;
	}

if (tcprice <= tDownpayment)
	{
		document.tor.downpayment.focus();
   		document.tor.downpayment.select();
		alert("Downpayment amount cannot be the same or higher than your loan amount");
		return false;
	}

if (tIntrate == "")
	{
		document.tor.intrate.focus();
   		document.tor.intrate.select();
		alert("Please enter the interest rate.\nExample: 3.75");
		return false;
	}	

if ((tDownpayment < 0) || (tIntrate < 0) )
	{
		alert("Negative input not allowed");
		return false;
	}

check_dpayment();
document.tor.installment.value = calcRound(cInstallment);
}

function check_dpayment()
{
if (document.tor.downpayment.value != "")
	{
	margin = (document.tor.cprice.value - document.tor.downpayment.value) / document.tor.cprice.value;
	if (margin > 0.9)
		{
		//alert("Please enter a downpayment amount. The minimum downpayment shall be 10% of car purchase price.");
		document.tor.downpayment.focus();
   		document.tor.downpayment.select();
		}
	//else {
	//alert("Please enter a downpayment amount. The minimum downpayment shall be 10% of car purchase price.");
	//return false;
	//}
	}
}

//<!-- end loan calculator -->