

function formatCurrency(num,precision)
{
  num = num.toString().replace(/\ |\,/g,'');
  if(isNaN(num)) 
  num = "0";
  cents = Math.floor((num*100+0.5)%100);
  num = Math.floor((num*100+0.5)/100).toString();
  if(cents < 10) 
	cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));
	if (precision > 0)
	{
	  return (' ' + num + ',' + cents);
	}
	else if (precision == 0)
	{
	  return (' ' + num);
	}
}

function LP_data()
{
var key=window.event.keyCode;//codigo de tecla.

if (key!=44 && key!=46)
{ if (key < 48 || key > 57)
{//si no es numero 
window.event.keyCode=0;//anula la entrada de texto.
}
}
}

function calcular()
{
sueldo = document.form1.sueldo.value;
sueldo = sueldo.toString().replace(',','.');
pagas = document.form1.pagas.value;
var indice = document.form1.contrato.selectedIndex;
var contrato = document.form1.contrato.options[indice].value;
var indice2 = document.form1.laboral.selectedIndex;
var laboral = document.form1.laboral.options[indice2].value;
hijos = document.form1.hijos.value;
if (hijos == '') { hijos = 0; }
var indice3 = document.form1.tipo.selectedIndex;
var tipo = document.form1.tipo.options[indice3].value;


if (sueldo == '')
{
	alert ('El campo Sueldo bruto anual no puede estar vacio.');
	document.form1.sueldo.focus();
}
else if (sueldo < 0)
{
	alert ('El campo Sueldo bruto anual no puede ser negativo.');
	document.form1.sueldo.focus();
}
else if (pagas == '')
{
	alert ('El campo Número de pagas no puede estar vacio.');
	document.form1.pagas.focus();
}
else if (pagas < 0)
{
	alert ('El campo Número de pagas no puede ser negativo.');
	document.form1.pagas.focus();
}
else if (hijos < 0)
{
	alert ('El campo Número de hijos no puede ser negativo.');
	document.form1.hijos.focus();
}
else
{
	// LIMITE A
	if ((hijos == 0) && (tipo == 'A')) { limite_a = ""; }
	else
	{
		if ((hijos == 1) && (tipo == 'A')) 	{ limite_a = 10750;	}
		else
		{
			if ((hijos > 1) && (tipo == 'A')) { limite_a = 12030; }
			else { limite_a = ""; }
		}
	}
	
	// LIMITE B
	if ((hijos == 0) && (tipo == 'B')) { limite_b = 10600; }
	else
	{
		if ((hijos == 1) && (tipo == 'B')) { limite_b = 11825; }
		else
		{
			if ((hijos > 1) && (tipo == 'B')) { limite_b = 13135; }
			else { limite_b = ""; }
		}
	}
	
	// LIMITE C
	
	if ((hijos == 0) && (tipo == 'C')) { limite_c = 7515; }
	else
	{
		if ((hijos == 1) && (tipo == 'C')) { limite_c = 8215; }
		else
		{
			if ((hijos > 1) && (tipo == 'C')) { limite_c = 8965; }
			else { limite_c = ""; }
		}
	}
	
	limite = limite_a + limite_b + limite_c;
	
	if (contrato == 0) { ss_mensual = sueldo * (0.0635/12); } else { ss_mensual = sueldo * (0.064/12); }
	
	if (hijos > 2) { e12 = 600; } else { e12 = 0; }
	if (laboral == 2) { e11 = 600; }
	if (laboral == 1) { e11 = 1200; }
	if (laboral == 0) { e11 = 0; }
	if (sueldo <= 8200)
	{
		e10 = 3500;
	}
	else
	{
		if (sueldo <= 13000) { 	e10 = 3500 - (0.2291 * (sueldo - 8200)); }  else { e10 = 2400; }
	}
	
	if ((hijos = "") || (hijos == 0))
	{
		e9 = 3400;
	}
	else
	{
		if (hijos == 1)
		{
			e9 = 4800;
		}
		else
		{
			if (hijos == 2)
			{
				e9 = 4900;
			}
			else
			{
				if (hijos == 3)
				{
					e9 = 5600;
				}
				else
				{
					if (hijos > 3)
					{
						e9 = 5700;
					}
				}
			}
		}
	}
	
	e13 = sueldo - e12 - e11 - e10 - e9
	
	
	// IF(E13<4161,6;0,15*E13;IF(E13<14357,52;624,24+(E13-4161,6)*0,24;IF(E13<26842,32;3071,26+(E13-14357,52)*0,28;IF(E13<46818;6567+(E13-26842,32)*0,37;13958+(E13-46818)*0,45))))
	if (e13 < 4161.6)
	{
		cuota_retencion = 0.18 * e13;
	}
	else
	{
		if ((e13 < 14357.52) && (e13 >= 4161.6))
		{
			cuota_retencion = 624.24 + (e13 - 4161.6) * 0.24;
		}
		else
		{
			if ((e13 < 26842.32) && (e13 >= 14357.52))
			{
				cuota_retencion = 3071.26 + (e13 - 14357.52) * 0.28;
			}
			else
			{
				if ((e13 < 46818) && (e13 >= 26842.32))
				{
					cuota_retencion = 6567 + (e13 - 26842.32) * 0.37;
				}
				else
				{
					cuota_retencion = 13958 + (e13 - 46818) * 0.45;
				}
			}
		}
	}
	tipo_retencion = cuota_retencion / sueldo * 100;
	
	
	if (limite == "") 
	{ 
		
		irpf = 0; 
	} 
	else
	{
		if (sueldo < limite_a) { irpf = 0; } else { irpf = Math.round(tipo_retencion,0); }
	}
	
	if (pagas > 12)
	{
		extra = (1 - irpf/100) * sueldo / pagas;
	}
	else
	{
		extra = "";
	}
	
	neto = (1 - irpf/100) * sueldo/pagas - ss_mensual;
	
	document.form1.irpf.value = formatCurrency(irpf,2);
	document.form1.extra.value = formatCurrency(extra,2);
	document.form1.neto.value = formatCurrency(neto,2);
}
}

