var flag="";

function carrega_div_noticias(id_elem)
{
	elem_div=document.getElementById("activa_div_"+id_elem);
	elem_div_1=document.getElementById("activa_div_1_"+id_elem);
	elem_td=document.getElementById("td_mostra_div_"+id_elem);
	
	flag=id_elem;
	
	elem_td.style.color="#000000";
	elem_td.style.fontWeight="normal";
	elem_td.style.background="#E8E8E8";
	elem_div.style.display="block";
	elem_div_1.style.display="block";

	for(i=1; i<total_areas; i++)
	{
		if(document.getElementById("activa_div_"+i))
		{
			if("activa_div_"+i !="activa_div_"+id_elem)
			{
				document.getElementById("activa_div_"+i).style.display="none";
				document.getElementById("activa_div_1_"+i).style.display="none";
				document.getElementById("td_mostra_div_"+i).style.color="#FFFFFF";
				document.getElementById("td_mostra_div_"+i).style.background="#666666";
				document.getElementById("td_mostra_div_"+i).style.fontWeight="normal";
			}
		}
	}
}

function actica_link_areas(id_elem)
{
	elem_td=document.getElementById("td_mostra_div_"+id_elem);
	elem_td.style.color="#000000";
	elem_td.style.fontWeight="normal";
	elem_td.style.background="#E8E8E8";
	elem_td.style.cursor="pointer";
	
	
	for(i=1; i<total_areas; i++)
	{
		if(document.getElementById("td_mostra_div_"+i))
		{
			if("td_mostra_div_"+i !="td_mostra_div_"+id_elem)
			{
				if(flag!=i)
				{
					document.getElementById("td_mostra_div_"+i).style.color="#FFFFFF";
					document.getElementById("td_mostra_div_"+i).style.background="#666666";
					document.getElementById("td_mostra_div_"+i).style.fontWeight="normal";
				}
			}
		}
	}
}

function desactiva_link_areas()
{
	if(flag)
	{
		for(i=1; i<total_areas; i++)
		{
			if(document.getElementById("td_mostra_div_"+i))
			{
				if(flag!=i)
				{
					document.getElementById("td_mostra_div_"+i).style.color="#FFFFFF";
					document.getElementById("td_mostra_div_"+i).style.background="#666666";
					document.getElementById("td_mostra_div_"+i).style.fontWeight="normal";
				}
			}
		}
	}
}
function roda_divs(cot)
{	
	var total=total_areas-1;
	var layer=cot;
	carrega_div_noticias(cot);
	if(cot<total) num=cot+1;
	else num=1;
	t=setTimeout("roda_divs(num)",5000);
}

//***************************************************/

function top_news(id)
{		
	var div1=document.getElementById("div1");
	var div2=document.getElementById("div2");
	var div3=document.getElementById("div3");																
	var ultimas=document.getElementById("ultimas");
	var lidas=document.getElementById("mais_lidas");
	var comentadas=document.getElementById("mais_comentadas");																
	if(id=="ultimas")
	{
		div1.style.display="block";
		ultimas.style.background="#dddddd";
		ultimas.style.color="#000000";
		ultimas.style.fontWeight="bold";
		
		div2.style.display="none";
		lidas.style.background="#666666";
		lidas.style.color="#FFFFFF";
		lidas.style.fontWeight="normal";
		
		div3.style.display="none";
		comentadas.style.background="#595959";
		comentadas.style.color="#FFFFFF";
		comentadas.style.fontWeight="normal";
		
	}
	if(id=="mais_lidas")
	{
		div2.style.display="block";
		lidas.style.background="#dddddd";
		lidas.style.color="#000000";
		lidas.style.fontWeight="bold";
		
		div1.style.display="none";
		ultimas.style.background="#494949";
		ultimas.style.color="#FFFFFF";
		ultimas.style.fontWeight="normal";
		
		div3.style.display="none";
		comentadas.style.background="#595959";
		comentadas.style.color="#FFFFFF";
		comentadas.style.fontWeight="normal";
	}
	if(id=="mais_comentadas")
	{
		div3.style.display="block";
		comentadas.style.background="#dddddd";
		comentadas.style.color="#000000";
		comentadas.style.fontWeight="bold";
		
		div1.style.display="none";
		ultimas.style.background="#494949";
		ultimas.style.color="#FFFFFF";
		ultimas.style.fontWeight="normal";
		
		div2.style.display="none";
		lidas.style.background="#666666";
		lidas.style.color="#FFFFFF";
		lidas.style.fontWeight="normal";
	}
}



function verRes(id1,id2,id3)
{
	var div_ver1=document.getElementById(id1);
	var div_ocultar=document.getElementById(id2);
	var div_ocultar2=document.getElementById(id3);
	div_ver1.style.display="block";
	div_ocultar.style.display="none";
	div_ocultar2.style.display="none";
}

//***************************************************/

function ImagePreload() 
{
	var d=document; 
	if(d.images)
	{ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=ImagePreload.arguments; 
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		{ 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
	}
}		
function ClearField(field)
{
	if (field.defaultValue==field.value) field.value="";
}

//************************************************/

function SubmitReservation(elmButtonID, blnSubmitType)
{
	var strLinkVariable;
	var elmNewsID=document.getElementById("elmNewsID");
	var elmNewsHeader=document.getElementById("elmNewsHeader");
	var elmReservationNewsID=document.getElementById("intReservationNewsID"+elmButtonID);
	var elmtReservationNewsHeader=document.getElementById("intReservationNewsHeader"+elmButtonID);
	var elmtReservation=document.getElementById("intReservation");
	var frmReservation=document.getElementById("frmReservation");
	
	if(blnSubmitType==0)
	{
		elmNewsID.value=elmReservationNewsID.value;
		elmNewsHeader.value=elmtReservationNewsHeader.value;
		strLinkVariable='catalogo_noticias_insere_trata.php?Form=altera';
	}
	else 
	{
		elmNewsID.value=elmReservationNewsID.value;
		elmtReservation.value=0;
		strLinkVariable='catalogo_noticias_insere.php?accao=alterar';
	}
	
	frmReservation.action=strLinkVariable; 
	frmReservation.submit();  
	return true;
}
function SwitchMenu(menu,on, off, off2)
{
	var el_on=document.getElementById(menu+"_"+on);
	var el_off=document.getElementById(menu+"_"+off);
	var e2_off=document.getElementById(menu+"_"+off2);
	
	el_on.style.display="block";
	el_off.style.display="none";
	if(e2_off !=null) e2_off.style.display="none";
}

function ShowConditions(Group_ID, strDiv)
{
	var elmSelection=document.getElementById(Group_ID);    
	var elmSelected=elmSelection.selectedIndex;              
	var elmValue=elmSelection.options[elmSelected].value;    
	var elmDiv=document.getElementById(strDiv);
	if(elmValue==2) elmDiv.style.display="block";
	else elmDiv.style.display="none";
}

function ClearField(field)
{
	if (field.defaultValue==field.value) field.value='';
}
function ImagePreload() 
{
	var d=document; 
	if(d.images)
	{ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=ImagePreload.arguments; 
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		{ 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
	}
}
function RemoveAllOptions ( strElement )
{
	var intCounter;
	for(intCounter=strElement.options.length - 1; intCounter >=0; intCounter --)
	{
		strElement.remove( intCounter );
	}
}

function AddOption( strSelectbox,strText,strValue )
{
	var strOption=document.createElement("OPTION");
	strOption.text=strText;
	strOption.value=strValue;
	strSelectbox.options.add(strOption);
}
function CheckDes(strElementID)
{
	var elmCheckBox=document.getElementById(strElementID);
	
	for (intCounter=0; intCounter < 12; intCounter ++)
	{
		if(document.getElementById('chkImages' + (intCounter + 1)) !=elmCheckBox)
		{
			document.getElementById('chkImages' + (intCounter + 1)).checked=false;
		}
	}
}				
function CheckAct(strElementID)
{
	var elmCheckBox=document.getElementById(strElementID);
	
	if(elmCheckBox.checked==false) document.getElementById('chkMain').value="0";
	else document.getElementById('chkMain').value=elmCheckBox.value;
}				
function ClearValue (strElement)
{
	document.getElementById(strElement).value="";	
}				
function ClearFileInputField(tagId) 
{ 
	document.getElementById(tagId).innerHTML=document.getElementById(tagId).innerHTML; 
} 
function RemoveAllOptions ( strElement )
{
	var intCounter;
	for(intCounter=strElement.options.length - 1; intCounter >=0; intCounter --)
	{
		strElement.remove( intCounter );
	}
}                
function AddOption( strSelectbox,strText,strValue,intIDORG)
{
	var strOption=document.createElement("OPTION");
	strOption.text=strText;
	strOption.value=strValue;
	if(intIDORG==strValue) strOption.selected=true;
	strSelectbox.options.add(strOption);
}
function ExtensionsValidation()
{
	var strExtension=new Array();
	var objFieldValue=new Array();

	strExtension[0]=".jpg";
	strExtension[1]=".jpeg";
	strExtension[2]=".png";
	strExtension[3]=".tif";
	strExtension[4]=".bmp";

	objFieldValue[0]=document.form.img_peq1.value;
	objFieldValue[1]=document.form.img_peq2.value;
	objFieldValue[2]=document.form.img_peq3.value;
	objFieldValue[3]=document.form.img_peq4.value;
	objFieldValue[4]=document.form.img_peq5.value;
	objFieldValue[5]=document.form.imgurl1.value;
	for (var intObjectCounter=0; intObjectCounter < objFieldValue.length; intObjectCounter++)
	{
		var blnSubmit=0;
		var strFieldExtension=objFieldValue[intObjectCounter].substr(objFieldValue[intObjectCounter].lastIndexOf('.'));
		if(strFieldExtension !="")
		{
			for(var intExtensionCounter=0; intExtensionCounter < strExtension.length; intExtensionCounter++) 
			{
				if(strFieldExtension==strExtension[intExtensionCounter]) 
				{ 
					blnSubmit=1; 
				}
			}
			
			if(blnSubmit==0) 
			{
				alert("Apenas são permitidos ficheiros do tipo imagem!");
				return false;
			}
		}
	}
	return true;
}

function ValidaImagem()
{
	
	var img=document.getElementById("img_peq1").value;
	var url_img=document.getElementById("imgurl1").value;
	var erro=0;

	if(!img) erro=1;
	if(!url_img) erro=1;

	if(erro!=0)
	{
		alert("Necessário preencher os campos da Imagem de Destaque!");
		return false;
	}
}

function ValidaExtencao()
{
	var img=document.getElementById("img_peq1");
	var ext,i;
	
	ext=img.value.split(".");
	i=ext.length-1;
	if(img.value)
	{
		if(ext[i]!="jpg" && ext[i]!="JPG")
		{
			alert("Imagem não suportada!!!");
			img.value="";
			img.focus();	
		}
	}
}

function top_news(id)
{		
	var div1=document.getElementById("div1");
	var div2=document.getElementById("div2");
	var div3=document.getElementById("div3");
	
	var ultimas=document.getElementById("ultimas");
	var lidas=document.getElementById("mais_lidas");
	var comentadas=document.getElementById("mais_comentadas");
	
	if(id=="ultimas")
	{
		div1.style.display="block";
//		ultimas.style.background="#dddddd";
	//	ultimas.style.color="#000000";
		ultimas.style.fontWeight="bold";
		
		div2.style.display="none";
	//	lidas.style.background="#666666";
	//	lidas.style.color="#FFFFFF";
		lidas.style.fontWeight="normal";
		
		div3.style.display="none";
		//comentadas.style.background="#595959";
	//	comentadas.style.color="#FFFFFF";
		comentadas.style.fontWeight="normal";
		
	}
	if(id=="mais_lidas")
	{
		div2.style.display="block";
		//lidas.style.background="#dddddd";
	//	lidas.style.color="#000000";
		lidas.style.fontWeight="bold";
		
		div1.style.display="none";
		//ultimas.style.background="#494949";
	//	ultimas.style.color="#FFFFFF";
		ultimas.style.fontWeight="normal";
		
		div3.style.display="none";
		//comentadas.style.background="#595959";
	//	comentadas.style.color="#FFFFFF";
		comentadas.style.fontWeight="normal";
	}
	if(id=="mais_comentadas")
	{
		div3.style.display="block";
		//comentadas.style.background="#dddddd";
	//	comentadas.style.color="#000000";
		comentadas.style.fontWeight="bold";
		
		div1.style.display="none";
		//ultimas.style.background="#494949";
		//ultimas.style.color="#FFFFFF";
		ultimas.style.fontWeight="normal";
		
		div2.style.display="none";
		//lidas.style.background="#666666";
	//	lidas.style.color="#FFFFFF";
		lidas.style.fontWeight="normal";
	}
}

function ShowHideMenu(objID, intHeight)
{
	var objMenu;
	var intOriginalHeight 
	
	objMenu=document.getElementById(objID).style;
	
	if(parseInt(objMenu.height)==20) 
	{
		ShowMenu(objMenu, intHeight);
	}
	else
	{
		HideMenu(objMenu, intHeight);
	}
}

function ShowMenu(objMenu, intHeight) 
{
	intOriginalHeight=parseInt(objMenu.height);
	if(intOriginalHeight < intHeight) 
	{
		objMenu.height=intOriginalHeight + 15 + "px";
		setTimeout(function(){ShowMenu(objMenu, intHeight)}, 5);
	}
}

function HideMenu(objMenu, intHeight) 
{
	intOriginalHeight=parseInt(objMenu.height);
	if(intOriginalHeight > 20) 
	{
		objMenu.height=intOriginalHeight - 15 +"px";
		setTimeout(function(){HideMenu(objMenu, intHeight)}, 5);
	}
}

//************************ AREA RESERVADA ****/
function AddToKeywords()
{
	var keys=document.getElementById("keywords2").value;
	var newsrel=document.getElementById("newsrel").value;
	var split_keys,split_newsrel;
	var i=j=0;
	var array_aux=new Array();
	var test=new Array();
	var array_final=new Array();
	
	split_keys=keys.split(";");
	if(newsrel!="" && newsrel.length>=3)
	{
		split_newsrel=findKeywords("newsrel","keywords2",3);
		for(i=0; i<=split_keys.length-1; i++)
			array_aux[i]=split_keys[i];			
		for(i=split_keys.length; i<=(split_keys.length)+(split_newsrel.length)-1; i++)
			array_aux[i]=split_newsrel[i-split_keys.length];
		

		for(i=0; i<=array_aux.length-1; i++)
		{
			if(!test[array_aux[i]])
			{
				if(i!=0) array_final+=";";
				array_final+=array_aux[i];
				j++;
				test[array_aux[i]]=1;
			}
		}

		if(array_final)
			document.getElementById("keywords2").value=array_final;
	}
	else
		alert("Só pode adicionar palavras com 3 ou mais caracteres.");
}

function separaKeywords()
{
	var keys=document.getElementById("keywords2").value; 
	var i=0;
	
	for(i=0; i<=keys.length-1; i++)
	{
		keys=keys.replace(";"," ");
		keys=keys.replace("<p>","");
		keys=keys.replace("</p>","");				
	}
		
	return keys;
}

function trataResultado(val)
{
	var news=new Array();
	var aux,i;
	aux=val.split("|");
	return aux;
}

var isOpera=(navigator.userAgent.indexOf('Opera') !=-1);
var isIE=(!isOpera && navigator.userAgent.indexOf('MSIE') !=-1)

function carrega_pagina(url,destino,tipo)
{
	var qq="";
	var msg="";
	if(tipo==1)
	{
		qq=document.getElementById("newsrel").value;
		msg="Noticias Relacionadas";
	}
	if(tipo==2)
	{
		qq=document.getElementById("keywords2").value;
		msg="keywords";
	}
	
	if(qq)
	{
		try
		{
			var ajax=new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(e)
		{
			try
			{
				var ajax=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(ex) 
			{
				try
				{
					var ajax=new XMLHttpRequest();
				}
				catch(exc)
				{
					alert("Esse browser não tem recursos para uso do Ajax");
					ajax=null;
				}
			}
		}
		
		if(ajax)
		{
			var idCentral=document.getElementById(destino);
			var optInfo;

			if(tipo==1)	ajax.open("GET",url+"&nr="+qq,true);
			if(tipo==2)	ajax.open("GET",url+"&nr="+separaKeywords(),true);
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");                
			ajax.onreadystatechange=function() 
			{   
				if(ajax.readyState==4)
				{ 
					
					var texto=ajax.responseText;
					idCentral.innerHTML=texto;
					
					res=trataResultado(texto);
					
					if(texto==1) alert("Não foram encontrados dados correspondentes à pesquisa por "+msg);
					else
					{
						
						for(var x=0; x<res.length-1; x++)
						{
							//Cria um elemento por cada resultado
							var kk=document.createElement('option');
							optInfo=res[x].split(";");
							kk.value=optInfo[0];
							kk.text=optInfo[1];
							try
							{
								document.getElementById("esq").add(kk,null); //Standard
							}catch(error){
								document.getElementById("esq").add(kk); // IE only
							}
						}
					}
				}
			}
			ajax.send(null);
		}
	}
	else alert("É necessário preencher o campo "+msg+".");
}

var limit_max=5;

function move_item(origemID,destinoID,cnt) 
{	
	//Limite de insersão de 5 noticias relacionadas
	var tam_lista=document.form.dir.length;
	if(cnt==1) tam_lista+=1;
	if(cnt==2) tam_lista-=1;
	if(tam_lista<=5)
	{
		//Mover noticias de uma ListBox para outra
		var origem=document.getElementById(origemID);
		var destino=document.getElementById(destinoID);
			
		for(var count=0; count < origem.options.length; count++)
		{
			if(origem.options[count].selected==true)
			{
				var option=origem.options[count];

				var newOption=document.createElement("option");
				newOption.value=option.value;
				newOption.text=option.text;
				newOption.selected=true;
				try
				{
					destino.add(newOption,null); //Standard
					origem.remove(count,null);
				}catch(error){
					destino.add(newOption); // IE only
					origem.remove(count);
				 }
				count--;
			}
		}
	}
	else
	{
		alert("Só é permitido relacionar "+limit_max+" notícias.");
	}
}

function Clear(id)
{
	document.getElementById(id).options.length=0;
}

function UpdateNewsRel()
{
	var ElSel=document.getElementById('dir');
	var i;
	var m_str="";
	for (i=ElSel.length-1; i>=0 ; i--)
	{
		m_str=m_str + elSel2.options[i].value + " ; ";
	}
	ElSel.value=m_str;
}

function selectAllOptions()
{
  var selObj=document.getElementById("dir");
  for (var i=0; i<selObj.options.length; i++) {
	selObj.options[i].selected=true;
  }

}

function RemoveAllOptions2 ( strElement )
{
	var intCounter;
	for(intCounter=strElement.options.length - 1; intCounter >=0; intCounter --)
	{
		strElement.remove( intCounter );
	}
}

function AddOption2( strSelectbox,strText,strValue,intIDSubFamily)
{
	var strOption=document.createElement("OPTION");
	strOption.text=strText;
	strOption.value=strValue;
	if(intIDSubFamily==strValue) strOption.selected=true;
	strSelectbox.options.add(strOption);
}
function RemoveAllOptions3 ( strElement )
{
	var intCounter;
	for(intCounter=strElement.options.length - 1; intCounter >=0; intCounter --)
	{
		strElement.remove( intCounter );
	}
}

function AddOption3( strSelectbox,strText,strValue,intIDKK)
{
	var strOption=document.createElement("OPTION");
	strOption.text=strText;
	strOption.value=strValue;
	if(intIDKK==strValue) strOption.selected=true;
	strSelectbox.options.add(strOption);
}
function mostraCalendario(form,caixa)
{
  var newurl=showModalDialog("popup_calenD.php","janela_","dialogHeight: 260px; dialogWidth: 220px; dialogTop: 0px; dialogLeft: 0px; edge: Sunken; center: No; help: No; resizable: No; status: No;");
  document.forms[form].elements[caixa].value=newurl;
}
function findKeywords(origem,destino,b)
{
	
	var titulo=document.getElementById(origem).value;
	var t_aux=tam=0;
	var array=array_aux=""; 
	var i=j=0;
	var test=new Array();
	
	for(i=0; i<=origem.length-1; i++)
	{
		titulo=titulo.replace(":"," ");
		titulo=titulo.replace(","," ");
		titulo=titulo.replace("."," ");
		titulo=titulo.replace("'"," ");
		titulo=titulo.replace("\""," ");
		titulo=titulo.replace("|"," ");
	}

	array=titulo.split(" ");
	for(i=0; i<=array.length-1 ; i++)
	{ 	
		tam=array[i].length;
		if(tam>=3)
		{
			if(!test[array[i]])
			{
				if(j!=0) 
					if(b==0) array_aux+=";";
					else array_aux+=" ";
				array_aux+=array[i];
				j++;
				test[array[i]]=1;
			}
		}
	}
	if(b==3) return array_aux.split(" ");
	else if(array_aux!="")
		document.getElementById(destino).value=array_aux;
}