// FUNÇÕES JAVASCRIPT - AJAX
//==================================================
//var servidor="http://localhost/portal/";
var servidor="http://www.imositio.pt/";
//var servidor="http://www.imositus.com/";
//==================================================
// ObtemXmlHttp()
//==================================================
function ObtemXmlHttp()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("MSXML2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


//==================================================
// ChamaHttp(url,funcao)
//==================================================
function ChamaHttp(url,funcao)
{ 
var xmlHttp;
// Ajax
// =============================================================
//alert(url);
xmlHttp = ObtemXmlHttp();
if (xmlHttp==null){
  return false;
} 
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=function(){
 if (xmlHttp.readyState == 4){
  if (xmlHttp.status == 200)
    funcao(xmlHttp);
  else
    {
    // erro
    }
 }
}  
xmlHttp.send(null);
return true;
}

//==================================================
// ChamaHttpid(url,funcao,id)
//==================================================
function ChamaHttpid(url,funcao,id)
{ 
var xmlHttp;
// Ajax
// =============================================================
xmlHttp = ObtemXmlHttp();
if (xmlHttp==null){
  return false;
} 
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=function(){
 if (xmlHttp.readyState == 4){
  if (xmlHttp.status == 200)
    funcao(id,xmlHttp);
  else
    {
    // erro
    }
 }
}  
xmlHttp.send(null);
return true;
}
// FUNÇÕES JAVASCRIPT - LOCAL
//==================================================
//==================================================

//==================================================
// mudadistrito()
//==================================================
function mudadistrito()
{
  //alert("muda distrito");
  vardistrito=document.getElementById("distrito");
  distritoid=vardistrito.options[vardistrito.selectedIndex].value;
  url=servidor+"ajax/concelho-ajax.php?distritoid="+distritoid;  
  //coloca as combo inactivas
  if (document.getElementById("concelho"))  
  {
    document.getElementById("concelho").disabled=true;
    // actualiza o concelho
    ChamaHttp(url,actualizaconcelho);    
  }  
  if (document.getElementById("freguesia"))    
    document.getElementById("freguesia").disabled=true;
  if (document.getElementById("zona"))
    document.getElementById("zona").disabled=true;    

}     

//==================================================
// mudaconcelho()
//==================================================
function mudaconcelho()
{
  //alert("muda concelho");
  vardistrito=document.getElementById("distrito");  
  varconcelho=document.getElementById("concelho");
  distritoid=vardistrito.options[vardistrito.selectedIndex].value;
  concelhoid=varconcelho.options[varconcelho.selectedIndex].value;  
  url=servidor+"ajax/freguesia-ajax.php?distritoid="+distritoid+"&concelhoid="+concelhoid;  
  //coloca as combo inactivas
  if (document.getElementById("freguesia"))    
  {
    document.getElementById("freguesia").disabled=true;
    // actualiza a freguesia
    ChamaHttp(url,actualizafreguesia);    
  }  
  if (document.getElementById("zona"))
    document.getElementById("zona").disabled=true;    
}     

//==================================================
// mudafreguesia()
//==================================================
function mudafreguesia()
{
  //alert("muda freguesia");
  vardistrito=document.getElementById("distrito");  
  varconcelho=document.getElementById("concelho");
  varfreguesia=document.getElementById("freguesia");  
  distritoid=vardistrito.options[vardistrito.selectedIndex].value;
  concelhoid=varconcelho.options[varconcelho.selectedIndex].value;  
  freguesiaid=varfreguesia.options[varfreguesia.selectedIndex].value;    
  url=servidor+"ajax/zona-ajax.php?distritoid="+distritoid+"&concelhoid="+concelhoid+"&freguesiaid="+freguesiaid; 
  //coloca as combo inactivas
  if (document.getElementById("zona"))
  {
    document.getElementById("zona").disabled=true;    
    // actualiza a zona
    ChamaHttp(url,actualizazona);
  }  
}     

//==================================================
// actualizaconcelho(xmlhttp)
//==================================================
function actualizaconcelho(xmlhttp)
{
  //alert("actualiza concelho");
  //alert ("Resposta:"+xmlhttp.responseText);
  varconcelho=document.getElementById("concelho");
  PreencheCombo(varconcelho,xmlhttp.responseText,"(Concelhos)");   
  // Activa combo
  document.getElementById("concelho").disabled=false;
}     

//==================================================
// actualizafreguesia(xmlhttp)
//==================================================
function actualizafreguesia(xmlhttp)
{
  //alert("actualiza freguesia");
  //alert ("Resposta:"+xmlhttp.responseText);
  varfreguesia=document.getElementById("freguesia");
  PreencheCombo(varfreguesia,xmlhttp.responseText,"(Freguesias)");
  // Activa combo
  document.getElementById("freguesia").disabled=false;     
}  

//==================================================
// actualizazona(xmlhttp)
//==================================================
function actualizazona(xmlhttp)
{
  //alert("actualiza freguesia");
  //alert ("Resposta:"+xmlhttp.responseText);
  varzona=document.getElementById("zona");
  PreencheCombo(varzona,xmlhttp.responseText,"(Zonas)");
  // Activa combo
  document.getElementById("zona").disabled=false;   
}  
 
//==================================================
// PreencheCombo(combo,resposta,strinicio)
//================================================== 
function PreencheCombo (combo, resposta,strinicio)
{			
  //alert("Preenche combo");
	combo.options.length= 0;
	var array= resposta.split (";");
	var is= 1;
  combo.options[0]= new Option (strinicio, 0);	
	for (var i= 0; i < array.length; i += 2)
	{
		combo.options[is]= new Option (array[i+1], array[i]);
		is++;
	}
}

//==================================================
// setfocus_pesquisa()
//================================================== 
function setfocus_pesquisa()
{			
  if (document.getElementById("pesquisaid"))
    document.getElementById("pesquisaid").focus();
}

//==================================================
// mudaimagem_detalhe(id,imagem)
//================================================== 
function mudaimagem_detalhe(id,imagem)
{			
  if (document.getElementById(id))
    document.getElementById(id).src=imagem;
}
// FUNÇÕES JAVASCRIPT - IMOVEL
//==================================================
//==================================================

//==================================================
// mudatipoimovel()
//==================================================
function mudatipoimovel()
{
  //alert("muda tipoimovel");
  vartipofamiliaimovel=document.getElementById("tipofamiliaimovel");
  tipofamiliaimovelid=vartipofamiliaimovel.options[vartipofamiliaimovel.selectedIndex].value;
  url=servidor+"ajax/tipoimovel-ajax.php?id="+tipofamiliaimovelid;  
  //coloca as combo inactivas
  if (document.getElementById("tipoimovel"))
    document.getElementById("tipoimovel").disabled=true;
  // actualiza o tipoimovel
  ChamaHttp(url,actualizatipoimovel);
}     
//==================================================
// actualizatipoimovel(xmlhttp)
//==================================================
function actualizatipoimovel(xmlhttp)
{
  //alert("actualiza tipoimovel");
  //alert ("Resposta:"+xmlhttp.responseText);
  vartipoimovel=document.getElementById("tipoimovel");
  PreencheCombo(vartipoimovel,xmlhttp.responseText,"(Tipo imovel)");
  // Activa combo
  document.getElementById("tipoimovel").disabled=false;     
}     
// FUNÇÕES JAVASCRIPT - BACKOFFICE
//==================================================
//==================================================

//==================================================
// chama_backoffice()
//==================================================
function chama_backoffice(ecran,id)
{
  window.location='backoffice.php?op=galeria&accao=editar&id='+id;
} 

// FUNÇÕES JAVASCRIPT - Estatisticas
//==================================================
//==================================================

//==================================================
// estatisticas_mediador()
//==================================================
function estatisticas_mediador()
{
  url=servidor+"ajax/mediadorstats-ajax.php?mediadorid="+document.getElementById("mediadorid").value;  
  // actualiza o concelho
  ChamaHttp(url,retorno_estatisticas_mediador);      
}     
//==================================================
// retorno_estatisticas_mediador()
//==================================================
function retorno_estatisticas_mediador(xmlhttp)
{
 //alert ("Resposta:"+xmlhttp.responseText);
}

