/*NEW SIMULADOR*/
		//var UF 		= "20570.83";
	var EURO 	= "790.27";
	var DOLAR 	= "506.64";
	var UF="";
	var valor_en_UF="";
	function CALCULAR_VALORES_DESDE()
	{
		
		UF = document.getElementById('uf');
		if(UF.value==""){alert("Falta el valor de la UF");return;}
		UF.value = UF.value.replace(/,/g,'.'); 
		
		var valor_en_UF = document.getElementById('valor_uf');
		if(valor_en_UF.value==""){alert("Favor ingresar valor de la propiedad");return;}
		valor_en_UF.value = valor_en_UF.value.replace(/,/g,'.'); 

		var new_PESO = Math.round(valor_en_UF.value * UF.value);
		
		CALCULAR_DIVIDENDOS(new_PESO);
		
	}
	function CALCULAR_DIVIDENDOS(PESOS)
	{
		
		//declaramos variable fundamentales para este calculo
			var var_tasa=document.getElementById('tasa');
			if(var_tasa.value==""){alert("Favor ingresar la tasa de interes");return;}
			var_tasa.value = var_tasa.value.replace(/,/g,'.'); 
			
			var var_pie_porciento=100 - document.getElementById('monto_financiar').value;
			if(var_pie_porciento < 0){alert("Favor ingresar % a financiar");document.getElementById('monto_financiar').focus();return;}
			if(var_pie_porciento == 0){var_pie_porciento=100;}
			
			var var_annos=document.getElementById('annos');
			
		//obtenemos nuevos valores
			var tasa = (var_tasa.value /100)/12;//12 = numero de meses
			var valor_pie = (PESOS /100)*var_pie_porciento;
			var monto_credito = PESOS - valor_pie;
			var plazo = var_annos.value *12;
			if(monto_credito==0){monto_credito=PESOS;}
		//asignamos valores a financiar
		
			var RES_MF_uf = monto_credito / UF.value;
			document.getElementById('RES_MF_uf').innerHTML = "<b>"+FormatDecimal(RES_MF_uf)+"</b>";
			
			var RES_MF_dolar = monto_credito / DOLAR;
			document.getElementById('RES_MF_dolar').innerHTML = "<b>"+FormatDecimal(RES_MF_dolar)+"</b>";
			
			var RES_MF_euro = monto_credito / EURO;
			document.getElementById('RES_MF_euro').innerHTML = "<b>"+FormatDecimal(RES_MF_euro)+"</b>";
			
			var RES_MF_peso = FormatNumerico(Math.round(monto_credito), ".", ",", 0);
			document.getElementById('RES_MF_peso').innerHTML = "<b>"+RES_MF_peso+"</b>";

		//factor comun
			var factor_comun_PASO = 1 + tasa;
			var factor_comun = factor_comun_PASO;
			for (i=0; i<plazo; i++)
			{
				factor_comun = factor_comun * factor_comun_PASO;
			}
		
		//obtenemos dividendo y divisor
			var dividendo = tasa * factor_comun;
			var divisor = factor_comun - 1;
			var division_resultado = dividendo / divisor;
			
		//obtenemos el valor de el dividendo de la vivienda
			var dividendo_peso = Math.round(monto_credito * division_resultado);
		
		//asignamos valores de dividendos mensuales
			var RES_DM_uf = dividendo_peso / UF.value;
			document.getElementById('RES_DM_uf').innerHTML="<b>"+FormatDecimal(RES_DM_uf)+"</b>";
			
			var RES_DM_dolar = dividendo_peso / DOLAR;
			document.getElementById('RES_DM_dolar').innerHTML="<b>"+FormatDecimal(RES_DM_dolar)+"</b>";
			
			var RES_DM_euro = dividendo_peso / EURO;
			document.getElementById('RES_DM_euro').innerHTML="<b>"+FormatDecimal(RES_DM_euro)+"</b>";
			
			var RES_DM_peso = FormatNumerico(dividendo_peso, ".", ",", 0);
			document.getElementById('RES_DM_peso').innerHTML="<b>"+RES_DM_peso+"</b>";
			
		//asignamos valores de renta minima para el comprador
			var renta_minima_peso = Math.round(dividendo_peso * 4);
			
			var RES_RM_uf = renta_minima_peso / UF.value;
			document.getElementById('RES_RM_uf').innerHTML="<b>"+FormatDecimal(RES_RM_uf)+"</b>";
			
			var RES_RM_dolar = renta_minima_peso / DOLAR;
			document.getElementById('RES_RM_dolar').innerHTML="<b>"+FormatDecimal(RES_RM_dolar)+"</b>";
			
			var RES_RM_euro = renta_minima_peso / EURO;
			document.getElementById('RES_RM_euro').innerHTML="<b>"+FormatDecimal(RES_RM_euro)+"</b>";
			
			var RES_RM_peso = FormatNumerico(renta_minima_peso, ".", ",", 0);
			document.getElementById('RES_RM_peso').innerHTML="<b>"+RES_RM_peso+"</b>";
			
	}
	function DOS_DECIMALES(valor)
	{
		var new_valor=valor;
		new_valor=new_valor*100;
		new_valor=Math.floor(new_valor);
		new_valor=new_valor/100;
		return(new_valor);
	}
	//
	function FormatDecimal(valor)
	{
		var myString = new String(DOS_DECIMALES(valor));
		var myArray = myString.split('.');
		var decimal;
		//alert(myArray[1]);
		if(myArray[1] == undefined){decimal="";}else{decimal=","+myArray[1];}
		
		var numerico = myArray[0];
		var largo = numerico.length;
		var ind1;
		
		if(largo > 3)
		{
			ind1= FormatNumerico(myArray[0], ".", ",", 0);
		}
		else
		{
			ind1 = numerico;
		}
		
		return ind1 + decimal;
	}
	//
	function FormatNumerico(Valor, SepMil, SepDec, NumDec) 
	{
		//Comprobamos si viene con decimales y separamos el valor entero y el decimal
		var ValorEntero; var ValorEntero2 = ""; var ValorDecimal = ""; var arrayNum = Valor.toString().split("."); ValorEntero = arrayNum[0].toString();
		if (arrayNum.length==2) 
		{
			ValorDecimal = arrayNum[1].toString();
			ValorDecimal = ValorDecimal.substring(0,NumDec)
		}
		else
		{
			for (var con=1;con<=NumDec;con++) ValorDecimal += "0"; 
		}
		//Formateamos la parte entera con separador de millar pasado por par metros
		for (con=ValorEntero.length-3;con>-1;con-=3) ValorEntero2 = ValorEntero.substring(con,con+3) + SepMil + ValorEntero2;
		 
		//Eliminamos el £ltimo caracter
		ValorEntero2 = ValorEntero2.substring(0,ValorEntero2.length-1);
		
		//A¤adimos el resto de la cifra si lo hubiera calculando el resto (m¢dulo) de la divisi¢n entre 3
		if (ValorEntero.length%3>0) ValorEntero2 = ValorEntero.substring(0,ValorEntero.length%3) + SepMil + ValorEntero2;
		 
		//Devolvemos el n£mero formateado seg£n el n£mero de decimales pasado por par metros
		if (NumDec>0) return ValorEntero2 + SepDec + ValorDecimal; else

		return ValorEntero2; 
	}
	
/*START*/
	CALCULAR_VALORES_DESDE();

