var gerrflag=false;
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)&&(document.ahorro.r1[2].checked==false))
		{ alert("You must choose an option to complete the calculation","Banco General");
		  return false;
		}	
}	

function Calcular()
{
	if (document.ahorro.r1[2].checked==true)
	{ VF(); }
	else if (document.ahorro.r1[0].checked==true)
	{ document.ahorro.AN.value = floor(ahorro_ano()/12);
		VF();
	}
	else if (document.ahorro.r1[1].checked==true)
	{ document.ahorro.AM.value = formato(ahorro_ano());
		VF();
	}
}	

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;
	
	
	/*a_n=eval(document.ahorro.AN.value);	
	i_r=replacePct(document.ahorro.IR.value);	
	b_i=replaceChars(document.ahorro.BI.value);//Math.abs(replaceChars(document.ahorro.BI.value));
	b_i=replaceChar(b_i);
	a_m=replaceChars(document.ahorro.AM.value);
	a_m=replaceChar(a_m);*/	
	
	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);
	document.ahorro.MON.value = eval(eval(b_i)+(a_m*n));//cantidad total
	document.ahorro.INT.value = formato(eval(BF_1 - document.ahorro.MON.value));//interes acumulados
    document.ahorro.BF.value=formato(BF_1);
  
}

function ahorro_ano(){

	editar_campo();

	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 IR = replacePct(document.ahorro.IR.value) / 1200;
	var BI = replaceChars(document.ahorro.BI.value);
	var BI = replaceChar(BI);
	var AM = replaceChars(document.ahorro.AM.value);
	var AM = replaceChar(AM);
	var BF1 = replaceChars(document.ahorro.BF.value);
	var BF1 = replaceChar(BF1);*/
	
	var AN = eval(document.ahorro.AN.value);	
	
	if (BF1<="0" || BF1=="")
	{
		alert("Your savings goal must be greater than 0.");
		document.ahorro.BF.value='10000';		
		document.ahorro.BF.focus();
		document.ahorro.BF.select();	
		init();
		return false;					
	}
	
	if (AM<="0" || AM=="")
	{
		if (BI=="0" || IR=="0")
		{
			alert("Your monthly savings contribution must be greater than 0.");
			document.ahorro.AM.value='100';		
			document.ahorro.AM.focus();
			document.ahorro.AM.select();	
			init();
			return false;					
		}
	}	
	
	if (AN<="0" || AN=="")
	{
		alert("The year must be greater than 0.");
		document.ahorro.AN.value='1';		
		document.ahorro.AN.focus();
		document.ahorro.AN.select();	
		init();
		return false;					
	}	
		
	//if (checkForm(BF1) && checkForm(IR) && checkForm(BI) && checkForm(AM) && checkForm(AN));
	if (BF1>0)
	{
		if (parseInt(BI) >= parseInt(BF1))
		{
			if (gerrflag) 
				gerrflag=false;
			else
			{
				gerrflag=true;
				alert("Your current savings exceed your savings goal");
				document.ahorro.BI.value='1000';		
			}				
			init();		
			return false;
		}
	}
	
	if (IR==0)
		{			
		var OP = (BF1 - BI) / AM;
		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);
		var x = Math.pow(1 + IR,Y);
		var OP = (Math.log((-BF1 * IR + AM) / (AM - IR * BI)) / Math.log(1 + IR));
		var MI = ((IR * (-BF1 + x * BI)) / (-1 + x))* -1;
		var MA = (OP);
		
		}

	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 setear()
{
	document.ahorro.BF.value='10000';
	document.ahorro.BI.value='1000';
	document.ahorro.AM.value='100';
	document.ahorro.IR.value='2.00'; //document.ahorro.IR.value='2.25';
	document.ahorro.AN.value='1';
	document.ahorro.INT.value='';
}	


function cambioIR()
{
 if (document.ahorro.BI.value<3000) 
 { document.ahorro.IR.value='2.00'; }//2.25
 else if (document.ahorro.BI.value>=3000) 
 { document.ahorro.IR.value='2.25';} //2.50
}

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 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='1000';		
           
	if 	(!IsNumber(document.ahorro.AM.value) || document.ahorro.AM.value.length ==0  || document.ahorro.AM.value ==0)
		document.ahorro.AM.value='100';

	if 	(!IsNumber(document.ahorro.IR.value) || document.ahorro.IR.value.length ==0  || document.ahorro.IR.value ==0)
		document.ahorro.IR.value='2.00'; //document.ahorro.IR.value='2.25';
		
	if 	(!IsNumber(document.ahorro.AN.value) || document.ahorro.AN.value.length ==0  || document.ahorro.AN.value ==0)
		document.ahorro.AN.value='1';		
}	

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("Please use numerical data only in all fields.");
  return false;
  }
  else {
  return true;
  }
}