//alert("JavaScript"+ '\n' + "Loaded");
/******************************************************Correcção Erro de Flash******************************************************/
function flash(url,w,h){
	document.writeln('<!--[if IE]>');
	document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+w+'" height="'+h+'">');
	document.writeln('<param name="movie" value="'+url+'">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="wmode" value="transparent">');
	document.writeln('<\/object>');
	document.writeln('<![endif]-->');
	document.writeln('<!--[if !IE]> <!-->');
	document.writeln('<object type="application/x-shockwave-flash" data="'+url+'" width="'+w+'" height="'+h+'">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('<param name="controller" value="false">');
	document.writeln('<param name="autoplay" value="true">');
	document.writeln('<param name="menu" value="true">');
	document.writeln('<param name="wmode" value="transparent">');
	document.writeln('alt : <a href="'+url+'">'+url+'<\/a>');
	document.writeln('<\/object>');
	document.writeln('<!--<![endif]-->');      
}
/****************************************************FIM Correcção Erro de Flash****************************************************/

	/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
	var rec = window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
	@end @*/


/******************************************************Registo de Utilizadores******************************************************/
function verificaRegistoUtilizador(mensagem) {
	alerta = "";
	if (document.getElementById('nome_perfil_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('nome_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('apelido_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('pais_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('horario_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('profissao_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('mail_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('password_1_0').style.display=='none') { 
		alerta = 1;
	}
	if (document.getElementById('password_2_0').style.display=='none') { 
		alerta = 1;
	}
	
	if (alerta != "") {
		alert(mensagem);
	} else {
		document.registo.submit();
	}
}
/****************************************************FIM Registo de Utilizadores****************************************************/




/******************************************************Outros******************************************************/
function confirmacao(txt,caminho){
	var resposta = confirm(txt)
	if (resposta){
		window.location = caminho;
	}
}



// função que verifica se o endereco de mail é valido
function checkEmail(fieldValue){ //reference to email field passed as argument

	mostraDIV('mail_0', 'mail_1');
	//var fieldValue = emField.value; // store field's entire value in variable
	
	// Begin Valid Email Address Tests
	
	//if field is not empty
	if(fieldValue != ""){
		
		var atSymbol = 0;
		
		//loop through field value string
		for(var a = 0; a < fieldValue.length; a++){
		
			//look for @ symbol and for each @ found, increment atSymbol variable by 1
			if(fieldValue.charAt(a) == "@"){
				atSymbol++;
			}
	
		}
	
		// if more than 1 @ symbol exists
		if(atSymbol > 1){
			// then cancel and don't submit form
			//alert("Por favor coloque um endereço de mail valido.");
			mostraDIV('mail_1', 'mail_0');
		}
		
		// if 1 @ symbol was found, and it is not the 1st character in string
		if(atSymbol == 1 && fieldValue.charAt(0) != "@"){
			
			//look for period at 2nd character after @ symbol
			var period = fieldValue.indexOf(".",fieldValue.indexOf("@")+2);
			
			// "." immediately following 1st "." ?
			var twoPeriods = (fieldValue.charAt((period+1)) == ".") ? true : false;
			
			//if period was not found OR 2 periods together OR field contains less than 5 characters OR period is in last position
			if(period == -1 || twoPeriods || fieldValue.length < period + 2 || fieldValue.charAt(fieldValue.length-1)=="."){
				// then cancel and don't submit form
				//alert("Por favor coloque um endereço de mail valido.");
				mostraDIV('mail_1', 'mail_0');
			}
		
		}
		// no @ symbol exists or it is in position 0 (the first character of the field)
		else{
			// then cancel and don't submit form
			//alert("Por favor coloque um endereço de mail valido.");
				mostraDIV('mail_1', 'mail_0');
		}
	}
	// if field is empty
	else{
		// then cancel and don't submit form
		//alert("Por favor coloque um endereço de mail valido.");
		mostraDIV('mail_1', 'mail_0');
	}
		
	// all tests passed, submit form, vallid email addreess
	
}


function showHide(div1, div2){

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(div2).style.visibility = 'hidden';
		document.getElementById(div1).style.visibility = 'visible';		
	} else {
		if (document.layers) { // Netscape 4
			document.div2.visibility = 'hidden';
			document.div1.visibility = 'visible';
		} else { // IE 4
			document.all.div2.style.visibility = 'hidden';
			document.all.div1.style.visibility = 'visible';
		}
	}

}


function show(div, model_choice){

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(div).style.visibility = 'visible';
		document.getElementById(model_choice).style.visibility = 'visible';
	} else {
		if (document.layers) { // Netscape 4
			document.div.visibility = 'visible';
			document.model_choice.visibility = 'visible';			
		} else { // IE 4
			document.all.div.style.visibility = 'visible';
			document.all.model_choice.style.visibility = 'visible';			
		}
	}

}


function hide(div, model_choice){

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(div).style.visibility = 'hidden';
		document.getElementById(model_choice).style.visibility = 'hidden';		
	} else {
		if (document.layers) { // Netscape 4
			document.div.visibility = 'hidden';
			document.model_choice.visibility = 'hidden';
		} else { // IE 4
			document.all.div.style.visibility = 'hidden';
			document.all.model_choice.style.visibility = 'hidden';			
		}
	}

}


function checkUnCheck(elementID) {
	
	estado = document.getElementById(elementID).checked;
	
	if (estado != true) {
		document.getElementById(elementID).checked = true;
	} else {
		document.getElementById(elementID).checked = false;
	}
	
}

function radio_check(elementID) {
	document.getElementById(elementID).checked = true;
}


function mostra_postal_contentor(){
	var msg = new String(document.getElementById("msg").value);
	msg = msg.replace(/\n/g, "<br>");
//	var mArr = msg.split("\n"); //   .replace(/\n/, "<br>");
//	msg="";
//	for(var i =0; i < mArr.length;i++)
//		msg+=mArr[i]+"<br>";
	if(document.all)
		this.mensagem.innerHTML = msg;
	else
		document.getElementById("mensagem").innerHTML = msg;
	document.getElementById("postal_contentor").style.top = "0px";
	document.getElementById("postal_contentor").style.left = "0px";
	document.getElementById("postal_contentor").style.visibility = "visible";
		
}

function esconde_postal_contentor(){
	document.getElementById("postal_contentor").style.top = "-5000px";
	document.getElementById("postal_contentor").style.left = "-5000px";
	document.getElementById("postal_contentor").style.visibility = "hidden";
	
}


function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
  else countfield.value = maxlimit - field.value.length;
}
/****************************************************FIM Outros****************************************************/


/******************************************************Fotos Utilizador******************************************************/
// Script Source: CodeLifter.com
// Copyright 2003,2006
// Do not remove this notice.

// MODIFIED: Timed closure added 04.05.2006 - etLux

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Set the value in milliseconds to close the window
// automatically after this amount of time
// 1000 milliseconds equals one second

var closeTime =100000;

// Do not edit below this line...
// ================================
var closeCode = ";setTimeout('self.close()',"+closeTime+")";
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');

writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()'+closeCode+'">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()'+closeCode+'" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}
/****************************************************FIM Fotos Utilizador****************************************************/




/****************************************************Mota functions****************************************************/

function envia_form_recuperar(nID, string) {

	oElement = document.getElementById(nID)
	
	if (oElement.email.value.length == 0) { 
		alert(string); 
		return false; 
	} 
	oElement.envio.disabled = true;
	return true;
}


function MostraPesquisaAvancada() {
 document.getElementById('PesquisaAvancada').style.display='block';
}

function MostraPesquisaSimples() {
 document.getElementById('msg').style.display='none';
}

// abrir uma janela de pop-up
function openPopUp(file_name, string){

	window.open(file_name, string, "resizable=no, location=no, height = 600, width = 760, status=no, scrollbars=yes, menubar=no")

}

function costumPOPUp(url, titulo, largura, altura, scrollBars){
	window.open(url,titulo,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + scrollBars + ',resizable=0,width=' + largura + ',height=' + altura + '');
}

function postal_preview(url, titulo, largura, altura){
	window.open(url,titulo,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,width=' + largura + ',height=' + altura + '');
}

// abrir uma janela de pop-up
/*function PopUpOpenNameSurname(){

	var nome, apelido, nIDContacto;

	nome = opener.document.getElementById('nome').value;
	apelido = opener.document.getElementById('apelido').value;
	nIDContacto = opener.document.getElementById('nIDContacto').value;
	
	document.getElementById('nome').value = nome;
	document.getElementById('apelido').value = apelido;		
	document.getElementById('nIDContacto').value = nIDContacto;			

}*/

function vaibuscar(){
	
	var nIDContacto = document.getElementById("nIDContacto").value;
	var nome = document.getElementById("nome").value;
	var apelido = document.getElementById("apelido").value;	
	
	openPopUp('add_inf.php?nIDContacto=' + nIDContacto + '&nome=' + nome + '&apelido= ' + apelido , 'add_inf');
}

function devolveUserMFriend(nID){
	
	opener.document.getElementById('nIDUtilizadorFrom').value = nID;	
	window.close();

}


function camposObrigatorios(){

	var email, nIDUtilizadorFrom;

	email = document.getElementById("email");
	sms = document.getElementById("sms");	
	nIDContacto = document.getElementById("nIDContacto");
	dataHora1 = document.getElementById("dataHora1");
	
	if(email.checked == true){
		
		if(nIDContacto.value == 0){
			
			alert("Ao seleccionar o envio por e-mail terá de seleccionar o contacto.");
			return false;
			
		}
		
		if(dataHora1.value.lenght == 0){
			
			alert("Ao seleccionar o envio por e-mail terá de seleccionar um data.");
			return false;
			
		}
		
	}
	
	if(sms.checked == true){
		
		if(nIDContacto.value == 0){
			
			alert("Ao seleccionar o envio por sms terá de seleccionar o contacto.");
			return false;
			
		}
		
		if(dataHora2.value.lenght == 0){
			
			alert("Ao seleccionar o envio por sms terá de seleccionar um data.");
			return false;
			
		}
		
	}	
	
	return false;

}

function campos_obrigatorios_escolar(){

	var titulo;

	titulo = document.getElementById("titulo");

	if(titulo.value == 0){
		
		alert("Para inserir um aviso escolar terá de inserir um título.");
		return false;	
		
	}
	
	return true;	

}


function devolveContactMFriend(nID,nome,apelido,dia,mes){
	opener.document.getElementById('nIDContacto').value = nID;
	opener.document.getElementById('nomeContacto').value = nome;
	opener.document.getElementById('apelidoContacto').value = apelido;
	opener.document.getElementById('dia_aniversario').options[dia].selected = true;
	opener.document.getElementById('mes_aniversario').options[mes].selected = true;
	
	window.close();
}

function devolve_nIDPostal(nID){
	opener.document.getElementById('nIDPostal').value = nID;
	window.close();
}

function tipo_Aniversario(){	
	if(document.getElementById("contacto_utilizador1").checked) {
		document.getElementById('tipo_contacto').style.display = "none";
		document.getElementById('tipo_utilizador').style.display = "block";
	} else {
		document.getElementById('tipo_contacto').style.display = "block";
		document.getElementById('tipo_utilizador').style.display = "none";
	}
}


function swapDivsByCheckBox(radioB,div1,div2){	
	if(document.getElementById(radioB).checked) {
		document.getElementById(div1).style.display = "none";
		document.getElementById(div2).style.display = "block";
	} else {
		document.getElementById(div1).style.display = "block";
		document.getElementById(div2).style.display = "none";
	}
}

function mostraDIV(mostraID,escID){
	document.getElementById(escID).style.display='none';
	document.getElementById(mostraID).style.display='block';
}

function getURL(url) {
	var req = new XMLHttpRequest();
	req.onreadystatechange = returnURL;
	req.open('GET', url, false); 
	req.send(null);
	if(req.status == 200) {
  		return req.responseText;
  	}
}

function returnURL() {
    if (req.readyState == 4) { // Complete
        if (req.status == 200) { // OK response
            return req.responseText;
        } else {
        }
    }
}

function registo_verificar_nomePerfil(nome) {
	if (parseInt(nome.length) < 3) {
		mostraDIV('nome_perfil_1', 'nome_perfil_0');
	} else {
		if (getURL('http://www.feirafranca.pt/nome_perfil.php?nome=' + nome) == 1) {
			mostraDIV('nome_perfil_1', 'nome_perfil_0');
		} else {
			mostraDIV('nome_perfil_0', 'nome_perfil_1');
		}
	}
}

function registo_verificar_tamanho(texto,mostraID,escondeID,tamanho) {
	if (parseInt(texto.length) < parseInt(tamanho)) {
		mostraDIV(mostraID, escondeID);
	} else {
		mostraDIV(escondeID, mostraID);
	}
}

function registo_verificar_password(texto1,texto2,mostraID,escondeID) {
	if (texto1 == texto2) {
		mostraDIV(mostraID, escondeID);
	} else {
		mostraDIV(escondeID, mostraID);
	}
}

function registo_verificar_seleccao(valor,mostraID,escondeID) {
	if (valor == 0) {
		mostraDIV(mostraID, escondeID);
	} else {
		mostraDIV(escondeID, mostraID);
	}
}







/****************************************************Alterar a imagem e link da pagina de artigo****************************************************/
function fotoMostrar(URLImagem) {
	alert("URLImagem");
	document.getElementById('Foto_Mostrar').src=URLImagem;
	document.getElementById('Foto_Mostrar_Link').href=URLImagem;
	return false;
}





/******************************************************TESTES******************************************************/
function fernando() {
	alert("nando");	
}

function showlt(event, id, givenURL) {
    document.getElementById("tmpItemFrm").innerHTML = "<div><img src='http://www.m-friends.net/gifs/loading.gif' alt='...'/></div>";
    var url = givenURL + id;
    retrieveURL(url);
    showstats(event, "tmpItemFrm");
}

function retrieveURL(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest;
        req.onreadystatechange = processStateChange;
        try {
            req.open("GET", url, true);
        } catch (e) {
        }
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processStateChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processStateChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById("tmpItemFrm").innerHTML = req.responseText;
        } else {
        }
    }
}

function clearlt() {
    document.getElementById("tmpItemFrm").style.display = "none";
}

function processStateChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById("tmpItemFrm").innerHTML = req.responseText;
        } else {
        }
    }
}

var hideMenu = hideMenu ? hideMenu : 0;
function showstats(e, i, xoffset){
	var o = document.getElementById(i);
	var y = 0;
	var maxX;
	var maxY;
	if (document.all && !window.opera) {
		if (document.documentElement && document.documentElement.scrollTop) {
			maxX = document.documentElement.clientWidth + document.documentElement.scrollLeft;
			maxY = document.documentElement.clientHeight + document.documentElement.scrollTop;
			y = event.clientY + document.documentElement.scrollTop;
			x = event.clientX + document.documentElement.scrollLeft;
		}
		else {
			y = event.clientY + document.body.scrollTop;
			x = event.clientX + document.body.scrollLeft;
		}
	}
	else {
		maxX = window.innerWidth + document.documentElement.scrollLeft;
		maxY = window.innerHeight + document.documentElement.scrollTop;
		y = e.pageY;
		x = e.pageX;
	}
	o.style.position = "absolute";
	o.style.display = "block";
	var divX = o.offsetWidth;
	var divY = o.offsetHeight;
	while (maxX > 200 && x + divX > maxX - 35) {
		x = x - (divX + 30);
	}
	while (y + divY > maxY - 35) {
		y = y - 1;
	}
	if (hideMenu) {
		x = x + 170;
	}
	x = x + 15;
	y = y - 5;
	o.style.top = y + "px";
	o.style.left = x + "px";
}
/****************************************************FIM TESTES****************************************************/

function comprar(mensagem) {
	if (confirm(mensagem)) {
		document.form_comprar.submit();
	} else {
		return false;
	}
}
function licitar(mensagem) {
	if (confirm(mensagem)) {
		document.form_licitar.submit();
	} else {
		return false;
	}
}
