function formatCurrency(num, interes)
{
	num = num.toString().replace(/\,/g,'.');
	interes.value=num;
}

function calcularCuotaHipoteca(cantidad, cantidadPts, interes, anos, cuota, cuotaPts) 
{
	var num_text = new String(cantidad.value);
	var mortAmount = new String();
	var i = 0;
	var j = 0;
  
	for(i = 0; i<num_text.length; i++)
	{
		if(!(isNaN (num_text.charAt(i)))) //si es un numero
		{
			mortAmount += num_text.charAt(i);
		}
	}
	
	mortAmount = parseInt(mortAmount,10); 
   
	if (isNaN(mortAmount)) 
	{
		alert("Debe indicar la cantidad a hipotecar");
		cantidad.value="";
		cantidad.focus();
		return false;
	}
	else 
	{
		// 
		// check the other fields ...
		//
		var mortRate = interes.value;
		var mortRate = parseFloat(mortRate);
		if (isNaN(mortRate)) 
		{
			alert("El % interés no es numérico");
			interes.value="";
			interes.focus();
			return false;
		}
  
		//
		else 
		{
			var mortYears = anos.value;
			var mortYears = parseInt(mortYears,10);
			if (isNaN(mortYears)) 
			{
				alert("El número de años no es numérico");
				anos.value = "";
				anos.focus();
				return false;
			}
		}
	}

	//

	//  so if everything is valid, call on the monthly() function
	//  to compute the monthly payment
	//
	var res = monthly(mortAmount, mortRate, mortYears);
	cantidadPts.value = formatoPts(mortAmount) ;
	cuota.value = formatoEuros(res);
	cuotaPts.value = formatoPts(res);
}

//
// the monthly() function - following the formula
//
function monthly(mortAmount, mortRate, mortYears) 
{
	var Irate = mortRate / 1200;
	var Pmts = mortYears * 12;
	var	Loan = mortAmount;
	return Loan * (Irate / (1 - (1 / Math.pow(1+Irate,Pmts))))
}

function formatoMonetario(decimal){

	var aux;
	var formato = new String();

	aux = parseInt(decimal);
	aux = new String(aux);
	num = aux.length;
	
	// coloco los puntos

	j=0;// variable contadora de puntos
	for(i=num-1;i>=0;i--){
		if(((j)%3) == 0){// hacemos el modulo de 3 para ir colocando puntos
			formato =  aux.charAt(i) + '.' + formato;
		}
		else{
			formato =  aux.charAt(i) + formato;	
		}
		j++;
	}

	var res = new String(formato);
	res = res.slice(0,-1);
	return res;
}

//  script que pasándole, un decimal de devuele una unidad a euros
function formatoEuros(decimal)
{
	var formato = new String();
	formato = formatoMonetario(decimal);
	formato += ' €';

	return formato;
}

//script que pasándole, un decimal (en euros) de devuele una unidad de pts 
function formatoPts(decimal)
{
	var formato ;
	var aux = decimal* 166.638;
	formato = formatoMonetario(aux);
	formato += ' Pts';

	return formato;
}

