var monthlyPayment;
var deposit;
var totalPrice;
var totalToPay;

function CalcFinance(totalCost, terms) {
		monthlyPayment = 0;
		deposit = 0;
		totalPrice = 0;
		//totalCost = ((parseFloat(totalCost) + 105) * 1.175).toFixed(2);
		
		totalPrice = parseFloat(totalCost);
		
		var toFinance = totalCost;
		deposit = round_decimals1(toFinance * 0.10,2);
		
		var toPayNow = round_decimals1((Number(deposit)),2);
		
		
		var financeBalance = (totalCost - toPayNow).toFixed(2);
		
                
		
		var terms2 = terms;
		switch(terms.replace(/ /g, '')) {
			case '6':
			case '9':
				terms2 = terms;
                                monthlyPayment = round_decimals1(totalCost - toPayNow,2) / terms2;
                                
				break;
			case '24':
				monthlyPayment = round_decimals1(totalCost - toPayNow,2) * 0.05007;
                                terms2 = terms;
				break;
			case '36':
				monthlyPayment = round_decimals1(totalCost - toPayNow,2) * 0.0363;
                                terms2 = terms;
				break;
                        case '39':
                                monthlyPayment = round_decimals1(totalCost - toPayNow,2) * 0.04574;
                                terms2 = terms;
                                break;
                        case '12-36':
                                monthlyPayment = round_decimals1(totalCost - toPayNow,2) * 0.05142;
                                terms2 = 36;
                                break;
                        case '6-42':
                                monthlyPayment = round_decimals1(totalCost - toPayNow,2) * 0.04092;
                                terms2 = 42;
                                break;
			default:
				
				break;
		}
		
		
		
		if(terms2 == '6' || terms2 == '9') {
			//If the 'balance to finance' amount does not come to an amount which is divisible equally by 6 or 9
			//then we need to recalculate the deposit to give us a number which would leave
			//an amount which IS equally divisible by 6 or 9.
			var adjustment = (Number(totalCost - deposit) * 100).toFixed(2) % terms2;
			if(adjustment > 0) {
				deposit = parseFloat(deposit) + parseFloat(adjustment / 100);
				deposit = deposit.toFixed(2);
				monthly = (totalCost - deposit) / terms2;
				financeBalance = totalCost - deposit;
			}
			
			monthlyPayment = round_decimals1(totalCost - toPayNow,2) / terms2;
			
		
		}
		//alert(monthlyPayment + '   ' + terms2 + '  ' + deposit);
		totalToPay = parseFloat((monthlyPayment.toFixed(2) * terms2)).toFixed(2);	
		totalToPay = parseFloat(totalToPay) + parseFloat(deposit);
		
		
	}
	
	function round_decimals1(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_zeros1(result3, decimals)
	}
	function pad_with_zeros1(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
	}