﻿// JScript File

function clickButton(buttonid) {

  var evt = window.event;
  var bt = document.getElementById(buttonid);

  if (evt.keyCode == 13) { 
	   bt.click(); 
       return true; 
      } 
}  

function mostraFoto(item_index, max_items) {          
            for (var i = 0; i <= max_items; i++) {
				//alert("item a processar: " + i);
				
				if (i == item_index) {					
                    document.getElementById('galeria_img_' + i).style.display = "block";  
                 } 
                 else {
                    document.getElementById('galeria_img_' + i).style.display = "none";                     
                }
            }   
}

function changeDisplay(id){
	var popup = document.getElementById(id);
	if(popup.style.display == 'block'){
		popup.style.display = 'none';
	} else {
		popup.style.display = 'block';
	}
}	

function resetEmail(intro_id,intro_msg, input_nome, default_nome, input_email, default_email) {

    //document.getElementById('form').style.display = 'block';
    document.getElementById(intro_id).innerHTML = intro_msg;
    document.getElementById(input_nome).value = default_nome;
    document.getElementById(input_email).value = default_email;
}


function openEnviar(){
	var popupEnviar = document.getElementById('divEnviarAmigo');
	var calculadora = document.getElementById('calculator')

	if(calculadora.style.display == 'block'){        
		calculadora.style.display = 'none';
	}
	
	closeDownloads(12);	//Maximo da categorias

	
	if(popupEnviar.style.display == 'block'){
		//alert('display = none');
		popupEnviar.style.display = 'none';
	} else {
		//alert('display = block');
		popupEnviar.style.display = 'block';
		window.scrollTo(0,0)
	}
	
}	

function openCalculator(iNumBoxes){

    boxenviaramigo = document.getElementById('divEnviarAmigo')
	if(boxenviaramigo.style.display == 'block'){        
        boxenviaramigo.style.display = 'none';
    }

    closeDownloads(12);	//Maximo da categorias
    
	var popup = document.getElementById('calculator');
	
	if(popup.style.display == 'block'){
		//alert('display = none');
		popup.style.display = 'none';
	} else {
		//alert('display = block');
		popup.style.display = 'block';
		window.scrollTo(0,0)
	}
	
}	

function closeCalculator () {
	var calculadora = document.getElementById('calculator');

	if(calculadora.style.display == 'block'){        
		calculadora.style.display = 'none';
	}
}


function openDownloads(boxName,h,numBoxs)
{

    calculadora  = document.getElementById('calculator');
    boxenviaramigo = document.getElementById('divEnviarAmigo')
    
    if(calculadora.style.display == 'block'){        
        calculadora.style.display = 'none';
    }
          
	if(boxenviaramigo.style.display == 'block'){        
        boxenviaramigo.style.display = 'none';
    }   
        
    for(i=1;i<numBoxs+1;i++) {
        
        var popup = document.getElementById('downsBox' + i);
        var currentBoxID = 'downsBox' + boxName;
       
        if (popup !== null) {

			if(popup.style.display == 'block' && popup.id != currentBoxID ){
	        
				popup.style.display = 'none';
	            
			} else if (popup.id == currentBoxID) {
				
				if (popup.style.display == 'block') {
					popup.style.display = 'none';
				} else {
					updatePos(boxName,h);
	    		
					popup.style.display = 'block';
				}
			}      
        }
    }  
}	


function calcula()
{
    //variaveis lidas 
      var price = Number(document.getElementById("ctl00_ContentPlaceHolder1_Ctr_calculator1_price").value);
      var mortage = Number(document.getElementById("ctl00_ContentPlaceHolder1_Ctr_calculator1_mortgage").value.replace(",","."));
      var years = Number(document.getElementById("ctl00_ContentPlaceHolder1_Ctr_calculator1_term").value);
      var interestRate = Number(document.getElementById("ctl00_ContentPlaceHolder1_Ctr_calculator1_interest").value.replace(",","."));
      var interestedRent = Number(document.getElementById("ctl00_ContentPlaceHolder1_Ctr_calculator1_rent").value.replace(",","."));
    
    //variáveis a escrever
    
     var amountLoan = document.getElementById("amountLoan");
     var mouyhs = document.getElementById("mouyhs");
     var rentalIncomeMonthly = document.getElementById("rentalIncomeMonthly");
     var loanRepaymentMonthlyCapital = document.getElementById("loanRepaymentMonthlyCapital");
     var loanRepaymentMonthlyInterest = document.getElementById("loanRepaymentMonthlyInterest");
     
     if ((price != "")&&(mortage !="")&&(years !="")&&(interestRate !="")&&(interestedRent !=""))
     {
     amountLoan.innerHTML = round(price * mortage/100);
     mouyhs.innerHTML = years*12;
     rentalIncomeMonthly.innerHTML = round((interestedRent/100 * price) / 12 );
     loanRepaymentMonthlyCapital.innerHTML = round(((price * mortage/100)/ (years * 12)) * (1+ (interestRate / 100)));
     loanRepaymentMonthlyInterest.innerHTML = round(((price * mortage/100)/ (years * 12)) * (interestRate / 100));
    }
    else
    {
    alert (mensagem);
    }
}
function round(x) {
 return Math.round(x*100)/100;
 }

// menu donwloads empreendimentos - jd

function closeDownloads(numBoxs)
{
        
    for(i=1;i<numBoxs+1;i++) {
    
        var popup = document.getElementById('downsBox' + i);
        
        if (popup !== null) {
       
			if(popup.style.display == 'block'){
	        
				popup.style.display = 'none';
			}       
       } 
    }  
}	



function updatePos(boxName,h)
{        
    
              
        x= findPosX(document.getElementById('downsLink' + boxName));
		y= findPosY(document.getElementById('downsLink' + boxName)); 
				
		document.getElementById('downsBox' + boxName).style.top  = (y- (h + 50))+'px';
	    document.getElementById('downsBox' + boxName).style.left =  (x)+'px';
	    	           
	return true;
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

