/* A seguinte Função cria o objecto XMLHttpRequest */

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http = createRequestObject(); 
var http_tempo = createRequestObject(); 
var http_farm = createRequestObject(); 

function carregafuncoes(){
	gettempo();
	getfarmacias();
}



/* Chamar dados do tempo */
function gettempo(){
	http_tempo.open('get', 'ajax/info_meteo.php');
	http_tempo.onreadystatechange = handletempo; 
	http_tempo.send(1);
}

/* Mostra dados do tempo */
function handletempo(){
	if(http_tempo.readyState == 4){ //Finished loading the response
		var response = http_tempo.responseText;
		document.getElementById('tempo').innerHTML = response;
	}
}


/* Chamar dados do farmacias*/
function getfarmacias(){
	http_farm.open('get', 'ajax/info_farmacias.php');
	http_farm.onreadystatechange = handlefarmacias; 
	http_farm.send(1);
}

/* Mostra dados do farmacias */
function handlefarmacias(){
	if(http_farm.readyState == 4){ //Finished loading the response
		var response = http_farm.responseText;
		document.getElementById('farmacias_servico').innerHTML = response;
	}
}



/* Chamar ficheiro da Edição */
function getedicao(){
	http.open('get', 'javascript/veredicao.php?edicao='+document.formedicao.nameedicao.options[document.formedicao.nameedicao.selectedIndex].value);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A verificar Edição...';
	http.onreadystatechange = handleProducts; 
	http.send(1);
}

/* Mostra Resultado da Edição */
function handleProducts(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('numedicao').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
	}
}

/* Chamar ficheiro de Página Central*/
function getpagina(id,tipo){
	http.open('get', 'javascript/verpagina.php?link='+id+'&tipo='+tipo);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A carregar Informação...';
	http.onreadystatechange = handlepagina; 
	http.send(1);
}

/* Mostra Resultado da Página Central */
function handlepagina(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('conteudo_central').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
	}
}


/* Chamar ficheiro de Ver Sondagem*/
function getvotacao(id,tipo){
	http.open('get', 'javascript/vervotacao.php');
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A carregar Sondagem...';
	http.onreadystatechange = handlevotacao; 
	http.send(1);
}

/* Mostra Resultado de Ver Sondagem */
function handlevotacao(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('conteudo_central').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
	}
}

/* Chamar ficheiro de Votar*/
function getvotar(resp){
	http.open('get', 'javascript/votar.php?resp='+resp);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>Realizar o seu Voto...';
	http.onreadystatechange = handlevotar; 
	http.send(1);
}

/* Mostra Resultado de Votar */
function handlevotar(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('conteudo_central').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
		alert('Obrigado pelo seu voto!');
	}
}

/* Chamar ficheiro de Comentario*/
function getcomentario(id_noticia,nome,email,msg){
	http.open('get', 'javascript/comentar.php?id_noticia='+id_noticia+'&nome='+nome+'&email='+email+'&mensagem='+msg);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>Realizar o seu Comentário...';
	http.onreadystatechange = handlecomentario; 
	http.send(1);
}

/* Mostra Resultado de Comentario */
function handlecomentario(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('comentario_result').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
		alert('Obrigado pelo seu Comentário!');
	}
}


/* Chamar ficheiro do Jornalismo do Cidadão*/
function getcomentario1(nome,email,contacto,titulo,msg){
	http.open('get', 'javascript/newjorcid.php?nome='+nome+'&email='+email+'&contacto='+contacto+'&titulo='+titulo+'&noticia='+msg);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>Adicionar Notícia...';
	http.onreadystatechange = handlecomentario1; 
	http.send(1);
}

/* Mostra Resultado do Jornalismo do Cidadão */
function handlecomentario1(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('divjorcid').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
		alert('Obrigado pelo seu Contributo! \n A sua notícia vai ser analisada e as melhores serão publicadas no jornal.');
	}
}


/* Chamar ficheiro de Procura*/
function getsearch(procura){
	http.open('get', 'javascript/search.php?procura='+procura);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>Realizar a Procura...';
	http.onreadystatechange = handlesearch; 
	http.send(1);
}

/* Mostra Resultado da Procura*/
function handlesearch(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('conteudo_central').innerHTML = response;
		document.getElementById('informacao').style.display='none';	
	}
}


/* Chamar ficheiro do calendario */
function getdata(month,year){
	http.open('get', 'javascript/verdata.php?month='+month+'&year='+year);
	document.getElementById('informacao').style.display='block';
	document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A carregar calendário...';
	http.onreadystatechange = handledata; 
	http.send(1);
}

/* Mostra Resultado do calendario */
function handledata(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('tabela_calendario').innerHTML = response;
		document.getElementById('informacao').style.display='none';
	}
}

/* Chamar ficheiro de acontecimento */
function getacont(dia,mes,ano,opcao,id,texto,tipo){


if(tipo!=''){
	var selObj = document.getElementById(tipo);
	var selIndex = selObj.selectedIndex;
	var tipo = selObj.options[selIndex].value;
}
if(texto!=''){
	texto=document.getElementById(texto).value;
}
	http.open('get', 'javascript/veracont.php?dia='+dia+'&mes='+mes+'&ano='+ano+'&opcao='+opcao+'&id='+id+'&texto='+texto+'&tipo='+tipo);
	document.getElementById('informacao').style.display='block';
	if (opcao=='ver'){
		document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A listar acontecimentos...';
	}
	if (opcao=='apagar'){
		document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A eliminar acontecimento...';
	}
	if (opcao=='inserir'){
		document.getElementById('informacao').innerHTML = '<img src=\'imagens/carregando.gif\'><br>A inserir acontecimento...';
	}
	http.onreadystatechange = handleacont;
	http.send(1);
}

/* Mostra Resultado de acontecimento */
function handleacont(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		document.getElementById('tabela_acont').innerHTML = response;
		document.getElementById('informacao').style.display='none';		
		getdata(document.getElementById('mes_actual').value,document.getElementById('ano_actual').value);
	}
}



