//FUNCIONES PARA CALCULO DE HIPOTECAS

	function resetear1() {
		document.getElementById("error_plazo").style.color = '';
		document.getElementById("error_interes").style.color = '';
		document.getElementById("error_precio").style.color = '';
	}
	
	function resetear2() {
		document.getElementById("error_cuota2").style.color = '';
		document.getElementById("error_interes2").style.color = '';
		document.getElementById("error_plazo2").style.color = '';
	}
	
	function calcularPrecio() {
		resetear2();
		m = document.getElementById("cuota2").value.replace(',','.');
		i = document.getElementById("interes2").value.replace(',','.');
		n = document.getElementById("plazo2").value;
		
		pago_inicial = document.getElementById("entrada").value;
				
		if(isNaN(pago_inicial) || pago_inicial == '') {
			pago_inicial = 0;
			document.getElementById("entrada").value = '';
		}
		
		if(isNaN(m) || m =='' || m==0){
			document.getElementById("error_cuota2").style.color = '#FF0000';
			result2 = '';
		}else if(!(validarPorcentajeHipoteca(i))){
			document.getElementById("error_interes2").style.color = '#FF0000';
			result2 = '';
			}else if(!(validarPlazo(n))){
				document.getElementById("error_plazo2").style.color = '#FF0000';
				document.getElementById("plazo2").value = '';
				result2 = '';
			}else{
				r=eval(i/(100*12));
				temp3 = 1 - potencia(1+r,(-12*n));
				temp4 = m*temp3;
				result2 = parseInt(temp4/r) - parseInt(pago_inicial);
			}
	
		document.getElementById("total").value = result2;
	}
	
	function validarPorcentajeHipoteca(i){
		res=true;
		if((isNaN(i))||(i>100)||(i<=0)){
			res=false;
		}
		return res;
	}
	
	function esNumerico(i){
		res=true;
		if(isNaN(i)){
			res=false
		}	
		return res;
	}
	
	function calculoMensual(){
		resetear1();

		var result;
		var m=0;
		var h=0;
		var r=0;
		var i=0;
		var n=0;
		
		gastos_iniciales = document.getElementById("gastos").value;
		ahorros_iniciales = document.getElementById("ahorros").value;
		
		if(isNaN(gastos_iniciales) || gastos_iniciales == '') {
			gastos_iniciales = 0;
			document.getElementById("gastos").value = '';
		}
		
		if(isNaN(ahorros_iniciales) || ahorros_iniciales == '') {
			ahorros_iniciales = 0;
			document.getElementById("ahorros").value = '';
		}

		h = parseInt(document.getElementById("precio").value) - parseInt(ahorros_iniciales) + parseInt(gastos_iniciales);

		n=document.getElementById("plazo").value;
		i=document.getElementById("interes").value.replace(',','.');
		if(!(validarPlazo(n))){
			document.getElementById("error_plazo").style.color = '#FF0000';
			document.getElementById("plazo").value = '';
			result = '';
		}else if(!(validarPorcentajeHipoteca(i))){
			document.getElementById("error_interes").style.color = '#FF0000';
			result = '';
			}else if(isNaN(h) || h =='' || h==0){
				document.getElementById("error_precio").style.color = '#FF0000';
				result = '';
			}else{
					r=eval(i/(100*12));
					var temp1= eval(1+r);			
					var temp2 = eval(potencia(temp1,(-12*n)));
					var temp3 = eval(1-temp2);
					var temp4 = eval(h*r);
					m=temp4/temp3;
					result = redondearValor(Math.abs(m),2);
			}
		document.getElementById("cuota").value = result;
		document.getElementById("cuota").value = document.getElementById("cuota").value.replace('.',',');
	}
	
	function redondearValor(numero,decimales)
	{
		var valor=numero*Math.pow(10,decimales);
		//Para redondear con un numero concreto de decimales mutiplico redondeo y divido
		//Compruebo si es un valor numerico
		if (isNaN(valor)) {
			valor="0";		
		}else{
			valor=Math.round(valor);
			valor=valor/Math.pow(10,decimales);		
			}
		return valor;
	}
	
	function validarPlazo(i){
		res=true;
		if((isNaN(i)) || i==''){
			res=false;
		}
		else {
			if (i>35) {
				alert("El plazo máximo para tu hipoteca es de 35 años");
				res=false;
			}
		}
		return res;
	}
	
	function potencia(base,exponente){
	var tmp=1;
	var esNegativo=0;
	var finale=0;
		if(exponente==0){
			return tmp;
		}else{
			tmp=base;
			if(exponente<0){
				esNegativo=1;
				exponente=Math.abs(exponente);
			}
			for(i=1;i<exponente;i++){
				tmp=eval(base*tmp);
			}
			if(esNegativo==1){
				tmp=eval(1/tmp);
			}
		}
		return(tmp);
	}
	

/******************** VALIDACIONES *************************/
		
	var whitespace = " \t\n\r";
	
	function isNumber(cadena)	{
		for (cont=0;cont<cadena.length;cont++) {
			if (isNaN(cadena.charAt(cont))){return false;}
		}
		return true
	}
	
	
	function isEmail (s)	{
		if (isWhitespace(s)) return false;	
		var i = 1;
		var sLength = s.length;
		while ((i < sLength) && (s.charAt(i) != "@"))	{
			i++;
		}
	
		if ((i >= sLength) || (s.charAt(i) != "@")) return false;
		else i += 2;
	
		while ((i < sLength) && (s.charAt(i) != "."))    {
			i++;
		}
	
		if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	
		else return true;
	}
	
	function isEmpty(s)	{
	   return ((s == null) || (s.length == 0));
	}
	
	function isWhitespace (s)	{
		var i;
		if (isEmpty(s)) return true;
		for (i = 0; i < s.length; i++)	{   
			var c = s.charAt(i);	
			if (whitespace.indexOf(c) == -1) return false;
		}
		return true;
	}