// JavaScript Document

<!--
    
    function cmdCalc_Click(form) {
	if(isNaN(form.txtTotal.value) || isNaN(form.txtInt.value) || isNaN(form.txtIntOnly.value) || isNaN(form.txtPeriod.value))
	{
		alert("Please enter numbers only in all form fields.");
		return false;
	}
        /*if (form.txtTotal.value == 0 || form.txtTotal.value.length == 0) {
            alert ("The Amount field can't be 0 or contain letters!");
            form.txtTotal.focus(); }
        else */ if (form.txtIntOnly.value == 0 || form.txtInt.value.length == 0) {
            alert ("The Interest Rate field can't be 0 or contain letters!");
            form.txtInt.focus(); }
        else if (form.txtPeriod.value == 0 || form.txtPeriod.value.length == 0) {
            alert ("The Payment Period field can't be 0 or contain letters!");
            form.txtPeriod.focus(); }
        else
            calculatePayment(form);
    }

    function calculatePayment(form) {
		var princ = 0;
		var prIntOnly = 0;
        var intRate = 0;
        var months = 0;
        var monthlyTotal = 0;
		var monRepayIntOnly = 0;
		var overallMonthTotam = 0;
		var totalRepay = 0;
		var totalIntPaid = 0;
	   /* get amounts */
        princ = form.txtTotal.value;
		prIntOnly = form.txtIntOnly.value;
		/* handle empty values */
		if(princ == ""){
		princ = 0;
		/*alert("y");*/
		}
		if(prIntOnly == ""){
		ptIntOnly = 0;
		/*alert("x");*/
		}
		/* do math*/
        intRate = (form.txtInt.value/100) / 12;
        months = form.txtPeriod.value * 12;
     	monthlyTotal = ((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
		monRepayIntOnly = prIntOnly * intRate;
		/*alert(monRepayIntOnly);*/
		overallMonthTotal = monthlyTotal + monRepayIntOnly;
		totalIntPaid = ((months * monthlyTotal) - princ) + (months * monRepayIntOnly);
		form.txtMonthTotal.value = round_decimals(overallMonthTotal, 2);
		totalRepay = (months * monthlyTotal) + (months * monRepayIntOnly);
		form.txtTotalRepay.value = round_decimals(totalRepay, 2);
		form.txtTotalIntPaid.value = round_decimals(totalIntPaid, 2);
		form.txtPayments.value = months;
    }
	
	/* kudos to Paul McFedries and Logophilia Limited (http://www.mcfedries.com/).*/
	function round_decimals(original_number, decimals) {
		var result1 = original_number * Math.pow(10, decimals)
		var result2 = Math.round(result1)
		var result3 = result2 / Math.pow(10, decimals)
		return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
//-->

