//Alta nuevo sitio
conErrores=0;
listaErrores=new Array();
function addError(num)
{
	conErrores=1;

	if(num)
	{				
		switch (num){
		case 1:listaErrores.push("Debes introducir el titulo del PIO"); break;
		case 2:listaErrores.push("Tienes que introducir la descripcion"); break;
		case 3:listaErrores.push("Tienes que seleccionar una provincia"); break;
		}				
	}
}
function makeErrorList()
{
	var txtError="<h1 class=\"error\">Hay errores en el formulario</h1><ol class=\"error\">";
	for (i=0; i<listaErrores.length;i++) txtError+="<li>"+listaErrores[i]+"</li>";
	txtError+="</ol>";
	return txtError;
}


// Validacion de formulario alta_pio
function validar_form_alta()
{
	//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 titulo=document.getElementById("titulo");
	var provincia=document.getElementById("provincia");
	var descripcion=document.getElementById("descripcion");
		
		//Verificamos el nombre del sitio
		if (titulo.value=='') addError(1);		

		//Validamos tipo, provincia, poblacion, descr_breve y caracteristicas
		if (descripcion.value==0) addError(2);
		if (provincia.value==0) addError(3);
			
	//Finalmente si todo es correcto enviamos formulario
	if (conErrores==0) document.altaPio.submit();
	else mostrarAvisos(makeErrorList());
} 
//-hasta aqui comprobamos si los datos del sitio se introducen correctamente


function borrarPio()
{
	if (confirm("Seguro que deseas eliminar este PIO?"))
		return true;
	else
		return false;			
}

function borrarFotoPio(id)
{
	
	if(confirm("Seguro que deseas eliminar esta foto?"))
	{
		
	var filePHP=pathRoot+"/pio/eliminar_foto.php?id="+id;
	var content=Request();	
	
	content.open("GET",filePHP,true);
	content.onreadystatechange=function(){
		if (content.readyState==4)
			{	
				if(content.responseText==1)
				hideDiv(document.getElementById("foto_"+id));
				//alert(content.responseText);
			}
	}
	
	content.send(null);	
	}
}



function showRankPriv(val)
{
	if(timerRestoreRankPriv){ clearTimeout(timerRestoreRankPriv); timerRestoreRankPriv=false;}

	var bgres=(val*16);	
	txtShowVal(val);		
	document.getElementById("bloque_rnk_priv").style.backgroundPosition="0px -"+bgres+"px";
	return true;
}

var timerRestoreRankPriv=false;
function restoreShowRankPriv()
{
	var val=document.getElementById("priv_nivel").value;
	timerRestoreRankPriv=setTimeout("showRankPriv("+val+");txtShowVal("+val+",'act');",1000);	
}

function txtShowVal(val,type)
{
	if(!type) type='over';
	
	if(type=='over') var txt="Puntuar: ";
	else var txt="Puntuación actual: "+val;
	switch(val)
	{
		case 1:txt+=" Muy malo "; break;
		case 2:txt+=" Malo "; break;
		case 3:txt+=" Normal "; break;
		case 4:txt+=" Bueno "; break;
		case 5:txt+=" Muy bueno "; break;
	}	
	document.getElementById("txt_level_priv").innerHTML=txt;
}
function fixShowRankPriv(val,el)
{	
	if(timerRestoreRankPriv){ clearTimeout(timerRestoreRankPriv); timerRestoreRankPriv=false;}
	el.blur();
	votarAjax(val);
	txtShowVal(val,'act');
}
function votarAjax(val)
{
	var ref=document.getElementById("ref").value;
	var tabla=document.getElementById("tabla").value;
	var metodo="GET";
	var filePHP=pathRoot+"/pio/inc/votos.php?tabla="+tabla+"&ref="+ref+"&val="+val;		
	var contentF=Request();
	contentF.open(metodo,filePHP,true);	
	contentF.onreadystatechange=function(){
		if (contentF.readyState==4)
		{											
			//Cargamos los datos en la ventana	
			var resultado=contentF.responseText; 
			if(resultado!='')
			{	
				//mostramos el nombre en el enlace
				document.getElementById("block_vot_site").innerHTML=resultado;				
			} 
			
		}
	}
	contentF.send(null);			
}