// JavaScript Document

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	obj.visibility=v; }
}

//função para gerar uma janela popup
function nova_janela(onde,largura,altura) {
	if (onde!="") {
		var a = screen.width ;
		var b = screen.height ;
		if (largura == 0) { largura=640 }
		if (altura == 0) { altura=480 }
		metade_largura=largura/2;
		metade_altura=altura/2;	
		var resultado = (a / 2) - metade_largura ;
		var resultado2 = (b / 2) - metade_altura ;
		window.open (onde, '_blank', 'scrollbars=yes,status=no,width=' + largura +',height=' + altura + ',left=' + resultado + ',top=' + resultado2 + ',menubar=no');
	}
}	


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function bannerclick(id) {
    http.open('get', 'gateway.php?action=banner&id='+id);
    http.send(null);
}


function carregaagenda(ano,mes,dia) {
	http.open('get', 'gateway.php?action=agenda&ano='+ano+'&mes='+mes+'&dia='+dia);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("calendar").innerHTML=response;
					
				}
		}		
	}
    http.send(null);
}





function carregatipologias(natureza) {
    http.open('get', 'gateway.php?action=tipologias&natureza='+natureza);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				document.getElementById("pesq_imoveis_sub0").innerHTML=response;
		}		
	}
    http.send(null);
}


function carregaconcelhos(distrito) {
    http.open('get', 'gateway.php?action=concelhos&distrito='+distrito);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("pesq_imoveis_sub1").innerHTML=response;
					document.getElementById("pesq_imoveis_sub2").innerHTML="";
				}
		}		
	}
    http.send(null);
}

function carregaconcelhos3(distrito) {
    http.open('get', 'gateway.php?action=concelhos&distrito='+distrito);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("pesq_imob_sub1").innerHTML=response;
				}
		}		
	}
    http.send(null);
}

function carregafreguesias(distrito,concelho) {
    http.open('get', 'gateway.php?action=freguesias&distrito='+distrito+'&concelho='+concelho);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("pesq_imoveis_sub2").innerHTML=response;
				}
		}		
	}
    http.send(null);
}

function carregaimoveisdestaquesimob(imob) {
	http.open('get', 'gateway.php?action=imoveisdestaque&imobid='+imob);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("destaquesimoveis").innerHTML=response;
					
				}
		}		
	}
    http.send(null);
}

function carregaempreendimentosdestaques(distrito,concelho) {
	http.open('get', 'gateway.php?action=imoveisdestaque&distrito='+distrito+'&concelho='+concelho+'&empreendimentos=1');
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("destaquesempreendimentos").innerHTML=response;
				}
		}		
	}
    http.send(null);
}

function carregaimoveisdestaques(distrito,concelho) {
	http.open('get', 'gateway.php?action=imoveisdestaque&distrito='+distrito+'&concelho='+concelho);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("destaquesimoveis").innerHTML=response;
					
				}
				carregafreguesias(distrito,concelho);
		}		
	}
    http.send(null);
}

function carregaimoveisdestaques2(distrito,concelho) {
	http.open('get', 'gateway.php?action=imoveisdestaque2&distrito='+distrito+'&concelho='+concelho);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("destaquesimoveis").innerHTML=response;
					
				}
				carregafreguesias(distrito,concelho);
		}		
	}
    http.send(null);
}

function converteconcelho(concelho) {
    http.open('get', 'gateway.php?action=converteconcelho&concelho='+concelho);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					responsearray=response.split(";");
					
					for (xi=0;xi<=document.pesqimoveis.distrito.length;xi++) {
						if (document.pesqimoveis.distrito.options[xi].value==responsearray[0]) {
							document.pesqimoveis.distrito.selectedIndex=xi;
							carregaconcelhos2(responsearray[0],responsearray[1]);
							break;
						}
					}
					
					
					
				}
		}		
	}
    http.send(null);
}

function converteconcelho2(concelho) {
	
    http.open('get', 'gateway.php?action=converteconcelho&concelho='+concelho);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					responsearray=response.split(";");
					for (xi=0;xi<=document.pesqimob.imobdistrito.length;xi++) {
						if (document.pesqimob.imobdistrito.options[xi].value==responsearray[0]) {
							document.pesqimob.imobdistrito.selectedIndex=xi;
							carregaconcelhos4(responsearray[0],responsearray[1]);
							break;
						}
					}
					
					
					
				}
		}		
	}
    http.send(null);
}

function carregaconcelhos2(distrito,concelho) {
    http.open('get', 'gateway.php?action=concelhos&distrito='+distrito);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("pesq_imoveis_sub1").innerHTML=response;
					
					for (xi=0;xi<=document.pesqimoveis.concelho.length;xi++) {
						if (document.pesqimoveis.concelho.options[xi].value==concelho) {
							document.pesqimoveis.concelho.selectedIndex=xi;
							carregaimoveisdestaques(distrito,concelho);
							break;
						}
					}

					
				}
		}		
	}
    http.send(null);
}

function carregaconcelhos4(distrito,concelho) {
    http.open('get', 'gateway.php?action=concelhos&distrito='+distrito);
    http.onreadystatechange = function() {
		if(http.readyState == 4){
				var response = http.responseText;
				if (response.length > 0) {
					document.getElementById("pesq_imob_sub1").innerHTML=response;
					
					for (xi=0;xi<=document.pesqimob.concelho.length;xi++) {
						if (document.pesqimob.concelho.options[xi].value==concelho) {
							document.pesqimob.concelho.selectedIndex=xi;
							document.pesqimob.submit();
							break;
							
						}
					}

					
				}
		}		
	}
    http.send(null);
}

function pesq_imoveis_expande(onde) {
	document.getElementById("pesq_imoveis_subs").style.display='block';
	onde.className ="cx_input";
}

function pesq_imob_expande(onde) {
	document.getElementById("pesq_imob_subs").style.display='block';
	onde.className ="cx_input";
}

function toggleLayer( whichLayer )
{
	  var elem, vis;
	  if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	  else if( document.all ) // this is the way old msie versions work
		  elem = document.all[whichLayer];
	  else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	  vis = elem.style;
	  // if the style.display value is blank we try to figure it out here
	  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}	

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.alt; if (!nm){ nm=val.name}; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- O campo '+nm+' deve conter um endereço de email.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- O campo '+nm+' deve conter um número.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- O campo '+nm+' deve conter um número entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- O campo '+nm+' deve ser preenchido.\n'; }
  } if (errors) alert('Ocorreram os seguintes erros:\n'+errors);
  document.MM_returnValue = (errors == '');
}