//Tenta criar o objeto xmlHTTP
try{
    xmlhttpsegmento = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttpsegmento = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttpsegmento = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttpsegmento = false;
        }
    }
}

//Fila de conexões
filacid=[];
ifilacid=0;

//Carrega via XMLHTTP a url recebida e coloca seu valor
//no objeto com o id recebido
function buscaSegmento(catCod, sitCod, id, vlrDefault) {
	if ( sitCod != '' ) {
		//Adiciona à fila
		filacid[filacid.length]=[id, ajax_path_shared + '_ajax.buscaSegmento.php?catCod=' + catCod + '&sitCod=' + sitCod, vlrDefault];
		//Se não há conexões pendentes, executa
		if((ifilacid+1)==filacid.length) ajaxRunSegmento();
	} else { 
		input = document.getElementById(id);
		input.options.length = 0;
		input.options[0] = new Option("-- Selecione --", '0'); 
	}
}

//Executa a próxima conexão da fila
function ajaxRunSegmento(){
    //Abre a conexão
	//alert(fila[ifila][1]);
    xmlhttpsegmento.open("GET",filacid[ifilacid][1],true);
    //Função para tratamento do retorno
    xmlhttpsegmento.onreadystatechange=function() {
        if (xmlhttpsegmento.readyState==4 && xmlhttpsegmento.status == 200) {
            //Mostra o HTML recebido
            //--  retorno=unescape(xmlhttp.responseText.replace(/\+/g," "))
			var result = xmlhttpsegmento.responseXML;
			var segXml = result.getElementsByTagName("seg");
			
			input = document.getElementById(filacid[ifilacid][0]);
			input.options.length = 0;
			input.disabled = true;
			input.options[0] = new Option("-- Selecione --", ''); 

			for (var x = 0; x < segXml.length; x++) {
				//segXml[x].getAttribute('cod');
				input.options[x+1] = new Option(segXml[x].firstChild.nodeValue, segXml[x].getAttribute('cod')); 
			}
			input.disabled = false;
			if (filacid[ifilacid][2] > 0) input.value = filacid[ifilacid][2];

			//Roda o próximo
            ifilacid++;
            if(ifilacid<filacid.length)setTimeout("ajaxRunSegmento()",20);
        }
    }
    //Executa
    xmlhttpsegmento.send(null);
}
