//****************************************************************************************************
//****************************************************************************************************
function ComprobarNumerico(objeto,comas,puntos,decimales,longitud) {
	var retorno,valor;
	valor = objeto.value
	
	if (!EsNumerico(objeto,objeto.value,puntos)){
		alert ("El valor introducido es erróneo: '" + valor + "'");
		objeto.focus();
		return false;
	}
	if (!ContadorComas(objeto,objeto.value,comas)) {
		alert ("El valor introducido es erróneo");
		objeto.focus();
		return false;
	}
	if (objeto.value.length>0) {
		objeto.value=(FormatearImporte(objeto.value,puntos,decimales,longitud));
	}
	return true;
}	
	

//****************************************************************************************************
//****************************************************************************************************
function EsNumerico(obj,dato,puntos)
{
	var i;
	var longitud;
	var caracter;
	var sumo = 0;
	longitud = dato.length;
	valido = "0123456789,";
	if (puntos=='s')
	{
		valido = valido + ".";
	}
	for ( i=0; i <= longitud; i++)
	{
		caracter = dato.substring(i,i+1)
		if (valido.indexOf(caracter) == -1)
		{
			obj.value = dato.substring(0,i);
			return false;
		}
	}
	return true;
}
//****************************************************************************************************
//****************************************************************************************************
function ContadorComas(obj,dato,comas)
{
  var contador;
  var i;
  contador = 0;
  for (i=0;i<dato.length;i++)
  {
		if (dato.substring(i,i+1)==",")
    {
			contador++;
      if (contador > comas)
			{
				obj.value = dato.substring(0,i);
				return false;
			}
    }
	}
	return true;		
}

//****************************************************************************************************
//****************************************************************************************************
function FormatearImporte(dato,puntos,decimales,longitud)
{
	var entero;
	var decimal;
	var i;
	var posicion;
	var enteroSin,enteroCon;
	var decimalSin,decimalCon;
	
	posicion = dato.length
  for (i=0;i<dato.length;i++)
  {
		if (dato.substring(i,i+1)==",")
		{	
			posicion = i;
		}
	}
	// Recuperamos la parte entera
	entero = dato.substring(0,posicion);
	
	// Recuperamos la parte decimal
	decimal = dato.substring(posicion + 1,dato.length);
	
	// Eliminamos los puntos de la parte entera
	enteroSin = QuitarPuntos(entero);
	
	// Quitamos los ceros a la izquierda
	enteroSin = String(QuitarCerosIzq(enteroSin));

	// Si el campo lleva puntos, se los ponemos
	enteroCon = enteroSin;
	if (puntos=='s')
		enteroCon = PonerPuntos(enteroSin);

	// Eliminamos los puntos en la parte decimal
	decimalSin = QuitarPuntos(decimal);

	// Formateamos la parte decimal
	if (decimales=='s')
	{
		decimalCon = ParteDecimal(decimalSin,longitud);
		return (enteroCon + ',' + decimalCon);
	}
	else
	{
		return (enteroCon);
	}
}

//****************************************************************************************************
//****************************************************************************************************
function QuitarPuntos(dato)
{
	var aux,i;
	aux = '';

	for (i=0;i<dato.length;i++)
	{
		if (dato.substring(i,i+1)!=".")
			aux = aux + dato.substring(i,i+1);
	}
	return aux;
}

//****************************************************************************************************
//****************************************************************************************************
function PonerPuntos(dato)
{
	var aux,i;
	aux = '';

	for (i=0;i<dato.length;i++)
	{
		if (((dato.length-i)%3)==0 && (i>0))
			aux = aux + '.';
		aux = aux + dato.substring(i,i+1);
	}
	return aux;
}

//****************************************************************************************************
//****************************************************************************************************
function ParteDecimal(dato,longitud)
{
	var aux;

	if (longitud!=dato.length)
	{
		switch (dato.length)
		{	
			case 0:
				aux = "00";
				break;
			case 1:
				aux = dato + "0";
				break;
			default :
				aux = dato.substring(0,2);
		}
	}
	else
	{
		aux = dato.substring(0,2);
	}
	return aux;
}

//****************************************************************************************************
//****************************************************************************************************
function QuitarCerosIzq(dato)
{
	aux = dato * 1;
	return aux;
}

//****************************************************************************************************
//****************************************************************************************************
function calcula_cuota()
{
	var Importe;			/* IMPORTE NOMINAL DE LA OPERACION */
	var IntTecleado		/* INTERES TECLEADO */
	var IntReal;			/* INTERES REAL */
	var Tiempo;				/* TIEMPO EN AÑOS*/
	var PeriodoAnio;	/* NUMERO DE PERIODOS EN UN AÑO */
	var Cuota;				/* VALOR DE CUOTA DE AMORTIZACION */
	var NCuotas;			/* NUMERO DE CUOTAS DE AMORTIZACION */
	var Temporal;			/* OPERANDO TEMPORAL */
	var Posicion;

	// Importe de la operacion
	Importe	= document.forms[0].Importe.value;
	Importe = QuitarPuntos(Importe);
	
	// Cambiamos la coma por un punto
	Posicion = Importe.indexOf(',');
	if (Posicion != -1)
	{
		Importe = Importe.substring(0, Posicion)+'.'+Importe.substring(Posicion+1);
	}

	// Interés tecleado
	IntTecleado = document.forms[0].interes.value;
	// Cambiamos la coma por un punto
	Posicion = IntTecleado.indexOf(',');
	if (Posicion != -1)
	{
		IntTecleado = IntTecleado.substring(0, Posicion)+'.'+IntTecleado.substring(Posicion+1);
	}

	// Tiempo en años
	Tiempo			= document.forms[0].plazo.value;
	
	// Nº de pagos en un años
	
	//PeriodoAnio = "12";
	PeriodoAnio = 12 / document.forms[0].factor.value
		
	if(document.forms[0].nuCuotas)
		{
		PeriodoAnio = document.forms[0].nuCuotas.value
		}
	// Cálculo de la cuota
	IntReal			= IntTecleado / (PeriodoAnio * 100);
	NCuotas			= PeriodoAnio * Tiempo;
	Temporal		= Math.pow((1 + IntReal), NCuotas);
	
	Cuota = (Importe * IntReal * Temporal)/(Temporal - 1);
	Cuota = String(Cuota)
	// Cambiamos el punto por una coma
	Posicion = Cuota.indexOf('.');
	if (Posicion != -1)
	{
		Cuota = Cuota.substring(0, Posicion)+','+Cuota.substring(Posicion+1);
	}
	Cuota = FormatearImporte(Cuota,'s','s','2');
	document.forms[0].salida.value = Cuota;
}

//****************************************************************************************************
//****************************************************************************************************
function calcula_total()
{
	var Importe;			/* IMPORTE NOMINAL DE LA OPERACION */
	var IntTecleado		/* INTERES TECLEADO */
	var IntReal;			/* INTERES REAL */
	var Tiempo;				/* TIEMPO EN AÑOS*/
	var PeriodoAnio;	/* NUMERO DE PERIODOS EN UN AÑO */
	var Total;				/* VALOR DE Total DE AMORTIZACION */
	var NCuotas;			/* NUMERO DE CUOTAS DE AMORTIZACION */
	var Temporal;			/* OPERANDO TEMPORAL */
	var Posicion;

	// Importe de la operacion
	Importe	= document.forms[0].Importe.value;
	Importe = QuitarPuntos(Importe);
	
	// Cambiamos la coma por un punto
	Posicion = Importe.indexOf(',');
	if (Posicion != -1)
	{
		Importe = Importe.substring(0, Posicion)+'.'+Importe.substring(Posicion+1);
	}

	// Interés tecleado
	IntTecleado = document.forms[0].interes.value;
	// Cambiamos la coma por un punto
	Posicion = IntTecleado.indexOf(',');
	if (Posicion != -1)
	{
		IntTecleado = IntTecleado.substring(0, Posicion)+'.'+IntTecleado.substring(Posicion+1);
	}

	// Tiempo en años
	Tiempo			= document.forms[0].plazo.value;

	// Nº de pagos en un años
	PeriodoAnio = "12";
	PeriodoAnio = 12 / document.forms[0].opcion.value

	IntReal			= IntTecleado / (PeriodoAnio * 100);
	NCuotas			= PeriodoAnio * Tiempo;
	Temporal		= Math.pow((1 + IntReal), NCuotas);
	
	Total = (Importe * (Temporal - 1))/(IntReal * Temporal)

	Total = String(Total)
	// Cambiamos el punto por una coma
	Posicion = Total.indexOf('.');
	if (Posicion != -1)
	{
		Total = Total.substring(0, Posicion)+','+Total.substring(Posicion+1);
	}
	Total = FormatearImporte(Total,'s','s','2');
	document.forms[0].salida.value = Total;

}

//****************************************************************************************************
//****************************************************************************************************
function cuenta_vivienda()
{
	var Importe;
	var moneda;
	var Deduccion;
	var Posicion;
	
	Importe	= document.forms[0].Importe.value;
	Importe = QuitarPuntos(Importe);
	moneda  = document.forms[0].moneda.value
	// Cambiamos la coma por un punto
	Posicion = Importe.indexOf(',');
	if (Posicion != -1)
	{
		Importe = Importe.substring(0, Posicion)+'.'+Importe.substring(Posicion+1);
	}
	
	// Calculamos el 15%
	Deduccion = Importe * 0.15;
	
	if (moneda=='PT')
	{
		if (Deduccion > Number('225000'))
		{
			Deduccion = '225.000';
		}
		else
		{
			Deduccion = String(Deduccion)
			// Cambiamos el punto por una coma
			Posicion = Deduccion.indexOf('.');
			if (Posicion != -1)
			{
				Deduccion = Deduccion.substring(0, Posicion)+','+Deduccion.substring(Posicion+1);
			}
			Deduccion = FormatearImporte(Deduccion,'s','n','0');
		}
	}
	else
	{
		if (Deduccion > Number('1352.27'))
		{
			Deduccion = '1.352,27';
		}
		else
		{
			Deduccion = String(Deduccion)
			// Cambiamos el punto por una coma
			Posicion = Deduccion.indexOf('.');
			if (Posicion != -1)
			{
				Deduccion = Deduccion.substring(0, Posicion)+','+Deduccion.substring(Posicion+1);
			}
			Deduccion = FormatearImporte(Deduccion,'s','s','2');
		}
	}		
	return Deduccion;
}