function esNumero(temp){
        cadenaInput=""+temp;
        if (cadenaInput=="NaN"){
                return false;
        }
        for ( var j=0 ; j <= cadenaInput.length-1 ; j++ ){
                var caracter=cadenaInput.charAt(j);
                if (caracter <"0" || caracter >"9"){
                        if (caracter!="."){
                                return false;
                        }
                }
        }
        return true;
}


function entreLimites(inf,sup,temp,cadena){
        resultado=true
        if(temp.value=="") {
                temp.value = 0
        }
        cantidad=quitaMillares(temp.value);
        if (esNumero(cantidad)){
                if (cantidad < inf || cantidad > sup){
                        alert("El valor debe estar comprendido entre "+formatoMoneda(inf,".")+" y "+formatoMoneda(sup,".")+cadena);
                        temp.value="";
                        resultado=false;
                }
        } else {
                alert("Por favor, introduzca un numero.");
                temp.value="";
                resultado=false;
        }
        return resultado;
}

function calculaPrestamo(temp){
        if (validas == 1) {
        temp.formCapital.value=quitaMillares(temp.formCapital.value);
        var capital=temp.formCapital.value;
        var tiempo=temp.formTiempo.value;
        var interes=temp.formInteres.value/1200;
        factor=Math.pow((1+interes),tiempo)
        cuota=capital * ( (factor*interes) / (factor-1) );
        temp.formCapital.value=formatoMoneda(temp.formCapital.value);
        temp.formTiempo.value=formatoMoneda(temp.formTiempo.value);
        
        if("NaN"==""+cuota || "Infinity"==""+cuota){
                cuota=0;
        }
        temp.formResultado.value=formatoMoneda(Math.round(cuota));
        }
        else{
        temp.formResultado.value=""
        }
}

var elem=new Array(6,9,11,12,13,17,19)
var datos = new Array(4)
var nomdatos =new Array("Capital inicial","Tipo de interés","Plazo de amortización","Mensualidad")
var capitemp=0
var intetemp=0
var plazotemp=0
var mensutemp=0

function borradato(num){
	document.hipoteca1.elements[num].value=""
}

function borradatos(){
	for (i=0;i<4;i++){
		document.hipoteca1.elements[i].value="";
	}
	
}

function compruebadatos(){
sigo = true
if (!(document.hipoteca1.hipot.value == "")){
document.hipoteca1.hipot.value = Delete_miles(document.hipoteca1.hipot.value);}
if (!(document.hipoteca1.mens.value == "")){
document.hipoteca1.mens.value = Delete_miles(document.hipoteca1.mens.value);}

  if (document.hipoteca1.elements[2].value > 40)
  {
	alert("El plazo maximo permitido para la amortizacion es de 40 años");
	//document.hipoteca1.elements[1].value = "";
	document.hipoteca1.elements[2].value = ""; 
	return;
  }

	for (i=0;i<4;i++){
	
		if (isNaN(document.hipoteca1.elements[i].value)){
			sigo=false
			alert("El dato introducido en "+nomdatos[i]+" no es un número"+document.hipoteca1.elements[i].value)
			break
		}
	}
	if (sigo){
		if (!(document.hipoteca1.plazo.value == "")) {
		document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) *12;}
		calculaprestamo()
	}
}


function calculaprestamo(){
var suma=0
correcto=true
	for (i=0;i<4;i++){
		datos[i]=document.hipoteca1.elements[i].value
		if (datos[i]!=""){
			suma+=1
		}
	}
	if (suma==4){
		correcto=false
		document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;
		alert("Para que los calculos puedan realizarse de forma satisfactoria es necesario que deje uno de los datos vacío");
	}else{
		if (suma<3){
			correcto=false
			document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;
			document.hipoteca1.elements[2].value = ""; 
			alert("Es necesario que introduzca tres datos")
		}
	}
	if (correcto){
		if (datos[1]==""){
			mtemp=datos[0]/datos[2]
			if (datos[3]<mtemp){
				correcto=false
				alert("Lo datos introducidos en capital, plazo y mensualidad no son acordes")
			}else{
				calculainteres()
			}
		}
		if (datos[0]==""){
			id=datos[1]/1200

			//******************Operación espejo***********************
			if (plazotemp==datos[2] && intetemp==id && parseInt(mensutemp)==datos[3]){
				res=mensutemp/((Math.pow(1+intetemp,plazotemp)*intetemp)/(Math.pow(1+intetemp,plazotemp)-1))
			}else{
				res=datos[3]/((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			}
			res=parseInt(res)
			
			document.hipoteca1.elements[0].value=res
			document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;
		}
		if (datos[2]==""){
			id=datos[1]/1200
			//res=-Math.log((datos[0]*i/datos[3])-1)/Math.log(1+id)
			dividendo1=Math.log(datos[3])
			suma1=(-datos[0]*id)
			
			suma2=datos[3]
			sumando=parseFloat(suma1)+parseFloat(suma2)
			
			dividendo2=Math.log(sumando)
			divisor=Math.log(1+id)
			
			res=(dividendo1-dividendo2)/divisor

			res=Math.ceil(res)
			document.hipoteca1.elements[2].value=res
			if ((res /12) > 40 || isNaN(res)) {
				alert("El plazo maximo permitido para la amortizacion es de 40 años");
				//document.hipoteca1.elements[1].value = "";
				document.hipoteca1.elements[2].value = ""; 
				return;
			}

			//**********Modificamos el capital inicial******************
			cinicial=datos[3]/((Math.pow(1+id,res)*id)/(Math.pow(1+id,res)-1))
			if (document.hipoteca1.elements[0].value!=parseInt(cinicial)){ 
				document.hipoteca1.elements[0].value=parseInt(cinicial)
				//document.hipoteca1.plazo.value = Math.round(parseInt(document.hipoteca1.plazo.value) / 12);
				//alert("El capital inicial ha sido modificado")
			}		
		document.hipoteca1.plazo.value = Math.round(parseInt(document.hipoteca1.plazo.value) / 12);
		}
		if (datos[3]==""){
			id=datos[1]/1200
			//res=datos[0]/((1-Math.pow(1+id,2)-datos[2])/id)
			res=datos[0]*((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			intetemp=id
			capinitemp=datos[0]
			plazotemp=datos[2]
			mensutemp=res
			dato=res+""
			indi=dato.indexOf(".")
			if (dato!=-1){
				
				deci=dato.substring(indi+1,indi+3)
				
				if (parseInt(deci)>=50){
					document.hipoteca1.elements[3].value=Math.ceil(res)
				}else{
					document.hipoteca1.elements[3].value=parseInt(res)
				}
			}else{
				res=parseInt(res)
				document.hipoteca1.elements[3].value=res
			}				document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;

		}
	}
	
}

function calculainteres(){
	inter=4.50
	
	//******************Operación espejo***********************************
			if (plazotemp==datos[2] && capinitemp==datos[0] && parseInt(mensutemp)==datos[3]){
				d3=mensutemp
				d2=plazotemp
				d0=capinitemp
			}else{
		
				d0=datos[0]
				d3=datos[3]
				d2=datos[2]
			}
	//**********************************************************************
	do{
	id=inter/1200
	res=d0*((Math.pow(1+id,d2)*id)/(Math.pow(1+id,d2)-1))
	if (parseInt(res)>d3){
			inter=inter-0.01
	}
	if (parseInt(res)<d3){
			inter= inter+0.01
	}
	
	comparo=(parseInt(d3))+parseInt(31)
	
	comparo1=(parseInt(d3))-parseInt(31)

	}
	while (parseInt(res)>parseInt(comparo) || parseInt(res)<parseInt(comparo1))
	inter=inter*100
	inter=parseInt(inter)
	inter=inter/100
	document.hipoteca1.elements[1].value=inter
	//**********Modificamos el capital inicial******************
			id=inter/1200
			cinicial=datos[3]/((Math.pow(1+id,datos[2])*id)/(Math.pow(1+id,datos[2])-1))
			if (document.hipoteca1.elements[0].value!=parseInt(cinicial)){ 
				document.hipoteca1.elements[0].value=parseInt(cinicial)
				//document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;
				//alert("El capital inicial ha sido modificado")
			}
	document.hipoteca1.plazo.value = parseInt(document.hipoteca1.plazo.value) / 12;
	capinitemp=0
	plazotemp=0
	
}

