//*************random para ser usado no final da url para evitar cache do navegador****************
function GM_Random() {
  today = new Date();
  num= Math.abs(Math.sin(today.getTime()));
  return num;  
}

//*************verifica se o navegador tem suporte ao AJAX****************
function GM_Verifica_ajax(){ 
  if(typeof(XMLHttpRequest)!='undefined'){
    return new XMLHttpRequest();
  }
  var axO=['Microsoft.XMLHTTP','Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
  for(var i=0;i<axO.length;i++){ 
    try{ 
      return new ActiveXObject(axO[i]);
    }
    catch(e){} 
  }
  return null;
}


//função que retorna para o combo desejado os valores retornados do link da var url
function GM_Combo(url,Campo,Selecionado)
{
		//alert(Campo);
		var ajax = GM_Verifica_ajax();
    if(ajax)
		{
				var Rnd = GM_Random();
        if(url.indexOf("?")>=0)
				{
					// já tem parametros vindos na url
        	url = url + "&" + Rnd;
        }
				else
				{ 
					url = url + "?" + Rnd;
				}
        ajax.onreadystatechange = GM_Status_ajax
        ajax.open("GET", url ,true);
        ajax.setRequestHeader("Cache-Control", "no-cache");
        ajax.setRequestHeader("Pragma", "no-cache");
        ajax.send(null);
        return true;
   }
		else
		{
        alert("Este navegador não tem suporte ao AJAX!");
				return false;
    }
    function GM_Status_ajax()
		{
        if (ajax.readyState==4)
				{
            if(ajax.status == 200)
						{
							//alert(document.getElementById(Campo).value);
							document.getElementById(Campo).options.length = 1;
							var vRetorno = ajax.responseText;
							//alert(vRetorno);
							var vId_Nome = vRetorno.split('*');
						  //insere os novos valores
							 for(var i=0; i < vId_Nome.length-1; i++) 
							 {
								 var Lista = vId_Nome[i].split('|'); //separa ID do Nome pela Barra(|)
								 var vId = Lista[0];
								 var vNome = Lista[1];
								 opcoes = document.createElement("option");
								 opcoes.value = vId;
								 opcoes.text = vNome;
								 if (Selecionado == vId)
								 {
										opcoes.setAttribute("selected", "true");
								 }
								 document.getElementById(Campo).options.add(opcoes);
							 }
            }
						else
						{
                alert("Carregamento falhou!");
            }
            ajax = null
        }
    }
}

