
function ValidateForm()
{

 if ((isEmpty(document.getElementById('calcform:finPrice').value)) || (!isNumeric(document.getElementById('calcform:finPrice').value)) || document.getElementById('calcform:finPrice').value == 0) {
   	alert('Please specify the vehicle purchase price (including VAT).'); 
	document.getElementById('calcform:finPrice').focus(); 
	return false; }			
	
else if ((isEmpty(document.getElementById('finExtras').value)) || (!isNumeric(document.getElementById('finExtras').value))) {
   	alert('Please specify a valid extras amount.'); 
	document.getElementById('finExtras').focus(); 
	return false; }			
	
else if ((isEmpty(document.getElementById('finDeposit').value)) || (!isNumeric(document.getElementById('finDeposit').value))) {
   	alert('Please specify a valid deposit amount.'); 
	document.getElementById('finDeposit').focus(); 
	return false; }			
	
else if ((isEmpty(document.getElementById('finTerm').value)) || (!isNumeric(document.getElementById('finTerm').value)) || (document.getElementById('finTerm').value == 0) || (document.getElementById('finTerm').value > 72)) {
   	alert('Please specify a valid term. The maximum term is 72 months.'); 
	document.getElementById('finTerm').focus(); 
	return false; }				
	
else if ((isEmpty(document.getElementById('finBalloonPerc').value)) || (!isNumeric(document.getElementById('finBalloonPerc').value)) || document.getElementById('finBalloonPerc').value > 50) {
   	alert('Please specify a valid balloon percentage. The maximum balloon percentage is 50%'); 
	document.getElementById('finBalloonPerc').focus(); 
	return false; }				

else 
	{
	CalculateLoanAmt();
	}
}	

// ##############################################################################
// ##############################################################################
// ##############################################################################


function CalculateLoanAmt()
{
// DEAL INPUTS
var getPrice = document.getElementById('calcform:finPrice').value;
var getExtras = document.getElementById('finExtras').value;
var getDeposit = document.getElementById('finDeposit').value;
var getBalloonPerc = document.getElementById('finBalloonPerc').value;
var getBalloonAmt = document.getElementById('finBalloonAmt').value;
var getTerm = document.getElementById('finTerm').value;
var getRate = document.getElementById('finRate').value;

// ADMIN AND INSURANCE INPUTS
var getOptAdmin = document.getElementById('optAdminFee').checked;
var getAmtAdmin = document.getElementById('amtAdminFee').value;
var getOptInit = document.getElementById('optInitFee').checked;
var getAmtInit = document.getElementById('amtInitFee').value;
var getOptCPP = document.getElementById('optCPP').checked;
var getAmtCPP = 0;
var getTotalCPP = 0;
var getOptCoverPlus = document.getElementById('optCoverPlus').checked;
var getAmtCoverPlus = 0;
var getTotalCoverPlus = 0;
var getBalloonCPP = 0;
var getVAPS = 0;
var getMonthlyTotal = 0;
var userMessage = '';

// INTEREST RATE
var i = getRate;
i = i / 100.0;
i /= 12;
var pow = 1;
for (var j = 0; j < getTerm; j++)
pow = pow * (1 + i);


// LOAN AMOUNT (+-INITIATION FEE)
if (parseFloat(getDeposit) < (parseFloat(getPrice) + parseFloat(getExtras))) {
     if (getOptInit == true) {
        	var getLoan = FormatString((parseFloat(getPrice) + parseFloat(getAmtInit) + parseFloat(getExtras) - parseFloat(getDeposit)));
        	} else {
        	var getLoan = FormatString((parseFloat(getPrice) + parseFloat(getExtras) - parseFloat(getDeposit)));   
        	}
	} else {
    alert('The deposit cannot be greater than the loan amount');
	getDeposit = 0;
    document.getElementById('finDeposit').value = 0;		
   	var getLoan = FormatString((parseFloat(getPrice) + parseFloat(getExtras)));  	
	}
document.getElementById('finLoan').value = getLoan;


// RESIDUAL
getBalloonAmt = FormatString (getBalloonPerc * getPrice / 100);
document.getElementById('finBalloonAmt').value = getBalloonAmt;
getFinalLoan = (getLoan -  getBalloonAmt / pow);	


// CPP
if (getOptCPP == true) {   	

	getAmtCPP = (((parseFloat(getBalloonAmt) * 0.5) + (parseFloat(getLoan) - parseFloat(getAmtInit))) * 0.00185);       	
	getTotalCPP = getAmtCPP * getTerm;
	document.getElementById('amtCPP').value = FormatString(getAmtCPP);
	getVAPS += getAmtCPP;
	userMessage = '<p>You selected the CPP option. Your <b>monthly</b> payment of <b>R' + FormatString(getAmtCPP) +'</b> has been added to the monthly and total loan repayment amount.</p><hr size=1>';	
	} else {
	document.getElementById('amtCPP').value = 0;
	userMessage += '';	
	}
	
// COVERPLUS
if (getOptCoverPlus == true) {

	getAmtCoverPlus = ((parseFloat(getPrice) + parseFloat(getExtras)) * 0.00101);	
	getTotalCoverPlus = getAmtCoverPlus * getTerm;
	document.getElementById('amtCoverPlus').value = FormatString(getAmtCoverPlus);	
	getVAPS += getAmtCoverPlus;
	userMessage += '<p>You selected the Coverplus option. Your <b>monthly</b> payment of <b>' + FormatString(getAmtCoverPlus) +'</b> has been added to the monthly and total loan repayment amount.</p><hr size=1>';		
	} else {	
	document.getElementById('amtCoverPlus').value = 0;
	userMessage += '';
	}	
	
// REPAYMENT AMOUNT (+-ADMIN FEE +- CPP +- COVERPLUS)

var getPayments = (getFinalLoan * pow * i) / (pow - 1);

document.getElementById('finPayment').value = FormatString(getPayments);
document.getElementById('finPaymentAdminFee').value = FormatString(getAmtAdmin);

document.getElementById('finVAPS').value = FormatString(getVAPS);

var getMonthlyTotal = parseFloat(getPayments) + parseFloat(getAmtAdmin) + parseFloat(getVAPS);	
document.getElementById('finMonthly').value = FormatString(getMonthlyTotal);	

var finFinalTotal = FormatString(((parseFloat(getPayments) + parseFloat(getAmtAdmin)) * getTerm)  + parseFloat(getTotalCoverPlus) + parseFloat(getTotalCPP));
document.getElementById('finTotal').value = finFinalTotal;	

// INTEREST
document.getElementById('finInterest').value = FormatString((((parseFloat(getPayments)) * getTerm) + parseFloat(getBalloonAmt)) - parseFloat(getLoan));	
	
if (getBalloonAmt > 0) {
   	var finFinalTotalWithBalloon = FormatString(parseFloat(getBalloonAmt) + parseFloat(finFinalTotal));
   	userMessage += '<p>The total amount <b>excludes</b> the Balloon amount of <b>R' + getBalloonAmt + '</b> that will be payable at the end of the contract period. The total payable will therefore be <b>R' + finFinalTotalWithBalloon + '</b></p><hr size=1>';
} else {
   	userMessage += '';
}

document.getElementById('message').innerHTML = userMessage;

}

// ##############################################################################
// ##############################################################################
// ##############################################################################

function ClickToPrint()
{ 
var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=650,height=600,left=100,top=25"; 

var strAdmin;
var strInitiation;
var strCPP;
var strCoverPlus;
if (document.getElementById('optAdminFee').checked){strAdmin = "Yes"} else {strAdmin = "No"};
if (document.getElementById('optInitFee').checked){strInitiation = "Yes"} else {strInitiation = "No"};
if (document.getElementById('optCPP').checked){strCPP = "Yes"} else {strCPP = "No"};
if (document.getElementById('optCoverPlus').checked){strCoverPlus = "Yes"} else {strCoverPlus = "No"};
var screenUserMessage = document.getElementById('message').innerHTML;
var printUserMessage = screenUserMessage.replace(/<HR SIZE=1>/g, "<br>");

var content_value = "<table border=1 bordercolor=#e6e6e6 cellpadding=4 cellspacing=0>";
	content_value += "<tr><td colspan=5><b>Vehicle Repayment Calculator</td></tr>";
	content_value += "<tr><td colspan=2 bgcolor=#e6e6e6><b>Vehicle Finance Inputs</td>";	
	content_value += "<td colspan=3 bgcolor=#e6e6e6><b>Bank fees and insurance options</td></tr>";			
	content_value += "<tr><td width=25%>Purchase price:</td><td width=25%>R" + FormatString(document.getElementById('calcform:finPrice').value) + "</td>";		
	content_value += "<td width=30%>Monthly admin fee:</td><td>" + strAdmin + "</td><td width=25%>R" + FormatString(document.getElementById('amtAdminFee').value) + "</td></tr>";	
	content_value += "<tr><td width=20%>Total value of extras:</td><td>R" + FormatString(document.getElementById('finExtras').value) + "</td>";	
	content_value += "<td>Once-off initiation fee:</td><td>" + strInitiation + "</td><td>R" + FormatString(document.getElementById('amtInitFee').value) + "</td></tr>";	
	content_value += "<tr><td>Deposit:</td><td>R" + FormatString(document.getElementById('finDeposit').value) + "</td>";	
	content_value += "<td>Include Monthly CPP:</td><td>" + strCPP + "</td><td>R" + FormatString(document.getElementById('amtCPP').value) + "</td></tr>";	
	content_value += "<tr><td>Finance amount:</td><td>R" + FormatString(document.getElementById('finLoan').value) + "</td>";	
	content_value += "<td>Include Monthly Coverplus:</td><td>" + strCoverPlus + "</td><td>R" + FormatString(document.getElementById('amtCoverPlus').value) + "</td></tr>";
	content_value += "<tr><td>Term:</td><td>" + document.getElementById('finTerm').value + " months </td>";
	content_value += "<td rowspan=9 colspan=3 valign=top>" + printUserMessage + "</td></tr>";		
if (document.getElementById('calcform:siteID').value!='islamic'){
	content_value += "<tr><td>Interest rate:</td><td>" + document.getElementById('finRate').value + "%</td></tr>";
	}else{
	content_value += "<tr><td>Fixed profit ratio:</td><td>" + document.getElementById('finRate').value + "%</td></tr>";	
	}
	content_value += "<tr><td>Balloon %:</td><td>" + document.getElementById('finBalloonPerc').value + "%</td></tr>";	
	content_value += "<tr><td>Balloon amount:</td><td>R" + FormatString(document.getElementById('finBalloonAmt').value) + "</td></tr>";		
	content_value += "<tr><td>Basic repayment:</td><td>R" + FormatString(document.getElementById('finPayment').value) + "</td></tr>";	
	content_value += "<tr><td>Admin Fee:</td><td>R" + FormatString(document.getElementById('finPaymentAdminFee').value) + "</td></tr>";		
	content_value += "<tr><td>Value added products:</td><td>R" + FormatString(document.getElementById('finVAPS').value) + "</td></tr>";	
	content_value += "<tr><td><b>Monthly repayment:</td><td><b>R" + FormatString(document.getElementById('finMonthly').value) + "</td></tr>";			
	content_value += "<tr><td>Interest paid:</td><td>R" + FormatString(document.getElementById('finInterest').value) + "</td></tr>";	
	content_value += "<tr><td>Total paid:</td><td>R" + FormatString(document.getElementById('finTotal').value) + "</td></tr>";	
	content_value += "<tr><td colspan=5><b>Disclaimer:</b> All calculations made on calculators supplied on this site, together with rates quoted, are guidelines only and are subject to confirmation at the time of finalising any transactions. All information regarding the products, fees and/or costs which are included in and form a fundamental basis of the calculations are subject to change at any time prior to a final pre-agreement quote being handed to the User. The User indemnifies " + document.getElementById('calcform:sitename').value + " against any loss or liability, which the User may suffer as a result of the use of any calculator. The site and all information provided on this site and the services provided on this site, are provided ''as is''. The information provided on this site should not be treated as professional advice of any kind.</td></tr>";					
	content_value += "<table>";  
  
var docprint=window.open("","",disp_setting); 
	docprint.document.open(); 
	docprint.document.write('<html><head><title>Vehicle Purchase Price Calculator</title>'); 
	docprint.document.write('<style type="text/css">td {font-family: tahoma; font-size: 11px; color: #000000; }</style></head>');
	docprint.document.write('<body onLoad="self.print()">');                       
	docprint.document.write(content_value);          
	docprint.document.write('</body></html>'); 
	docprint.document.close(); 
	docprint.focus(); 
}


