/*Desarrollo realizado por NewPyme www.abanfin.com Prohibida la reproducción total o parcial por cualquier medio salvo autorización expresa de NewPyme S.L. +34 965 106 380 www.newpyme.com */

//__________________________________________________________
// Indica el número de años que entre dos fechas son bisiestos
function ajustebisiestos(fecha_inicial,fecha_final) 
{
var mesini,mesfin,diasajuste,diaini,diafin;

diasajuste=0;

for (var year=fecha_inicial.getFullYear(); year<=fecha_final.getFullYear(); year++){
	if (Bisiesto(year))
	  diasajuste = diasajuste+1;
}
diaini = fecha_inicial.getDate()
mesini=fecha_inicial.getMonth()
diafin = fecha_final.getDate()
mesfin=fecha_final.getMonth()

if ((mesini>1 || (mesini=1 && diaini==29)) && Bisiesto(fecha_inicial.getFullYear())) 
	diasajuste = diasajuste - 1;
if ((mesfin<2)  && Bisiesto(fecha_final.getFullYear())) 
	diasajuste = diasajuste - 1;
if (diaini==29 && mesini==1 && diafin==29 && mesfin==1)
	diasajuste = diasajuste + 1;
	
  return diasajuste;
}

function Bisiesto(year) 
{
if ((year % 4 == 0) && (( year % 100 != 0) || (year % 400 ==0)))
  return true;
else
  return false;
}


//____________________________________________________________
// Comprueba que un campo no esté vacio
function tieneDatos(Valor) { 
 for (var i=0; i<Valor.length; i++) { 
   if ((" \t\n\r").indexOf(Valor.charAt(i))==-1) return true; 
   } 
 return false; 
}
 
//____________________________________________________________
// Comprueba que un campo tenga un valor de numérico

function esNumero(valor)
{ 
  var valores = "0123456789.";
  var comprobado = valor;
  seanumero="true"
  for (i = 0;  i < comprobado.length;  i++)
  {
    ch = comprobado.charAt(i);
    for (j = 0;  j < valores.length;  j++)
      if (ch == valores.charAt(j))
        break;
    if (j == valores.length) {seanumero="false";break;}
  }

  return seanumero;
}
//____________________________________________________________
// Comprueba que una variable sea número

function esNumero2(valor,variable)
{ 
  var valores = variable;
  var comprobado = valor;
  seanumero="true"
  for (i = 0;  i < comprobado.length;  i++)
  {
    ch = comprobado.charAt(i);
    for (j = 0;  j < valores.length;  j++)
      if (ch == valores.charAt(j))
        break;
    if (j == valores.length) {seanumero="false";break;}
  }

  return seanumero;
}

//____________________________________________________________
// Comprueba que un campo tenga un valor de fecha

function esFecha(Valor) { 
 seafecha="false";
 if(Valor.indexOf("/")!=-1)
 {
 seanumero=esNumero2(Valor,"0123456789/");
 	if (seanumero=="true")
 	{
	
	var DatosFecha = Valor.split("/"); 
 	var Fecha = new Date(); 
 	Fecha.setFullYear(DatosFecha[2],DatosFecha[1]-1,DatosFecha[0]); 
	if(Fecha.getMonth()==DatosFecha[1]-1 && DatosFecha[2].length==4){seafecha="true";}
 	}
 }

 if(Valor.indexOf("-")!=-1)
 {
 seanumero=esNumero2(Valor,"0123456789-");
 	if (seanumero=="true")
 	{
	
	var DatosFecha = Valor.split("-"); 
 	var Fecha = new Date(); 
 	Fecha.setFullYear(DatosFecha[2],DatosFecha[1]-1,DatosFecha[0]); 
	if(Fecha.getMonth()==DatosFecha[1]-1 && DatosFecha[2].length==4){seafecha="true";}
 	}
 }
return seafecha;
}
//---------------------------------------------------------------------------------
// Obtención de la fecha en si mísma

function valorfecha(Valor) 
{
 if(Valor.indexOf("/")!=-1)
 {
	
	var DatosFecha = Valor.split("/"); 
 	var Fecha = new Date(); 
 	Fecha.setFullYear(DatosFecha[2],DatosFecha[1]-1,DatosFecha[0]); 
	Fecha.setHours(0);
	Fecha.setMinutes(0);
	Fecha.setSeconds(0);
	Fecha.setMilliseconds(0);
 }

 if(Valor.indexOf("-")!=-1)
 {	
	var DatosFecha = Valor.split("-"); 
 	var Fecha = new Date(); 
 	Fecha.setFullYear(DatosFecha[2],DatosFecha[1]-1,DatosFecha[0]); 
	Fecha.setHours(0);
	Fecha.setMinutes(0);
	Fecha.setSeconds(0);
	Fecha.setMilliseconds(0);
 }
 return Fecha; 
}

// Suma el contenido de un array
function sumaarray(valor)
{
  var sum = 0;
  for (var i = 0; i < valor.length; i++) 
  {
    sum += valor[i];
  }
  return sum;
}

//--------------------------------------------------------------------------------------
// Formato de números sin decimales
function formato(numero) {
	numero = String(Math.round(numero))
	contador = 1
	for (pos = (numero.length - 1); pos > 0; pos--) {
		contador++
		if (contador > 3) {
			numero = numero.substring(0,pos) + "." + numero.substr(pos)
			contador = 1
		}
	}
	return(numero)
}

//-------------------------------------------------------------------------------------
// Formato de número con decimales

function formatodec(numero,moneda) 
{
var entero;
var decimal;
var coma;
var final;

if(moneda==0){final=".- Ptas.";}else{final=".- Euros";}
numero=String(Math.round(numero*100)/100);


var negativo=numero.indexOf("-");
if(negativo!=-1){numero=numero.substring(1,numero.length);}

var pos=numero.indexOf(".");
if(pos==-1){decimal=0;coma="";}else{decimal=numero.substr(pos,3);coma=","+decimal.substr(1,2);};
if (pos==-1){entero=numero;}else{entero=numero.substring(0,pos);};

	contador = 1
	for (pos = (entero.length - 1); pos > 0; pos--) {
		contador++
		if (contador > 3) {
			entero = entero.substring(0,pos) + "." + entero.substr(pos)
			contador = 1
		}
	}

	if (moneda==0){numero=entero+final;}else{numero=entero+coma+final;}
	if (negativo!=-1)
	{
		if (entero!==0 || decimal!==0)
		{numero= "-" + numero;}
		else
		{
			if (entero==0 && decimal==0)
			{}
			else
			{numero="-" + numero;}
	
		}
		
		
	}
	return (numero)
}

//---------------------------------------------------------------------------------------
// Cambia la moneda de la operación
<!--
function cambiar(form)
{
var importe;
var informa;
importe=form.importe.value*1;

if(form.moneda[0].checked){form.moneda[0].checked=0;informa=1;}
if(form.moneda[1].checked){form.moneda[1].checked=0;informa=0;}
if (informa==1){form.importe.value=Math.round((importe/166.386)*100)/100;form.moneda[1].checked=1;}
if (informa==0){form.importe.value=Math.round(importe*166.386);form.moneda[0].checked=1;}
calculos(form)
}
//-->
//---------------------------------------------------------------------------------------
// Cambia la moneda de la operación con tres campos, recálculos de cuota
<!--
function cambiartres(form)
{
var cpendiente;
var amortizacion;
var cuotainicial;
var informa;

cpendiente=form.cpendiente.value*1;
amortizacion=form.amortizacion.value*1;
cuotainicial=form.cuotainicial.value*1;

if(form.moneda[0].checked){form.moneda[0].checked=0;informa=1;}
if(form.moneda[1].checked){form.moneda[1].checked=0;informa=0;}

if (informa==1)
{
form.cpendiente.value=Math.round((cpendiente/166.386)*100)/100;
form.amortizacion.value=Math.round((amortizacion/166.386)*100)/100;
form.cuotainicial.value=Math.round((cuotainicial/166.386)*100)/100;
form.moneda[1].checked=1;
}
else
{
form.cpendiente.value=Math.round(cpendiente*166.386);
form.amortizacion.value=Math.round(amortizacion*166.386);
form.cuotainicial.value=Math.round(cuotainicial*166.386);
form.moneda[0].checked=1;
}
calculos(form)
}
//-->
