<!--
//Round number functions to X decimal places, defaults to 2
function digitRound(number,X) 
{
	// rounds numbers
	X = (!X ? 2 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

//remove decimal point for calculations	
function removeDecimalPoint(value)
{
  return value.replace(/,|\./g, "");
}	

//Function to work out the amount that can be loaned
function loansum()
{
	//get form values.
	var theform = document.formCalculator1.value;
	var affordAmt = parseFloat(removeDecimalPoint(document.formCalculator1.affordAmt.value));	
	var termLeg = parseFloat(document.formCalculator1.term1.value);
	var termrates =  0;

	//pick term rate max and min afford.
	switch (termLeg)
	{
	
		case 36:
			termrates = 0.0352;
			maxaffSpend = 1600;
			minaffSpend = 28;
			var maxAffSpendMessage = 'Ce montant est trop \351lev\351.\n\nLe montant maximal est de' + maxaffSpend +  ' \u20AC.';
			var minAffSpendMessage = 'Ce montant nest pas assez \351lev\351.\n\nLe montant minimal est de' + minaffSpend +  ' \u20AC.';
			break;
			
		case 48:
			termrates = 0.02866;
			maxaffSpend = 1280;
			minaffSpend = 22;
			var maxAffSpendMessage = 'Ce montant est trop \351lev\351.\n\nLe montant maximal est de' + maxaffSpend +  ' \u20AC.';
			var minAffSpendMessage = 'Ce montant nest pas assez \351lev\351.\n\nLe montant minimal est de' + minaffSpend +  ' \u20AC.';
			break;
			

	} 
			
	
	
	//check for empty fields
	if (document.formCalculator1.affordAmt.value.length==0 || document.formCalculator1.affordAmt.value.length==null)
	{
	alert('Veuillez entrer un montant (uniquement les chiffres).');
	document.formCalculator1.affordAmt.focus();
	document.formCalculator1.affordAmt.select(); 
	return false; 
	}
	if (isNaN(affordAmt)) {
	alert('Veuillez entrer un montant (uniquement les chiffres).');
	document.formCalculator1.affordAmt.focus();
	document.formCalculator1.affordAmt.select();
	return false; 
	}		
	
	if(parseInt(affordAmt) > maxaffSpend) 
	{
		alert(maxAffSpendMessage);
		document.formCalculator1.affordAmt.focus();
		document.formCalculator1.affordAmt.select();
	} 
	else if(parseInt(affordAmt) < minaffSpend) 
	{
		alert(minAffSpendMessage);
		document.formCalculator1.affordAmt.focus();
		document.formCalculator1.affordAmt.select();		
	}else
	{
	var totAmtPayable = parseInt(affordAmt / termrates);
	document.getElementById('totalpayable').value = parseFloat(digitRound(totAmtPayable,2));
	
	}

}

function monthlySum(){
	var theform = document.formCalculator.value;
	var spendAmt = parseFloat(removeDecimalPoint(document.formCalculator.spendAmt.value));  
	var termLeg = parseFloat(document.formCalculator.term.value);
	var termrates =  0;
	//maximun and minimun spending
	var maxSpend = 50000;
	var minSpend = 800;
	//alert messages for input errors
	var maxSpendMessage = 'Ce montant est trop \351lev\351.\n\nLe montant maximal est de ' + maxSpend +  ' \u20AC.';
	var minSpendMessage = 'Ce montant nest pas assez \351lev\351.\n\nLe montant minimal est de ' + minSpend +  ' \u20AC.';	

	
	switch (termLeg)
	{

		case 36:
		if (spendAmt >= 800 && spendAmt <= 1500) 
			{termrates = 0.0352;}
			else if (spendAmt >= 1500 && spendAmt <= 3000) 
			{termrates = 0.0334;}
			else if (spendAmt >= 3000 && spendAmt <= 7000) 
			{termrates = 0.03288;}	
			else if (spendAmt >= 7000 && spendAmt <= 15000) 
			{termrates = 0.03243;}		
			else if (spendAmt >= 15000 && spendAmt <= 50000) 
			{termrates = 0.0322;}	
	
		break;
			
		case 48:
		if (spendAmt >= 800 && spendAmt <= 1500) 
			{termrates = 0.02866;}
			else if (spendAmt >= 1500 && spendAmt <= 3000) 
			{termrates = 0.0267;}
			else if (spendAmt >= 3000 && spendAmt <= 7000) 
			{termrates = 0.02622;}	
			else if (spendAmt >= 7000 && spendAmt <= 15000) 
			{termrates = 0.02575;}		
			else if (spendAmt >= 15000 && spendAmt <= 50000) 
			{termrates = 0.02551;}	
		break;	
	} 

	
	
//check for empty fields
	if (document.formCalculator.spendAmt.value.length==0 || document.formCalculator.spendAmt.value.length==null)
	{
	alert('Veuillez entrer un montant (uniquement les chiffres).');
	document.formCalculator.spendAmt.focus();
	document.formCalculator.spendAmt.select(); 
	return false; 
	}	
	if (isNaN(spendAmt)) {
	alert('Veuillez entrer un montant (uniquement les chiffres).');
	document.formCalculator.spendAmt.focus();
	document.formCalculator.spendAmt.select();
	return false; 
	}		
	
	
	if(parseInt(spendAmt) > maxSpend) 
	{
		alert(maxSpendMessage);
		document.formCalculator.spendAmt.focus();
		document.formCalculator.spendAmt.select();		
	} else if(parseInt(spendAmt) < minSpend) {
		alert(minSpendMessage);
		document.formCalculator.spendAmt.focus();
		document.formCalculator.spendAmt.select();		
	}else{	var totalPayable = (spendAmt * termrates);
			document.getElementById('canAfford').value = parseFloat(digitRound(totalPayable,2));
	}
}
//-->
