// JavaScript Document

	// ---------------------------------------------------------
	function GetValue(sValue)
	{
		var sRaw = "";
		var sChar = "";
		var i = 0;
	
		sValue += "";
		for (i=0; i <= sValue.length; i++) {
	
			sChar = sValue.substring(i, i+1);
	
		if ((sChar >= "0") && (sChar <= "9")){
				sRaw = sRaw + sChar;
			}
		}
	
		if (sRaw.length > 0) {
			return parseInt(sRaw);
		}
		else {
			return 0;
		}
	}
	
	// ---------------------------------------------------------
	function GetValueRate(sValue)
	{
		var sRaw = "";
		var sChar = "";
		var bHasDecimal = 0;
		var i = 0;
	
		sValue += "";
		for (i=0; i <= sValue.length; i++) {
	
			sChar = sValue.substring(i, i+1);
	
			if ((sChar >= "0") && (sChar <= "9")){
				sRaw = sRaw + sChar;
			}
	
			if ((sChar == ",")){// || (sChar == ".")){
				bHasDecimal = 1;
				sRaw = sRaw + ".";
			}
		}
	
		if (bHasDecimal) {
			return parseFloat(sRaw);
		}
	
		if (sRaw.length > 0) {
			return parseInt(sRaw);
		}
		else {
		   return 0;
		}
	}
	
	// ---------------------------------------------------------
	function FormatOutput(iValue, nDec) {
		var bIsNegative = 0;
		var iPos = 0;
		var sChar = "";
		var sTempNumber = "";
		var sNoChars = "";
		var sDollars = "";
		var sCents = "00";
		var sDollarAmount = "";
		var sFormated = "";
		var x = 0;
	
		if (iValue != "") {
	
			sTempNumber = iValue + "";
			if (sTempNumber.charAt(0) == "-") {
				bIsNegative = 1;
				sTempNumber = sTempNumber.substring(1, sTempNumber.length);
			}
	
			sTempNumber
			IndexOfDec = sTempNumber.indexOf(".");
	
			if (IndexOfDec == -1) {
				sDollars = sTempNumber;
				sCents = "00";
			}
			else if (IndexOfDec == 0) {
				sDollars = "0";
				sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length);
			}
			else {
				sDollars = sTempNumber.substring(0, IndexOfDec);
				if (IndexOfDec == (sTempNumber.length - 1)) {
					sCents = "00";
				}
				else {
					sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length)
					sCents += "0";
					sCents = sCents.charAt(0) + sCents.charAt(1);
				}
			}
	
			sFormated = sDollars;
			x = sDollars.length;
			iPos = 0;
			while (x > 0) {
				x--;
				sChar = sDollars.charAt(x);
				rounded = Math.round(iPos/3);
				if ( (iPos/3 == rounded ) & (iPos != 0) ) {
					sDollarAmount = "." + sDollarAmount;
				}
				sDollarAmount = sChar +  sDollarAmount;
				iPos++;
			}
	
			if (nDec) {
			  if (bIsNegative) {
				  sFormated = "-" + sDollarAmount + "," + sCents + "";
			  }
			  else {
				  sFormated = sDollarAmount + "," + sCents  + "";
			  }
		}
			else {
			  if (bIsNegative) {
				  sFormated = "-" + sDollarAmount + "";
			  }
			  else {
				  sFormated = sDollarAmount + "";
			  }
		}
	
			return (sFormated);
		}
		else {
	
			return("0" + "");
		}
	}
	
	// ---------------------------------------------------------
	function FormatOutputPercent(iValue, nOfDec) {
	 return(iValue + " %")
	}
	// ---------------------------------------------------------
	function CalcularPagoMensual(anios, interes, prestamo) {
	
		if (interes >= 1) {
			(interes = interes / 100);
		}
		interesMensual = (interes / 12);
	
		totalPagos = (anios * 12);
	
		if (interes == 0) {
			pago = (prestamo / totalPagos);
		}
	
		else {
			pago = (prestamo * interesMensual) / (1 - Math.pow((1+interesMensual), (-1*totalPagos)));
		}
	
		return(pago);
	}
	
	function CalcularPrestamoConPagoMensual(anios, interes, pagoMes) {
	
		if (interes >= 1) {
			(interes = interes / 100);
		}
		interesMensual = (interes / 12);
	
		totalPagos = (anios * 12);
	
		if (interes == 0) {
			prestamo = (pagoMes / totalPagos);
		}
	
		else {
			prestamo = pagoMes /((interesMensual) / (1 - Math.pow((1+interesMensual), (-1*totalPagos))));
		}
	
		return(prestamo);
	}
	
	
	
	// ---------------------------------------------------------
function ReCalcularCV() {

    // get user's data

	FPresupuestoMaximo = GetValue(document.form.PresupuestoMaximo.value);
    FIntereses           = GetValueRate(document.form.Intereses.value);
    FAnios           = GetValue(document.form.Anios.options[document.form.Anios.selectedIndex].text);
    iPagoMensual     = CalcularPagoMensual(FAnios, FIntereses, FPresupuestoMaximo);
    iPagoMensual     = Math.round(iPagoMensual * 100) / 100;
    iPagoTotal   = ((iPagoMensual * 12) * FAnios);
    iPagoTotal     = Math.round(iPagoTotal * 100) / 100;

    //// FORMAT VARIABLES
	
    FIntereses           = FormatOutput(FIntereses,2);
    iPagoMensual     = FormatOutput(iPagoMensual,2);


    //// SPIT OUT RESULTS
	document.form.PresupuestoMaximo.value =FormatOutput(FPresupuestoMaximo,0);
    document.form.Intereses.value          = FIntereses;
    document.form.PagoMensual.value    = iPagoMensual;
}
	
	
	
function RecalcularPrestamo(){
	
		FCuotaMes2 = GetValueRate(document.form.CuotaMes2.value);
		FIntereses2        = GetValueRate(document.form.Intereses2.value);
		FAnios2           = GetValue(document.form.Anios2.options[document.form.Anios2.selectedIndex].text);
		

		if (FIntereses2=="0") FIntereses2="4.5";
		
		iPrestamo2     = CalcularPrestamoConPagoMensual(FAnios2, FIntereses2, FCuotaMes2);
		iPrestamo2     = Math.round(iPrestamo2 * 100) / 100;

		//// FORMAT VARIABLES
		FCuotaMes2  = FormatOutput(FCuotaMes2,2);
		FIntereses2 = FormatOutput(FIntereses2,2);
		iPrestamo2  = FormatOutput(iPrestamo2,2);
		
		//// SPIT OUT RESULTS
		document.form.CuotaMes2.value    = FCuotaMes2;
		document.form.Intereses2.value          = FIntereses2;
		document.form.Prestamo2.value    = iPrestamo2;
	
	}
	
	
function ReCalcularAhorro(){
	
		FImporteHipoteca = GetValueRate(document.form.ImporteReciboHipoteca.value);
		FImporteVarios   = GetValueRate(document.form.ImporteRecibosVarios.value);
		FImporteTarjetas = GetValueRate(document.form.ImporteTarjetas.value);
	
		FCPHipoteca = GetValueRate(document.form.CPHipoteca.value);
		FCPPrestamos = GetValueRate(document.form.CPPrestamos.value);
		FCPTarjetas = GetValueRate(document.form.CPTarjetas.value);
		
		
		Intereses="4.5";
		Anios="30"
		
		iTotalImportes=FImporteHipoteca+FImporteVarios+FImporteTarjetas
		iTotalCP=FCPHipoteca+FCPPrestamos+FCPTarjetas
		
		iCuotaReunificada = CalcularPagoMensual(Anios, Intereses, iTotalCP);
		iCuotaReunificada = Math.round(iCuotaReunificada * 100) / 100;
		iAhorro=iTotalImportes-iCuotaReunificada

		//// FORMAT VARIABLES
		FImporteHipoteca  = FormatOutput(FImporteHipoteca,2);
		FImporteVarios = FormatOutput(FImporteVarios,2);
		FImporteTarjetas  = FormatOutput(FImporteTarjetas,2);
		
		FCPHipoteca  = FormatOutput(FCPHipoteca,2);
		FCPPrestamos = FormatOutput(FCPPrestamos,2);
		FCPTarjetas  = FormatOutput(FCPTarjetas,2);

		
		//// SPIT OUT RESULTS
		document.form.ImporteReciboHipoteca.value    = FImporteHipoteca;
		document.form.ImporteRecibosVarios.value  = FImporteVarios;
		document.form.ImporteTarjetas.value    = FImporteTarjetas;
	
		document.form.CPHipoteca.value   = FCPHipoteca;
		document.form.CPPrestamos.value  = FCPPrestamos;
		document.form.CPTarjetas.value   = FCPTarjetas;
		document.form.TotalCP.value=FormatOutput(iTotalCP,2);
		
		document.form.Ahorro.value = FormatOutput(iAhorro,2);
		document.form.PagoMensual.value  = FormatOutput(iTotalImportes,2);
		document.form.CuotaActual.value  = FormatOutput(iTotalImportes,2);
		document.form.CuotaReunificada.value    = FormatOutput(iCuotaReunificada,2);
	
}
		

	
function CompruebaEntradaNumerica(eventObj,obj){
		
	var keyCode
	
	// Comprueba el tipo de navegador
	if (document.all){ 
	keyCode=eventObj.keyCode
	}
	else{
	keyCode=eventObj.which
	}
	
	var str=obj.value;
	
	if (keyCode==46) { if (document.all) {eventObj.keyCode=keyCode=44;} else {eventObj.which=keyCode=44;}}
	
	
		
	if (keyCode==44){ 
		if (str.indexOf(",")>0){
			return false
		}
	}
	
	if((keyCode<48 || keyCode >58) && (keyCode != 44)){ //Permite solo enteros con decimales separados por coma
		return false
	}
	
	return true
	
}


