var gerrflag=false;
function setear()
{
	//document.ahorro.BF.value='10000';
	document.ahorro.BI.value='5000';
	//document.ahorro.IR.value='4.35'; //document.ahorro.IR.value='1.50';
	document.ahorro.AN.value='1';
	document.ahorro.INT.value='';
}	

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function verifica()
{
	if ((document.ahorro.r1[0].checked==false) && (document.ahorro.r1[1].checked==false))
		{ alert("Debe seleccionar una opcion para realizar el calculo","Banco General");
		  return false;
		}	
}	

function Calcular()
{
	/*if (document.ahorro.r1[1].checked==true)//pregunta 2
	{ VF(); }
	else if (document.ahorro.r1[0].checked==true)//pregunta 1
	{ document.ahorro.AN.value = floor(ahorro_ano()/12);
		VF();
	}*/
	RangoTasasIR();
}	

function VF(){
	a_n=eval(document.ahorro.AN.value);	
	i_r=document.ahorro.IR.value;	
	b_i=document.ahorro.BI.value;//Math.abs(replaceChars(document.ahorro.BI.value));
	/*a_m=document.ahorro.AM.value;*****************************************IMPORTANTE QUITAR*/
	
	i=eval((i_r/100)/12);
	n=eval(a_n*12);
	v=eval(1/(1+i));
	z=Math.pow((1+i),n);
	w=Math.pow(v,n);

//BF_1=eval((eval(b_i)+a_m*((1-w)/i)*(1+i))*z);//se utiliza aqui el campo de *ahorro mensual* NR
	BF_1=eval((eval(b_i)*((1-w)/i)*(1+i))*z);
//document.ahorro.MON.value = eval(eval(b_i)+(a_m*n));//cantidad total //campo *ahorro mensual* NR
	document.ahorro.MON.value = eval(eval(b_i)+(n));//cantidad total //campo *ahorro mensual* NR
	document.ahorro.INT.value = formato(eval(BF_1 - document.ahorro.MON.value));//interes acumulados
    document.ahorro.BF.value=formato(BF_1);
  
}

function editar_campo()
{
	//if 	(!IsNumber(document.ahorro.BF.value) || document.ahorro.BF.value.length ==0 || document.ahorro.BF.value ==0 )
		//document.ahorro.BF.value='10000';//!IsMoney
		
	if 	(!IsNumber(document.ahorro.BI.value) || document.ahorro.BI.value.length ==0  || document.ahorro.BI.value ==0  || document.ahorro.BI.value <5000)
		document.ahorro.BI.value='5000';		
           
	//if 	(!IsNumber(document.ahorro.IR.value) || document.ahorro.IR.value.length ==0  || document.ahorro.IR.value ==0)
		//document.ahorro.IR.value='4.35'; //document.ahorro.IR.value='1.50';
		
	if 	(!IsNumber(document.ahorro.AN.value) || document.ahorro.AN.value.length ==0  || document.ahorro.AN.value ==0 || document.ahorro.AN.value <1)
		document.ahorro.AN.value='1';		
}

//PARA COLOCAR LA TASA DEPENDIENDO DEL BALANCE INICIAL INTRODUCIDO
function cambioIR()
{
 if (document.ahorro.BI.value >=0 && document.ahorro.BI.value <=25000) 
 { document.ahorro.IR.value='2.50'; } //de 5000 a 25000 ------
 else 
 { RangoTasasIR();} 
}

function RangoTasasIR()
{
Tasa1= 2.50; //de 5000 a 25000 //25/sep/07 4.35 ------
Tasa2= 2.00; //de 25000.01 a 50000 //25/sep/07 3.50  -------
Tasa3= 2.00; //de 50000.01 en adelante  -------

IRvalor=document.ahorro.BI.value;
//alert("AN "+document.ahorro.AN.value);

 if (IRvalor >=5000 && IRvalor <=25000) 
 { //alert('4.35 '+IRvalor);
   IRGanadoT = IRvalor*(Tasa1/100); 
   document.ahorro.INT.value=formato(IRGanadoT) * document.ahorro.AN.value;}
 else if (IRvalor >=25000.01 && IRvalor <=50000) 
 { //alert('3.50 '+IRvalor);
   IRGanado1 = 25000 * (Tasa1/100); 
   IR_restante = IRvalor - 25000;
   IRGanado2 = IR_restante * (Tasa2/100); 
   IRGanadoT = IRGanado1 + IRGanado2;
   document.ahorro.INT.value= formato(IRGanadoT) * document.ahorro.AN.value;
 } 
 else if (IRvalor >=50000.01)
 { //alert('2.50');
   IRGanado1 = 25000 * (Tasa1/100); 
   IRGanado2 = 25000 * (Tasa2/100); 
   IR_restante = IRvalor - 50000;
   IRGanado3 = IR_restante * (Tasa3/100); 
   IRGanadoT = IRGanado1 + IRGanado2 + IRGanado3;
   document.ahorro.INT.value=formato(IRGanadoT) * document.ahorro.AN.value;
 }
}
	  
//validaciones de condiciones para calcular
function ahorro_ano(){
	editar_campo();
	
    //capturar los valores de los campos
	var IR = document.ahorro.IR.value / 1200;  
	var BI = document.ahorro.BI.value;
	var AM = document.ahorro.AM.value;//***************************
	var BF1 = document.ahorro.BF.value;	
	var AN = eval(document.ahorro.AN.value);
	alert(IR+" "+BI+" "+BF1+" "+AN);

	if (BF1<="0" || BF1=="")//balance final
	{
		alert("La meta de su ahorros debe ser mayor de 0.");
		document.ahorro.BF.value='10000';		
		document.ahorro.BF.focus();
		document.ahorro.BF.select();	
		init();
		return false;					
	}
	
	if (AN<="0" || AN=="")//numero de años
	{
		alert("El año debe ser mayores de 0.");
		document.ahorro.AN.value='1';		
		document.ahorro.AN.focus();
		document.ahorro.AN.select();	
		init();
		return false;					
	}	
		
	if (BF1>0)
	{
		if (parseInt(BI) >= parseInt(BF1))
		{
			if (gerrflag) 
				gerrflag=false;
			else
			{
				gerrflag=true;
				alert("Tus ahorros actuales exceden tu meta de ahorros");
				document.ahorro.BI.value='5000';		
			}				
			init();		
			return false;
		}
	}
	
	if (IR==0)
		{			
		//var OP = (BF1 - BI) / AM;***************************************
		var OP = (BF1 - BI);
		var MA = parseInt(OP);			
		var Y = eval(document.ahorro.AN.value*12);
		var MI = (BF1 - BI) / Y;
			
		}	
	else
		{
		var Y = eval(document.ahorro.AN.value*12);
		var AM = AM - (2*AM);//*******************FALLAS ACÁ******************
		var x = Math.pow(1 + IR,Y);
		var OP = (Math.log((-BF1 * IR + AM) / (AM - IR * BI)) / Math.log(1 + IR));//****************
	//alert(BF1+" "+IR+" "+BI+" "+Math.log((-BF1 * IR) / (IR * BI))+" "+ Math.log(1 + IR));		
		//var OP = (Math.log((-BF1 * IR) / (IR * BI)) / Math.log(1 + IR));
		var MI = ((IR * (-BF1 + x * BI)) / (-1 + x))* -1;
		var MA = (OP);
	//alert(Y+" "+x+" "+OP+" "+MI+" "+ MA);		
		}

	if (MI<0)
		MI=0;
	
	if (document.ahorro.r1[0].checked==true)
	{	return(MA); }
	else if (document.ahorro.r1[1].checked==true)
	{  return(MI); }
}

function rounding(n)
{
	pennies = n * 100 ;
	pennies = Math.round(pennies) ;
	strPennies = "" + pennies ;
	len = strPennies.length ;
	return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
}


function replaceChars(entry) {
	out = ","; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}
function replaceChar(entry) {
	out = "$"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replacePct(entry) {
	out = "%"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function formato(num) 
{			
	num= Math.floor(num*Math.pow(10,2))/Math.pow(10,2); 
	return (num);	
	//return ('$' + num );	
}

function IsMoney(val)
	{
		var number="0123456789$,.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}

function IsNumber(val)
	{
		var number="0123456789.";
		//var number="0123456789.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	
function IsPct(val)
	{
		var number="0123456789.%,";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	

function addPct(pct) {
			pct=replacePct(pct);
			pct=parseFloat(pct);		
		if (checkForm(pct));
	{
			return (pct + '%');
		}
}
	
function checkForm(toCheck) {
	isNum = true;
	for (j = 0; j < toCheck.length; j++) {
	if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Favor utilizar datos numéricos solamente en todos los campos.");
  return false;
  }
  else {
  return true;
  }
}