// JavaScript Document
function Cambia_Tipo(el){
	if (el.selectedIndex > 4 ) {//) =eval(el.options.length -1)){
		//document.getElementById("oculto_tiempo").style.display = "block";
		document.getElementById("oculto_caja_tiempo").style.display = "inline";
		document.fPrincipal.strDias.focus();
	} else {
		//document.getElementById("oculto_tiempo").style.display = "none";
		document.getElementById("oculto_caja_tiempo").style.display = "none";
	}
}

function Cambia_Tipo2(){
		document.fPrincipal.strTiempo.value="o";
		//document.getElementById("oculto_tiempo").style.display = "block";
		document.getElementById("oculto_caja_tiempo").style.display = "block";
		document.fPrincipal.strDias.style.display = "block";
		document.fPrincipal.strDias.focus();
}

function jRestablecer(f){
	document.getElementById('strCapital').value="";
	document.getElementById('strInteres').value="";
	document.getElementById('strTiempo').selectdIndex=0;
	document.getElementById('strDias').value="";
	document.getElementById('strTotal_Brutos').value="";
	document.getElementById('strTotal_Netos').value="";
	document.getElementById('strCapital').focus();
	document.getElementById('oculto_caja_tiempo').style.display='none';
}
function escribirErrores(texto)
	{
	var capa=document.getElementById('errores');
	capa.innerHTML ='';
	capa.innerHTML =texto;
	}
	
function borrarErrores()
	{
	var capa=document.getElementById('errores');
	capa.innerHTML ='&nbsp;';
	}

function jCalcular(f) {
    var Resultado;
	var nominal=document.getElementById('nominal').value;
	var capa=document.getElementById('errores');
	//fCapital = f.strCapital.value;	// Nominal --> entero y no nulo
	fCapital = document.getElementById('strCapital').value;
	fCapital = fCapital.replace("," , ".");
	if (!fCapital || !esEntero(fCapital) || fCapital == 0){
		capa.innerHTML ='';
		capa.innerHTML = "El capital debe ser un número";
		document.getElementById('strCapital').select();
		document.getElementById('strCapital').focus();
		return false;
	}
	fCapital = parseFloat(fCapital);
	//fInteres = f.strInteres.value;	// Interes	--> no nulo, decimal separado por "."
	fInteres = document.getElementById('strInteres').value;
	fInteres = fInteres.replace("," , ".");
	
	if(!fInteres || isNaN(fInteres) || fInteres == 0){
		capa.innerHTML ='';
		capa.innerHTML = "El interés debe ser un número";
		f.strInteres.select();
		f.strInteres.focus();
		return false;
	}
	fInteres = parseFloat(fInteres);

	var iopcion;
	iTiempo = f.strTiempo.selectedIndex;	// la select del periodo de tiempo, elegir uno
	if (iTiempo<=0) {
		capa.innerHTML ='';
		capa.innerHTML = "Debe seleccionar el periodo de tiempo";
		f.strTiempo.focus();
		return false;
	} else if (iTiempo<5) { 
		iopcion=1;
		iDias=f.strTiempo[f.strTiempo.selectedIndex].value;
		iDias = parseInt(iDias) * 30;	
	} else {	
		iopcion=2;	
		iDias = f.strDias.value;
		if (!iDias||!esEntero(iDias)){
			//alert("El periodo de tiempo debe ser valido."); 
			capa.innerHTML ='';
			capa.innerHTML = "Debe escribir el periodo de tiempo";
			f.strDias.select();
			f.strDias.focus();
			return false;
		} else {
			iDias = parseInt(iDias)
			switch (iTiempo) {
				case 5:
					iDias = iDias;
				break;
				
				case 6:
					iDias = iDias * 30;
				break;
				
				case 7:
					iDias = iDias * 360;
				break;
			}
			if (iDias<=360) {
				iopcion=1;	
			} else if (iDias<721) {
				iopcion=2;	
			} else {
				iopcion=3;	
			}
		}
	}
	if(nominal == 1)
		{
		fInteres = fInteres/100; //Interes diario en tanto por uno
		fInteres = fInteres + 1;
		fInteres=Math.pow(fInteres, (1/12)) - 1.0;
		fInteres=(fInteres)*100*12;
		}
    if(iopcion == 1) {
	    Resultado = (fCapital * fInteres) * iDias / 36000; //(fCapital * fInteres) * iDias / 36500;
	} else {
	    Resultado = fCapital * Math.pow((1 + fInteres / 100),(iDias / 360)) - fCapital;	//fCapital * Math.pow((1 + fInteres / 100),(iDias / 365)) - fCapital;
	}

    f.strTotal_Brutos.value = formatea(Math.round(Resultado*100)/100);
    
    /*if(iopcion == 1){
		// I_netos = I_brutos - [(15/100) * I_brutos] --> ¿¿ Los I_brutos formateados?
		f.strTotal_Netos.value = formatea(Math.round(((f.strTotal_Brutos.value * 1) - (0.18 * f.strTotal_Brutos.value))*100)/100);
	} else if (iopcion==2) {
		// I_netos = I_brutos * [(15/100) * I_brutos]
		f.strTotal_Netos.value = formatea(Math.round(((f.strTotal_Brutos.value * 1) - (0.18 * f.strTotal_Brutos.value))*100)/100);
	} else if (iopcion==3) {
		f.strTotal_Netos.value = formatea(Math.round(((f.strTotal_Brutos.value * 1) - (0.18 * f.strTotal_Brutos.value))*100)/100);
	  //Resultado = 0.60 * Resultado;
	//f.strTotal_Netos.value = formatea(Math.round((Resultado * (0.18 * Resultado))*100)/100);
	}*/
    if (fCapital<=6000) {
        impuestos = 0.19;
    } else {
        impuestos = 0.21;
    }
    f.strTotal_Netos.value = formatea(Math.round(((f.strTotal_Brutos.value * 1) - (impuestos * f.strTotal_Brutos.value))*100)/100);		
    f.strTotal_Brutos.value=f.strTotal_Brutos.value.replace("." , ","); 
    f.strTotal_Netos.value=f.strTotal_Netos.value.replace("." , ",");
	     
}


function formatea(numero) {
	numero = numero + "";
	if(numero.indexOf(".")<0) {
		numero = numero + ".00";
	} else {
		var subcadena = numero.substr(numero.length-2,2);
		if(subcadena.indexOf(".")>=0) {
			numero = numero + "0";
		}
	}	
	if(numero.length < 8) numero = numero +"\t"; 
	return numero;
}

function esEntero(numero) {
	for (var i = 0; i < numero.length; i++) {
		var oneChar = numero.charAt(i);			
		if ((oneChar < "0" || oneChar > "9") ) {
			return(false);
		}
	}
	return (true);
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/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));
return (((sign)?'':'-')  + num + ',' + cents + ' ');
}

function validar(objeto){
	if (objeto.value==""){
		objeto.value=0;
		}
	objeto.value=new NumberFormat(objeto.value).toFormatted();
}

function actualizarImpuestos(inversion) {
    if (inversion>6000) {
        impuestos = "21";
    } else {
        impuestos = "19";
    }
    document.getElementById("tipoimpuesto").innerHTML = impuestos;
}
