// menus
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

//abrir popup (reproductor)

function popup (pagina) {
var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=420, height=161, top=85, left=140";
window.open(pagina,"",opciones);
}

function lista () {
var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=420, height=161, top=85, left=140";
window.open("lista.html","",opciones);
}


//lista de correo

var estado = 0;

function lista_correo ()  {
	var taboa, taboatr, taboatd, taboaultima;
	
	switch (estado)
	{
		case "0":
			taboa = document.getElementById('lista_correo');
			taboatr = taboa.insertRow(taboa.rows.length);
			taboatd = taboatr.insertCell(taboatr.cells.length);
			taboatd.innerHTML = "<form action='envia_mail.php' method='post' enctype='multipart/form-data' name='formulario'><div class='texto_pq'>Alta en lista de correo >><input name='mail_lista' type='text' value='' size='20'><input type='submit' class='boton_formulario' value='Alta!'></div></form>";
			estado = 1;
			break;
			
		case "1":
			taboaultima = document.all.lista_correo.rows.length;
			document.all.lista_correo.deleteRow(taboaultima);
			estado = 0;
			break;
	}
	
}



//tenda - cesta da compra

var contLin1 = 1;
var js1 = 2;

var contador = 1;
var total2 = 0;
var totarticulos = 0;

function agregar_cesta(referencia) {
	var tr, td, cesta, autor, articulo, prezo, unidades, total1, span;
	
	switch (referencia)
	{
		case "101":
		 	autor = document.forms.formulario.au_bichos.value;
		  	articulo = document.forms.formulario.cd_bichos.value;
		  	prezo = document.forms.formulario.pz_bichos.value;
		  	unidades = document.forms.formulario.uni_bichos.value;
		  	span = document.getElementById("spantotal"); 
		  	total1 = prezo * unidades;
		 	cesta = document.getElementById('cesta');
		 	tr = cesta.insertRow(cesta.rows.length);
			td = tr.insertCell(tr.cells.length);
			td.innerHTML = "<div class='texto_pq' align='center'>" + unidades + "</div><input name='unidades_" +  contador + "' type='hidden' id='unidades_" +  contador + "' value='" + unidades + "'>";
			td = tr.insertCell(tr.cells.length);
			td.innerHTML = "<div class='texto_pq' align='left'>" + autor + "</div><input name='autor_" +  contador + "' type='hidden' id='autor_" +  contador + "' value='" + autor + "'>";
			td = tr.insertCell(tr.cells.length);
			td.innerHTML = "<div class='texto_pq' align='left'>" + articulo + "</div><input name='articulo_" +  contador + "' type='hidden' id='articulo_" +  contador + "' value='" + articulo + "'>";
			td = tr.insertCell(tr.cells.length);
			td.innerHTML = "<div class='texto_pq' align='right'>" + total1 + "</div><input name='total1_" +  contador + "' type='hidden' id='total1_" +  contador + "' value='" + total1 + "'>";
			total2 = total2 + total1;		
			span.innerHTML = total2;
			totarticulos++;
			document.forms.formulario.tot_lineas.value=totarticulos;
			document.forms.formulario.total_final.value=total2;
			contador++;
			break;
	}
	
}



function borrar_cesta() {
	ultima = document.all.tabla.rows.length - 1;
	ultima2= document.all.tabla.rows.length - 2;
	if(contLin1 > 2){
	if(ultima > -1){
		document.all.tabla.deleteRow(ultima);
		document.all.tabla.deleteRow(ultima2);
		contLin1--;
		js1--;
		document.forms.formulario.js1.value=js1 - 1;
	}
	}
}


function foto(id) {

var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=600, height=600, top=85, left=140";
var pagina2 = "arquivos/fotos/visor.php?id=" + id;
window.open(pagina2,"",opciones);

}


var titulopordefecto = ":: indana ::"; 
var ventana;
var cont=0;

function afoto(cual,titulo){
	cual = "arquivos/prensa/" + cual;
	if(cont==1){ventana.close();ventana=null}
	if(titulo==null){titulo=titulopordefecto}
	ventana=window.open('','ventana','resizable=yes,scrollbars=no')
	ventana.document.write('<html><head><title>' + titulo + '</title></head><body style="overflow:hidden" marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" scroll="no" onUnload="opener.cont=0"><img src="' + cual + '" onLoad="opener.redimensionar(this.width, this.height)">');
	ventana.document.close();
	cont++;
}
function redimensionar(ancho, alto){
	ventana.resizeTo(ancho+12,alto+28);
	ventana.moveTo((screen.width-ancho)/2,(screen.height-alto)/2); //centra la ventana. Eliminar si no se quiere centrar el popup
}
function down_disco (id) {
    $.ajax ({
        url: 'arquivos/discos/descargar_archivo.php',
        type: 'POST',
        data: 'ds='+id,
        dataType: 'json',
        success: function (mostrar) {
            location.href=mostrar['link'];
            },       
        error: function () {alert('error')}
    });
}

function enviar_contacto(lg) {
    
    if  (lg=="gl") {
        erro1 = "Falta o teu nome";
        erro2 = "Falta o teu mail (Introduce unha dirección válida)";
        erro3 = "Completa a mensaxe";
        form_action = "contacta_grupo.php?lg=gl";
    }
    else {
        erro1 = "Falta tu nombre";
        erro2 = "Falta tu e-mail (Introduce una dirección válida)";
        erro3 = "Completa el mensaje";
        form_action = "contacta_grupo.php";
    }

    
    var divo = document.getElementById('lista_errores');
    while(divo.hasChildNodes()){
      divo.removeChild(divo.lastChild);
      }
    
    var nombre = $('#nome').attr('value');
    var email = $('#mail').attr('value');
    var ciudad = $('#ciudad').attr('value');
    var msj = $('#comentario').attr('value');
    
    var errores = "<ul>";
    var control = 0;
    
    if (nombre == "") {
        control=1;
        errores = errores + "<li>" + erro1 + "</li>";
    }
    
    if (email == "") {
        control=1;
        errores = errores + "<li>" + erro2 + "</li>";
    }
    else {
        var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
        if (!filter.test(email)){
        control=1;
        errores = errores + "<li>" + erro2 + "</li>";
        }
    }
    
    if (msj == "") {
        control=1;
        errores = errores + "<li>" + erro3 + "</li>";
    }
    
    if (control==1) {
      errores = errores + "</ul>";
      
      $("div#lista_errores").append(errores);
      $("div#error_contacto").slideDown();
    }
    else {
      $("div#error_contacto").slideUp("fast");
      $.ajax ({
          url: 'contacta_grupo.php',
          type: 'POST',
          data: 'email='+email+ '&nombre='+nombre+ '&msj='+msj+ '&ciudad='+ciudad+ '&lg='+lg,
          success: function (enviado) {
              location.href = form_action;
          },       
          error: function () {alert('error')}
      });
    }
        
}
