﻿function toggleDiv(whatDiv)
{
    if (document.getElementById(whatDiv).style.display == 'block')
    {
        document.getElementById(whatDiv).style.display = 'none';
    }
    else
    {
        document.getElementById(whatDiv).style.display = 'block';
    }
}

function showDiv(whatDiv)
{
    document.getElementById(whatDiv).style.display = 'block';
}

function hideDiv(whatDiv)
{
    document.getElementById(whatDiv).style.display = 'none';
}

function formatNumber(nStr)
{
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function calcularDEC()
{
    var valor = document.getElementById('valorP').value;
    var reembolso = document.getElementById('reembolso').value;
    var spc = document.getElementById('spc1').checked;
    campoTotal = document.getElementById('total');
    
    if ((valor < 250) ||(valor > 4000))
    {
        alert('O valor pretendido terá de estar compreendido entre € 250 e € 4000');
        return;
    }
    
    var TSPC = 0.0045;
    var RMM = 20;
    var ISUC= (valor * 0.04) / 100;
    var valorSPC = valor * TSPC;
    var amortizacao = 0;
    
    amortizacao = (valor * reembolso) / 100;
    
    if (amortizacao < RMM)
    {
        amortizacao = RMM;
    }
     
    if (spc == true)
    {
        campoTotal.value = (ISUC + valorSPC + amortizacao).toFixed(2);
    }
    else
    {
        campoTotal.value = (ISUC + amortizacao).toFixed(2);
    }
    
    arrTAEG = new Array(8);

    for (i = 0; i < arrTAEG . length; ++ i)
    {
	    arrTAEG[i] = new Array(3);
	}

    // 500
    arrTAEG[0][0] = '22,86';
    arrTAEG[0][1] = '20,54';
    arrTAEG[0][2] = '16,15';
    
    // 1000
    arrTAEG[1][0] = '24,14';
    arrTAEG[1][1] = '21,12';
    arrTAEG[1][2] = '16,56';
    
    // 1500
    arrTAEG[2][0] = '24,37';
    arrTAEG[2][1] = '21,28';
    arrTAEG[2][2] = '16,69';
    
    // 2000
    arrTAEG[3][0] = '24,46';
    arrTAEG[3][1] = '21,35';
    arrTAEG[3][2] = '16,75';
    
    // 2500
    arrTAEG[4][0] = '24,50';
    arrTAEG[4][1] = '21,40';
    arrTAEG[4][2] = '16,79';
    
    // 3000
    arrTAEG[5][0] = '24,53';
    arrTAEG[5][1] = '21,42';
    arrTAEG[5][2] = '16,81';
    
    // 3500
    arrTAEG[6][0] = '24,55';
    arrTAEG[6][1] = '21,44';
    arrTAEG[6][2] = '16,83';
    
    // 4000
    arrTAEG[7][0] = '24,56';
    arrTAEG[7][1] = '21,45';
    arrTAEG[7][2] = '16,84';
          
    var divTAEG1 = document.getElementById("taeg1");
    //var divTAEG2 = document.getElementById("taeg2");
    var divMontante = document.getElementById("montante");
    var iTAEG;
    
    switch (parseInt(reembolso))
    {
        case 10:
            iTAEG = 1;
            break;
            
        case 20:
            iTAEG = 2;
            break;
        
        default:
            iTAEG = 0;
            break;
    }
    
    if (divTAEG1 != null)
    {
        divTAEG1.innerHTML = arrTAEG[valor / 500 - 1][iTAEG];
    }
    
    /*
    
    if (divTAEG2 != null)
    {
        divTAEG2.innerHTML = arrTAEG[valor / 500 - 1][iTAEG];
    }*/
    
    if (divMontante != null)
    {
        divMontante.innerHTML = formatNumber(valor);
    }
    
}

function changeImage(whichOne)
{

    var array_img = document.getElementsByName("prodBullet");
    var array_label = document.getElementsByTagName("label");
	
	for (i = 0;i < array_img.length; i++)
	{
	    array_img[i].src= "img/dot.gif";
	}
	
	for (i = 0;i < array_label.length; i++)
	{
	    array_label[i].className = "normalSim";
	}
	
	var image = document.getElementById('img' + whichOne);
	image.src = "img/seta_y.gif";
	
	var label = document.getElementById('lab' + whichOne);
	label.className = "boldSim";

}

function setCredito(IDCredito, changeImg)
{
    vbak = "";
    tipo_credito = IDCredito;
    if (changeImg == "1")
    {
        changeImage(IDCredito);
    }
    SliderUpdate();
}

/**
 * MC: Montante de Crédito
 * P: Prestação
 * N: Numero de meses
 */
function calci12_1(MC, P, N)
{
    var tmax=1.0;
    var tmin=0.0;
    var taux=0.0

    var MCC=P*(1.0-Math.pow(1.0+(tmax-tmin)/2,N*-1))/(tmax-tmin)/2;
    for(;Math.abs(tmax-tmin)>0.000000001;)
    {
      if(MCC<MC)
        tmax=tmin+(tmax-tmin)/2.0;
      else
        tmin=tmin+(tmax-tmin)/2.0;
        
        taux=tmin+(tmax-tmin)/2.0;
        MCC=P*(1.0-Math.pow(1.0+taux,N*-1))/taux;
    }
    
    taux=tmin+(tmax-tmin)/2.0;
    return Math.floor((Math.pow(1.0+taux,12.0)-1)*10000.0+0.5)/100.0;
}

function calci12(MC, P1, P2, N)
{
    var tmax=1.0;
    var tmin=0.0;
    var taux=tmin+(tmax-tmin)/2.0;

    var MCC=calcMC(P1, P2, N, taux);
    for(;Math.abs(tmax-tmin)>0.00000001;)
    {
      if(MCC<MC)
        tmax=tmin+(tmax-tmin)/2.0;
      else
        tmin=tmin+(tmax-tmin)/2.0;
        
        taux=tmin+(tmax-tmin)/2.0;
        MCC=calcMC(P1, P2, N, taux);
    }
    
    taux=tmin+(tmax-tmin)/2.0;
    return Math.floor((Math.pow(1.0+taux,12.0)-1)*10000.0+0.5)/100.0;
}

function calcMC(P1, P2, N, r)
{
  var MCC=0;
  for(i=1;i<=N;i++)
  {
    if(i<=2)
      MCC+=P1/Math.pow(1.0+r,i);
    else
      MCC+=P2/Math.pow(1.0+r,i);
  }
  
  return MCC;
}

function openSim()
{
    var wWidth = 375;
    //var wHeight = 320; // easyflex
    var wHeight = 480; // flexibom zero
    var wTop = (screen.availHeight /2) - (wHeight / 2);
    var wLeft = (screen.availWidth /2) - (wWidth / 2);
    
    window.open('popSim3.aspx', 'simulador', 'width=' + wWidth + ', height=' + wHeight + ', top=' + wTop + ', left=' + wLeft);
}

function openPopup(pageName, wWidth, wHeight)
{
    var wTop = (screen.availHeight /2) - (wHeight / 2);
    var wLeft = (screen.availWidth /2) - (wWidth / 2);
    
    window.open(pageName, 'popup', 'width=' + wWidth + ', height=' + wHeight + ', top=' + wTop + ', left=' + wLeft);
}

function openPopup2(pageName, wWidth, wHeight)
{
    var wTop = (screen.availHeight /2) - (wHeight / 2);
    var wLeft = (screen.availWidth /2) - (wWidth / 2);
    
    window.open(pageName, 'popup', 'width=' + wWidth + ', height=' + wHeight + ', top=' + wTop + ', left=' + wLeft + ', scrollbars = 1');
}

function closeLayer(layerID)
{
    var layer = document.getElementById(layerID);
    layer.style.visibility = 'hidden';
}

function openLayer(layerID, wWidth, wHeight)
{

    var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );


    //var wTop = (window.availHeight /2) - (wHeight / 2);
    var wTop = 100;
    var wLeft = (myWidth /2) - (wWidth / 2);
    
    document.write('<div id=\"' + layerID + '\" style=\"position: absolute; border: solid 1px #000000; width:' + wWidth + '; height:' + wHeight + '; top:' + wTop + ' ;left:' + wLeft + ' ;background-color: #eeeeee; z-index: 999;\">');
    document.write('<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">');
    document.write('<tr>');
    document.write('<td align=\"right\" width=\"100%\" height=\"16\" style=\"background-color: #ffffff;\"><img src=\"img/x_close.gif\" width=\"16\" height=\"16\" style=\"cursor: hand;\" onclick=\"closeLayer(\'' + layerID + '\');\"></td>');
    document.write('</tr>');
    document.write('<tr>');
    document.write('<td width=\"100%\" height=\"100%\">');
    document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"320\" height=\"240\" id=\"flexibom_zero\" align=\"middle\">');
    document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\" />');
    document.write('<param name=\"movie\" value=\"video/flexibom_zero.swf\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#000000\" /><embed src=\"video/flexibom_zero.swf\" quality=\"high\" bgcolor=\"#000000\" width=\"320\" height=\"240\" name=\"flexibom_zero\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');
    document.write('</object>');
    document.write('</td>');
    document.write('</tr>');
    document.write('</table>');
    document.write('</div>');
}

function checkKey3(whatEvent, whatField)
{
    if ((whatEvent.keyCode < 48) ||(whatEvent.keyCode > 57))
    {
        whatEvent.returnValue = false;
    }

    if ((whatField.value >= 500) && (whatField.value <=50000))
    {
        calcularCredito3();
    }
}

function calcularCredito3()
{
    var valor = document.getElementById('valorP_3').value;
    var valor12;
    var valorR;
    
    var prazoDDL = document.getElementById('nMeses_3');
    var prazo = prazoDDL.options[prazoDDL.selectedIndex].value
    
    var total12 = document.getElementById('total12_3');
    var totalR = document.getElementById('totalR_3');
    
    var total12_SV = document.getElementById('total12_3_sv');
    var totalR_SV = document.getElementById('totalR_3_sv');
    
    var total12_PPC = document.getElementById('total12_3_ppc');
    var totalR_PPC = document.getElementById('totalR_3_ppc');
    
    /*****************************************************/
    // SEM SEGURO
    var montCredito = Mont_Financiado(valor, prazo, 0);
    // 2 primeiros meses
    valor12 = (montCredito / prazo).toFixed(2);
    total12.innerHTML = valor12;
    
    // meses restantes
    valorR = ((montCredito - 2 * valor12) * I12) / (1 - Math.pow((1 + I12), ((prazo - 2) * (-1))));
    totalR.innerHTML = valorR.toFixed(2);
    
    //TAEG
    var divTAEG = document.getElementById("taeg3");
    divTAEG.innerHTML = calci12(valor, valor12, valorR , prazo);
    
    /*****************************************************/
    // COM SEGURO DE VIDA
    var montCredito = Mont_Financiado(valor, prazo, 1);
    // 2 primeiros meses
    valor12 = (montCredito / prazo).toFixed(2);
    total12_SV.innerHTML = valor12;
    
    // meses restantes
    valorR = ((montCredito - 2 * valor12) * I12) / (1 - Math.pow((1 + I12), ((prazo - 2) * (-1))));
    totalR_SV.innerHTML = valorR.toFixed(2);
    
    //TAEG
    var divTAEG_SV = document.getElementById("taeg3_sv");
    divTAEG_SV.innerHTML = calci12(valor, valor12, valorR , prazo);
    
    /*****************************************************/
    // COM PLANO DE PROTECÇÃO AO CRÉDITO
    var montCredito = Mont_Financiado(valor, prazo, 2);
    // 2 primeiros meses
    valor12 = (montCredito / prazo).toFixed(2);
    total12_PPC.innerHTML = valor12;
    
    // meses restantes
    valorR = ((montCredito - 2 * valor12) * I12) / (1 - Math.pow((1 + I12), ((prazo - 2) * (-1))));
    totalR_PPC.innerHTML = valorR.toFixed(2);
    
    //TAEG
    var divTAEG_PPC = document.getElementById("taeg3_ppc");
    divTAEG_PPC.innerHTML = calci12(valor, valor12, valorR , prazo);
    
}

function setTaxas(whichOne)
{
    switch(whichOne)
    {
        case 1:
            arrTSV = new Array(0.001364, 0.002594, 0.003882, 0.005232, 0.006646, 0.008122, 0.010041, 0.011726, 0.013494, 0.015346, 0.017291, 0.019327, 0.022220);
            arrTISUC = new Array(0.00240, 0.0028, 0.0032, 0.0036, 0.0040, 0.0044, 0.0050, 0.0060);
            break;
            
        case 2:
            arrTSV = new Array(0.001270, 0.002970, 0.006390, 0.006390, 0.01016, 0.01016, 0.01425, 0.01425, 0.01694, 0.01694, 0.01991, 0.01991);
            arrTISUC = new Array(0.00240, 0.0028, 0.0032, 0.0036, 0.0040, 0.0044, 0.0050, 0.0060);
            break;
    }

}

function replaceContent(objectName, content)
{
    var objObject = document.getElementById(objectName);
    if (objObject)
    {
        objObject.innerHTML = content;
    }
}