function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.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 MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function abre_janela_abstract(link){	

	var largura = 332;
	var altura = 509;
	var w = largura;
	var h = altura;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',title=Sociedade Portuguesa de Reumatologia,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no';
	win = window.open(link, 'SPR', winprops);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();

}

function imprimir(link){	

	var largura = 650;
	var altura = 700;
	var w = largura;
	var h = altura;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',title=Sociedade Portuguesa de Reumatologia,status=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no';
	win = window.open(link, 'SPR', winprops);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

function ver_imagem1(path, titulo, w, h){	
	
	var largura = w;
	var altura = h;
	var screenX = screen.width;
	var screenY = screen.height;
	var winl;
	var wint;
	
	if (w > screenX){
		largura = screenX;
		if (h > screenY){
			altura = screenY;
			largura = (w * screenY) / (h);
		} else {
			altura = (h * screenX) / (w);
		}
	} else {
		largura = w;
		if (h > screenY){
			altura = screenY;
			largura = (w * screenY) / (h);
		} else {
			altura = h;
		}
	}

	winl = (screenX - largura) / 2;
	wint = (screenY - altura) / 2;

	winprops = 'height='+altura+',width='+largura+',top='+wint+',left='+winl+',status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,directories=no';
	win = window.open('imagem.php?titulo='+titulo+'&imagem='+path+'&width='+screenX+'&height='+screenY,'', winprops);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

function ver_imagem(path, titulo){	
	
	var w = document.getElementById('imgwidth').value;
	var h = document.getElementById('imgheight').value;
	var largura = w;
	var altura = h;
	var screenX = screen.width;
	var screenY = screen.height;
	var winl;
	var wint;
	
	if (w > screenX){
		largura = screenX;
		if (h > screenY){
			altura = screenY;
			largura = (w * screenY) / (h);
		} else {
			altura = (h * screenX) / (w);
		}
	} else {
		largura = w;
		if (h > screenY){
			altura = screenY;
			largura = (w * screenY) / (h);
		} else {
			altura = h;
		}
	}

	winl = (screenX - largura) / 2;
	wint = (screenY - altura) / 2;

	winprops = 'height='+altura+',width='+largura+',top='+wint+',left='+winl+',status=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,directories=no';
	win = window.open('imagem.php?titulo='+titulo+'&imagem='+path+'&width='+screenX+'&height='+screenY,'', winprops);
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

//esta funcao serve para alterar a cor do texto de press_realeases ao passar o rato
function changeStyleById(id, color){
	if (document.getElementById){
		var nodeObj = document.getElementById(id);
		nodeObj.style.color = color;
	}
}

function MultiSelector( list_target){
	this.list_target = list_target;
	this.count = 0;
	this.id = 0;
	this.addElement = function( element ){
		if( element.tagName == 'INPUT' && element.type == 'file' ){
			element.name = 'file[]';
			element.className= 'caixa_texto_file';
			element.multi_selector = this;element.onchange = function(){
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				this.parentNode.insertBefore( new_element, this );
				this.multi_selector.addElement( new_element );
				this.multi_selector.addListRow( this );
				this.style.position = 'absolute';
				this.style.left = '-1000px';
			};
			this.count++;
			this.current_element = element;
		} else {
			alert( 'Error: not a file input element' );
		};
	};
	this.addListRow = function( element ){
		var new_row = document.createElement( 'div' );
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'remover';
		new_row_button.className = 'style10';
		new_row.element = element;
		new_row_button.onclick= function(){
			this.parentNode.element.parentNode.removeChild( 
			this.parentNode.element );
			this.parentNode.parentNode.removeChild( this.parentNode );
			this.parentNode.element.multi_selector.count--;
			this.parentNode.element.multi_selector.current_element.disabled = false;
			return false;
		};
		new_row.innerHTML = '<b>' + element.value + '</b>  ';
		new_row.appendChild( new_row_button );
		this.list_target.appendChild( new_row );
	};
};

function fillSelectFromArray(selectCtrl, itemArray, defaultItem, goodPrompt, badPrompt) {
	
	var i, j;
	var prompt;
		
	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null;
	}
	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
		j = 0;
	}
	else {
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}
	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {
			selectCtrl.options[j] = new Option(itemArray[i][0]);
			if (itemArray[i][1] != null) {
				selectCtrl.options[j].value = itemArray[i][1];
			}
			if(itemArray[i][1] == defaultItem && defaultItem != null){
				selectCtrl.options[j].selected = true;
			}
			j++;
		}
	 }
}

function changecolorlink_on(col){
	for(i=1;i<=3;i++){
		document.getElementById('row'+i+'col'+col).style.color='#406BA6';
	}
}

function changecolorlink_off(col){
	for(i=1;i<=3;i++){
		document.getElementById('row'+i+'col'+col).style.color='#000000';
	}
}

function posicao(tooltip){
	loc = 110+(wid*(tooltip-1));
	size=document.body.offsetWidth;
	if (size <= 994){
		return (loc);
	} else {
		dif = Math.floor((994 - size)/2);
		return (dif)+loc;
	}
}

//change the opacity for different browsers
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 20);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
	setTimeout("reduzir_imagem('"+ id +"',0,0);",(timer * speed));
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function reduzir_imagem(id,img_width,img_height){
	
	var object = document.getElementById(id);
	var div = document.getElementById('div_'+id);
	
	object.width=img_width;
	object.height=img_height;
	
	div.style.width=img_width;
	div.style.height=img_height;		
	div.style.zIndex="2";	
}

function makerequest(serverPage, objid){

	var xmlhttp = false;

	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		}catch(e){
			xmlhttp = false;
		}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest !='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	
	var obj = document.getElementById(objid);
	
	xmlhttp.open("POST", serverPage);
	
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	xmlhttp.onreadystatechange = function(){
		
		if (xmlhttp.readyState == 4)
			{
			if (xmlhttp.status == 200)
			{
				obj.innerHTML = xmlhttp.responseText;
							
				var div_flash = document.getElementById("header");
				var so = new SWFObject(div_flash.title, "SPR", div_flash.style.width, div_flash.style.height, "8.0.23", "#FFFFFF", true);
				so.write(div_flash);
				
				opacity('intro', 100, 0, 500);
			}
		}
	}
	
	xmlhttp.send(null);
}

function open_calendario(url, form, campo, dataEntrada)
{
    urlFinal = url + "?form=" + form + "&campo=" + campo;
			if(!dataEntrada == "") {
				urlFinal += "&data_entrada=" + dataEntrada;
			} 
			var newWindow;
    newWindow = window.open(urlFinal,'','height=250,width=255,toolbar=no,minimize=no,status=no,menubar=no,location=no,scrollbars=no');
}

function sub_caract_esq(str) {
    var str_sub=String(str);
    var regexp=/€/g;
    str_sub=str_sub.replace(regexp,'&euro;');
    regexp=/[”“]/g;
    str_sub=str_sub.replace(regexp,'"');
    regexp=/…/g;
    str_sub=str_sub.replace(regexp,'...');
    regexp=/–/g;
    str_sub=str_sub.replace(regexp,'-');
    return str_sub;
}


function ComandoSelected(nome,accao) {
    document.forms[nome].action=accao;
    document.forms[nome].submit();
}

function preview_nome(accao,w,h,nome)
{
	window.open('<?=$_SERVER[HTTPS]==on?$base:$URL?>'+accao,nome,toolbar=no,width='+w+',height='+h+',directories=no,status=no,scrollbars=yes,resizable=no,menubar=no,location=no);
}

function preview(accao,w,h)
{
	window.open('<?=$_SERVER[HTTPS]==on?$base:$URL?>'+accao,'<?=$LOGIN_USER?>',toolbar=no,width='+w+',height='+h+',directories=no,status=no,scrollbars=yes,resizable=no,menubar=no,location=no);
}

function nova(accao,w,h)
{
	window.open('<?=$_SERVER[HTTPS]==on?$base:$URL?>'+accao,'<?=$LOGIN_USER?>',toolbar=no,width='+w+',height='+h+',directories=no,status=no,scrollbars=no,resizable=no,menubar=no,location=no);
}

function preview2(accao,w,h)
{
	window.open('<?=$_SERVER[HTTPS]==on?$base:$URL?>'+accao,'Imagem_<?=$LOGIN_USER?>',toolbar=no,width='+w+',height='+h+',directories=no,status=no,resizable=yes,menubar=yes,location=no);
}

function preview3(accao,w,h)
{
	window.open(accao,'Imagem_<?=$LOGIN_USER?>',toolbar=no,width='+w+',height='+h+',directories=no,status=no,resizable=yes,menubar=yes,location=no);
}

function nltobr (str){
	var str=new String(str);
	var primeira_pos=str.search(/<[ ]*table/g);
	var temp_pos;
	var inicio=1;
	
	var ma_array=str.match(/<[ ]*\/[ ]*table/g);
	if (ma_array) {
	        ultima_pos=str.lastIndexOf(ma_array[ma_array.length-1]);
	}
	if (primeira_pos!=-1) {
	        var new_str=(str.slice(0,primeira_pos)).replace(/\n/g,"<br>");
	        new_str+=(str.slice(primeira_pos,ultima_pos));
	        new_str+=(str.slice(ultima_pos)).replace(/\n/g,"<br>");
	}
	else {
	        var new_str=str.replace(/\n/g,"<br>");
	}
	return new_str;
}

function mostra_imagem(strFile,file_dir) {
	var file_dir = file_dir || '/';
	if (strFile != '')
	    preview3(file_dir+strFile, 600, 400);

}

function addRow1(){
	
	elements = document.getElementById("tblGrid").getElementsByTagName("tr");   
    rowCount = 10;
    for (i = 0; i < elements.length; i++) {
    	rowCount++
    }
   	// alert(rowCount);
   	var newRow = document.getElementById("tblGrid");
	var oCell = newRow.insertCell();
	oCell.innerHTML = "<input type='text' name='nome"+rowCount+"' size='43' class='style12'>";
	
	oCell = newRow.insertCell();
	oCell.innerHTML = "<input type='text' name='afiliacao"+rowCount+"'  size='43' class='style12'>";
	
	oCell = newRow.insertCell();
	oCell.innerHTML = "<a href='#' onclick='removeRow(this);'>remover</a>";	
  		
}

function addRow()
{
	var tbl = document.getElementById('tblGrid');
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	
	// left esquerda
	var cellLeft = row.insertCell(0);
	var el1 = document.createElement('input');
	el1.type = 'text';
	el1.name = 'nome' + iteration;
	el1.id = 'nome' + iteration;
	el1.size = 43;
	el1.className = 'style12'
	
	// right center
	var cellCenter = row.insertCell(1);
	var el2 = document.createElement('input');
	el2.type = 'text';
	el2.name = 'afiliacao' + iteration;
	el2.id = 'afiliacao' + iteration;
	el2.size = 43;
	el2.className = 'style12'
	
	// right direira
	var cellRight = row.insertCell(2);
	var el3 = document.createElement('a');
	el3.onclick = function () {
	//var oRow = this.parentElement.parentElement;
	var i=this.parentNode.parentNode.rowIndex
	document.getElementById('tblGrid').deleteRow(i)		
	//	document.getElementById("tblGrid").deleteRow(this.parentElement.rowIndex);	
	};
	el3.setAttribute('alt', 'Clique aqui para remover');
	//el3.setAttribute("href", "javascript:removeRow("+el3+")");
	el3.appendChild(document.createTextNode("remover"));
	
	cellLeft.appendChild(el1);
	cellCenter.appendChild(el2);
	cellRight.appendChild(el3);

}

function removeRow(src){
	var i=src.parentNode.parentNode.rowIndex;
	document.getElementById('tblGrid').deleteRow(i);		
}

function addRowApc(){

	var tbl = document.getElementById('tblGrid');
	var lastRow = tbl.rows.length;

	var espaco = tbl.insertRow(lastRow);
	espaco.height=10;
	var cellEspaco = espaco.insertCell(0);
	cellEspaco.innerHTML = "&nbsp;";
	
	var row = tbl.insertRow(lastRow+1);

	// left esquerda
	var cellLeft = row.insertCell(0);
	var el1 = document.createElement('input');
	el1.type = 'text';
	el1.name = 'autor[]';
	el1.id = 'autor[]';
	el1.size = 43;
	el1.className = 'style12';
	
	row.insertCell(1);
	
	// right center
	var cellCenter = row.insertCell(2);
	var el2 = document.createElement('input');
	el2.type = 'text';
	el2.name = 'instituicao[]';
	el2.id = 'instituicao[]';
	el2.size = 43;
	el2.className = 'style12';
	
	// right direira
	var cellRight = row.insertCell(3);
	cellRight.align="right";
	var el3 = document.createElement('a');
	el3.onclick = function () {
	
		var i=this.parentNode.parentNode.rowIndex;
		var tabela = document.getElementById('tblGrid');
		tabela.deleteRow(i);
		tabela.deleteRow(i-1)

		if(tabela.rows.length<=50){
			document.getElementById('adicionar_autor').style.display='block';
		}else{
			document.getElementById('adicionar_autor').style.display='none';
		}			
	};
	
	el3.setAttribute('alt', 'Clique aqui para remover');
	el3.setAttribute('href','#');
	el3.setAttribute('class','style12');
	el3.appendChild(document.createTextNode("remover"));
	
	cellLeft.appendChild(el1);
	cellCenter.appendChild(el2);
	cellRight.appendChild(el3);
	
	if(tbl.rows.length<=50){
		document.getElementById('adicionar_autor').style.display='block';
	}else{	
		document.getElementById('adicionar_autor').style.display='none';
	}
}

function removeRowApc(src){
		
	var i= src.parentNode.parentNode.rowIndex;
	var tabela = document.getElementById('tblGrid');
	tabela.deleteRow(i);
	tabela.deleteRow(i-1)
	if(tabela.rows.length<=50){
		document.getElementById('adicionar_autor').style.display='block';
	}else{
		document.getElementById('adicionar_autor').style.display='none';
	}
}

function checkEnterPage(nform, evt)
{
	evt = (evt) ? evt : event
	var charCode = (evt.wich) ? evt.wich : evt.keyCode
	if (charCode == 13)
  	{
    	nform.submit()
    	return false
  	}
  	return true
}

function addquot (str){
    var str=new String(str);
    var new_str=str.replace(/["]/g,"&quot;");
    return new_str;
}

function limitInput(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function enviaConf(conferencista){	 
    
	var congresso = document.getElementById('congresso').value; 
	var conferencistas = document.getElementById('conferencistas').value;
		
	var array = conferencistas.split(/,/);
 
	//se o valor ja existe apaga da lista
	if(verifItems(array,conferencista)==true){
	 			
		var array = removeItems(array, conferencista);
		array = array.join(",");
		
		if( array.length<1){
			array = "todos";
		}
		
		document.getElementById('conferencistas').value = array;
		
	//senao, insere 
	}else{     
		if(conferencistas){  	  	
			conferencistas = conferencistas+","+conferencista;
			array = conferencistas.split(/,/);
			array = removeItems(array, 'todos');
			conferencistas = array.join(","); 
		}else{
			conferencistas = conferencista;	
		}
		document.getElementById('conferencistas').value = conferencistas;
	}
	
	document.getElementById('conf_acesso').href = 'download_conf_acesso.php?congresso='+congresso+'&conferencistas={'+document.getElementById('conferencistas').value+'}';
}

function enviaPres(resumo){	 
    
	var congresso = document.getElementById('congresso').value; 
	var resum = document.getElementById('apresentacao').value;
		
	var array = resum.split(/,/);
 
	//se o valor ja existe apaga da lista
	if(verifItems(array,resumo)==true){
	 			
		var array = removeItems(array, resumo);
		array = array.join(",");
		
		if( array.length<1){
			array = "todos";
		}
		
		document.getElementById('apresentacao').value = array;
		
	//senao, insere 
	}else{     
		if(resum){  	  	
			resum = resum+","+resumo;
			array = resum.split(/,/);
			array = removeItems(array, 'todos');
			resum = array.join(","); 
		}else{
			resum = resumo;	
		}
		document.getElementById('apresentacao').value = resum;
	}
	
	document.getElementById('apc_pdf_todas').href = 'download_apc_pdf_todas.php?congresso='+congresso+'&apresentacoes={'+document.getElementById('apresentacao').value+'}';
	document.getElementById('apc_word_todas').href = 'download_apc_word_todas.php?congresso='+congresso+'&apresentacoes={'+document.getElementById('apresentacao').value+'}';
}
  
function verifItems(array, value){
	
	var yesno = eval(array).join().indexOf(value)>=0;
	
	return yesno;
}

function removeItems(array, item){
   
	var i = 0;
	while (i < array.length){
		pos = array[i];          
		if (pos.toString() == item.toString()){
			array.splice(i, 1);       
		}
		i++;      
	}     
	return array;
} 


// Funções para a Sondagem

	function MultiRespostas( list_target){
		this.list_target = list_target;
		this.count = 0;
		this.id = 0;
		this.addElement = function( element ){
			if( element.tagName == 'INPUT' && element.type == 'text' ){
				element.name = 'resposta[]';
				element.className= 'admin_caixa_texto2';
				element.multi_selector = this;

				element.onchange = function(){
					var new_element = document.createElement( 'input' );
					new_element.type = 'text';
					this.parentNode.insertBefore( new_element, this );

					this.multi_selector.addElement( new_element );
					this.multi_selector.addListRow( this );
					this.style.position = 'absolute';
					this.style.left = '-1000px';
				};
				//if(this.current_element.value!=new_element.value){
					this.count++;
					this.current_element = element;


			} else {
				alert( 'Error: not a file input element' );
			}
		}
		this.addListRow = function( element ){
			var new_row = document.createElement( 'div' );
			var new_row_button = document.createElement( 'img' );
			new_row_button.id="menos";
			new_row_button.src = "../../ficheiro/interface/icons/menos_quadrado.jpg";
			//new_row_button.innerHTML = '<img src="../../ficheiro/interface/icons/menos.jpg" border=0>';
			new_row.element = element;
			new_row_button.onclick= function(){
				this.parentNode.element.parentNode.removeChild(
				this.parentNode.element );
				this.parentNode.parentNode.removeChild( this.parentNode );
				this.parentNode.element.multi_selector.count--;
				this.parentNode.element.multi_selector.current_element.disabled = false;
				return false;
			};
			new_row.innerHTML = '<input type="text" name="'+element.name+'" value="'+element.value+'" class="admin_caixa_texto2">&nbsp;&nbsp;';
			new_row.appendChild( new_row_button );
			this.list_target.appendChild( new_row );
		}
	}
	//fim das funções para a sondagem
