// JavaScript Document
function clickSegundoPeriodo() {
        if (document.form.check2Periodo.checked) {
                document.getElementById("table2Periodo").style.display = '';
        } else {
                document.getElementById("table2Periodo").style.display = 'none';
        }
}

var numPago = 0;

function hastaFChange(){
    var cadFecha = document.form.hastaF.value;
    var auxArray;
    if (compFecha(cadFecha)) {
                auxArray = cadFecha.split('/');
        var dia = auxArray[0];
        var mes = auxArray[1];
        var ano = auxArray[2];

        var fecha = new Date(mes + '/' + dia + '/' + ano);
        var milisegundos = fecha.getTime();
        milisegundos += (24 * 60 * 60 * 1000); // 1 Día
        fecha = new Date(milisegundos);
        document.form.desdeF2.value = fecha.getDate() + '/' + (fecha.getMonth()+1) + '/' + fecha.getFullYear();
    }
}

function insertarPago() {
        var fila;
        if ((document.form.pagoFecha.value!='') && (document.form.pagoImporte.value!=''))
        {
          fila = document.getElementById("tablePagos").insertRow(-1);
          var celda;
          celda = fila.insertCell(0);

          celda.innerHTML = "<center>"+document.form.pagoFecha.value+"</center>";
          celda = fila.insertCell(1);
          celda.innerHTML = "<center>"+document.form.pagoImporte.value+"</center>";

          numPago++;

          celda = fila.insertCell(2);
          celda.innerHTML = crearBotonElim(numPago);
          document.form.pagoFecha.value='';
          document.form.pagoImporte.value='';
          document.form.pagoFecha.focus();
        }
        else alert('Es necesario rellenar la fecha y el importe del pago.')
}

function crearBotonElim(num){
        return '<td><input type="image" class="noborder" src="img/borrar2.png" value="borrar" onClick="javascript:eliminarPago(' + num + '); return false;"></td>';
}

function eliminarPago(num) {
        document.getElementById("tablePagos").deleteRow(num);
        var celda,i,totalFilas;
        totalFilas = document.getElementById("tablePagos").rows.length;
        for (i = num; i<totalFilas; i++){
                celda = document.getElementById("tablePagos").rows[i].cells[2];
                celda.innerHTML = crearBotonElim(i);
        }
        numPago--;
}

function cambioTipoInteres(){
  if (document.form.tipoInteres.value == 4)
  {
    document.form.interesPactado.disabled=false
    document.form.interesPactado.focus();
  }
  else
  {
    document.form.interesPactado.disabled=true;
    document.form.interesPactado.value='';
  }
}

function cambioTipoInteres2()
{
  if (document.form.tipoInteres2.value == 4)
  {
    document.form.interesPactado2.disabled=false
    document.form.interesPactado2.focus();
  }
  else
  {
    document.form.interesPactado2.disabled=true;
    document.form.interesPactado2.value='';
  }
}




function enviarFormulario()
{
  var i, totalFilas;
  if (document.form.desdeF.value=='') alert('La fecha de inicio no es válida. Por favor, reintroduzca fecha')
  else
  {
    if (document.form.hastaF.value=='') alert('La fecha final del primer período no es válida. Por favor, reintroduzca fecha')
    else
    {
      if ((document.form.check2Periodo.checked)&&(document.form.hastaF2.value==''))  alert('La fecha final del segundo período no es válida. Por favor, reintroduzca fecha')
      else
      {
        totalFilas = document.getElementById("tablePagos").rows.length;
        document.form.auxTextArea.value='';
        if (totalFilas > 1) document.form.auxTextArea.value = document.getElementById("tablePagos").rows[1].cells[0].innerHTML + ';' + document.getElementById("tablePagos").rows[1].cells[1].innerHTML;
        for (i = 2; i<totalFilas; i++)
        { document.form.auxTextArea.value = document.form.auxTextArea.value + ':' +document.getElementById("tablePagos").rows[i].cells[0].innerHTML + ';'+document.getElementById("tablePagos").rows[i].cells[1].innerHTML;
        }
        document.form.desdeF2.disabled = false;
        document.form.submit();
      }
    }
  }
}

function borrarFormulario()
{
 var totalFilas,i;
 window.table2Periodo.runtimeStyle.display = 'none';
 totalFilas = document.getElementById("tablePagos").rows.length;
 for (i = totalFilas-1; i>0; i--)
 {
   document.getElementById("tablePagos").deleteRow(i);
 }
 numPago=0;
 document.form.reset();
 document.form.capitalInicial.focus();
}


function mostrarPantalla(){
 if (document.form.auxTextArea.value!='')
 {         var aux,cant,i,aux2;
           aux = document.form.auxTextArea.value.split(":");
           cant = aux.length;
           numPago=0;
           for (i = 0; i<cant ; i++)
           {
             aux2 = aux[i].split(";");

             var fila;
             fila = document.getElementById("tablePagos").insertRow();
             var celda;
             celda = fila.insertCell();

             celda.innerText = aux2[0];
             celda = fila.insertCell();
             celda.innerText = aux2[1];

             numPago++;

             celda = fila.insertCell();
             celda.innerHTML = crearBotonElim(numPago);
           }

 }
 clickSegundoPeriodo();
 document.form.interesPactado.disabled=(document.form.tipoInteres.value != 4)
 document.form.interesPactado2.disabled=(document.form.tipoInteres2.value != 4)
 document.form.capitalInicial.focus();

}