<!-- script que le introduces un decimal y te lo formatea en formato entero y con puntos	-->
<!-- autor :Salvador García Rizos															-->

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 -->
<!-- autor :Salvador García Rizos									-->

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 -->
<!-- autor :Salvador García Rizos											  -->

function formatoPts(decimal){

	var formato ;
	var aux = decimal* 166.638;
	formato = formatoMonetario(aux);
	formato += ' Pts';
	
	return formato;
}



  function doMort() {
//
//  first, statements to clear the text boxes ...
//
    document.MortCalc.Amount.value=" "
    document.MortCalc.Rate.value=" "
    document.MortCalc.Years.value=" "
    document.MortCalc.Payment.value=" "
    document.MortCalc.Amount.focus()
}


function Calc(myform) {
//
//  converts & strores the text box values as integer or floating point var's
//  this function passes the value of the text box to a variable,
//   uses the partseInt() function to convert the variable to a number, and
//  then uses the isNaN() function to verify the value is a number.
//
//  To pass the text box values to the function, you must
//    pass the form (in this case, myform) to the Calc function
//
  var num_text = new String(document.MortCalc.Amount.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("Debes indicar un importe a hipotecar en la casilla Cantidad");
     document.MortCalc.Amount.value=" ";
     document.MortCalc.Amount.focus();
     return false;
  }
  else {
// 
// check the other fields ...
//
  var mortRate = document.MortCalc.Rate.value;
   var mortRate = parseFloat(mortRate);
  if (isNaN(mortRate)) {
     alert("El % interés no es numérico");
     document.MortCalc.Rate.value=" ";
     document.MortCalc.Rate.focus();
     return false;
  }
  
//
  else {
  var mortYears = document.MortCalc.Years.value;
  var mortYears = parseInt(mortYears,10);
  if (isNaN(mortYears)) {
     alert("El número de años no es numerico");
     document.MortCalc.Years.value = " ";
     document.MortCalc.Years.focus();
     return false;
   }
  }
}

//

//  so if everything is valid, call on the monthly() function
//  to compute the monthly payment
//
  var res = monthly(mortAmount, mortRate, mortYears);
  document.MortCalc.AmountPts.value = formatoPts(mortAmount) ;
  document.MortCalc.Payment.value = formatoEuros(res);
  document.MortCalc.PaymentPta.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 formatCurrency(num) {

num = num.toString().replace(/\,/g,'.');
document.MortCalc.Rate.value=num;
}

