<!--

function setElemVal(Form,Elem,Val) {
  // Encuentro el formulario
  var f=MM_findObj(Form);

  // Busco el elemento que me interesa
  for (x=0; x < f.elements.length; x++ ) {
     if (f.elements[x].name == Elem) {
      f.elements[x].value = Val;
    }
  }//for x
}

function getElemVal(Form,Elem) {
  // Encuentro el formulario
  var f=MM_findObj(Form);

  // Busco el elemento que me interesa
  for (x=0; x < f.elements.length; x++ ) {
     if (f.elements[x].name == Elem) {
      return f.elements[x].value;
    }
  }//for x
}

function borra() {
  // Encuentro el formulario
  var f=MM_findObj('Calhipo');

  f.reset();
}

function verificar(numero) {
  if (isNaN(numero.value)) {
    alert('El valor introducido "' + numero.value + '" no es un número.');
    numero.select();
    return false;
  }
  else {
    return true;
  }
}

function calculacuota() {
  impor = parseInt(getElemVal('Calhipo','importe'));
  entra = parseInt(getElemVal('Calhipo','entrada'));
  imp= (impor) - entra
  plazo_meses = parseInt(getElemVal('Calhipo','plazo'));
  tipo_mensual = parseFloat(getElemVal('Calhipo','tipo'));

  if ( isNaN(impor) || impor == 0 ) {
    alert("Debe rellenar el campo 'Precio'");
    return;
  }

  if ( isNaN(entra) || entra == 0 ) {
    alert("Debe rellenar el campo 'Entrada'. Si no dispone de entrada, ponga como mínimo la cifra '1'");
    return;
  }

  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
    alert("Debe rellenar el campo 'Interés'");
    return;
  }

  if ( isNaN(plazo_meses) || plazo_meses == 0 ) {
    alert("Debe rellenar el campo 'Años'");
    return;
  }

  plazo_meses = plazo_meses * 12.0;
  tipo_mensual = tipo_mensual / 1200.0;
  cuota = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
    ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 );

  setElemVal('Calhipo','cuota', Math.round(cuota * 100.0) / 100.0);
}

function calculaimporte() {
  entra = parseInt(getElemVal('Calhipo','entrada'));
  plazo_meses = parseInt(getElemVal('Calhipo','plazo'));
  tipo_mensual = parseFloat(getElemVal('Calhipo','tipo'));
  cuota = parseInt(getElemVal('Calhipo','cuota'));

  if ( isNaN(entra) || entra == 0 ) {
    alert("Debe rellenar el campo 'Entrada'. Si no dispone de entrada, ponga como mínimo la cifra '1'");
    return;
  }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
    alert("Debe rellenar el campo 'Interés'");
    return;
  }
  if ( isNaN(plazo_meses) || plazo_meses == 0 ) {
    alert("Debe rellenar el campo 'Años'");
    return;
  }
  if ( isNaN(cuota) || cuota == 0 ) {
    alert("Debe rellenar el campo 'Importe cuota'");
    return;
  }

  plazo_meses = plazo_meses * 12.0;
  tipo_mensual = tipo_mensual / 1200.0;

  imp = cuota / ( tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
    ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 ) );
  impor= entra + imp;

  setElemVal('Calhipo','importe', Math.round(impor * 100.0) / 100.0);
}

function calculaentrada() {
  impor = parseInt(getElemVal('Calhipo','importe'));
  plazo_meses = parseInt(getElemVal('Calhipo','plazo'));
  tipo_mensual = parseFloat(getElemVal('Calhipo','tipo'));
  cuota = parseInt(getElemVal('Calhipo','cuota'));

  if ( isNaN(impor) || impor == 0 ) {
    alert("Debe rellenar el campo 'Precio'");
    return;
  }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
    alert("Debe rellenar el campo 'Interés'");
    return;
  }
  if ( isNaN(plazo_meses) || plazo_meses == 0 ) {
    alert("Debe rellenar el campo 'Años'");
    return;
  }
  if ( isNaN(cuota) || cuota == 0 ) {
    alert("Debe rellenar el campo 'Importe cuota'");
    return;
  }

  plazo_meses = plazo_meses * 12.0;
  tipo_mensual = tipo_mensual / 1200.0;

  impag = cuota / ( tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
    ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 ) );
  entrada = impor - impag;

  setElemVal('Calhipo','entrada', Math.round(entrada * 100.0) / 100.0);
}

function calculaplazo() {
  impor = parseInt(getElemVal('Calhipo','importe'));
  entra = parseInt(getElemVal('Calhipo','entrada'));
  imp = impor - entra;
  cuota = parseInt(getElemVal('Calhipo','cuota'));
  tipo_mensual = parseFloat(getElemVal('Calhipo','tipo'));

  if ( isNaN(impor) || impor == 0 ) {
    alert("Debe rellenar el campo 'Precio'");
    return;
  }
  if ( isNaN(entra) || entra == 0 ) {
    alert("Debe rellenar el campo 'Entrada'. Si no dispone de entrada, ponga como mínimo la cifra '1'");
    return;
  }
  if ( isNaN(tipo_mensual) || tipo_mensual == 0.0 ) {
    alert("Debe rellenar el campo 'Interés'");
    return;
  }
  if ( isNaN(cuota) || cuota == 0 ) {
    alert("Debe rellenar el campo 'Importe cuota'");
    return;
  }

  tipo_mensual = tipo_mensual / 1200.0;

  plazo_meses = 1;
  cuota_actual = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
    ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 );
  while (cuota_actual > cuota) {
    cuota_actual = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
      ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 );
    plazo_meses++
  }
  total=(plazo_meses - 1) /12;

  setElemVal('Calhipo','plazo', parseInt(total, 10));
}

function calculatipo() {
  impor = parseInt(getElemVal('Calhipo','importe'));
  entra = parseInt(getElemVal('Calhipo','entrada'));
  imp= impor - entra;
  cuota = parseInt(getElemVal('Calhipo','cuota'));
  plazo_meses = parseInt(getElemVal('Calhipo','plazo'));

  if ( isNaN(impor) || impor == 0 ) {
    alert("Debe rellenar el campo 'Precio'");
    return;
  }
  if ( isNaN(entra) || entra == 0 ) {
    alert("Debe rellenar el campo 'Entrada'. Si no dispone de entrada, ponga como mínimo la cifra '1'");
    return;
  }
  if ( isNaN(plazo_meses) || plazo_meses == 0 ) {
    alert("Debe rellenar el campo 'Años'");
    return;
  }
  if ( isNaN(cuota) || cuota == 0 ) {
    alert("Debe rellenar el campo 'Importe cuota'");
    return;
  }

  plazo_meses = plazo_meses * 12.0;

  tipo_mensual = 20.0 / 1200.0;
  inc_tipo_mensual = 10.0 / 1200.0;

  cuota_actual = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
    ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 );
  ultima_cuota = cuota_actual

  while ( cuota_actual != cuota) {
    cuota_actual = imp * tipo_mensual * Math.pow((tipo_mensual + 1.0),plazo_meses) /
      ( Math.pow((tipo_mensual + 1.0),plazo_meses) - 1.0 );

    if ((cuota_actual-cuota)*(ultima_cuota-cuota) < 0.0) {
      inc_tipo_mensual = inc_tipo_mensual / 2.0;
    }

    if (cuota_actual > cuota) { // se aplica un interes muy alto, hay que bajarlo
      tipo_mensual= tipo_mensual - inc_tipo_mensual;
    }
    else {// se aplica un interes muy bajo, hay que subirlo
      tipo_mensual= tipo_mensual + inc_tipo_mensual;
    }
    if ((tipo_mensual * 1200.0) > 20.0) {
      alert("El tipo de interes a aplicar es superior al interes permitido por un banco (20%)");
      almacena();
      return;
    }
    if ((tipo_mensual * 1200.0) < 0.5) {
      tipo_mensual = 0.5 / 1200.0;
    }

    ultima_cuota = cuota_actual
  }
  tipo_mensual = tipo_mensual * 1200.0;

  setElemVal('Calhipo','tipo', Math.round(tipo_mensual * 100.0) / 100.0);
}

function getArgs() {
  var args = new Object();
  var query = unescape(location.search.substring(1));

  if (query) {
    var pairs = query.split("&");
    var tmp = new Array();

    for (var i = 0; i < pairs.length; i++) {
      tmp = pairs[i].split("=");
      args[tmp[0]] = tmp[1];
    }
  }

  return args;
}

function putArgs(args) {
  for (var i in args) {
    if (! isNaN(args[i])) setElemVal('Calhipo', i, args[i]);
  }

  if (args['action']) eval (args['action']+"()");
}

function Help(num) {            // Popup a help window
  var win1 = window.open("buscar.cgi?help="+num,"HelpWin","width=280,height=350,toolbar=no,resizable=yes,scrollbars=yes,directories=no");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function SubmitForm(formName){
        var f=MM_findObj(formName);
   f.submit();
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
//-->

