// RESTAURANTE PASS ( Empresa )

function calculoAhorroRPempresa() {

	dias = document.getElementById("dias").value;
	importe = document.getElementById("importe").value.replace(',','.');
	empleados = document.getElementById("empleados").value;

	if (dias < 1 || dias > max_dias) {
		document.getElementById("dias").value = '';
		document.getElementById("error_dias").style.color = '#F00';
	}
	else {
		document.getElementById("error_dias").style.color = '';
	}
	
	if (importe > max_importe) {
		document.getElementById("ss_rp").value = redondearValor(document.getElementById("dias").value * (importe - max_importe) * empleados * ss);
	}
	else {
		document.getElementById("ss_rp").value = "EXENTO";
	}
	
	document.getElementById("importe_nomina").value = document.getElementById("dias").value * importe * empleados;
	document.getElementById("importe_rp").value = document.getElementById("importe_nomina").value;
	document.getElementById("ss_nomina").value = document.getElementById("importe_nomina").value * ss;
	document.getElementById("ahorro_anual").value = document.getElementById("ss_nomina").value * 11;

	document.getElementById("importe_nomina").value = redondearValor(document.getElementById("importe_nomina").value) + " ";
	document.getElementById("importe_rp").value = redondearValor(document.getElementById("importe_rp").value) + " ";
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}


// RESTAURANTE PASS ( Empresa )

function calculoAhorroRPempleado() {

	dias = document.getElementById("dias").value;
	importe = document.getElementById("importe").value.replace(',','.');
	irpf = document.getElementById("irpf").value.replace(',','.')/100;

	if (dias < 1 || dias > max_dias) {
		document.getElementById("dias").value = '';
		document.getElementById("error_dias").style.color = '#F00';
	}
	else {
		document.getElementById("error_dias").style.color = '';
	}
	
	if (importe > max_importe) {
		//alert("ss_rp = " + document.getElementById("dias").value + "* (" + importe + "-" + max_importe + ") * " + ss);
		document.getElementById("ss_rp").value = redondearValor((document.getElementById("dias").value * (importe - max_importe) * irpf) * ss);
	}
	else {
		document.getElementById("ss_rp").value = "EXENTO";
	}
	
	document.getElementById("importe_nomina").value = document.getElementById("dias").value * importe;
	document.getElementById("importe_rp").value = document.getElementById("importe_nomina").value;
	document.getElementById("ss_nomina").value = document.getElementById("importe_nomina").value * ss_empleado;
	document.getElementById("irpf_nomina").value = document.getElementById("importe_nomina").value * irpf;
	document.getElementById("total_nomina").value = document.getElementById("importe_nomina").value - (parseFloat(document.getElementById("ss_nomina").value) + parseFloat(document.getElementById("irpf_nomina").value));
	document.getElementById("total_rp").value = document.getElementById("importe_rp").value;

	document.getElementById("ahorro_anual").value = document.getElementById("total_rp").value - document.getElementById("total_nomina").value;

	document.getElementById("importe_nomina").value = redondearValor(document.getElementById("importe_nomina").value) + " ";
	document.getElementById("importe_rp").value = redondearValor(document.getElementById("importe_rp").value) + " ";
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("irpf_nomina").value = redondearValor(document.getElementById("irpf_nomina").value) + " ";
	document.getElementById("total_nomina").value = redondearValor(document.getElementById("total_nomina").value) + " ";
	document.getElementById("total_rp").value = redondearValor(document.getElementById("total_rp").value) + " ";
	
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}

// GUARDERΝA PASS ( Empresa )

function calculoAhorroGPempresa() {

	meses = document.getElementById("meses").value;
	importe = document.getElementById("importe").value.replace(',','.');
	empleados = document.getElementById("empleados").value;

	if (meses < 1 || meses > max_meses) {
		document.getElementById("meses").value = '';
		document.getElementById("error_meses").style.color = '#F00';
	}
	else {
		document.getElementById("error_meses").style.color = '';
	}
	
	document.getElementById("deduccion").value = deduccion + " %";
	
	document.getElementById("importe_nomina").value = document.getElementById("meses").value * importe * empleados;
	document.getElementById("importe_gp").value = document.getElementById("importe_nomina").value;
	document.getElementById("ss_nomina").value = document.getElementById("importe_nomina").value * ss;
	document.getElementById("deduccion_gp").value = document.getElementById("importe_nomina").value * (deduccion/100);
	
	document.getElementById("ahorro_anual").value = (parseFloat(document.getElementById("importe_nomina").value) + parseFloat(document.getElementById("ss_nomina").value)) - (parseFloat(document.getElementById("importe_gp").value) - parseFloat(document.getElementById("deduccion_gp").value));

	document.getElementById("importe_nomina").value = redondearValor(document.getElementById("importe_nomina").value) + " ";
	document.getElementById("importe_gp").value = redondearValor(document.getElementById("importe_gp").value) + " ";
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("deduccion_gp").value = redondearValor(document.getElementById("deduccion_gp").value) + " ";
	
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}

function calculoAhorroGPempleado() {

	meses = document.getElementById("meses").value;
	importe = document.getElementById("importe").value.replace(',','.');
	irpf = document.getElementById("irpf").value.replace(',','.')/100;

	if (meses < 1 || meses > max_meses) {
		document.getElementById("meses").value = '';
		document.getElementById("error_meses").style.color = '#F00';
	}
	else {
		document.getElementById("error_meses").style.color = '';
	}
	
	document.getElementById("importe_nomina").value = document.getElementById("meses").value * importe;
	document.getElementById("importe_gp").value = document.getElementById("importe_nomina").value;
	document.getElementById("ss_nomina").value = document.getElementById("importe_nomina").value * ss_empleado;
	document.getElementById("irpf_nomina").value = document.getElementById("importe_nomina").value * irpf;
	document.getElementById("total_nomina").value = parseFloat(document.getElementById("importe_nomina").value) - (parseFloat(document.getElementById("ss_nomina").value) + parseFloat(document.getElementById("irpf_nomina").value));
	document.getElementById("total_gp").value = document.getElementById("importe_gp").value;
	
	document.getElementById("ahorro_anual").value = document.getElementById("total_gp").value - document.getElementById("total_nomina").value;

	document.getElementById("importe_nomina").value = redondearValor(document.getElementById("importe_nomina").value) + " ";
	document.getElementById("importe_gp").value = redondearValor(document.getElementById("importe_gp").value) + " ";
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("irpf_nomina").value = redondearValor(document.getElementById("irpf_nomina").value) + " ";
	document.getElementById("total_nomina").value = redondearValor(document.getElementById("total_nomina").value) + " ";
	document.getElementById("total_gp").value = redondearValor(document.getElementById("total_gp").value) + " ";
	
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}

// FORMACIΣN PASS ( Empresa )

function calculoAhorroFPempresa() {

	importe = document.getElementById("importe").value.replace(',','.');
	empleados = document.getElementById("empleados").value;
	
	document.getElementById("deduccion").value = deduccion + " %";
	
	document.getElementById("importe_nomina").value = importe * empleados;
	document.getElementById("importe_gp").value = document.getElementById("importe_nomina").value;
	document.getElementById("ss_nomina").value = document.getElementById("importe_nomina").value * ss;
	document.getElementById("deduccion_gp").value = document.getElementById("importe_nomina").value * (deduccion/100);
	
	document.getElementById("ahorro_anual").value = (parseFloat(document.getElementById("importe_nomina").value) + parseFloat(document.getElementById("ss_nomina").value)) - (parseFloat(document.getElementById("importe_gp").value) - parseFloat(document.getElementById("deduccion_gp").value));

	document.getElementById("importe_nomina").value = redondearValor(document.getElementById("importe_nomina").value) + " ";
	document.getElementById("importe_gp").value = redondearValor(document.getElementById("importe_gp").value) + " ";
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("deduccion_gp").value = redondearValor(document.getElementById("deduccion_gp").value) + " ";
	
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}

function calculoAhorroFPempleado() {

	importe = document.getElementById("importe").value.replace(',','.');
	irpf = document.getElementById("irpf").value.replace(',','.')/100;

	document.getElementById("ss_nomina").value = importe * ss_empleado;
	document.getElementById("irpf_nomina").value = importe * irpf;

	if (document.getElementById("irpf").value != '') {
		document.getElementById("deduccion_gp").value = "EXENTO";	
	}
	else {
		document.getElementById("deduccion_gp").value = '';		
	}
	

	if (document.getElementById("irpf_nomina").value != '' && document.getElementById("ss_nomina").value != '') {
		document.getElementById("ahorro_anual").value = parseFloat(document.getElementById("irpf_nomina").value) + parseFloat(document.getElementById("ss_nomina").value);
	}
	else {
		document.getElementById("ahorro_anual").value = '';
	}
	
	if (document.getElementById("importe").value != '') {
		document.getElementById("ss_gp").value = "EXENTO";
	}
	else {
		document.getElementById("ss_gp").value = "";
	}
	
	document.getElementById("ss_nomina").value = redondearValor(document.getElementById("ss_nomina").value) + " ";
	document.getElementById("irpf_nomina").value = redondearValor(document.getElementById("irpf_nomina").value) + " ";
	
	document.getElementById("ahorro_anual").value = redondearValor(document.getElementById("ahorro_anual").value) + " ";

}

function redondearValor(sVal) {
	var nDec = 2;
	var n = parseFloat(sVal); 
    var s = "0.00"; 
    if (!isNaN(n)){ 
     n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec); 
     s = String(n); 
     s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1); 
     s = s.substr(0, s.indexOf(".") + nDec + 1); 
    } 
	
	//prefix = prefix || '';
	num = s + '';
	var splitStr = num.split('.');
	var splitLeft = splitStr[0];
	var splitRight = splitStr.length > 1 ? ',' + splitStr[1] : '';
	var regx = /(\d+)(\d{3})/;
	while (regx.test(splitLeft)) {
	splitLeft = splitLeft.replace(regx, '$1' + '.' + '$2');
	}
	
	resultado = splitLeft + splitRight;
	
	return resultado;
	
}

var nav4 = window.Event ? true : false;

function esNumerico(evt){	
	// Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || key == 8 || (key >= 48 && key <= 57));
}


function esReal(evt, valor){	
	// Backspace = 8, Enter = 13, '0' = 48, '9' = 57, ',' = 44, '.' = 46
	var key = nav4 ? evt.which : evt.keyCode;
	
	// Permite introducir un . (46) o una , (44) y solo uno de ellos para separar los decimales
	if (valor.indexOf('.') == -1 && valor.indexOf(',') == -1) {
		return (key <= 13 || key == 8 || (key >= 48 && key <= 57) || key == 44 || key == 46);		
	}
	else {
		return (key <= 13 || key == 8 || (key >= 48 && key <= 57));
	}
}