//ramdom para evitar cache de navegador
function Evita_cache(){
	today = new Date();
	num= Math.abs(Math.sin(today.getTime()));
	return num;  
}

//verifica se o navegador suporta ajax
function Verificando(){ 
	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 os dados da página solicitada
function Drop_down1(idioma) {
	var ajax = Verificando();
	var v_idioma = idioma.split('|'); //separa ID do Nome pela Barra(|)
	var id_idioma = v_idioma[0];
	var nm_idioma = v_idioma[1];	
	var url = "consulta.php?action=pais&id_idioma=" + id_idioma;  
	var Campo = "id_pais";
	// identifica o idioma
	document.getElementById("nm_idioma").value = nm_idioma; 
	//limpa os campos
	document.getElementById("id_pais_escolhido").value = ""; 
	document.getElementById("nm_pais").value = ""; 
	document.getElementById("nm_cidade").value = ""; 
	document.getElementById("id_pais").value = ""; 
	document.getElementById("id_cidade").value = ""; 
	
	if(ajax){
		var Rnd = Evita_cache();
		
		if(url.indexOf("?")>=0){
			// já tem parametros vindos na url
			url = url + "&" + Rnd;
		} else { 
			//não tem parâmetros vindo da URL
			url = url + "?" + Rnd;
		}
		
		ajax.onreadystatechange = Status_atual
		ajax.open("GET", url ,true);
		ajax.setRequestHeader("Cache-Control", "no-cache");
		ajax.setRequestHeader("Pragma", "no-cache");
		
		//enquanto espera o retorno da página exibe uma mensagem para o internauta
		MSG(1,'Carregando...');
		
		
		ajax.send(null);
		return true;
	} else {
		alert("Este navegador não tem suporte ao AJAX!");
		return false;
	}
	
	function Status_atual(){
		if (ajax.readyState==4){
			if(ajax.status == 200){
				//alert(document.getElementById(Campo).value);
				
				//monta o dropdown com as opções
				document.getElementById(Campo).options.length = 1;
				var vRetorno = ajax.responseText;
				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;
			
					document.getElementById(Campo).options.add(opcoes);
				}
			
				//quando termina de carregar apaga a mensagem de carregando
				MSG(2,'');
			} else {
				alert("Carregamento falhou!");
			}
		
			ajax = null
		} else {
			MSG(1,'Carregando...');
		}
	}
}

function Drop_down2(pais) {
	var ajax = Verificando();
	var v_pais = pais.split('/');
	var id_pais = v_pais[0];
	var nm_pais = v_pais[1];	
	var url = "consulta.php?action=cidade&id_pais=" + id_pais;  
	var Campo = "id_cidade";
	// identifica o pais
	document.getElementById("id_pais_escolhido").value = id_pais; 
	document.getElementById("nm_pais").value = nm_pais; 
	//limpa os campos
	document.getElementById("nm_cidade").value = ""; 
	document.getElementById("id_cidade").value = ""; 
	
	if(ajax){
		var Rnd = Evita_cache();
		
		if(url.indexOf("?")>=0){
			// já tem parametros vindos na url
			url = url + "&" + Rnd;
		} else { 
			//não tem parâmetros vindo da URL
			url = url + "?" + Rnd;
		}
		
		ajax.onreadystatechange = Status_atual
		ajax.open("GET", url ,true);
		ajax.setRequestHeader("Cache-Control", "no-cache");
		ajax.setRequestHeader("Pragma", "no-cache");
		
		//enquanto espera o retorno da página exibe uma mensagem para o internauta
		MSG(1,'Carregando...');
		
		
		ajax.send(null);
		return true;
	} else {
		alert("Este navegador não tem suporte ao AJAX!");
		return false;
	}
	
	function Status_atual(){
		if (ajax.readyState==4){
			if(ajax.status == 200){
				//alert(document.getElementById(Campo).value);
				
				//monta o dropdown com as opções
				document.getElementById(Campo).options.length = 1;
				var vRetorno = ajax.responseText;
				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;
			
					document.getElementById(Campo).options.add(opcoes);
				}
			
				//quando termina de carregar apaga a mensagem de carregando
				MSG(2,'');
			} else {
				alert("Carregamento falhou!");
			}
		
			ajax = null
		} else {
			MSG(1,'Carregando...');
		}
	}
}

function Drop_down3(cidade) {
	var v_cidade = cidade.split('/');
	var nm_cidade = v_cidade[1];	
	// identifica a cidade
	document.getElementById("nm_cidade").value = nm_cidade; 
}

//exibe mensagem enquanto carrega informações da página solicitada
function MSG(valor,mensagem) {
	if (valor == "1") {
		document.getElementById("msg").innerHTML = mensagem;
	} else {
		document.getElementById("msg").innerHTML = "";
	}
}

