function check()
{ 
	if(document.all.amt.value=="" || isNaN(parseFloat(document.all.amt.value)))
  { alert("Please enter a valid loan amount.");
    document.all.amt.value="";
    document.all.amt.focus();
    return false; }
  if(document.all.pay.value=="" || isNaN(parseFloat(document.all.pay.value))) { 
	  alert("Please enter a valid number of payments.");
	  document.all.pay.value="";
	  document.all.pay.focus();
	  return false; 
  }else if(document.all.pay.value>1000) {
	  alert("You may not be alive by the time you are done repaying this.  Please enter a number of payments under 1000.");
	  document.all.pay.focus();
	  return false; 
  }
  if(document.all.rate.value=="" || isNaN(parseFloat(document.all.rate.value)))
  { alert("Please enter the interest rate.");
    document.all.rate.value="";
    document.all.rate.focus();
    return false; }
  else if(document.all.rate.value>200) {
	  alert("That interest rate seems high.  Please enter an interest rate under 200%.");
	  document.all.rate.focus();
	  return false; 
  }
  
  if(document.all.purchaseprice.value > 10000000) {
	  alert("This is one expensive car.  Please enter a purchase price under $10,000,000.");
	  document.all.purchaseprice.focus();
	  return false; 
  }
  if(document.all.addprod.value > 10000000) {
	  alert("What kind of products did you add?  Please enter an amount under $10,000,000.");
	  document.all.addprod.focus();
	  return false; 
  }
  if(document.all.tnt.value > 10000000) {
	  alert("This is too much to pay in dmv fees.  Please enter an amount under $10,000,000.");
	  document.all.tnt.focus();
	  return false; 
  }
  if(document.all.fees.value > 10000000) {
	  alert("What kind of fees are this high?  Please enter an amount under $10,000,000.");
	  document.all.fees.focus();
	  return false; 
  }
  if(document.all.amt.value > 50000000) {
	  alert("This seems a bit pricey.  Please enter an amount under $10,000,000.");
	  document.all.amt.focus();
	  return false; 
  }
  if(document.all.down.value > 50000000) {
	  alert("That is a lot of money to put down on a vehicle.  Please enter an amount under $50,000,000.");
	  document.all.down.focus();
	  return false; 
  }
  if(document.all.subtotal.value > 50000000) {
	  alert("Are you sure you can afford this?  Please enter an amount under $50,000,000.");
	  document.all.subtotal.focus();
	  return false; 
  }
  if(document.all.tradein.value > 50000000) {
	  alert("That seems a bit high for a trade-in. Please enter an amount under $50,000,000.");
	  document.all.tradein.focus();
	  return false; 
  }
  
	
  if(document.all.amt.value!="" && document.all.pay.value!="" && document.all.rate.value!="")
  { show(); }
}

function clearScreen()
{ 
  var strPMT = "";
  var strDet = "";
  document.all.pmt.innerHTML = strPMT;
  document.all.det.innerHTML = strDet;
}

function fixVal(value,numberOfCharacters,numberOfDecimals,padCharacter)
{ var i, stringObject, stringLength, numberToPad;            

  value=value*Math.pow(10,numberOfDecimals);                
  value=Math.round(value);                                  

  stringObject=new String(value);                           
  stringLength=stringObject.length;                          
  while(stringLength<numberOfDecimals)                      
  {
    stringObject="0"+stringObject;                          
    stringLength=stringLength+1;                            
  }

  if(numberOfDecimals>0)
  {							    
    stringObject=stringObject.substring(0,stringLength-numberOfDecimals)+"."+
      stringObject.substring(stringLength-numberOfDecimals,stringLength);
  }

  if (stringObject.length<numberOfCharacters && numberOfCharacters>0)
  {
    numberToPad=numberOfCharacters-stringObject.length;     
    for (i=0; i<numberToPad; i=i+1)
    {
      stringObject=padCharacter+stringObject;
    }
  }

  if(isNaN(stringObject)) {
	  stringObject = "0.00";
	}
  return stringObject;                                       
}

function convertToPercent(fraction) {
	return Math.round((parseFloat(fraction) * 100) * 1000) / 1000;
}
function show()
{ 
  clearScreen();

  original_amount = RSP((document.all.amt.value).replace(/,/g, ""));  
  amount=parseFloat(original_amount);
  number=parseInt(document.all.pay.value);
  rate=parseFloat(document.all.rate.value);
  
  fees = document.all.fees.value;
  down = document.all.down.value;
  
  if (fees == '') { document.all.fees.value = 0; }
  if (down == '') { document.all.down.value = 0; }
    
  fees = parseFloat(document.all.fees.value);
  down = parseFloat(document.all.down.value);
  amount = amount;
  
  rate=rate/100;
  monthly=rate/12;
  payment=((amount*monthly)/(1-Math.pow((1+monthly),-number)));
  total=payment*number;
  interest=total-amount;

  var strPMT = "<table width='100%' border='0' cellspacing='7' cellpadding='0'>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Monthly Payment</td>";
  strPMT = strPMT + "<td class='price'>$"+fixVal(payment,0,2,' ')+"</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Loan Amount</td>";
  strPMT = strPMT + "<td class='price'>$"+fixVal(amount,0,2,' ')+"</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>No. of Payments</td>";
  strPMT = strPMT + "<td class='price'>"+number+"</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Monthly Rate</td>";
  strPMT = strPMT + "<td class='price'>"+convertToPercent(fixVal(monthly,0,5,' '))+"%</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Annual Rate</td>";
  strPMT = strPMT + "<td class='price'>"+convertToPercent(fixVal(rate,0,4,' '))+"%</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Total Paid</td>";
  strPMT = strPMT + "<td class='price'>$"+fixVal(total,0,2,' ')+"</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "<tr>";
  strPMT = strPMT + "<td class='desc'>Total Interest</td>";
  strPMT = strPMT + "<td class='price'>$"+fixVal(interest,0,2,' ')+"</td>";
  strPMT = strPMT + "</tr>";
  strPMT = strPMT + "</table>";
  document.all.pmt.innerHTML = strPMT;
  
  var strDet = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
  strDet = strDet + "<tr>";
  strDet = strDet + "<th>PMT</th>";
  strDet = strDet + "<th>Payment <br />Amount</th>";
  strDet = strDet + "<th>Interest <br />Paid</th>";
  strDet = strDet + "<th>Principal<br />Reduction</th>";
  strDet = strDet + "<th>Principal<br />Balance</th>";
  strDet = strDet + "</tr>";
  strDet = strDet + "<tr>";
  strDet = strDet + "<td>#</td>";
  strDet = strDet + "<td>-</td>";
  strDet = strDet + "<td>-</td>";
  strDet = strDet + "<td>-</td>";
  strDet = strDet + "<td>"+fixVal(amount,0,2,' ')+"</td>";
  strDet = strDet + "</tr>";

  newPrincipal=amount;

  for (i=1; i<=number; i++) { 
	newInterest=monthly*newPrincipal;
	reduction=payment-newInterest;
	newPrincipal=newPrincipal-reduction;
	
	if(i%2 == 0) {
	    strDet = strDet + "<tr>";
	  }else {
	    strDet = strDet + "<tr class='even'>";
	  }
	  strDet = strDet + "<td>"+i+"</td>";
	  strDet = strDet + "<td>"+fixVal(payment,0,2,' ')+"</td>";
	  strDet = strDet + "<td>"+fixVal(newInterest,0,2,' ')+"</td>";
	  strDet = strDet + "<td>"+fixVal(reduction,0,2,' ')+"</td>";
	  strDet = strDet + "<td>"+fixVal(newPrincipal,0,2,' ')+"</td>";
	  strDet = strDet + "</tr>";
  }

  strDet = strDet + "</table>";

  document.all.det.innerHTML = strDet;
  document.all.det.style.overflow = 'visible';
}

function RSP(text) 
{
	var TextValue = text;
	var ValueLength = TextValue.length;
	var ns = '';
	var flagVar = 0;
	for (var i = 0; i < ValueLength; i++) 
	{
		if (TextValue.charAt(i) != ' ')
		{
			ns += TextValue.charAt(i);
		}
	}
return ns;
}
