function select_cidade(lingua) {
	cidade = document.form1.select.options[document.form1.select.selectedIndex].value;
	if (cidade) 
	{
 	  location = "portfolio.php?lingua="+lingua+"&sub_zone=por_localizacao&cidade="+cidade;
	}
}


//Obter a resolução do monitor
function obter_resolucao_monitor()
{
	var largura, altura;
	largura = screen.width;
	altura = screen.height;
	
	alert ('A sua resolução é: '+largura+'x'+altura);
}

//Adicionar um endereço aos favoritos
function adicionar_link_favoritos(endereco, nome){
	window.external.addFavorite(endereco, nome);
}

function pesquisar(menu) {

	if (document.getElementById) 
		var crit = document.getElementById("pesquisa").value;
	else
		if (document.layers)
			var crit = document.pesquisa.value;
		else
			var crit = document.all.pesquisa.value;
	
	if (crit == '')
		location = "index.php?menu="+menu;
	else
		location = "index.php?menu="+menu+"&search="+crit;	
}

function get_elementos(id,tipo) {

	var form2 = "form"+id
	var elem = new Array(6);
	
	if (document.getElementById) 
	{ // DOM3 = IE5, NS6
	   	elem[0] = document.getElementById(form2).dados_ant.value;
		elem[1] = document.getElementById(form2).designacao_pt.value;
		elem[2] = document.getElementById(form2).designacao_en.value;
		if (tipo == 'download')
			if (document.getElementById(form2).novo_ficheiro.value == '')
				elem[3] = document.getElementById(form2).ficheiro_existente.value;
			else
				elem[3] = document.getElementById(form2).novo_ficheiro.value;
		else
			elem[3] = document.getElementById(form2).endereco_http.value;

		if (document.getElementById(form2).imagem_existente)
			if(document.getElementById(form2).imagem.value == '')
				elem[4] = document.getElementById(form2).imagem_existente.value;
			else
				elem[4] = document.getElementById(form2).imagem.value;		
		else
			elem[4] = document.getElementById(form2).imagem.value;
		
		if (tipo == 'cliente')
			elem[5] = document.getElementById(form2).tipo_cliente.value;
		else
			if (tipo == 'download')
				elem[5] = document.getElementById(form2).tipo_download.value;
			else
				elem[5] = document.getElementById(form2).tipo_link.value;
    } else {
        if (document.layers) { // Mozila
			elem[0] = document.form2.dados_ant.value;
			elem[1] = document.form2.designacao_pt.value;
			elem[2] = document.form2.designacao_en.value;
			elem[3] = document.form2.endereco_http.value;

			if (document.form2.imagem_existente)
				if(document.form2.imagem.value == '')
					elem[4] = document.form2.imagem_existente.value;
				else
					elem[4] = document.form2.imagem.value;		
			else
				elem[4] = document.form2.imagem.value;
			var type = "tipo_"+tipo;
			elem[5] = document.form2.type.value;
        }
        else { // IE 4
			elem[0] = document.all.form2.dados_ant.value;
			elem[1] = document.all.form2.designacao_pt.value;
			elem[2] = document.all.form2.designacao_en.value;
			elem[3] = document.all.form2.endereco_http.value;

			if (document.all.form2.imagem_existente)
				if(document.all.form2.imagem.value == '')
					elem[4] = document.all.form2.imagem_existente.value;
				else
					elem[4] = document.all.form2.imagem.value;		
			else
				elem[4] = document.all.form2.imagem.value;
			var type = "tipo_"+tipo;
			elem[5] = document.all.form2.type.value;
        }
    }
	return elem;
}

function compara_elementos(dados,elem_list) {
	
	var data = dados.split("|");

	if (data[1] != elem_list[1])
		return false;
	if (data[2] != elem_list[2])
		return false;
	if (data[3] != elem_list[3])
		return false;
	if (data[4] != elem_list[4])
		return false;
	if (data[5] != elem_list[5])
		return false;
	return true;
}

function guardar_dados(lista_ids,num,id_link,tipo) {
	
	var lista = lista_ids.split(" ");
	var i = num-1;	
	if (document.getElementById) 
	{ // DOM3 = IE5, NS6
		while (i >= 0) 
		{
			id = 'div_alterar_'+lista[i];
			if (document.getElementById(id).style.display == 'block')
			{
				var elem_list = get_elementos(lista[i],tipo);
				var flag = compara_elementos(elem_list[0],elem_list);
				if (flag == false) 
				{
					var conf = confirm("Alterou a informação do "+tipo+" em que se encontrava.Se carregar em Ok guarda a alteração.");
					hidediv(id);
					if (conf == true) {
						if (tipo == 'cliente')
							document.getElementById("form"+lista[i]).new_cliente.value = id_link;
						else 
							if (tipo == 'link')
								document.getElementById("form"+lista[i]).new_link.value = id_link;
							else
								document.getElementById("form"+lista[i]).new_download.value = id_link;
						document.getElementById("form"+lista[i]).submit();
					} else
						document.getElementById("form"+lista[i]).reset();
				}
				else {
					document.getElementById("form"+lista[i]).reset();
					hidediv(id);
				}
				break;
			}
		i -= 1;
		}
	
    } else {
        if (document.layers) 
		{ // Mozila
			while (i >= 0) 
			{
				id = 'div_alterar_'+lista[i];
				if (document.id.display == 'block')
				{
					var elem_list = get_elementos_link(lista[i]);
					var flag = compara_elementos(elem_list[0],elem_list);
					id_form = "form"+lista[i];
					if (flag == false) 
					{
						var conf = confirm("Alterou a informação do "+tipo+" em que se encontrava.Se carregar em Ok guarda a alteração.");
						document.id_form.reset();
						hidediv(id);
						if (conf == true) {
							var type = "new_"+tipo;
							document.id_form.tipo.value = id_link;
							document.id_form.submit();
						}
					}
					else {
						document.id_form.reset();
						hidediv(id);
					}
					break;
				}
			i -= 1;
			}
		} else { // IE 4
			while (i >= 0) 
			{
				id = 'div_alterar_'+lista[i];
				if (document.all.id.style.display == 'block')
				{
					var elem_list = get_elementos_link(lista[i]);
					var flag = compara_elementos(elem_list[0],elem_list);
					id_form = "form"+lista[i];
					if (flag == false) 
					{
						var conf = confirm("Alterou a informação do "+tipo+" em que se encontrava.Se carregar em Ok guarda a alteração.");
		             	document.all.id_form.reset();
						hidediv(id);
						if (conf == true) {
							var type = "new_"+tipo;
							document.all.id_form.tipo.value = id_link;
			             	document.all.id_form.submit();
						}
					}
					else {
						document.all.id_form.reset();
						hidediv(id);
					}
					break;
				}
			i -= 1;
			}
		}
	}
}

/* Usada para escondender um div dado o respectivo id*/
function hidediv(id) 
{
    // Função para esconder (hide) um elemento dado o respectivo id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'none';
    }
    else {
        if (document.layers) { // Mozila
            document.id.display = 'none';
        }
        else { // IE 4
            document.all.id.style.display = 'none';
        }
    }
}

function showdiv(id) {
    // Unção para apresentar um elemento dado o respectivo id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'block';
    }
    else {
				
        if (document.layers) { // Mozila
            document.id.display = 'block';
        }
        else { // IE 4
            document.all.id.style.display = 'block';
        }
    }
}

//Apagar link
function accao_conf_apagar_link(id, imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar este Link?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_link.php?id_link="+id+"&imagem="+imagem;
	}	
}


//Apagar Notícia
function accao_conf_apagar_noticia(id, imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar esta Noticia/Evento?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_noticiaevento.php?id_noticia_evento="+id+"&imagem="+imagem;
	}	
}


//Apagar imagem que aparece do lado esquerdo da página Notícia & Eventos
function accao_conf_apagar_img_noticia_evento(id, imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar a imagem existente?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_img_noticiaevento.php?id_imagem_eventos="+id+"&ficheiro_imagem="+imagem;
	}	
}

//Apagar cliente
function accao_conf_apagar_cliente(id, imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar este cliente?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_cliente.php?id_cliente="+id+"&imagem="+imagem;
	}	
}

//Apagar cliente
function accao_conf_apagar_utilizador(login)
{
	var conf=confirm("Tem a certeza que deseja apagar este Utilizador?"); //Confirma a eliminação?
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_utilizador.php?login="+login;
	}	
}

//Apagar download 
function accao_conf_apagar_download(id, imagem, documento)
{
	var conf=confirm("Tem a certeza que deseja apagar este download?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_download.php?id_download="+id+"&imagem="+imagem+"&documento="+documento;
	}	
}

//Apagar imagem de um determinado download
function accao_conf_apagar_img_download_2(id)
{
	var conf=confirm("Tem a certeza que deseja apagar a imagem existente?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_img_download.php?id_download="+id;
	}	
}


//Apagar cidade
function accao_conf_apagar_cidade(id)
{
	var conf=confirm("Tem a certeza que deseja apagar esta cidade?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_cidade.php?id_cidade="+id;
	}	
}


//Apagar cidade
function accao_conf_apagar_portfolio(id, img1_p, img2_p, img3_p, img4_p, img5_p, img1_g, img2_g, img3_g, img4_g, img5_g)
{
	var conf=confirm("Tem a certeza que deseja apagar este registo?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_portfolio.php?id_portfolio="+id+"&img1_p="+img1_p+"&img2_p="+img2_p+"&img3_p="+img3_p+"&img4_p="+img4_p+"&img5_p="+img5_p+"&img1_g="+img1_g+"&img2_g="+img2_g+"&img3_g="+img3_g+"&img4_g="+img4_g+"&img5_g="+img5_g;
	}	
}


//Apagar imagem de uma determinada página - 

//TODO: APAGAR ESTA FUNÇÃO DEPOIS DE ALTERAR TODAS AS PAGINAS ONDE ESTA FUNÇÃO É CHAMADA
// NAS PÁGINAS ONDE ESTA FUNÇÃO É CHAMADA, DEVE-SE SUBSTITUIR ESTA FUNÇAO PELA FUNCAO: accao_conf_apagar_imagem(pagina, id, inicio, nome_imagem)
function accao_conf_apagar_img(pagina, id, inicio)
{
	var conf=confirm("Tem a certeza que deseja apagar a imagem existente?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		switch (pagina)
		{
		case "download":
		  location = "scripts/accao_apagar_img_download.php?id_download="+id;
		  break
		case "cliente":
		  location = "scripts/accao_apagar_img_cliente.php?id_cliente="+id;
		  break
		case "link":
		  location = "scripts/accao_apagar_img_link.php?id_link="+id;
		  break
		case "tipo_interv":
		  location = "scripts/accao_apagar_img_tipo_interv.php?id_tipo_intervencao="+id+"&inicio"+inicio;
		  break
		case "tipo_cliente":
		  location = "scripts/accao_apagar_img_tipo_cliente.php?id_tipo_cliente="+id+"&inicio"+inicio;
		  break
		case "noticia":
		  location = "scripts/accao_apagar_img_noticia.php?id_noticia_evento="+id+"&inicio="+inicio;
		  break
		default:
		  alert("Menu inexistente!");
		}
	}	
}

//Apagar imagem de uma determinada página
function accao_conf_apagar_imagem(pagina, id, inicio, nome_imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar a imagem existente?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		switch (pagina)
		{
		case "download":
		  location = "scripts/accao_apagar_img_download.php?id_download="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		case "cliente":
		  location = "scripts/accao_apagar_img_cliente.php?id_cliente="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		case "link":
		  location = "scripts/accao_apagar_img_link.php?id_link="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		case "tipo_interv":
		  location = "scripts/accao_apagar_img_tipo_interv.php?id_tipo_intervencao="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		case "tipo_cliente":
		  location = "scripts/accao_apagar_img_tipo_cliente.php?id_tipo_cliente="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		case "noticia":
		  location = "scripts/accao_apagar_img_noticia.php?id_noticia_evento="+id+"&inicio="+inicio+'&nome_imagem='+nome_imagem;
		  break
		default:
		  alert("Menu inexistente!");
		}
	}	
}



function apagar_imagem_portfolio(id, campo_imagem, inicio, nome_imagem)
{
	var conf=confirm("Tem a certeza que deseja apagar esta imagem?");
	if (conf == false){ 
		window.location="#"; 
	}
	else if (conf == true){
		location = "scripts/accao_apagar_img_portfolio.php?id_portfolio="+id+"&campo_imagem="+campo_imagem+"&inicio="+inicio+"&nome_imagem="+nome_imagem;
	}
}


/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/


/* Verificar se os campos obrigatórios foram preenchidos */
function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		  {alert(alerttxt);return false}
		else {return true}
	}
}

function validate_form(thisform, tipo_form)
{
	switch (tipo_form) 
	{ 
	   	case "link" : 
			with (thisform)
			{
				if (validate_required(texto_pt, "Campo \"DesignaÃ§Ã£o pt\" deve ser preenchido!")==false)
					{texto_pt.focus();return false}
				if (validate_required(endereco_http, "Campo \"EndereÃ§o http\" deve ser preenchido!")==false)
					{endereco_http.focus();return false}
				if (validate_required(tipo_link, "Campo \"Tipo link\" deve ser preenchido!")==false)
					{tipo_link.focus();return false}
			}
		  	break;
			
		case "cliente" : 
			with (thisform)
			{
				if (validate_required(texto_pt, "Campo \"DesignaÃ§Ã£o pt\" deve ser preenchido!")==false)
					{texto_pt.focus();return false}
				if (validate_required(tipo_cliente, "Campo \"Tipo cliente\" deve ser preenchido!")==false)
					{tipo_link.focus();return false}
			}
		  	break; 
	   
	   	case "download" : 
			with (thisform)
			{
				if (validate_required(texto_pt, "Campo \"DesignaÃ§Ã£o pt\" deve ser preenchido!")==false)
					{texto_pt.focus();return false}
				if (validate_required(ficheiro, "Campo \"Ficheiro\" deve ser preenchido!")==false)
					{ficheiro.focus();return false}
			}
		  	break; 
		
		case "noticia_evento" : 
			with (thisform)
			{
				if (validate_required(data, "Campo \"Data\" deve ser preenchido!")==false)
					{data.focus();return false}
				if (validate_required(titulo_pt, "Campo \"Título pt\" deve ser preenchido!")==false)
					{titulo_pt.focus();return false}
			}
		  	break;
		
		case "img_noticia_evento" : 
			with (thisform)
			{
				if (validate_required(ficheiro_imagem, "Campo \"Imagem\" deve ser preenchido!")==false)
					{ficheiro_imagem.focus();return false}
			}
		  	break;
		
		case "tipo_interv" : 
			with (thisform)
			{
				if (validate_required(nova_imagem, "Deve-se indicar a imagem que se pretende inserir!")==false)
					{nova_imagem.focus();return false}
			}
		  	break;
		
		case "tipo_cliente" : 
			with (thisform)
			{
				if (validate_required(nova_imagem, "Deve-se indicar a imagem que se pretende inserir!")==false)
					{nova_imagem.focus();return false}
			}
		  	break;
			
		case "portfolio" : 
			with (thisform)
			{
				if (validate_required(titulo_pt, "Campo \"Titulo (pt)\" deve ser preenchido!")==false)
					{titulo_pt.focus();return false}
				if (validate_required(titulo_en, "Campo \"Titulo (en)\" deve ser preenchido!")==false)
					{titulo_en.focus();return false}
			}
		  	break; 
			
	   default: 
			with (thisform)
			{
				if (validate_required(texto_pt, "Campo \"DesignaÃ§Ã£o pt\" deve ser preenchido!")==false)
					{texto_pt.focus();return false}
				if (validate_required(endereco_http, "Campo \"EndereÃ§o http\" deve ser preenchido!")==false)
					{endereco_http.focus();return false}
				if (validate_required(tipo_link, "Campo \"Tipo link\" deve ser preenchido!")==false)
					{tipo_link.focus();return false}
			}
	} 
}


/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

function show_sub_menu(sub_menu)
{	
	alert(sub_menu);
}
