function formatNumber(valor) {
	var tmp = valor;
	if(tmp.match(/,/)) {
		if(tmp.match(/^,/))
			tmp = '0'+tmp;
		if(tmp.match(/,[0-9]$/))
			tmp = tmp+'0';
		if(sale = tmp.match(/([0-9]+,)([0-9]{2})[0-9]*/))
			tmp = sale[1]+sale[2];
	}
	else
		tmp = tmp + ',00';
	
	return tmp;
}

function initTarifa(targetId) {
	if (window.addEventListener) {
		document.getElementById(targetId).addEventListener('change', function(){setTarifa(targetId);}, false);
	}
	else {
		document.getElementById(targetId).onchange = function(){setTarifa(targetId);};
	}

}

theNewModalidadTotalInicial=0.00;
function setTarifa(targetId) {
	elemento = document.getElementById(targetId);
	var indice = elemento.options[elemento.selectedIndex].value;

	//alert(array_ids);//	62,220
	if( indice!=62 &&  indice!=220) {
		acumulado=0.00;
		for (i=0;i<array_ids.length;i++){
			document.getElementById('pvp_'+array_ids[i]).innerHTML='0%';
			document.getElementById('total_'+array_ids[i]).innerHTML='0,00';
			acumulado+=1.00+(eval('total_'+array_ids[i]+'_orig'))-1.00;
		}
		//alert(acumulado);
		theNewModalidadTotalInicial=modalidadTotalInicial - (acumulado);
		eval("var theNewModalidadTotalInicialPrint = '" + theNewModalidadTotalInicial + "';");
		document.getElementById('subtotal_carro').innerHTML=formatNumber( theNewModalidadTotalInicialPrint.replace(/\./, ',') );
	}else{
		for (i=0;i<array_ids.length;i++){
			document.getElementById('pvp_'+array_ids[i]).innerHTML=eval('pvp_'+array_ids[i]+'_orig');
			eval("var valorPrint = '" + eval('total_'+array_ids[i]+'_orig') + "';");
			document.getElementById('total_'+array_ids[i]).innerHTML=formatNumber( valorPrint.replace(/\./, ',') )+' Euros';
			eval("var subtotal_carro_origPrint = '" + modalidadTotalInicial + "';");
			document.getElementById('subtotal_carro').innerHTML=formatNumber(subtotal_carro_origPrint.replace(/\./, ','));
		}
	}

	var gastos =  document.getElementById(targetId + 'Euros');
	var gastosIVA =  document.getElementById(targetId + 'IVAEuros');
	var valorPrint = 0;
	var valorIVAPrint = 0;
	var valor = 0;
	var iva = 0;
	if(indice > 0) {
		// El valor
		eval("valor = " + targetId + "Tarifas[indice];");
		eval("valorPrint = '" + valor  + "';");
		valorPrint = formatNumber(valorPrint.replace(/\./, ','));
		// El IVA
		iva = valor * 0.18;
		eval("valorIVAPrint = '" + iva  + "';");
		valorIVAPrint = formatNumber(valorIVAPrint.replace(/\./, ','));
	}
	else {
		valorPrint = '0,00';
		valorIVAPrint = '0,00';
	}
	gastos.innerHTML = valorPrint;
	gastosIVA.innerHTML = valorIVAPrint;

	updateTotal(targetId, valor+iva);
}
function updateTotal(targetId, gastos) {
	var indice = elemento.options[elemento.selectedIndex].value;
	if( indice!=62 &&  indice!=220) {
		var totalInicial = theNewModalidadTotalInicial;
	}else{
		eval("var totalInicial = " + targetId + "TotalInicial;");
	}

	var suma = gastos+totalInicial;
	eval("var sumaPrint = '" + suma + "';");
	document.getElementById(targetId + 'TotalPrint').innerHTML = formatNumber(sumaPrint.replace(/\./, ','));
}

