function getUrl (url) {
    if( url.substring(0,1) == '/' ) {
        // Localização absoluta
        return document.location.protocol+"//"+document.location.host+url;
    }
    else {
        // Localização relativa
        var pathname = document.location.pathname;
        pathname = pathname.substring(0, pathname.lastIndexOf('/'));
        return document.location.protocol+"//"+document.location.host+pathname+"/"+url;
    }
}

function getHttpUrl (url) {
    if( url.substring(0,1) == '/' ) {
        // Localização absoluta
        return "http://"+document.location.host+url;
    }
    else {
        // Localização relativa
        var pathname = document.location.pathname;
        pathname = pathname.substring(0, pathname.lastIndexOf('/'));
        return "http://"+document.location.host+pathname+"/"+url;
    }
}

function janela (url, title, width, height, scrollbars, resize) {
	if (scrollbars == null) scrollbars='no';
	if (resize == null) resize='no';
	var opened = window.open(url, title, 'width='+width+',height='+height+',menubar=no,location=no,resizable='+resize+',scrollbars='+scrollbars+',status=yes,toolbar=no,titlebar=no');
	if (opened != null) opened.focus();
}

function janelaWindow (url, title, width, height, scrollbars, resize) {
	if (scrollbars == null) scrollbars='no';
	if (resize == null) resize='no';
	var opened = window.open(url, title, 'width='+width+',height='+height+',menubar=no,location=no,resizable='+resize+',scrollbars='+scrollbars+',status=yes,toolbar=no,titlebar=no');
	if (opened != null) opened.focus();
	
		return opened;
}

function janelaAjuda (url) {
	var opened = window.open(getHttpUrl(url), 'Ajuda', 'width=760,height=560,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=yes,titlebar=no');
	if (opened != null) opened.focus();
}

function janelaPopup(url, title, width, height, scrollbars, resize) {
     var newUrl = getUrl('externalPopup.jsp?body=' + url);
     janela(newUrl, title, width, height, scrollbars, resize);
}

function showHideElement (id) {
	if (true) return;
	var element = document.getElementById(id)
	var old = element.style.visibility;
	if (old == 'hidden') {
		element.style.visibility = 'visible';
		element.style.display = 'inline';
	} else {
		element.style.visibility = 'hidden';
		element.style.display = 'none';
	}
}

function janelaImpressos (url) {
	var opened = window.open(getHttpUrl(url), 'Impressos', 'width=760,height=560,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,titlebar=no');
	if (opened != null) opened.focus();
}

function janelaLocalizacao(localizacao) {
	var url = '/home/selectLocalizacao.jsp';
	if (localizacao.length > 0) url += '?localizacao=' + localizacao;
	var opened = window.open(url,
					   'SeleccaoLocalizacao',
		        	   'width=500,height=200,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaSF(rfid) {
	var url = '/home/ConsultaSF.jsp';
	if (rfid.length > 0) url += '?rfid=' + rfid;
	var opened = window.open(url,
					   'SeleccaoSF',
		        	   'width=250,height=120,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no')
}

function janelaContactarSF (jsp, numeroP, nif) {
	var url = jsp+'?numeroProcesso='+numeroP+'&nif='+nif;
	var opened = window.open(url,
					   'ContactarSF',
		        	   'width=600,height=300,menubar=no,location=no,resizable=yes,scrollbars=no,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaConsultaLocalizacao(localizacao) {
	var opened = window.open('/home/ConsultaLocalizacao.jsp?localizacao='+localizacao,
					   'ConsultaLocalizacao',
		        	   'width=480,height=200,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
}

function janelaConsultaFreguesia(localizacao) {
	var opened = window.open('/home/ConsultaFreguesia.jsp?localizacao='+localizacao,
					   'ConsultaFreguesia',
		        	   'width=480,height=200,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
}

function janelaPDF (url, title) {
	janela(url, title, 750, 500, 'yes', 'yes');
}

function janelaMaisInfoCertidao (url, title) {
	janela(url, title, 450, 520, 'yes', 'yes');
}

function janelaPDFWindow (url, title) {
	return janelaWindow(url, title, 750, 500, 'yes', 'yes');
}

function janelaZonamento(baseUrl, tipoCoefLoc, localizacao, coordX, coordY, dataImi01, numFichaAval, urlResposta, isEditable) {
    var url = baseUrl;
    url += '?codFreguesia=' + localizacao;
    if (tipoCoefLoc != "") url += '&afectacao=' + tipoCoefLoc;
    if (coordX != "") url += '&coordX=' + coordX;
    if (coordY != "") url += '&coordY=' + coordY;
	if (dataImi01 != "") url += '&dataimi01=' + dataImi01;
	if (numFichaAval != "") url += '&numFichaAval=' + numFichaAval;
	if (isEditable != undefined && isEditable != "") url += '&edit=' + isEditable; else url += '&edit=true';
    url += '&url=' + escape(urlResposta);

//    alert(url);

	var opened = window.open(url, 'Zonamento', "width=930, height=640, scrollbar=no,menubar=no,toolbar=no,status=yes,screenX=0,screenY=0,left=0,top=0")
	opened.focus();

}

function janelaZonamentoPublico (url) {
	//getHttpUrl(url)
	var opened = window.open('http://www.e-financas.gov.pt'+url, 'Zonamento', 'width=760,height=560,menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,titlebar=yes');
	if (opened != null) opened.focus();
}

function janelaBeneficiosIMT(beneficio) {
	var url = 'imt/janelaBeneficios.jsp';
	if (beneficio.length > 0) url += '?beneficio=' + beneficio;
	var opened = window.open(url,
					   'SeleccaoBeneficioIMT',
		        	   'width=350,height=375,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaOnusEncargosIMT(onusEncargos) {
	var url = 'imt/janelaOnusEncargos.jsp';
	if (onusEncargos.length > 0) url += '?onusEncargos=' + onusEncargos;
	var opened = window.open(url,
					   'SeleccaoOnusEncargosIMT',
		        	   'width=300,height=275,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaFactosTributariosIMT(factoTributario) {
	var url = 'imt/janelaFactosTributarios.jsp';
	if (factoTributario.length > 0) url += '?factoTributario=' + factoTributario;
	var opened = window.open(url,
					   'SeleccaoFactosTributariosIMT',
		        	   'width=400,height=550,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaPublicacaoDividas (url) {
	if(url.substring(0,7) != 'http://')
		url = getHttpUrl(url);

	var opened = window.open(url, 'Ajuda', 'width=800,height=600,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,titlebar=no');
	if (opened != null) opened.focus();
}

function janelaActividades(actividade,tipoActividade) {
	var url = 'selectActividade.action';
	url += '?tipoActividade='+tipoActividade;
	if (actividade.length > 0) url += '&actividade=' + actividade;
	var opened = window.open(url,
					   'SeleccaoActividade',
		        	   'width=550,height=475,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

function janelaTribunais(tribunal) {
	var url = 'insolvencias/consultaTribunais.jsp';
	if (tribunal.length > 0) url += '?tribunal=' + tribunal;
	var opened = window.open(url,
					   'SeleccaoTribunal',
		        	   'width=650,height=400,menubar=no,location=no,resizable=no,scrollbars=yes,status=no,toolbar=no,titlebar=no')
	if (opened != null) opened.focus();
	return opened;
}

/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
	init: function(att,val) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {					
					if(val == 'MapaParcelar'){
						oLink.onclick = MapaParcelar.openWin;
					} else {
						oLink.onclick = JSTarget.openWin;
					}										
				}
			}
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank','width=750,height=500,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,titlebar=no');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				if(document.getElementById("displayError"))
					document.getElementById("displayError").innerHTML="";
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {		
		if (obj.addEventListener){
			obj.addEventListener(type, fn, false);			
		}
		else if (obj.attachEvent) {			
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};

JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","pdf");});
JSTarget.addEvent(window, 'load', function(){JSTarget.init("class","MapaParcelar");});

/*
MapaParcelar function by dneves
*/
var MapaParcelar = {
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
			
			var url = this.getAttribute('href');
			
			if((String(url)).indexOf('?') == -1){
				var freguesia = document.getElementById("freguesia").value;
				var tipopredio = document.getElementById("tipopredio").value;
				var seccao = document.getElementById("seccao").value;
				var artigo = document.getElementById("artigo").value;
				var arvcol = document.getElementById("arvcol").value;
				url = url + "?freguesia=" + freguesia + "&tipopredio=" + tipopredio + "&artigo=" + artigo;
				
				if(seccao != null || typeof seccao != 'undefined'){
					url = url + "&seccao=" + seccao;
				}

				if(arvcol != null || typeof arvcol != 'undefined'){
					url = url + "&arvcol=" + arvcol;					
				}
			}
						
		    var oWin = window.open(url, '_blank','width=900,height=800,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				if(document.getElementById("displayError"))
					document.getElementById("displayError").innerHTML="";
				return false;
			}
			oWin = null;
			return true;
		}
	}
};

function mapaParcelar(){
	var url = 'consultaMapaParcelar.action';
	
	if((String(url)).indexOf('?') == -1){
		var freguesia = document.getElementById("freguesia").value;
		var tipopredio = document.getElementById("tipopredio").value;
		var seccao = document.getElementById("seccao").value;
		var artigo = document.getElementById("artigo").value;
		var arvcol = document.getElementById("arvcol").value;
		url = url + "?freguesia=" + freguesia + "&tipopredio=" + tipopredio + "&artigo=" + artigo;
		
		if(seccao != null || typeof seccao != 'undefined'){
			url = url + "&seccao=" + seccao;
		}

		if(arvcol != null || typeof arvcol != 'undefined'){
			url = url + "&arvcol=" + arvcol;					
		}
	}
				
    var oWin = window.open(url, '_blank','width=900,height=800,menubar=no,location=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no');

	if (oWin) {
		if (oWin.focus) oWin.focus();
		if(document.getElementById("displayError"))
			document.getElementById("displayError").innerHTML="";
		return false;
	}
	oWin = null;
	return true;
    
}

