function modificar_noticia(nid)
{
	if (document.getElementById("fecha_m").value == '') 
	{ 
		alert('Escribe la fecha de la noticia'); 
		document.getElementById("fecha_m").focus();
		return false; 
	} 
	if (document.getElementById("texto_m").value == '') 
	{ 
		alert('Escribe el texto de la noticia'); 
		document.getElementById("texto_m").focus();
		return false; 
	}
	if (document.getElementById("titulo_m").value == '') 
	{ 
		alert('Escribe el titulo de la noticia'); 
		document.getElementById("titulo_m").focus();
		return false; 
	}
	if (confirm(String.fromCharCode(191)+"Modificar los datos de la noticia?"))
	{
		document.getElementById("fModifica").submit();
	}
	else 
		return false;
}

function borra_noticia(nid)
{
	if (confirm(String.fromCharCode(191)+"Borrar la noticia?"))
	{
		document.getElementById("accion_" + nid).value = "borra";
		document.getElementById("form_" + nid).submit();
	}
	else 
		return false;
}

function nueva_noticia()
{
	if (valida_noticia())
	{
		if (confirm(String.fromCharCode(191)+"Añadir la noticia?"))
		{
			document.getElementById('fNuevo').submit();
		}
		else
			return false;
	}
}

function valida_noticia() { 
	if (document.getElementById("fecha_n").value == '') 
	{ 
		alert('Escribe la fecha de la noticia'); 
		document.getElementById("fecha_n").focus();
		return false; 
	} 

	if (document.getElementById("titulo_n").value == '') 
	{ 
		alert('Escribe el titulo de la noticia'); 
		document.getElementById("titulo_n").focus();
		return false; 
	}
	if (document.getElementById("texto_n").value == '') 
	{ 
		alert('Escribe el texto de la noticia'); 
		document.getElementById("texto_n").focus();
		return false; 
	}
  	return true;
}

function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try { 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e) { 
		try { 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 
	
	return xmlhttp; 
}

function carga_noticia(id)
{	
	ajax=nuevoAjax();
	ajax.open("GET", "carga_noticias.php?id="+id, true);	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var datos = ajax.responseText;
			var aDatos = datos.split('#');
			var nid = aDatos[0];
			var fecha = aDatos[1];
			var titulo = aDatos[2];
			var texto = aDatos[3];
			var imagen = aDatos[4];
			
			document.getElementById('div_modifica').style.display = 'block';
			document.getElementById('div_nuevo').style.display = 'none';
			document.getElementById('id_m').value = nid;
			document.getElementById('fecha_m').value = fecha;
			document.getElementById('titulo_m').value = titulo;
			document.getElementById('texto_m').value = texto;
			document.getElementById('foto').src = "../images/noticias/" + imagen;
			document.getElementById('imagen_actual').value = imagen;
		}
	}
	ajax.send(null);
}

function cancela_modificacion()
{
	document.getElementById('div_modifica').style.display = 'none';
	document.getElementById('div_nuevo').style.display = 'block';
}

function nuevo_producto()
{
	if (valida_producto())
	{
		if (confirm(String.fromCharCode(191)+"Añadir el producto?"))
		{
			document.getElementById('fNuevo').submit();
		}
		else
			return false;
	}
}

function valida_producto()
{
	if (document.getElementById("nombre_n").value == '') 
	{ 
		alert('Escribe el nombre del producto'); 
		document.getElementById("nombre_n").focus();
		return false; 
	}
	if (document.getElementById("categoria_n").value == '') 
	{ 
		alert('Indica la categoría del producto'); 
		document.getElementById("categoria_n").focus();
		return false; 
	}
	if (document.getElementById("texto_n").value == '') 
	{ 
		alert('Escribe el texto'); 
		document.getElementById("texto_n").focus();
		return false; 
	}
	return true;
}

function modificar_producto()
{
	if (document.getElementById("nombre_m").value == '') 
	{ 
		alert('Escribe el nombre del producto'); 
		document.getElementById("nombre_m").focus();
		return false; 
	}
	if (document.getElementById("categoria_m").value == '') 
	{ 
		alert('Indica la categoría del producto'); 
		document.getElementById("categoria_m").focus();
		return false; 
	}
	if (document.getElementById("texto_m").value == '') 
	{ 
		alert('Escribe el texto'); 
		document.getElementById("texto_m").focus();
		return false; 
	}
	if (confirm(String.fromCharCode(191)+"Modificar el producto?"))
	{
		document.getElementById("fModifica").submit();
	}
	else 
		return false;
}

function borra_producto(nid)
{
	if (confirm(String.fromCharCode(191)+"Borrar el producto?"))
	{
		document.getElementById("accion_" + nid).value = "borra";
		document.getElementById("form_" + nid).submit();
	}
	else 
		return false;
}

function carga_producto(id,tipo)
{	
	ajax=nuevoAjax();
	var ruta = "../images/productos/" + tipo + "/";
	ajax.open("GET", "carga_productos.php?id="+id+"&tipo="+tipo, true);
		
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var datos = ajax.responseText;
			var aDatos = datos.split('#');
			var nid = aDatos[0];
			var nombre = aDatos[1];
			var categoria = aDatos[2];
			var texto = aDatos[3];
			var imagen = aDatos[4];
			
			document.getElementById('div_modifica').style.display = 'block';
			document.getElementById('div_nuevo').style.display = 'none';
			document.getElementById('id_m').value = nid;
			document.getElementById('nombre_m').value = nombre;
			document.getElementById('categoria_m').value = categoria;
			document.getElementById('foto').src = ruta + imagen;
			document.getElementById('imagen_actual').value = imagen;
			document.getElementById('texto_m').value = texto;
		}
	}
	ajax.send(null);
}

function modificar_descarga(nid)
{
	if (document.getElementById("texto_m").value == '') 
	{ 
		alert('Escribe el texto del archivo'); 
		document.getElementById("texto_m").focus();
		return false; 
	}
	if (document.getElementById("nombre_m").value == '') 
	{ 
		alert('Escribe el titulo del archivo'); 
		document.getElementById("nombre_m").focus();
		return false; 
	}
	if (confirm(String.fromCharCode(191)+"Modificar los datos del archivo?"))
	{
		document.getElementById("fModifica").submit();
	}
	else 
		return false;
}

function borra_descarga(nid)
{
	if (confirm(String.fromCharCode(191)+"Borrar el archivo?"))
	{
		document.getElementById("accion_" + nid).value = "borra";
		document.getElementById("form_" + nid).submit();
	}
	else 
		return false;
}

function nueva_descarga()
{
	if (valida_descarga())
	{
		if (confirm(String.fromCharCode(191)+"Añadir el archivo?"))
		{
			document.getElementById('fNuevo').submit();
		}
		else
			return false;
	}
}

function valida_descarga() { 
	if (document.getElementById("nombre_n").value == '') 
	{ 
		alert('Escribe el nombre del archivo'); 
		document.getElementById("nombre_n").focus();
		return false; 
	}
	if (document.getElementById("texto_n").value == '') 
	{ 
		alert('Escribe el texto del archivo'); 
		document.getElementById("texto_n").focus();
		return false; 
	}
  	return true;
}

function carga_descarga(id)
{	
	ajax=nuevoAjax();
	ajax.open("GET", "carga_descargas.php?id="+id, true);
		
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var datos = ajax.responseText;
			var aDatos = datos.split('#');
			var nid = aDatos[0];
			var nombre = aDatos[1];			
			var texto = aDatos[2];
			var imagen = aDatos[3];
			var archivo = aDatos[4];
			
			document.getElementById('div_modifica').style.display = 'block';
			document.getElementById('div_nuevo').style.display = 'none';
			document.getElementById('id_m').value = nid;
			document.getElementById('nombre_m').value = nombre;
			document.getElementById('archivo').innerHTML = archivo;
			document.getElementById('archivo_actual').value = archivo;
			document.getElementById('texto_m').value = texto;
		}
	}
	ajax.send(null);
}

function modificar_categoria(nid)
{
	if (document.getElementById("descripcion_m").value == '') 
	{ 
		alert('Escribe la descripción de la categoría'); 
		document.getElementById("descripcion_m").focus();
		return false; 
	}
	if (document.getElementById("nombre_m").value == '') 
	{ 
		alert('Escribe el nombre de la categoria'); 
		document.getElementById("nombre_m").focus();
		return false; 
	}
	if (confirm(String.fromCharCode(191)+"Modificar los datos de la categoria?"))
	{
		document.getElementById("fModifica").submit();
	}
	else 
		return false;
}
function nueva_categoria()
{
	if (valida_categoria())
	{
		if (confirm(String.fromCharCode(191)+"Añadir la categoría?"))
		{
			document.getElementById('fNuevo').submit();
		}
		else
			return false;
	}	
}

function valida_categoria()
{
	if (document.getElementById("producto_n").value == '') 
	{ 
		alert('Indica el producto para la categoria'); 
		document.getElementById("producto_n").focus();
		return false; 
	}
	if (document.getElementById("nombre_n").value == '') 
	{ 
		alert('Escribe el nombre de la categoría'); 
		document.getElementById("nombre").focus();
		return false; 
	}
	if (document.getElementById("descripcion_n").value == '') 
	{ 
		alert('Escribe la descripción de la categoría'); 
		document.getElementById("descripcion").focus();
		return false; 
	}
	return true;
}
function borra_categoria(nid)
{
	if (confirm(String.fromCharCode(191)+"Borrar la categoría?"))
	{
		document.getElementById("accion_" + nid).value = "borra";
		document.getElementById("form_" + nid).submit();
	}
	else 
		return false;
}

function carga_categoria(id)
{	
	ajax=nuevoAjax();
	var categoria = document.getElementById('categoria_' + id).value;
	ajax.open("GET", "carga_categorias.php?id="+id+'&producto='+categoria, true);
		
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var datos = ajax.responseText;
			var aDatos = datos.split('#');
			var nid = aDatos[0];
			var nombre = aDatos[1];			
			var descripcion = aDatos[2];
			
			document.getElementById('div_modifica').style.display = 'block';
			document.getElementById('div_nuevo').style.display = 'none';
			document.getElementById('id_m').value = nid;
			document.getElementById('nombre_m').value = nombre;
			document.getElementById('producto_m').value = categoria;
			document.getElementById('descripcion_m').value = descripcion;
		}
	}
	ajax.send(null);
}

function carga_categorias()
{
	document.getElementById('fCategorias').submit();
	return true;
}

function modificar_proyecto(nid)
{
	if (document.getElementById("descripcion_m").value == '') 
	{ 
		alert('Escribe la descripción del proyecto'); 
		document.getElementById("descripcion_m").focus();
		return false; 
	}
	if (document.getElementById("titulo_m").value == '') 
	{ 
		alert('Escribe el titulo del proyecto'); 
		document.getElementById("titulo_m").focus();
		return false; 
	}
	if (confirm(String.fromCharCode(191)+"Modificar los datos del proyecto?"))
	{
		document.getElementById("fModifica").submit();
	}
	else 
		return false;
}
function nuevo_proyecto()
{
	if (valida_proyecto())
	{
		if (confirm(String.fromCharCode(191)+"Añadir el proyecto?"))
		{
			document.getElementById('fNuevo').submit();
		}
		else
			return false;
	}	
}

function valida_proyecto()
{
	if (document.getElementById("titulo_n").value == '') 
	{ 
		alert('Indica el titulo del proyecto'); 
		document.getElementById("titulo_n").focus();
		return false; 
	}
	if (document.getElementById("descripcion_n").value == '') 
	{ 
		alert('Escribe la descripcion'); 
		document.getElementById("descripcion_n").focus();
		return false; 
	}
	return true;
}
function borra_proyecto(nid)
{
	if (confirm(String.fromCharCode(191)+"Borrar el proyecto?"))
	{
		document.getElementById("accion_" + nid).value = "borra";
		document.getElementById("form_" + nid).submit();
	}
	else 
		return false;
}

function carga_proyecto(id)
{	
	ajax=nuevoAjax();
	ajax.open("GET", "carga_proyectos.php?id="+id, true);
		
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			var datos = ajax.responseText;
			var aDatos = datos.split('#');
			var nid = aDatos[0];
			var imagen = aDatos[1];			
			var titulo = aDatos[2];
			var descripcion = aDatos[3];
			var ruta = "../images/proyectos/" + id + "/";
			document.getElementById('div_modifica').style.display = 'block';
			document.getElementById('div_nuevo').style.display = 'none';
			document.getElementById('id_m').value = nid;
			document.getElementById('titulo_m').value = titulo;
			document.getElementById('imagen_actual').value = imagen;
			document.getElementById('foto').src = ruta + imagen;
			document.getElementById('descripcion_m').value = descripcion;
			
			carga_proyecto_imagenes(id);
		}
	}
	ajax.send(null);	
}

function carga_proyecto_imagenes(id)
{	
	ajax=nuevoAjax();
	ajax.open("GET", "carga_proyecto_imagenes.php?id="+id, true);
		
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			document.getElementById('td_fotos').innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}


function addRow(id)
{
	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
	var row = document.createElement("TR");
	var td1 = document.createElement("TD");
	var td2 = document.createElement("TD");
	var idRow=tbody.getElementsByTagName("tr").length+1;	
	var the_id = 'file' + idRow; 	

	td1.bgColor = "#FFFFFF";
	td1.innerHTML = "";
	td1.width= 110;

	td2.bgColor = "#FFFFFF";
	td2.innerHTML = "<input type='file' id='" + the_id + "' name='" + the_id + "' size='60'>";
	
	row.style.display = "block";
	row.id = "row" + idRow;	
	row.appendChild(td1);
	row.appendChild(td2);
	tbody.appendChild(row);	
}

function validaFormProducto()
{
	if (document.getElementById('nombre').value == '')
	{
		alert("Introduce tu nombre");
		return false;
	}
	if (document.getElementById('email').value == '')
	{
		alert("Introduce tu e-mail");
		return false;
	}
	if (document.getElementById('texto').value == '')
	{
		alert("Introduce el mensaje");
		return false;
	}
	document.getElementById('form1').submit();
}

function validaLogin()
{
	if (document.getElementById('login').value == '')
	{
		document.getElementById('td').innerHTML = 'Introduce el usuario';
		return false;
	}
	if (document.getElementById('password').value == '')
	{
		document.getElementById('td').innerHTML = 'Introduce el password';
		return false;
	}
	document.getElementById('fLogin').submit();
	return true;
}
