
function tryAjax () {
	
	try {
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	
	}catch(e) {
		
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		
		}catch(ex) {
			try {
				ajax = new XMLHttpRequest();
			}catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				ajax = null;
			}
		}
	}
	
	return ajax;
	
	
}


function montaSelect (selectId,text) {
	//// CRIA UM SELECT FIELD A PARTIR DOS DADOS RETORNADOS PELO AJAX NO FORMATO
	//// VALUE|LABEL#VALUE|LABEL ...

	var dataArray   = text.split('#');
	var selectOBJ = document.getElementById(selectId);
	
	selectOBJ.options.length = 1;
	
	if(dataArray.length > 0) {
	 
	 for(var i = 0 ; i < dataArray.length ; i++) {
	 	
	    var item = dataArray[i];
	  
	    options = item.split('|');
		
		var novo = document.createElement("option");
		
		    novo.setAttribute("id", "opcoes");
		    
		    novo.value = options[0];
		    novo.text  = options[1];
	
			selectOBJ.options.add(novo);
	 }
	 
	 selectOBJ.disabled = false;
	 
	}

	
}


function buscaSelect(selectId, php, where) {
	
	/// FAZ UMA BUSCA DOS DADOS POR AJAX E CHAMA A FUNÇÃO QUE MONTA O SELECT
	/// selectId = ID do select a ser populado
	/// php = php a ser chamado no ajax
	/// where = parametros para o ajax

	var selectOBJ = document.getElementById(selectId);
	var ajax = tryAjax();

	  if(ajax) {
	  	
	  	//deixa apenas o elemento 1 no option, os outros são excluídos

		 ajax.open("POST", php, true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
	        
			//após ser processado - chama função processXML que vai varrer os dados

			if (ajax.readyState == 4) {
	            if (ajax.status == 200) {

	            	if (ajax.responseText)
						montaSelect (selectId, ajax.responseText);
					else {
						selectOBJ.options.length = 1;
						selectOBJ.disabled = true;
					}
	                
	            } else {
	                alert('ERRO: '+ajax.statusText);
	            }
       		}


	     }
	     ajax.send(where);
		 
	     
	     return true;
	  }
	  
}




function ajaxSalvaEmail(email) {
	/// SALVA O EMAIL DA NEWSLETTER DA HOME
	var ajax = tryAjax();
	div = document.getElementById('news_msg');
	if(ajax) {
		ajax.open("POST", 'ajax/ajaxSalvaEmail.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					div.innerHTML = '<p class=\"p2\"> Seu E-mail foi cadastrado com sucesso!</p>';
					document.getElementById('email').value = '';
				} else 
					alert('ERRO: '+ajax.statusText);
			}
		}
		ajax.send("email="+email);
		return true;
	}
}


//Seta a sessão quantidade
function ajaxSetaSessaoQuantidade(valor, indice){
	var ajax = tryAjax();
	if(ajax) {

		ajax.open("POST", 'ajax/ajaxSetaSessao.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
				} else 
					alert('ERRO: '+ajax.statusText);
			}
		}
		ajax.send("valor="+valor+"&indice="+indice);
		return true;
	}
}




//previsao do tempo para uma cidade
function ajaxPrevisaoTempo(cidade){

	var div = document.getElementById('conteudo_prev');
	var ajax = tryAjax();
	if(ajax) {

		ajax.open("POST", 'ajax/ajaxPrevisao.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					div.innerHTML = ajax.responseText;
				} else 
					alert('ERRO: '+ajax.statusText);
			}
		}
		ajax.send("cidade="+cidade);
		//return true;
	}
}

//cotacao
function ajaxCotacao(moeda){

	var div = document.getElementById('conteudo_cotacao');
	var ajax = tryAjax();
	if(ajax) {
		
		ajax.open("POST", 'ajax/ajaxCotacao.php', true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					div.innerHTML = ajax.responseText;
				} else 
					alert('ERRO: '+ajax.statusText);
			}
		}
		ajax.send("moeda="+moeda);
		//return true;
	}
}

