function calculate() {

 if (document.loan.choice[0].checked) //Check the option user has selected
 var choice = "loan";
 if (document.loan.choice[1].checked)
 var choice = "monthly";
 
 if(document.loan.amount.value != "") //Check if user has entered an amount, if not, display error message
 {
 if(validate(document.loan.amount.value)) //Check if user entered valid amount
 var nAmount = parseFloat(document.loan.amount.value);
 else
 {document.getElementById("error").innerHTML="<span style=\"color:red;font-weight:bold\">Please enter a valid amount</span><br/>";clear();
 return false;}
 }
 else
 {document.getElementById("error").innerHTML="<span style=\"color:red;font-weight:bold\">Please enter an amount</span><br/>";clear();
 return false;}
 
 if(document.loan.down.value != "") //Check if down payment has been entered, if not, set down payment to 0
 {
 if(validate(document.loan.down.value)) //Check if user entered valid down payment
 {
 if(parseFloat(document.loan.down.value) >= parseFloat(document.loan.amount.value) && choice =="loan")
 {document.getElementById("error").innerHTML="<span style=\"color:red;font-weight:bold\">Your down payment has to be less than the loan amount</span><br/>";clear();
 return false;}
 else
 var nDown = parseFloat(document.loan.down.value);
 }
 else
 {document.getElementById("error").innerHTML="<span style=\"color:red;font-weight:bold\">Please enter a valid down payment</span><br/>";clear();
 return false;}
 }
 else
 {document.loan.down.value = "0";var nDown = 0;}
 
 var nProvtax = 8; //Set provincial tax to 8%
 var nFedtax = 5; //Set federal tax to 5%
 var nN= document.loan.length.selectedIndex; 
 var nTerm = parseFloat(document.loan.length[nN].value); //Get the loan period
 
 if(document.loan.rate.value == "0" || document.loan.rate.value == "") //If interest is 0 or has not been entered set interest to a very low value for calculations
 {var nRate = parseFloat(0.0000001);document.loan.rate.value = "0";}
 else if(document.loan.rate.value != "")
 {
 if(validate(document.loan.rate.value)) //Check if user entered valid interest rate
 var nRate = parseFloat(document.loan.rate.value); 
 else
 {document.getElementById("error").innerHTML="<span style=\"color:red;font-weight:bold\">Please enter a valid interest rate</span><br/>";clear();
 return false;}
 }
 
 var nGst = 0;
 var nPst = 0;
 var nI = nRate / 100 / 12;
 var nY = Math.pow(1 + nI, nTerm);
 
 if(choice=="loan") //If user chose the first option
 { 
 	//Add federal and provincial tax to the amount
  	nGst = (nAmount/100) * nFedtax; 
  	nPst = (nAmount/100) * nProvtax;
 	
 	//Calculate final amount minus the down payment
 	var fAmount =  (nAmount  + nGst +nPst) - (nDown);
 	
 	//Calculate the monthly payments for the loan
  	var nPayment = (fAmount * nY * nI) / (nY -1);
  
  	//Display the result to the user
  	document.getElementById("error").innerHTML="";
  	document.getElementById("result1").innerHTML="Loan Amount Requested";
  	document.getElementById("answer1").innerHTML="$" + currency(nAmount);
  	document.getElementById("result").innerHTML="Monthly Payment";
  	document.getElementById("answer").innerHTML="$" + currency(nPayment);
 }
 
 if(choice=="monthly") //If user chose the second option
 {
 	//Calculate the amount
	var fAmount =  nAmount * (nY -1)/(nI*nY);
	 
	//Calculate the loan based on the monthly payments and federal and provincial tax
	var loan = (fAmount + nDown)*100/(100+nProvtax+nFedtax);
	
	//Display the result to the user
	document.getElementById("error").innerHTML="";
  	document.getElementById("result1").innerHTML="Monthly Payment Desired";
  	document.getElementById("answer1").innerHTML="$" + currency(nAmount);
  	document.getElementById("result").innerHTML="Loan amount you are eligible for";
  	document.getElementById("answer").innerHTML="$" + currency(loan);
 }
 
}

//Function to display the result in currency format
function currency(amount)
{
	var delimiter = ",";
	var d = amount;
	var i = parseInt(amount);
	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; }
	return amount;
}

//Function to validate numeric input
function validate(value){
var value;
if (isNaN(value)) {
      return false;
      }
      else
      return true;
}

function clear(){
  	document.getElementById("result1").innerHTML="";
  	document.getElementById("answer1").innerHTML="";
  	document.getElementById("result").innerHTML="";
  	document.getElementById("answer").innerHTML="";
}