var aTextos = new Array();
aTextos[0] = "Debes dejar solo un campo en blanco";
aTextos[1] = "Has introducido una combinacion no valida de Capital, Intereses, y Plazo.";
aTextos[2] = "Se ha modificado el Capital inicial para que sea \ncoherente con el resultado de los intereses.";

function borradato(sElm) 
{
	var oElm = document.forms[0].elements[sElm];
	if(oElm) oElm.value="";
}

function toFloat(sAux) 
{
	sBux = "0";
	for(sc = 0; sc<sAux.length; sc ++) 
	{
		sCux = sAux.substr(sc, 1);
		if(!(sCux!="0" && sCux!="1" && sCux!="2" && sCux!="3" && sCux!="4" && sCux!="5" && sCux!="6" && sCux!="7" && sCux!="8" && sCux!="9"))sBux += sCux;
		if(sCux==",") sBux += ".";
	}
	return parseFloat(sBux);
}

function f_calc() 
{
	form = document.forms[0];
	capital = toFloat(form.ctl00$ContentPlaceHolder1$TextBox1.value);
	interes = parseFloat((form.ctl00$ContentPlaceHolder1$TextBox2.value.replace(",",".")));
	cuotas = 12;
	plazo = toFloat((form.ctl00$ContentPlaceHolder1$TextBox3.value));
	importe = toFloat(form.ctl00$ContentPlaceHolder1$TextBox4.value);
	ncb = 0;
	
	if(isNaN(importe) || importe==0)
	{
		ncb++;
	}
	
	if(isNaN(capital) || capital==0)
	{
		ncb++;
	}
	
	if(isNaN(interes) || interes==0)
	{
		ncb++;
	}
	
	if(isNaN(plazo) || plazo==0)
	{
		ncb++;
	}
	
	if(ncb!=1) 
	{
		alert(aTextos[0]); 
		return;
	}
	
	if(isNaN(importe) || importe==0) 
	{
		interescuo = ((interes/100)/cuotas);
		importe = Math.round((capital*interescuo*Math.pow(1+interescuo,plazo)) / (Math.pow((1+interescuo),plazo)-1)*100,0)/100;
		if(isNaN(importe)) 
		{ 
			alert(aTextos[1]); return; 
		}
		form.ctl00$ContentPlaceHolder1$TextBox4.value = toStr(importe)+"";
		
	} 
	else
	{ 
		if(isNaN(capital) || capital==0) 
		{
			interescuo = ((interes/100)/cuotas);
			capital = Math.round((Math.pow((1+interescuo),plazo)-1)*importe / (interescuo*Math.pow(1+interescuo,plazo))*100,0)/100;
			if(isNaN(capital)) { alert(aTextos[1]); return; }
			form.ctl00$ContentPlaceHolder1$TextBox1.value = toStr(capital)+"";
		} 
		else 
		{
			if(isNaN(interes) || interes==0) 
			{
				inter = 3.5;
				do 
				{
					interescuo = ((inter/100)/cuotas);
					res = capital*((Math.pow(1+interescuo,plazo)*interescuo)/(Math.pow(1+interescuo,plazo)-1))
					if(parseInt(res)>importe) inter=inter-0.01;
					if(parseInt(res)<importe) inter=inter+0.01;
					comparo=(parseInt(importe))+parseInt(31)
					comparo1=(parseInt(importe))-parseInt(31)
				} while (parseInt(res)>parseInt(comparo) || parseInt(res)<parseInt(comparo1))
				
				interes=parseInt(inter*100)/100;
				
				if(isNaN(interes)) 
				{ 
					alert(aTextos[1]); return; 
				}
				
				form.ctl00$ContentPlaceHolder1$TextBox2.value = toStr(interes);
				alert(aTextos[2]);
				interescuo = ((interes/100)/cuotas);
				capital = Math.round((Math.pow((1+interescuo),plazo)-1)*importe / (interescuo*Math.pow(1+interescuo,plazo))*100,0)/100;
				form.ctl00$ContentPlaceHolder1$TextBox1.value = toStr(capital)+"";
				return ;
			} 
			else
			{
				if(isNaN(plazo) || plazo==0) 
				{
					interescuo = (interes/cuotas/100);
					plazo = Math.round( (Math.log(importe) - Math.log(importe+(-capital*interescuo)) ) / Math.log(1+interescuo) , 0)/cuotas;
					if(isNaN(plazo)) { alert(aTextos[1]); return; }
					form.ctl00$ContentPlaceHolder1$TextBox3.value = toStr(parseInt(plazo*cuotas*10)/10);
					plazo = plazo * cuotas;
					alert(aTextos[2]);
					capital = parseInt((Math.pow((1+interescuo),plazo)-1)*importe / (interescuo*Math.pow(1+interescuo,plazo)))
					form.ctl00$ContentPlaceHolder1$TextBox1.value = toStr(capital)+"";
				}
			}
		}
	}
}

function f_replace(pajar, aguja, dedal) 
{
	pajarbis = "";
	for(sc = 0; sc<pajar.length; sc ++) 
	{
		agujabis = pajar.substr(sc,1);
		if(agujabis == aguja) 
		{ 
			pajarbis += dedal; 
		} 
		else 
		{ 
			pajarbis += agujabis; 
		}
	}
	
	return pajarbis;
}

function toStr(n) 
{
	cNum = new String(Math.abs(n));
	cNum = f_replace(cNum, ".", ",");
	pos = cNum.lastIndexOf(","); if(pos==-1) pos = cNum.length;
	pos=pos-3;
	
	while(pos>0) 
	{ 
		cNum = cNum.substr(0,pos) + "." + cNum.substr(pos,cNum.length); 
		pos=pos-3; 
	}
	
	if(n<0) cNum = "-" + cNum;
	
	pos = cNum.lastIndexOf(","); if(pos!=-1) cNum = cNum.substr(0,pos+3);
	pos = cNum.lastIndexOf(","); if(pos!=-1 && pos==cNum.length-2) cNum += "0";
	return cNum;
}

function f_write(str) 
{
	str = "<font face='Arial' size='2'>El plazo de amortizacion consta de " + str + "</font>";
	if(document.all) 
	{
		document.all['erklaren'].innerHTML = str;
	} 
	else 
	{
		document.layers['erklaren'].document.open();
		document.layers['erklaren'].document.write(str);
		document.layers['erklaren'].document.close();
	}
}

function solonumero()
{
  	try
  	{
		key=event.keyCode;
	}
	catch(e)
	{
		if(e.which != 0)
		{
			key = e.which;
		}
		else
		{
			key = e.keyCode;
		}

	}
	
	if ((key<48 && (key!=46 && key!=44)) || key>57)
	{
		return false;
	} 
}

