﻿// Archivo JScript
function validarFormulario()
{
    txtHipoteca1=document.getElementById("CelerisMaster_CPHGeneral_txtHipoteca1");
    txtCoche1=document.getElementById("CelerisMaster_CPHGeneral_txtCoche1");
    txtPrestamo1=document.getElementById("CelerisMaster_CPHGeneral_txtPrestamo1");
    txtTarjeta1=document.getElementById("CelerisMaster_CPHGeneral_txtTarjeta1");
    //txtLiquidez1=document.getElementById("CelerisMaster_CPHGeneral_txtLiquidez1");

    txtHipoteca2=document.getElementById("CelerisMaster_CPHGeneral_txtHipoteca2");
    txtCoche2=document.getElementById("CelerisMaster_CPHGeneral_txtCoche2");
    txtPrestamo2=document.getElementById("CelerisMaster_CPHGeneral_txtPrestamo2");
    txtTarjeta2=document.getElementById("CelerisMaster_CPHGeneral_txtTarjeta2");
    txtLiquidez2=document.getElementById("CelerisMaster_CPHGeneral_txtLiquidez2");

    bTodoOk=true;
    nParejasOk=0
    
    txtHipoteca1.value=parseInt(txtHipoteca1.value.replace(".",""));
    txtCoche1.value=parseInt(txtCoche1.value.replace(".",""));
    txtPrestamo1.value=parseInt(txtPrestamo1.value.replace(".",""));
    txtTarjeta1.value=parseInt(txtTarjeta1.value.replace(".",""));
    //txtLiquidez1.value=parseInt(txtLiquidez1.value.replace(".",""));
    txtHipoteca2.value=parseInt(txtHipoteca2.value.replace(".",""));
    txtCoche2.value=parseInt(txtCoche2.value.replace(".",""));
    txtPrestamo2.value=parseInt(txtPrestamo2.value.replace(".",""));
    txtTarjeta2.value=parseInt(txtTarjeta2.value.replace(".",""));
    txtLiquidez2.value=parseInt(txtLiquidez2.value.replace(".",""));
    
    if (txtHipoteca1.value!="" && txtHipoteca2.value!="" && !isNaN(txtHipoteca1.value) && !isNaN(txtHipoteca2.value) && txtHipoteca2.value!="0" && txtHipoteca1.value!="0")
    {
        nParejasOk=nParejasOk+1;
    }
    else
    {
        txtHipoteca1.value="0";
        txtHipoteca2.value="0";
    }
    if (txtCoche1.value!="" && txtCoche2.value!="" && !isNaN(txtCoche1.value) && !isNaN(txtCoche2.value) && txtCoche2.value!="0" && txtCoche1.value!="0")
    {
        nParejasOk=nParejasOk+1;
    }
    else
    {
        txtCoche1.value="0";
        txtCoche2.value="0";
    }
    if (txtPrestamo1.value!="" && txtPrestamo2.value!="" && !isNaN(txtPrestamo1.value) && !isNaN(txtPrestamo2.value) && txtPrestamo2.value!="0" && txtPrestamo1.value!="0")
    {
        nParejasOk=nParejasOk+1;
    }
    else
    {
        txtPrestamo1.value="0";
        txtPrestamo2.value="0";
    }
    if (txtTarjeta1.value!="" && txtTarjeta2.value!="" && !isNaN(txtTarjeta1.value) && !isNaN(txtTarjeta2.value) && txtTarjeta2.value!="0"&& txtTarjeta1.value!="0")
    {
        nParejasOk=nParejasOk+1;
    }
    else
    {
        txtTarjeta1.value="0";
        txtTarjeta2.value="0";
    }
    //if (txtLiquidez1.value!="" && txtLiquidez2.value!="" && !isNaN(txtLiquidez1.value) && !isNaN(txtLiquidez2.value) && txtLiquidez2.value!="0" && txtLiquidez1.value!="0")
    if (txtLiquidez2.value!="" && !isNaN(txtLiquidez2.value) && txtLiquidez2.value!="0" )
    {
        nParejasOk=nParejasOk+1;
    }
    else
    {
        //txtLiquidez1.value="0";
        txtLiquidez2.value="0";
    }
    
    if (nParejasOk<2)
    {
          alert("Tienes que introducir al menos dos gastos correctamente (números y mayores que cero)");
          return false;
    }
   
    return calcularNuevaCuota();
}


function calcularNuevaCuota()
{

nCuotaActual = parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtHipoteca1").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtCoche1").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtPrestamo1").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtTarjeta1").value); //+ parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtLiquidez1").value);
nImporteTotal = parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtHipoteca2").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtCoche2").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtPrestamo2").value)  + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtTarjeta2").value) + parseInt(document.getElementById("CelerisMaster_CPHGeneral_txtLiquidez2").value);
//nEuribor = 5.393;
nGastos = nImporteTotal * (1 + .12);
nPlazo = 35;
//nTipoMensual = nEuribor + 1.50;
nTipoMensual =  7.00;

nPlazo = nPlazo * 12.0;
nTipoMensual = nTipoMensual / 1200.0;
nCuotaNueva = nGastos * nTipoMensual * Math.pow((nTipoMensual + 1.0),nPlazo) / ( Math.pow((nTipoMensual + 1.0),nPlazo) - 1.0 );
nAhorro = nCuotaActual - nCuotaNueva;
nAhorroAnual = nAhorro * 12;

nAhorroMin = nCuotaActual * .10;
if (Math.round(nAhorro)>=Math.round(nAhorroMin))
{
    document.getElementById("divGastos").style.display="none";
    document.getElementById("divCalcular").style.display="block";
    document.getElementById("CelerisMaster_CPHGeneral_divCuotaMensual").innerHTML=formatCurrency(Math.round(nCuotaActual)) + "&nbsp;&euro;/mes";
    document.getElementById("CelerisMaster_CPHGeneral_divCuotaCeleris").innerHTML=formatCurrency(Math.round(nCuotaNueva)) + "&nbsp;&euro;/mes";
    document.getElementById("CelerisMaster_CPHGeneral_divAhorroMensual").innerHTML=formatCurrency(Math.round(nAhorro)) + "&nbsp;&euro;/mes";
    document.getElementById("CelerisMaster_CPHGeneral_hidCuotaMensual").value=formatCurrency(Math.round(nCuotaActual));
    document.getElementById("CelerisMaster_CPHGeneral_hidCuotaCeleris").value=formatCurrency(Math.round(nCuotaNueva));
    document.getElementById("CelerisMaster_CPHGeneral_hidAhorroMensual").value=formatCurrency(Math.round(nAhorro));
}
else
{
    document.getElementById("divGastos").style.display="none";
    document.getElementById("divNoViable").style.display="block";
}
return false;
}

function volverACalcular()
{
document.getElementById("divGastos").style.display="block";
document.getElementById("divNoViable").style.display="none";
document.getElementById("divCalcular").style.display="none";
return false;
}

function mostrarFinal()
{
document.getElementById("divGastos").style.display="none";
document.getElementById("divCalcular").style.display="none";
document.getElementById("divFinal").style.display="block";
return false;
}

function formatCurrency(num) 
	{
		num = num.toString().replace(/\$|\,/g,"");
		if(isNaN(num))
		{
			num = "0";
		}
		num = Math.floor(num*100+0.50000000001);
		num = Math.floor(num/100).toString();
		
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		{
			num = num.substring(0,num.length-(4*i+3)) + "." + num.substring(num.length-(4*i+3));
		}
		return(num);
	}
