function treureQuotes(v) {
	return v.replace(/"/g, "");
};

function treureBR(v) {
	return v.replace(/<br>+/g, "");
};

function treureEnter(v) {
	return v.replace(/[\n\r]+/g, " ");
};

function deshabilitat() {
	alert('Funció deshabilitada per l\'usuari de proves');
}

function PrintContent(ctrl){
	var DocumentContainer = document.getElementById('serveis');
	var WindowObject = window.open('', "Serveis", "width=420,height=225,top=250,left=345,toolbars=no,scrollbars=no,status=no,resizable=no");

	WindowObject.document.write(DocumentContainer.innerHTML);

	WindowObject.document.close();
	WindowObject.focus();
	WindowObject.print();
	//WindowObject.close();
}
function gotoServeis(paramDir,paramDia,paramMes,paramAno) {
	window.location = paramDir + ".php?dia=" + paramDia + "&mes=" + paramMes + "&ano=" + paramAno;
}

function gotoServeisPopup(paramDir,paramDia,paramMes,paramAno) {
	window.opener.location = paramDir + ".php?dia=" + paramDia + "&mes=" + paramMes + "&ano=" + paramAno;
	window.close();
}

function finestra(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

function finestraFoto(theURL,winName,features)
{
	myImage = document.getElementById(winName)
	var ancho = myImage.width;
	var alto = myImage.height;

	var ample = 480;
	var alt = 640;

	if( ancho > alto ) { // imatge més ample que alta
		ample = 640;
		alt = 480;
	}
	else if( ancho == alto )	{
		ample = 640;
		alt = 640;
	}

	var masURL = '&width=' + ample + '&height=' + alt;
	theURL += masURL;

	ample = ample + 15;
	alt = alt + 15;
	
	var masFeatures = ',width=' + ample + ',height=' + alt;
	features += masFeatures;
	
	window.open(theURL,winName,features);
}

function finestraForum()
{
	window.open('phpBB2/index.php','FòrumAVPC','width=660,height=570,resizable=yes,scrollbars=yes');
	location.reload();
}

function check()
{
	if( document.forms['passwordForm'].password.value != "" && document.forms['passwordForm'].npassword1.value != "" && document.forms['passwordForm'].npassword2.value != "" )
	{
		return true;
	}
	else
	{
		alert( "Has d'omplir tots els camps" );
		return false;
	}
}

function checkBlank( formulari )
{
	if( formulari.username.value == '' || formulari.password.value == '' )
	{
		alert( "Has d'omplir tots els camps" );
		return false;
	}
	else
	{
		return true;
	}
}

function confirmar( missatge )
{
	return confirm( missatge );
}

function sendFormFoto()
{
	if(document.forms['formFoto'].foto.value == ''){
	  alert( 'Has de seleccionar una imatge' );
	  return false;
	}else return true;
}

function sendFormDoc() {
	if(document.forms['formDoc'].doc.value == '') {
		alert( 'Has de seleccionar un document' );
	  	return false;
	}
	else {
		if(document.forms['formDoc'].public.checked == true)
		{
			if(confirm( "El document es publicarà a la secció 'Documentació' de la web.\nSegur que vols continuar?" )) {
				return true;
			}
			else return false;
		}
		else return true
	}
}

function missatgeNouUsuari( username, password )
{
	alert('NOU MEMBRE:\n\nNom usuari: ' + username + '\nPassword: ' + password);
	return true;
}


function findObj(n, d)
{
	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=findObj(n,d.layers[i].document);

	if(!x && document.getElementById)
		x=document.getElementById(n);

	return x;
}


function validateForm()
{
  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  
  for (i=0; i<(args.length-2); i+=3)
  {
  	test=args[i+2];
  	val=findObj(args[i]);
  	if(val)
  	{
  		nm=val.name;
  		if((val=val.value)!="")
  		{
      			if(test.indexOf('isEmail')!=-1)
      			{
      				p=val.indexOf('@');
        			if(p<1 || p==(val.length-1))
        				errors+='- El camp <'+nm+'> ha de contenir una adreça de correu vàlida.\n';
      			}
      			else if(test!='R')
      			{
        			if(isNaN(val))
        				errors+='- El camp <'+nm+'> ha de contenir un nombre.\n';
        			
        			if(test.indexOf('inRange') != -1)
        			{
        				p=test.indexOf(':');
          				min=test.substring(8,p);
          				max=test.substring(p+1);
          
          				if(val<min || max<val)
          					errors+='- El camp <'+nm+'> ha de contenir un nombre entre '+min+' i '+max+'.\n';
    				}
    			}
    		}
    		else if(test.charAt(0) == 'R')
    			errors += '- El camp <'+nm+'> és necessari.\n';
    	}
  }
  
  if(errors)
  	alert('Han ocorregut els següents errors:\n'+errors);
  
  document.returnValue = (errors == '');
}


function ShowDate() 
{
	date = new Date();
	var ndw = date.getDay(); 
	var dm = date.getDate();
	var nm = date.getMonth();
	var y = date.getYear(); 

	if (y<200)
		y+=1900;
	var dw = '';
	var m = '';
 
	if(nm == 0) {m = 'gener';}
	if(nm == 1) {m = 'febrer';}
	if(nm == 2) {m = 'març';}
	if(nm == 3) {m = 'abril';}

	if(nm == 4) {m = 'maig';}
	if(nm == 5) {m = 'juny';}
	if(nm == 6) {m = 'juliol';}
	if(nm == 7) {m = 'agost';}
	if(nm == 8) {m = 'setembre';}
	if(nm == 9) {m = 'octubre';}

	if(nm == 10){m = 'novembre';}
	if(nm == 11){m = 'desembre';}

	if(ndw == 0){dw = 'Diumenge';}
	if(ndw == 1){dw = 'Dilluns';} 
	if(ndw == 2){dw = 'Dimarts';}
	if(ndw == 3){dw = 'Dimecres';}

	if(ndw == 4){dw = 'Dijous';} 
	if(ndw == 5){dw = 'Divendres';}
	if(ndw == 6){dw = 'Dissabte';}

	var fecha = '&nbsp;'+dw+', '+dm+' '+m+' '+y;

	document.write(fecha); 
} 


function selecciona(id)
{
  id=document.getElementById(id);

  if(id.options.length <= 0 || id.options[0].value == "-")
  {
  	alert('Han ocorregut els següents errors:\n- Has de seleccionar algun voluntari.');
  	return false;
  }
  else
  {
	  for (i=0; ele = id.options[i]; i++)
	  {
	    ele.selected = true;
	  }
	  return true;
  }
}

function afegirUsuari() {
	obj=document.getElementById('sel1');
	if (obj.selectedIndex==-1) return;
  for (i=0; opt=obj.options[i]; i++)
    if (opt.selected) {
    	valor=opt.value; // almacenar value
    	txt=obj.options[i].text; // almacenar el texto
    	obj.options[i]=null; // borrar el item si está seleccionado
    	obj2=document.getElementById('sel2');
      if (obj2.options[0].value=='-') // si solo está la opción inicial borrarla
        obj2.options[0]=null;
    	opc = new Option(txt,valor);
    	eval(obj2.options[obj2.options.length]=opc);
  }	
}

function treureUsuari() {
	obj=document.getElementById('sel2');
	if (obj.selectedIndex==-1)return;
  for (i=0; opt=obj.options[i]; i++)
    if (opt.selected) {
    	valor=opt.value; // almacenar value
    	txt=obj.options[i].text; // almacenar el texto
    	obj.options[i]=null; // borrar el item si está seleccionado
    	obj2=document.getElementById('sel1');
      if (obj2.options[0].value=='-') // si solo está la opción inicial borrarla
        obj2.options[0]=null;
    	opc = new Option(txt,valor);
    	eval(obj2.options[obj2.options.length]=opc);
  }	
}

function eliminaNoticia(identificador)
{
	if(confirmar('Estàs segur que vols eliminar aquesta notícia?')) {
		window.location='eliminanoticia.php?id='+identificador;
	}
	else {
		return false;
	}
}

function eliminaActivitat(identificador)
{
	if(confirmar('Estàs segur que vols eliminar aquesta activitat?')) {
		window.location='eliminaactivitat.php?id='+identificador;
	}
	else {
		return false;
	}
}