document.MaxFractionalPartDigits = 2;
document.aIntegerElements = ['N'];
document.aIntegerQuantityElements = ['I', 'C'];
document.aDoubleQuantityElements = new Array();
document.aDoubleElements = ['R'];

function thLTrim(s)
{
  var i = 0;
  var j = 0;

  for (i = 0; i <= s.length - 1; i++)
     if (s.substring(i, i + 1) != ' ')
     {
       j = i;
       break;
     }

  return s.substring(j, s.length);
}

function thRTrim(s)
{
  var j = 0;

  for (var i = s.length - 1; i > -1; i--)
    if (s.substring(i, i + 1) != ' ')
    {
      j = i;
      break;
    }

  return s.substring(0, j + 1);
}

function thTrim(s)
{
  return thLTrim(thRTrim(s));
}

function thStringifiedNumber_thFormatBeforeParsing()
{
  var strSource = thTrim(String(this.value));
  var chrFilteredCharacter;
  var strExponentialPart = '';
  this.value = '';

  for (var i = 0; i < strSource.length; i++)
  {
    chrFilteredCharacter = strSource.substring(i, i + 1);

    if (chrFilteredCharacter != '.' && chrFilteredCharacter != ',')
    {
      this.value = this.value + chrFilteredCharacter;
    }
    else if (chrFilteredCharacter == ',')
    {
      strExponentialPart = 'e-' + String(strSource.length - 1 - i);
    }
  }

  this.value = this.value + strExponentialPart;
}

function thStringifiedNumber(s)
{
  this.value = s;

  this.thFormatBeforeParsing = thStringifiedNumber_thFormatBeforeParsing;
}

function thParseFloat(s)
{
  var temp_number = new thStringifiedNumber(s);

  temp_number.thFormatBeforeParsing();
//  alert('format = ' + temp_number.value);
  return parseFloat(temp_number.value);
}

function thParseInt(s)
{
  var temp_number = new thStringifiedNumber(s);

  temp_number.thFormatBeforeParsing();
//  alert('format = ' + temp_number.value);
  return parseInt(temp_number.value);
}

function thUpdateFields() {

//  alert('R = ' + document.Form.R.value);
  R = thParseFloat(document.Form.R.value); // Tipo de interes anual 
  N = thParseInt(document.Form.N.value); // Numero de a–os
  I = thParseFloat(document.Form.I.value); // Importe deseado
  C = thParseFloat(document.Form.C.value); // Cuota mensual
  //divisa = document.Form.divisa.options[document.Form.divisa.selectedIndex].value;
  divisa = document.Form.divisa.value;

  if(isNaN(R) || R<=0) {
    alert('es preciso introducir un valor válido para el campo Tipo de interés anual '); //quitado 31-7-2002  + R); 
  }
  else if(!isNaN(N) && N>0 && !isNaN(I) && I>0) {

    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = thRounding(C,divisa); 
    //quitado 31-7-2002 Reformat(document.Form.C);
  }

  else if(!isNaN(I) && I>0 && !isNaN(C) && C>0) {

    N = Math.round(Math.log(1-(I*R/(C*1200)))/(-12*Math.log(1+R/1200)));
    document.Form.N.value = N;
    // recalculamos C
    C = I*(R/1200)/(1-Math.pow(1+R/1200,-N*12));
    document.Form.C.value = thRounding(C,divisa); 
    //quitado 31-7-2002 Reformat(document.Form.C);
  }

  else if(!isNaN(N) && N>0 && !isNaN(C) && C>0) {

    I = C*(1-Math.pow(1+R/1200,-N*12))/(R/1200);
    document.Form.I.value = thRounding(I,divisa);
    //quitado 31-7-2002 Reformat(document.Form.I);
  }
  else {

  alert('Es preciso introducir el Tipo de interés y dos de los tres restantes campos');
  }

}



function thChangeCurrencyFields() {
  var aInterIQ, aInterDQ;

  aInterIQ = document.aIntegerQuantityElements;
  aInterDQ = document.aDoubleQuantityElements;

  document.aIntegerQuantityElements = aInterDQ;
  document.aDoubleQuantityElements = aInterIQ;

  I = thParseFloat(document.Form.I.value); // Importe deseado
  C = thParseFloat(document.Form.C.value); // Cuota mensual

  //toCurrency = document.Form.divisa.options[document.Form.divisa.selectedIndex].value;
  toCurrency = document.Form.divisa.value;

  if(!isNaN(I) && I>0)
  {
    document.Form.I.value = thChangeCurrency(I,toCurrency);
    //quitado 31-7-2002 Reformat(document.Form.I);
  }

  if(!isNaN(C) && C>0)
  {
    document.Form.C.value = thChangeCurrency(C,toCurrency);
    //quitado 31-7-2002 Reformat(document.Form.C);
  }

  return true;

}


function thChangeCurrency(anynum,toCurrency) {
  var retvalue = '';
  currency = 166.386; // Pesetas/1Euro
  
  if(toCurrency == 'Peseta') {
    // pasamos de Euros a Pesetas:
//    retvalue = Math.round(anynum*currency);
    retvalue = thRounding(anynum*currency, toCurrency);
  }
  else {     
    // pasamos de Pesetas a Euros:
    retvalue = thRounding(anynum/currency);
  }

  return retvalue;
}



function thRounding(anynum,currency) {
  var destination = '';
  var chrFilteredCharacter = '';

  var source = '';
  if(currency == 'Peseta') {
    // redondeamos a 0 decimales
    source = source + Math.round(anynum);
  }
  else {     
    // redondeamos a 2 decimales
    source = source + Math.round(anynum*100)/100;
  }

  source = String(source);

  for (var i = 0; i < source.length; i++)
  {
    chrFilteredCharacter = source.substring(i, i + 1);

    if (chrFilteredCharacter == '.')
    {
      destination = destination + ',';
    }
    else
    {
      destination = destination + chrFilteredCharacter;
    }
  }

  return destination;
}
