//Alta nueva agenda
conErrores=0;
listaErrores=new Array();
function addError(num)
{
	conErrores=1;

	if(num)
	{				
		switch (num){
		case 1:listaErrores.push("Tienes que seleccionar el tipo del evento"); break;
		case 2:listaErrores.push("Tienes que introducir el nombre"); break;
		case 3:listaErrores.push("Tienes que seleccionar una provincia"); break;
		case 4:listaErrores.push("Tienes que introducir la poblacion"); break;
		case 5:listaErrores.push("Tienes que seleccionar Fecha Inicio"); break;
		case 6:listaErrores.push("Tienes que seleccionar Fecha Fin"); break;
		case 7:listaErrores.push("Tienes que introducir la descripcion"); 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 tipo=document.getElementById("tipo");
	var nombre=document.getElementById("nombre");
	var provincia=document.getElementById("provincia");
	var poblacion=document.getElementById("poblacion");
	var fecha_ini=document.getElementById("fecha");
	var fecha_fin=document.getElementById("fecha_fin");
	var descripcion=document.getElementById("descr_micro");
		//Validamos tipo, nombre
		if (tipo.value==0) addError(1);		
		if (nombre.value=='') addError(2);		

		//Validamos provincia, poblacion, fecha inicio, fecha fin, descripcion
		if (provincia.value==0) addError(3);
		if (poblacion.value=='') addError(4);
		if (fecha_ini.value==0) addError(5);
		if (fecha_fin.value==0) addError(6);
		if (descripcion.value=='') addError(7);
			
	//Finalmente si todo es correcto enviamos formulario
	if (conErrores==0) document.altaAgenda.submit();
	else mostrarAvisos(makeErrorList());
} 
//-hasta aqui comprobamos si los datos del evento se introducen correctamente


function borrarAgenda()
{
	if (confirm("Seguro que deseas eliminar este evento?"))
		return true;
	else
		return false;			
}

function borrarFotoAgenda(id)
{
	
	if(confirm("Seguro que deseas eliminar esta foto?"))
	{
		
	var filePHP=pathRoot+"/agenda/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 mostrarFecha(valor,destino)//para el calendario 
{
	any=valor.substr(0,4);
	mes=valor.substr(5,2);
	dia=valor.substr(8,2); dia=eval(dia);
	document.getElementById(destino).value=dia+"-"+mes+"-"+any;
	document.getElementById("diaNac").value=dia;
	document.getElementById("mesNac").value=mes;
	document.getElementById("anyNac").value=any;
	setFechaManual();
}

function setFechaManual()
{
	//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 dia=document.getElementById("diaNac");var vdia=dia.value;
	var mes=document.getElementById("mesNac");var vmes=mes.value;
	var any=document.getElementById("anyNac");var vany=any.value;
	if((vdia!='') && (vmes!='') && (vany!=''))
	{						
		if(vdia.length==1) {vdia="0"+vdia; dia.value=vdia;}
		if( (parseInt(vdia,10)<1) || (parseInt(vdia,10)>31) ) { vdia.value=''; addError(38);}		
		if(vmes.length==1) {vmes="0"+vmes; mes.value=vmes;}
		if( (parseInt(vmes,10)<1) || (parseInt(vmes,10)>12) ) { vmes.value=''; addError(39);}
		
		if(vany.length!=4) {any.value=''; addError(40);}		
		
	} else addError(37);
		
	//Finalmente si todo es correcto enviamos formulario
	if (conErrores==0) { 
		document.getElementById("fecha").value=vany+'-'+vmes+'-'+vdia;
		document.getElementById("fechaDiv").value=vdia+'-'+vmes+'-'+vany;
	}
	else mostrarAvisos(makeErrorList());
}

										
function show()
	{
		showDiv(document.getElementById("periodo"));
	}

function hide()
	{
		hideDiv(document.getElementById("periodo"));
	}
									