﻿// JScript File
var oXMLDoc = null;
var tipoBrowser="";

var arr_Moradas = new Array();
var arr_Telefones = new Array();
var arr_Anuncios = new Array();
var arr_UrlAnuncio = new Array();

var inc_moradas =0; // nº incrementado de frases
var inc_Anuncios =0; // nº incrementado de frases 
var boolExistemMoradas = false;
var boolExistemAnuncios = false;

function getXmlMoradas(varEmpresa) {
    var strFicheiro = "temp/moradas_" + varEmpresa + ".xml";
    GetDadosXml(strFicheiro);

	if (oXMLDoc.childNodes.length != 0){
		var strTempMorada="";
		var strTempTelefone="";
		var moradas = oXMLDoc.getElementsByTagName('MORADA');
	
		for (i=0;i<moradas.length;i++){ 
			if (moradas[i].getElementsByTagName('DESCRICAO1')[0].firstChild.length > 0){
				strTempMorada = moradas[i].getElementsByTagName('DESCRICAO1')[0].firstChild.nodeValue;
				strTempMorada += '\n' + moradas[i].getElementsByTagName('DESCRICAO2')[0].firstChild.nodeValue;
				strTempMorada += "\n" + moradas[i].getElementsByTagName('DESCRICAO3')[0].firstChild.nodeValue;
				try{
				strTempMorada += "\n" + moradas[i].getElementsByTagName('DESCRICAO4')[0].firstChild.nodeValue;
				}
				catch (e) { }

				try{
				strTempMorada += "\n" + moradas[i].getElementsByTagName('DESCRICAO5')[0].firstChild.nodeValue;
				}
				catch(e){}

			
			strTempTelefone = moradas[i].getElementsByTagName('TELEFONE')[0].firstChild.nodeValue;
			}
			arr_Moradas[i] = strTempMorada + "";
			arr_Telefones[i]=strTempTelefone;
		}
	    return true;
	}
	return false;
}

function moradasIni(){ 
//var incTot =0; //numero incrementado máximo de execução do script
//var TotalCiclos = 100; //numero máximo de execução do script

    if (boolExistemMoradas!=true){ //NÃO HÁ ficheiro
        setObjInnerText(document.getElementById("preMoradas"),"");
        setObjInnerText(document.getElementById("preTelefone"),"");
	    return;
	}

	//se apenas tem um registo não corre o script para os fades
	if (arr_Moradas.length == 1) {
	    setObjInnerText(document.getElementById("preMoradas"), arr_Moradas[0].valueOf());
	    setObjInnerText(document.getElementById("preTelefone"), arr_Telefones[0].valueOf());
	    changeOpac_Anuncio(100, "divMoradas");
	    return;
	}
/*
	if (incTot == TotalCiclos ){ //FIM DO CICLO TOTAL FICA ESTATICO
	    inc = 0;
		str01="SLOT Lisboa" + "\n" +  "Rua Castilho,nº5" + "\n" + "4º andar, sala 40" + "\n" + "1250-066 Lisboa";
		strTelefone01="";
		setObjInnerText(document.getElementById("preMoradas"),str01);
		setObjInnerText(document.getElementById("preTelefone"),strTelefone01);
        return;
	}
	if (inc == 0 ){ //INICIALIZAÇÃO
		setObjInnerText(document.getElementById("preMoradas"),str01);
		setObjInnerText(document.getElementById("preTelefone"),strTelefone01);
	}
*/		

	//CICLO DAS MORADAS
	if (inc_moradas < arr_Moradas.length) {
        setTimeout("fadetext_Moradas()",10); 
		inc_moradas+=1;
		//	res1.value=inc;
	}
	else
	{
		//REINICIA O CICLO DAS MORADAS
		if (inc_moradas == arr_Telefones.length ){
			//incTot+=1;
			inc_moradas=0;
			setTimeout("moradasIni()",10); 
		}
	}		
}


/************************************************************************
* FUNCS COMUNS
*************************************************************************/
function GetDadosXml(xmlFile){
	if (window.ActiveXObject) {// code for IE
	    try {	
			oXMLDoc = new ActiveXObject("Msxml2.XMLHTTP"); // newer IE
			oXMLDoc.async = false;
			oXMLDoc.load(xmlFile);
			tipoBrowser="IE_new"
			}
		catch(e){
			try{
				oXMLDoc = new ActiveXObject("Microsoft.XMLDOM"); // older IE
				oXMLDoc.async = false;
				oXMLDoc.load(xmlFile);
				tipoBrowser="IE_old"
				}
			catch(e){
			    alert("não enc")
			    return false;	
			}
		}
	}	
	else if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xmlHTTP = new XMLHttpRequest();
		if (xmlHTTP.overrideMimeType) {
			xmlHTTP.overrideMimeType('text/xml')}
			
		xmlHTTP.open('get', xmlFile, false);
		xmlHTTP.send('');
		oXMLDoc = xmlHTTP.responseXML;
		tipoBrowser="Moz_SS"
	}
	else if (document.implementation.createDocument) { // outro browser
			oXMLDoc = document.implementation.createDocument('','',null) ;
			oXMLDoc.async=false;
			oXMLDoc.load (xmlFile);
	}			
	else{
		alert('Erro na obtenção do ficheiro de XML');
        return false;		
	}		
	return true;
}

function opacidadeMoradas(id, opacStart, opacEnd, millisec) {
	//velocidade para cada frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//determina a direcção do fade (fade in/out)
	if(opacStart > opacEnd) {
	    setTimeout("forneceMoradas()",5);
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
	    setTimeout("forneceMoradas()",5);
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	return timer;
}
//muda a opacidade para dif. browsers
function changeOpac(intOpacidade, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (intOpacidade / 100);
	object.MozOpacity = (intOpacidade / 100);
	object.KhtmlOpacity = (intOpacidade / 100);
	object.filter = "alpha(opacity=" + intOpacidade + ")";
}

//inicializa a opacidade para dif. browsers
function InicilizaOpac(id) {
	var object = document.getElementById(id).style; 
	object.opacity = 0;
	object.MozOpacity = 0;
	object.KhtmlOpacity = 0;
	object.filter = "alpha(opacity=" + 0 + ")";
}

function fadetext_Moradas(){ 
	var maxOpac;
	var tempPausa = 8000; //tempo de pausa em cada morada
	maxOpac = opacidadeMoradas("divMoradas", 0, 100, 1500);
	if (maxOpac >= 100)
		//setTimeout("InicilizaOpac('" + "divMoradas" + "')",10);
		setTimeout("moradasIni()",tempPausa); 
}

/*** /FUNCS COMUNS *******************************************************/

function forneceMoradas(){
        setObjInnerText(document.getElementById("preMoradas"),arr_Moradas[inc_moradas-1].valueOf());	    
        setObjInnerText(document.getElementById("preTelefone"),arr_Telefones[inc_moradas-1].valueOf());	    
	    return;
}

/*************************************************************************
*Execução de fade para anúncios das últimas oportunidades
*
*
*
*
*
**************************************************************************/

function getXmlAnuncios(varEmpresa) {
    var strFicheiro = "temp/anuncios_" + varEmpresa + ".xml";
    GetDadosXml(strFicheiro);
    
   	if (oXMLDoc.childNodes.length != 0){
		var strTempAnuncio="";
		var strTempUrl ="";
		var anuncios = oXMLDoc.getElementsByTagName('Table');
	
		for (j=0;j<anuncios.length;j++){ 
			if (anuncios[j].getElementsByTagName('titulo')[0].firstChild.length > 0){
				strTempAnuncio = anuncios[j].getElementsByTagName('titulo')[0].firstChild.nodeValue;
				strTempAnuncio += '\n' + "Sector: " + anuncios[j].getElementsByTagName('sector_ds')[0].firstChild.nodeValue;
				strTempAnuncio += "\n" + "Categoria: " + anuncios[j].getElementsByTagName('categoria_DS')[0].firstChild.nodeValue;
			try{
				strTempAnuncio += "\n" + "Àrea: " + anuncios[j].getElementsByTagName('area_ds')[0].firstChild.nodeValue;
			}
			catch(e){}
			
			strTempAnuncio += "\n" + " ";
		    /*	
			try{
				strTempAnuncio += "\n" + anuncios[j].getElementsByTagName('data_insercao')[0].firstChild.nodeValue;
			}
			catch(e){}
			*/
		}
			
			//carregamento do URL da página que serve de base 
			if (anuncios[j].getElementsByTagName('ofertaid')[0].firstChild.length > 0){
				strTempUrl = "oferta.aspx?ID=" + anuncios[j].getElementsByTagName('ofertaid')[0].firstChild.nodeValue;
			}	
			arr_Anuncios[j]=strTempAnuncio;
			arr_UrlAnuncio[j]=strTempUrl;
		}
		return true;
	}
	return false;
}


/*
Esta função foi desactivada pois começou a correr a que está a cima
*/
function getXmlAnuncios_OLD(varEmpresa) {
    var strFicheiro = "temp/anuncios_" + varEmpresa + ".xml";
    GetDadosXml(strFicheiro);
    
   	if (oXMLDoc.childNodes.length != 0){
		var strTempAnuncio="";
		var strTempUrl ="";
		var anuncios = oXMLDoc.getElementsByTagName('ANUNCIO');
	
		for (j=0;j<anuncios.length;j++){ 
			if (anuncios[j].getElementsByTagName('DESCRICAO1')[0].firstChild.length > 0){
				strTempAnuncio = anuncios[j].getElementsByTagName('DESCRICAO1')[0].firstChild.nodeValue;
				strTempAnuncio += '\n' + anuncios[j].getElementsByTagName('DESCRICAO2')[0].firstChild.nodeValue;
				strTempAnuncio += "\n" + anuncios[j].getElementsByTagName('DESCRICAO3')[0].firstChild.nodeValue;
			try{
				strTempAnuncio += "\n" + anuncios[j].getElementsByTagName('DESCRICAO4')[0].firstChild.nodeValue;
			}
			catch(e){}
			try{
				strTempAnuncio += "\n" + anuncios[j].getElementsByTagName('DESCRICAO5')[0].firstChild.nodeValue;
			}
			catch(e){}
			
			}
			
			if (anuncios[j].getElementsByTagName('url_anuncio')[0].firstChild.length > 0){
				strTempUrl = anuncios[j].getElementsByTagName('url_anuncio')[0].firstChild.nodeValue;
			}	
			arr_Anuncios[j]=strTempAnuncio;
			arr_UrlAnuncio[j]=strTempUrl;
		}
		return true;
	}
	return false;
}
function anunciosIni(){ 

    if (boolExistemAnuncios!=true){ //NÃO HÁ DADOS NO XML
		setObjInnerText(document.getElementById("preAnuncios"),"");
	    return;
    }

    //se apenas tem um registo não corre o script para os fades
    if (arr_Anuncios.length == 1) {
        document.getElementById("preAnuncios").codigoAnuncio = arr_UrlAnuncio[0].valueOf();
        setObjInnerText(document.getElementById("preAnuncios"), arr_Anuncios[0].valueOf());
        changeOpac_Anuncio(100, "divAnuncios");
        return;
    }    
	if (inc_Anuncios < arr_Anuncios.length ) {
        setTimeout("fadetext_Anuncios()",1000); 
		inc_Anuncios+=1;
	}
	else
	{
		if (inc_Anuncios == arr_Anuncios.length ){
			//incTot_Anuncios+=1; INTERMINAVEL
			inc_Anuncios=0;
		    setTimeout("anunciosIni()",10); 
		}
	}		
}

function fadetext_Anuncios(){ 
	var maxOpac;
	var tempPausa_Anuncios = 5000; //tempo de pausa em cada morada
	maxOpac = opacidadeAnuncio("divAnuncios", 0, 100, 1500);
	if (maxOpac >= 100)
		setTimeout("anunciosIni()",tempPausa_Anuncios); 
}

function opacidadeAnuncio(id, opacStart, opacEnd, millisec) {
	//velocidade para cada frame
	var speed_Anuncio = Math.round(millisec / 100);
	var timer_Anuncio = 0;
	
	//determina a direcção do fade (fade in/out)
	if(opacStart > opacEnd) {
	    setTimeout("forneceAnuncio()",5);
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac_Anuncio(" + i + ",'" + id + "')",(timer_Anuncio * speed_Anuncio));
			timer_Anuncio++;
		}
	} else if(opacStart < opacEnd) {
        setTimeout("forneceAnuncio()",5);
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac_Anuncio(" + i + ",'" + id + "')",(timer_Anuncio * speed_Anuncio));
			timer_Anuncio++;
		}
	}
	return timer_Anuncio;
}

function forneceAnuncio(){
		document.getElementById("preAnuncios").codigoAnuncio = arr_UrlAnuncio[inc_Anuncios-1].valueOf();
        setObjInnerText(document.getElementById("preAnuncios"),arr_Anuncios[inc_Anuncios-1].valueOf());	    
	    return;
}

function apontaAnuncio(){
	var strUrl = document.getElementById("preAnuncios").codigoAnuncio;
	window.location.href=strUrl;
}

//muda a opacidade para dif. browsers
function changeOpac_Anuncio(intOpacidade, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (intOpacidade / 100);
	object.MozOpacity = (intOpacidade / 100);
	object.KhtmlOpacity = (intOpacidade / 100);
	object.filter = "alpha(opacity=" + intOpacidade + ")";
}


function getObjInnerText(obj){ 
if (obj.innerText) { // IE; 
	return obj.innerText; 
} 
else{ 
	if (obj.textContent) 
	{ 
		return obj.textContent; 
	} 
	else 
		alert("Atenção: O browser não é suportado pela aplicação. Tente usaando o IE ou Firefox."); 
	} 
} 

function setObjInnerText(obj,valor){ 
if (obj.innerText) { // IE; 
	obj.innerText = valor;
} 
else{ 
	if (obj.textContent) 
	{ 
		obj.textContent = valor;
	} 
	else 
		alert("Atenção: O browser não é suportado pela aplicação. Tente usaando o IE ou Firefox."); 
	} 
} 
