function setFocus(caixa) {document.getElementById(caixa).focus(); }

function redireccionar(endereco)
{
    var destino=endereco;
    var versao_browser = navigator.appVersion;

       if (versao_browser.indexOf("MSIE") >= -1) // IE
       {
          window.location.href=destino;
       }
       else // outros
       {
          window.open(destino, target="_self");
       }   
}

function muda(node,imagem,imagem2)
{
	// Para abrir
	// nextSibling: indica o próximo elemento após o elemento em questão
	if (node.nextSibling.style.display == 'none')
	{
		// Muda a imagem (caso exista)
		if (node.childNodes.length > 0)
		{
			if (node.childNodes.item(0).nodeName == "IMG")
			{
				node.childNodes.item(0).src = imagem2; //menos
			}
		}

		node.nextSibling.style.display = 'block';
	}
	// Para fechar
	else
	{
		// Muda a imagem (caso exista)
		if (node.childNodes.length > 0)
		{
			if (node.childNodes.item(0).nodeName == "IMG")
			{
				node.childNodes.item(0).src = imagem; //mais
			}
		}

		node.nextSibling.style.display = 'none';
	}

}

// abrir janela (utilizado para calendário e para janelas de comunicação com formulários)
function abre(pag,altura,largura,scroll)
{
var janlargura = (screen.width - largura) / 2;
var janaltura= (screen.height - altura) / 2;
winprops = 'toolbar=no,width='+largura+',height='+altura+',top='+janaltura+',left='+janlargura+',scrollbars='+scroll+',noresizable,statusbar=0'
window.open(pag,'geral',winprops);
}




// alert de confirmação
function confirma(texto,endereco) {
	var resposta = confirm(texto)
	if (resposta){
		window.location = endereco;
	}
}

function letras(e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
	//return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 9);
	if (k==0)
		document.getElementById('password').focus();
	return (k > 96 && k < 123 || k == 8 || k == 9);
}

function numeros(e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
	return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 9);
	/*
	if (k==0)
		document.getElementById('topo_esq').focus();
	return (k > 47 && k < 58 || k == 8 || k == 9);
	*/
}

function fechaRefrescaParent(ficheiro) {
	window.opener.location.href=ficheiro;  
	window.close();	
}

//// para impressão de iframes a partir da parent
function CheckIsIE()
{
if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') { return true;}
else { return false; }
}


function imprimirIframe(nomeFrame)
{

if (CheckIsIE() == true)
{
eval("document." + nomeFrame).focus();
eval("document." + nomeFrame).print();
}
else
{
window.frames[nomeFrame].focus();
window.frames[nomeFrame].print();
}

}

function isNumero(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

// melhoria da isNumero
// permite a o separador decimal
// apenas se casas_decimais = true
function isNumero_v2(evt,casas_decimais)
{
 
 var codigoCaractere;
 
 // var codigoCaractere = (evt.which) ? evt.which : event.keyCode
 if (evt.which) {
 	codigoCaractere = evt.which;
 }
 else {
 	 if (evt.keyCode == 0) {
	 	codigoCaractere = event.keyCode;
	 }
 }

 if (codigoCaractere > 31 && (codigoCaractere < 48 || codigoCaractere > 57)) {
 	if (casas_decimais && codigoCaractere != 46)
	   return false;
 }

 return true;
}

function pre_submit(id_form) {
	document.getElementById(id_form).submit();	
}

function pre_submit_v2(id_form,params) {
	document.getElementById(id_form).action = params;
	document.getElementById(id_form).submit();
}

function mudaImagem(nome, source)
{
  document[nome].src = source;
}

///// Fade + Alpha (para mensagens)
///////////////////////////////////////////////////////////////////
function fadeOut(id, time) {
	target = document.getElementById(id);
	alpha = 100;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha <= 0)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha -= 2;
			}, timer);
}

function fadeIn(id, time) {
	target = document.getElementById(id);
	alpha = 0;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha >= 100)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha += 2;
			}, timer);
}

function setAlpha(target, alpha) {
	target.style.filter = "alpha(opacity="+ alpha +")";
	target.style.opacity = alpha/100;
}


///////////////////////////////////////////////////////////////////


function isCheckboxesPreenchidas(id_checks) {

	var aChk       = document.getElementsByName(id_checks);  
	//var aChk       = document.getElementById(id_checks);  
	var preenchido = false;

	for (var i=0;i<aChk.length;i++){  
		 if (aChk[i].checked == true){  

			preenchido = true;
			break; 
			// para o valor aChk[i].value  
		 }  
	}
	
	return preenchido;

}

function processaCheckboxes(origem,destino) {

	var orig       = document.getElementsByName(origem);  
	var dest       = document.getElementById(destino);  
	var str_meses  = '';

	for (var i=0;i<orig.length;i++){  
		 if (orig[i].checked == true){  			 
			str_meses += orig[i].value + ',';
		 }  
	}

	dest.value = str_meses.substr(0,str_meses.length -1);
	
}

function replicaTextarea(origem,destino) {
	var txt_orig = document.getElementById(origem);
	var txt_dest = document.getElementById(destino);
	txt_dest.value = txt_orig.value;
}
