//Funciones para el apartado de usuarios.
//Alta, login...
conErrores=0;
listaErrores=new Array();
function addError(num)
{
	conErrores=1;

	if(num)
	{				
		switch (num){
		case 1:listaErrores.push("Has de posar el nom de la empresa"); break;
		case 2:listaErrores.push("Tens que indicar la població"); break;		
		case 3:listaErrores.push("Has de posar el nom de l'activitat"); break;		
		}				
	}
}
function makeErrorList()
{
	var txtError="<h1 class=\"error\">Hi han errors al formulari</h1><ol class=\"error\">";
	for (i=0; i<listaErrores.length;i++) txtError+="<li>"+listaErrores[i]+"</li>";
	txtError+="</ol>";
	return txtError;
}
function validar_form_empresa() 
{
	//Si ya hemos validado alguna vez, limpiamos los avisos de error y vaciamos el array
	if (conErrores==1) { limpiarAvisos(); totErr=listaErrores.length;  for (i=0; i<totErr;i++) listaErrores.shift(); conErrores=0;}
	
	var nombre=document.getElementById("nombre");	
	var poblacion=document.getElementById("q_poblacion_id");
	
	//Validamos 
		if (nombre.value=='') addError(1); 
		if (poblacion.value=='') addError(2);

	//Finalmente si todo es correcto enviamos formulario
	if (conErrores==0) document.datosEmpresa.submit();
	else mostrarAvisos(makeErrorList());
} 
function eliminarEmpresa(id)
{
    if(confirm("Estas segur que vols eliminar aquesta empresa?")) location.href=pathRoot+"/admin/eliminar_empresa.php?id="+id;
}
function validar_form_actividad() 
{
	//Si ya hemos validado alguna vez, limpiamos los avisos de error y vaciamos el array
	if (conErrores==1) { limpiarAvisos(); totErr=listaErrores.length;  for (i=0; i<totErr;i++) listaErrores.shift(); conErrores=0;}
	
	var actividad=document.getElementById("actividad");	
	
	//Validamos 
		if (actividad.value=='') addError(3); 


	//Finalmente si todo es correcto enviamos formulario
	if (conErrores==0) document.datosActividad.submit();
	else mostrarAvisos(makeErrorList());
} 
function eliminarActividad(id)
{
    if(confirm("Estas segur que vols eliminar aquesta activitat?")) location.href=pathRoot+"/admin/eliminar_actividad.php?id="+id;
}
// FOTOS //
function genFileField(num)
{
	var formulario='<div class="bloqueAddFotoVideo">';
	formulario+='<b>Imatge #'+num+'</b> <input name="url'+num+'" size="40" id="url'+num+'" type="file" /><br>';	
	formulario+='</div>';
	return formulario;
}
function addCampoImagen()
{
	var fTot=document.getElementById("numFotos");
	fTot.value=eval(parseInt(fTot.value)+1);
	var form=genFileField(fTot.value);
	var newField = document.createElement("div");
	newField.innerHTML=form;
	document.getElementById("fotosUpload").appendChild(newField);
	//Limitamos a añadir 9 campos
	if(fTot.value==9) 
	{ 	var bot=document.getElementById("addFoto");
		bot.value="Heu arribat al màxim de fotos";
		bot.disabled="disabled";
	}		
}
function borrarFotoGuia(id)
{
	
	if(confirm("Segur que vols eliminar aquesta foto?"))
	{
		
	var filePHP=pathRoot+"/admin/eliminar_foto.php?id="+id;
	var content=Request();	
	
	hideDiv(document.getElementById("afoto_"+id)); //ocultamos enlace
	document.getElementById("mfoto_"+id).innerHTML="eliminant...";
	
	content.open("GET",filePHP,true);
	content.onreadystatechange=function(){
		if (content.readyState==4)
			{	
				if(content.responseText==1)
				{	hideDiv(document.getElementById("foto_"+id));
				}
				else
				{
					showDiv(document.getElementById("afoto_"+id)); //ocultamos enlace
					document.getElementById("mfoto_"+id).innerHTML="";
					alert("No s'ha pogut eliminar la foto");
				}
				
			}
	}
	
	content.send(null);	
	}
}
// VIDEOS //
function genVideoField(num)
{
	var formulario='<div class="bloqueAddFotoVideo">';
	formulario+='<b>Video #'+num+'</b>';
	formulario+='Web: <select onchange="verifTipoVideo(this.value,'+num+');" name="tipoVideo'+num+'"><option value="1" selected="selected">Youtube</option><option value="2">Blip.tv</option></select>';
	formulario+='Codi: <input style="left:0px;" type="text" name="video'+num+'" id="video'+num+'" size="40" /><br />';
	formulario+='<div class="thVideo" id="thVideo_'+num+'">Miniatura: <input type="text" name="thvideo'+num+'" id="thvideo'+num+'" size="70" /><br />';
	formulario+='</div>';				
	return formulario;
}
function addCampoVideo()
{
	var fTot=document.getElementById("numVideos");
	fTot.value=eval(parseInt(fTot.value)+1);
	var form=genVideoField(fTot.value);
	var newField = document.createElement("div");
	newField.innerHTML=form;
	document.getElementById("videosUpload").appendChild(newField);
	//Limitamos a añadir 9 campos
	if(fTot.value==50) 
	{ 	var bot=document.getElementById("addVideo");
		bot.value="Heu arribat al màxim de videos (50)";
		bot.disabled="disabled";
	}		
}
function verifTipoVideo(val,num)
{
	var capa=document.getElementById('thVideo_'+num);
	if(val==1) hideDiv(capa);
	else if(val==2) showDiv(capa);
}
function borrarVideoGuia(id)
{
	
	if(confirm("Segur que vols eliminar aquest video?"))
	{
		
	var filePHP=pathRoot+"/admin/eliminar_video.php?id="+id;
	var content=Request();	
	
	hideDiv(document.getElementById("avideo_"+id)); //ocultamos enlace
	document.getElementById("mvideo_"+id).innerHTML="eliminant...";
	
	content.open("GET",filePHP,true);
	content.onreadystatechange=function(){
		if (content.readyState==4)
			{	
				if(content.responseText==1)
				{	hideDiv(document.getElementById("videoimg_"+id));
				}
				else
				{
					showDiv(document.getElementById("avideo_"+id)); //ocultamos enlace
					document.getElementById("mvideo_"+id).innerHTML="";
					alert("No s'ha pogut eliminar el video");
				}
				
			}
	}
	
	content.send(null);	
	}
}
function helpVideo(w)
{
	var box=document.getElementById("boxHelpVideos");
	var linkh=document.getElementById("linkHelp");	
	if(w=='s') { linkh.href="javascript:helpVideo('h')"; linkh.innerHTML="Ocultar ajuda"; showDiv(box); }
	else if(w=='h') { linkh.href="javascript:helpVideo('s')"; linkh.innerHTML="Mostrar ajuda per pujar els videos"; hideDiv(box); }
}
function ver_video_blip(cod)
{
	$("a").blur();
	//cambiamos clase de th activo
	$("img").removeClass("activo");
	$("#vidth_"+cod).addClass("activo");
	var contVid=document.getElementById("content_player_vid");
	//codigo embded de blip.tv
	var codigoBlip="<embed src=\"http://blip.tv/play/"+cod+"\" type=\"application/x-shockwave-flash\" width=\"500\" height=\"421\" allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed>";
	contVid.innerHTML=codigoBlip;
	showDiv(contVid);
}
function ver_video_youtube(cod)
{
	$("a").blur();
	//cambiamos clase de th activo
	$("img").removeClass("activo");
	$("#vidth_"+cod).addClass("activo");
	var contVid=document.getElementById("content_player_vid");
	//codigo embded de youtube
	var codigoYouTube='<embed id="videoyoutube" src="www.youtube.com/v/'+cod+'&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed>';

	contVid.innerHTML=codigoYouTube;	
	var url='http://www.youtube.com/v/'+cod+'&hl=en&fs=1';
	document.getElementById('videoyoutube').src=url;
	showDiv(contVid);
}

function show_video_youtube(cod)
{
	showDiv(document.getElementById(cod));
}
