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");
			return false;
		}
	/*if (document.ahorro.BF.value > 10000)
	    {    alert("La meta de su ahorros no puede exceder los 10,000 dolares");
	        return false;	
        }*/
	if (document.ahorro.BI.value < 100 || document.ahorro.BI.value=="")
	{
			alert("The initial deposit must be equal to or greater than 100.");
			document.ahorro.BI.value='100';		
			return false;					
	}		
	if (document.ahorro.BF.value<=0 || document.ahorro.BF.value=="")
	{
	    if (document.ahorro.r1[1].checked==true){
		alert("Your savings goal must be greater than 0.");
		document.ahorro.BF.value='10000';		
		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;
	//alert("vf-am"+a_m);
	
	/*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
	M_on=document.ahorro.MON.value;
	In_t=formato(eval(BF_1 - document.ahorro.MON.value));//interes acumulados
	/*************código par delimitar resultado de BF hasta 10,0000 los que tienen un solo para de // */
	//if ((BF_1) < 10000) 
	  //{ //document.ahorro.MON.value = M_on;
	    document.ahorro.INT.value = In_t; 
        document.ahorro.BF.value=formato(BF_1);
	 // }
	//else   
	 // {
	 //   if (document.ahorro.r1[1].checked==true){
	//	document.ahorro.BF.value=document.ahorro.BF.value-5;
	//	//alert(BF_1+"-"+document.ahorro.BF.value);		
	//    while (BF_1 > 10000){
	//	Calcular();}}
	//	else		
	//    {alert("El calculo de su meta excede los 10,000 dolares.\n El cálculo es: "+formato(BF_1)); }
	   /*alert("El calculo de su meta excede los 10,000 dolares.\n El cálculo es: "+formato(BF_1));
	   setearmeta();
	   Calcular();*/
	 // }	
	
	/*alert(document.ahorro.MON.value);
	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 BF1 = document.ahorro.BF.value;
	//alert(document.ahorro.BF.value);
	var IR = document.ahorro.IR.value / 1200;
	var BI = document.ahorro.BI.value;
	var AM = document.ahorro.AM.value;
	/*************código par delimitar resultado de BF hasta 10,0000
	//if (BF1 > 10000) {
	 //   BF1 = document.ahorro.BF.value='10000'; 
		//alert("hola");
	//	}
	//else
	//  { BF1 = BF1;
	//    //alert("hola2"); 
	//  }	
	//*******************************************************
	/*
		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("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;					
	} removido en modificacion de no limitar la calculadora*/
	
	/*if (AM<="0" || AM=="")
	{
		if (BI=="0" || IR=="0")
		{
			alert("El ahorro mensual debe ser mayor de 0.");
			document.ahorro.AM.value='100';		
			document.ahorro.AM.focus();
			//document.ahorro.AM.select();	
			init();
			return false;					
		}
	}*/	

        if (BI=="0" || BI=="")
		{
			alert("Your initial deposit must be greater than 0.");
			document.ahorro.BI.value='100';		
			document.ahorro.BI.focus();
			//document.ahorro.AM.select();	
			init();
			return false;					
		}	
	//if (AN =="0")
	//{
	//	alert("Escoja por cuanto tiempo desea ahorrar");//alert("El año debe ser mayores de 0.");
		//document.ahorro.AN.options[0].selected = true;//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='100'; //document.ahorro.BI.value='1000';	
			}
			//init();	
			return 100;				
		}
	}
	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='1.50';
//	document.ahorro.AN.value='1';
//	document.ahorro.INT.value='';
//}	

function setearmeta()
{
    if (document.ahorro.r1[2].checked==true){
	document.ahorro.BF.value='';}
	else {document.ahorro.BF.value='10000';}
	document.ahorro.BI.value='100';//document.ahorro.BI.value='1000';
    if (document.ahorro.r1[1].checked==true){
	document.ahorro.AM.value='';}
	else {document.ahorro.AM.value='50';}
	document.ahorro.IR.value='1.50';
	document.ahorro.AN.options[0].selected = true;
	document.ahorro.IR.value='4.50';
	document.ahorro.INT.value='';
}

function cambioIR()
{
 if (document.ahorro.AN.value==2) 
 { document.ahorro.IR.value='4.50'; }
 else if (document.ahorro.AN.value==3) 
 { document.ahorro.IR.value='4.75';} 
 else if (document.ahorro.AN.value==5) 
 { document.ahorro.IR.value='5.00';} 
}
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)
	    if (document.ahorro.r1[2].checked==true){
		    document.ahorro.BF.value='';}//!IsMoney
		else
		   {document.ahorro.BF.value='10000';}		
	if 	(!IsNumber(document.ahorro.BI.value) || document.ahorro.BI.value.length ==0)
		document.ahorro.BI.value='100';//document.ahorro.BI.value='1000';		

	if 	(!IsNumber(document.ahorro.AM.value) || document.ahorro.AM.value.length ==0)
        if (document.ahorro.r1[1].checked==true){
		    document.ahorro.AM.value='';}
		else
		   {document.ahorro.AM.value='50';}
	if 	(!IsNumber(document.ahorro.IR.value) || document.ahorro.IR.value.length ==0)
		document.ahorro.IR.value='4.50';
		
	if 	(!IsNumber(document.ahorro.AN.value) || document.ahorro.AN.value.length ==0)
		document.ahorro.AN.options[0].selected = true;//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;
  }
}