var xmlHttp
var saidaatual 


function getOptions(idSaida, tabela, valor, option, foreignKey, foreignKeyValue)
{
	if(tabela=='bairro' && foreignKeyValue=='') {
		preenche_bairro(document.pesquisa.cidade.value);
		return;
	}
	
	
	if(foreignKeyValue=="cidade") {
		document.getElementById('regiao').innerHTML='<select name="regiao" id="cbo_regiao" disabled="disabled"></select>';
		document.getElementById('bairro').innerHTML='<select name="bairro[]" id="cbo_bairro" disabled="disabled"></select>';		
	} else {
		if(foreignKeyValue=="regiao") {
			document.getElementById('bairro').innerHTML='<select name="bairro[]" id="cbo_bairro" disabled="disabled"></select>';		
		} else  {
	
	
document.getElementById(idSaida).innerHTML="<select><option>Carregando...</option></select>";
saidaatual = idSaida;
if (tabela=="" || option=="")
  { 
  document.getElementById(idSaida).innerHTML="<select></select>"
  return
  }
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser não suporta AJAX")
  return
  }
var url="getoptions.php"
url=url+"?tabela="+tabela+"&"
url=url+"valor="+valor+"&"
url=url+"option="+option+"&"
url=url+"foreign_key="+foreignKey+"&"
url=url+"foreign_key_value="+foreignKeyValue+"&"

if(document.pesquisa.multiple) {
	if(document.pesquisa.multiple.value=="0") {
		url=url+"multiple=0&";
	}
}

url=url+"sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
		}
	}

}



function preenche_bairro(cidade) 
	{		
document.getElementById('bairro').innerHTML="<select><option>Carregando...</option></select>";
saidaatual = 'bairro';
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser não suporta AJAX")
  return
  } 
var url="getoptions.php"
url=url+"?preenche_bairro=true&"
url=url+"foreign_key_value="+cidade+"&"
if(document.pesquisa.multiple) {
	if(document.pesquisa.multiple.value=="0") {
		url=url+"multiple=0&";
	}
}
url=url+"sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)		
}

function stateChanged()
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 responseText=xmlHttp.responseText.replace(/\+/g," ")
 responseText=unescape(responseText)
 document.getElementById(saidaatual).innerHTML=responseText 
 
 	if(saidaatual=='regiao') {
		xmlHttp=GetXmlHttpObject();
		preenche_bairro(document.pesquisa.cidade.value);
	}

 } 
}

function GetXmlHttpObject()
{
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;
}