function estaMarcat(marcat){
	if (marcat == true){
		document.getElementById('seleccionats').value++;
	} else {
		document.getElementById('seleccionats').value--;
	}
}
function canviaEstat(id, op){
	enviaForm(id, op);
}
function mostraContingut(id){
	document.getElementById('tipus').value = "ContingutCat";
	document.getElementById('ordre').value = "";
	document.getElementById('sentit').value = "";
	enviaForm(id, '');
}
function pujaOrdre(id){
	enviaForm(id, 'pujaordre');
}
function baixaOrdre(id){
	enviaForm(id, 'baixaordre');
}
function desaOrdre(){
	marcaTot();
	document.getElementById('op').value = 'desaordre';
	document.getElementById('formulari').submit();
}
function busca(){
	document.getElementById('formulari').submit();
}
function enviaForm(id, op){
	desmarcaTot();
	marca(id);
	document.getElementById('op').value = op;
	document.getElementById('formulari').submit();
}
function desmarcaTot(){
	var i = 0;
	while(document.getElementById('id'+i)){
		document.getElementById('id'+i).checked = false;
		i++;
	}
	document.getElementById('seleccionats').value = 0;
}
function marca(id){
	document.getElementById(id).checked = true;
	estaMarcat(true);
}
function marcaTot() {
	var i = 0;
	while(document.getElementById('id'+i)){
		document.getElementById('id'+i).checked = true;
		i++;
	}
	document.getElementById('seleccionats').value = i;
}
function selecciona(sel){
	if(sel)
		marcaTot();
	else
		desmarcaTot();
}
function canviaLlista(){
	document.getElementById('op').value = 'veure';
	document.getElementById('formulari').submit();
}
function focusCerca(){
	if(document.getElementById('cerca')){
		document.getElementById('cerca').focus();
	}
	if(typeof(carrega) == "function")
		carrega();
}
function validaDates(ini, fi){
	var retorn = true;
	if(fi && ini){
		var di = parseInt(ini.replace(/[- :]/g,""));
		var fi = parseInt(fi.replace(/[- :]/g,""));
		if(di > fi)
			retorn = false;
	}
	return retorn;
}
function esborraData(id){
	if(document.getElementById(id))
		document.getElementById(id).value = "";
}
function canviaOrdre(ordre){
	var ordreActual = document.getElementById('ordre').value;
	var sentitActual = document.getElementById('sentit').value;
	if(ordre == ordreActual){
		document.getElementById('sentit').value = (sentitActual == 'asc' ? 'desc' : 'asc');
	}
	document.getElementById('ordre').value = ordre;
	document.getElementById('formulari').submit();
}
function canviaPagina(pagina){
	document.getElementById('inici').value = pagina;
	document.getElementById('formulari').submit();
}
function finestraEmergent(bot, titol, amplada, alcada){
	var finestra = window.open(bot, titol, 'width='+amplada+',height='+alcada+'scrollbars=1');
	finestra.moveTo((screen.width-amplada)/2, (screen.height-alcada)/2);
	finestra.focus();
}
function passaValor(id){
	document.getElementById(id+'visible').value = document.getElementById(id).value;
	seleccionaInput(id);
}
function seleccionaInput(id){
	document.getElementById(id+'boto').select();
}
function muntaMenu(id){
	$(" #menus ul ").css({display: "none"}); // Opera Fix
	$(" #menus li").hover(function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
			},function(){
			$(this).find('ul:first').css({visibility: "hidden"});
	});
}
function inicialitzaCercador(){
	if(document.getElementById('cercador')){
		if(document.getElementById('textCerca').value == ""){
			document.getElementById('textCerca').onclick = function(){
				document.getElementById('textCerca').value = "";
			};
			document.getElementById('textCerca').value = "cerca...";
		}
	}
}
function inicialitzaPagina(){
	muntaMenu('menus');
	carregaGaletes();
	inicialitzaCercador();
	if(typeof(carrega) == 'function')
		carrega();
}
function inicialitzaPortada(){
	carregaGaletes();
	inicialitzaCercador();
	if(typeof(carrega) == 'function')
		carrega();
}
function carregaGaletes(){
	document.cookie="ampladaPantalla="+screen.width+";";
	document.cookie="alcadaPantalla="+screen.height+";";
}
function validaArxiu(idNou, idActual){
	var retorn = false;
	if(document.getElementById(idNou).value != ''){
		if(document.getElementById(idActual).value == '')
			document.getElementById(idActual).value = document.getElementById(idNou).value;
		retorn = true;
	}
	return retorn;
}
function marcaGrup(camp){
	if(document.getElementById(camp) && document.getElementById("grup_"+camp)){
		var nomCamps = document.getElementById("grup_"+camp).value;
		var ids = nomCamps.split(',');
		for(var i=0; i < ids.length; i++){
			if(document.getElementById(ids[i]))
				document.getElementById(ids[i]).checked = document.getElementById(camp).checked;
		}
	}
}
function mouElementAmunt(element){
	var pare, node, germa;
	
	if(document.getElementById(element)){
		node = document.getElementById(element);
		pare = node.parentNode;
		germa = node.previousSibling;
		while(germa != null && germa.nodeType != node.nodeType)
			germa = germa.previousSibling;
		
		if(pare != null && germa != null){
			pare.replaceChild(germa, node);
			pare.insertBefore(node, germa);
			fletxesOrdre(element);
			fletxesOrdre(germa.id);
		}
	}
}
function mouElementAvall(element){
	var pare, node, germa;
	
	if(document.getElementById(element)){
		node = document.getElementById(element);
		pare = node.parentNode;
		germa = node.nextSibling;
		while(germa != null && germa.nodeType != node.nodeType)
			germa = germa.nextSibling;
		
		if(pare != null && germa != null){
			pare.replaceChild(node, germa);
			pare.insertBefore(germa, node);
			fletxesOrdre(element);
			fletxesOrdre(germa.id);
		}
	}
}
function fletxesOrdre(element){
	var node, germa, nodeOrdre, bot, classe;
	
	if(document.getElementById(element) && document.getElementById('ordre'+element)){
		node = document.getElementById(element);
		nodeOrdre = document.getElementById('ordre'+element);
		
		germa = node.previousSibling;
		while(germa != null && germa.nodeType != node.nodeType)
			germa = germa.previousSibling;
		
		classe = "imgbuida";
		bot = "javascript: void(0);";
		if(germa != null){
			classe = "imgpo";
			bot = "javascript: mouElementAmunt('"+element+"');";
		}
		nodeOrdre.innerHTML = '<div class="'+classe+'" onclick="'+bot+'">&nbsp;</div>';
		
		germa = node.nextSibling;
		while(germa != null && germa.nodeType != node.nodeType)
			germa = germa.nextSibling;
		
		classe = "imgbuida";
		bot = "javascript: void(0);";
		if(germa != null){
			classe = "imgbo";
			bot = "javascript: mouElementAvall('"+element+"');";
		}
		nodeOrdre.innerHTML += ' <div class="'+classe+'" onclick="'+bot+'">&nbsp;</div>';
	}
}
