var arrBotones = new Array('portada', 'tienda', 'noticias', 'comunidad', 'micuenta', 'torneos', 'nosotros', 'contactar', 'ayuda');
var ruta = "i/";
var prefijo = "btn_";
var sufijo = "_on.gif";

var ns4=document.layers;
var ns6=document.getElementById &&!document.all;
var ie4=document.all;
var plusExplorer = (ns4||ns6)?0:15;

var theTop=0,theLeft=0;

var alto=0;
var ancho=0;

// distiguimos entre explorer 5.5 y 6 para este tipo de docomentos 
// http://www.quirksmode.org/js/doctypes.html

if (document.documentElement && document.documentElement.scrollTop) 
{    
    theTop = document.documentElement.scrollTop;
    theLeft = document.documentElement.scrollLeft;
    alto = document.documentElement.offsetHeight;
    ancho =  document.documentElement.offsetWidth;
}

if (document.body)
{
    theTop = document.body.scrollTop;
    theLeft = document.body.scrollLeft;
    alto += document.body.clientHeight;
    ancho += document.body.clientWidth;
}

var x = 300; // valores por defecto
var y = 380;

//ESTA VARIABLE GUARDA EN QUE SECCION ESTAMOS PARA DEJARLO ACTIVO
//HAY QUE ESCRIBIRLO EN JAVASCRIPT EN CADA PAGINA... O que un PHP lo haga ;)
var activo = "";
//Ej: activo = 'portada' para que ponga oscuro el boton portada

function toggleSection(cual)
{
	var sec = document.getElementById(cual);
	if(sec.style.display=='block')
		sec.style.display='none';
	else
		sec.style.display='block';

}

function ponerEventos()
{
	var img;
	var objImg;
	var a;
	
	if(!document.images) return;

	for(var i=0; i < arrBotones.length; i++)
	{
		img = arrBotones[i];
		objImg = document.getElementById(img);
		a = objImg.parentNode;
		if (a && activo != img)
		{
			a.onmouseover = function () {
				var id = this.childNodes[0].id;
				document.images[id].src = ruta + prefijo + id + sufijo;
				return false;
			}

			a.onmouseout = function () {
				var id = this.childNodes[0].id;
				document.images[id].src = ruta + prefijo + id + ".gif";
				return false;
			}
		}
	}//for

	//ponemos el activo oscuro
	if(activo != "")
	{
		document.images[activo].src = ruta + prefijo + activo + sufijo;
	}

}

/* para modificar la ficha flotante */
function ponerFichaCarta(rutafoto, nombreEsp, nombreIng, tipo, coste, fr, descripcion)
{
	var foto, spanNombre1, spanNombre2, spanTipo, spanCoste, spanFr, spanDesc;

	foto = document.images["fotodetalle"];
	foto.src = rutafoto;
	spanNombre1 = document.getElementById('txt_nombre_es');
	spanNombre1.childNodes[0].nodeValue = nombreEsp;
	//alert(spanNombre1.childNodes[0].nodeValue);
	spanNombre2 = document.getElementById('txt_nombre_en');
	spanNombre2.childNodes[0].nodeValue = nombreIng;
	spanTipo = document.getElementById('txt_tipo');
	spanTipo.childNodes[0].nodeValue = tipo;
	spanCoste = document.getElementById('txt_coste');
	//spanCoste.childNodes[0].nodeValue = coste;
        //alert(spanCoste.innerHTML);
    spanCoste.innerHTML=coste;
	spanFr = document.getElementById('txt_fr');
	spanFr.childNodes[0].nodeValue = fr;
	spanDesc = document.getElementById('txt_descripcion');
	spanDesc.innerHTML = URLDecode(descripcion);

	var divCarta = document.getElementById("detallecarta");
	if(divCarta.style.display == 'none' || divCarta.style.display=='')
        {
		divCarta.style.top = (25+y+plusExplorer)+"px";
		divCarta.style.left = (15+x) +"px";
		divCarta.style.display = 'block';
                // alert ("x: "+x+"\n"+"y: "+y+"\n"+"theTop: "+theTop+"\n"+"theLeft: "+theLeft);
                // alert ("alto: "+alto+"\n"+"ancho: "+ancho);
	}
}

function quitarFichaCarta()
{
	var divCarta = document.getElementById("detallecarta");
	divCarta.style.display = 'none';
}


function ponerFichaProducto(rutafoto, nombre, familia, eslogan, descripcion)
{
	var foto, spanNombre, spanFamilia, spanEslogan, spanDesc;

	foto = document.images["fotodetalle"];
	foto.src = rutafoto;
	spanNombre = document.getElementById('txt_nombre_es');
	spanNombre.childNodes[0].nodeValue = nombre;
	//alert(spanNombre1.childNodes[0].nodeValue);
	//spanFamilia = document.getElementById('txt_familia');
	//spanFamilia.childNodes[0].nodeValue = familia;
	spanEslogan = document.getElementById('txt_eslogan');
	spanEslogan.childNodes[0].nodeValue = URLDecode(eslogan);
	spanDesc = document.getElementById('txt_descripcion');
	spanDesc.childNodes[0].nodeValue = URLDecode(descripcion);

	var divProducto = document.getElementById("detalleproducto");
	if(divProducto.style.display == 'none' || divProducto.style.display=='')
        {
		divProducto.style.top = (25+y+plusExplorer)+"px";
		divProducto.style.left = (15+x) +"px";
		divProducto.style.display = 'block';
	}
}

function quitarFichaProducto()
{
	var divProducto = document.getElementById("detalleproducto");
	divProducto.style.display = 'none';
}

function textoExplicativoEnvio()
{
	campoTipo=document.getElementById('metodoenvio');
	tipoEnvio=campoTipo.options[campoTipo.selectedIndex].value;
	switch (tipoEnvio) {
		case "0":
			texto = 'Por favor, selecciones el m&eacute;todo de env&iacute;o que desea.';
			break;
		case "1":
			texto = 'Recoger&aacute; sus productos en la tienda en el plazo m&aacute;ximo de una semana.';
			break;
		case "2":
			texto = 'El paquete ser&aacute; enviado al destinatario por correo ordinario.';
			break;
		case "3":
			texto = 'El paquete ser&aacute; enviado al destinatario por correo urgente.';
			break;
		case "4":
			texto = 'El paquete ser&aacute; env&iacute;ado al comprador mediante Agencia de transportes.';
			break;
		default:
			texto = 'Tipo de env&iacute;o sin descripci&oacute;n.';
			break;
	}
	document.getElementById('txt_explicacionenvio').innerHTML = texto;
	document.getElementById('hoja3').style.visibility='visible';
	actualizaResumen();
}

function textoExplicativoPago()
{
	campoTipo=document.getElementById('metodopago');
	tipoPago=campoTipo.options[campoTipo.selectedIndex].value;	
	switch(tipoPago) {
		case "0":
			texto = 'Por favor, seleccione la forma de pago que desea.';
			break;		
		case "1":
			texto = 'El pedido ser&aacute; pagado al recogerlo en la propia tienda.';
			break;
		case "2":
			texto = 'El pedido ser&aacute; pagado mediante abono o ingreso en cuenta corriente.';
			break;
		case "3":
			texto = 'El pedido ser&aacute; pagado al recogerlo en la oficina de correos.';
			break;
		case "4":
			texto = 'Pago mediante Paypal.';
			break;
		case "5":
			texto = 'Pago Seguro Tarjeta de Cr&eacute;dito o D&eacute;bito.';
			break;
			
			
		default:
			texto = 'Tipo de Pago sin descripci&oacute;n';
			break;
	}
	document.getElementById('txt_explicacionpago').innerHTML = texto;
	document.getElementById('hoja2').style.visibility='visible';
	document.getElementById('hoja3').style.visibility='hidden';
	actualizaEnvio(tipoPago);
}

function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function get_mouse(e) 
{
    // Version antigua
    //    x=(ns4||ns6)?e.pageX:event.x+theLeft;
    //    y=(ns4||ns6)?e.pageY:event.y+theTop;
    // Nueva version: (al final)
    // http://evolt.org/article/Mission_Impossible_mouse_position/17/23335/index.html
   if( e )
   {
      if( typeof( e.pageX ) == 'number' )
      {
         x = e.pageX;
         y = e.pageY;
      }
      else
      {
         x = e.clientX;
         y = e.clientY;
      }
   }
   else
   {
      e = window.event;
      x = e.clientX;
      y = e.clientY;
      if( document.documentElement
        && ( document.documentElement.scrollTop
            || document.documentElement.scrollLeft ) )
      {
         x += document.documentElement.scrollLeft;
         y += document.documentElement.scrollTop;
      } 
      else if( document.body
         && ( document.body.scrollTop
             || document.body.scrollLeft ) )
      {
         x += document.body.scrollLeft;
         y += document.body.scrollTop;
      }
   }
}

function avc(id) { // abrir ventana detalle carta
	window.open('fichaCarta.php?id=' + id,'DetalleCarta','width=700,height=568,status=no,toolbar=no,menubar=no,scrollbars=yes');
}

function avp(id) { // abrir ventana detalle carta
        window.open('fichaProducto.php?id=' + id,'DetalleProducto','width=700,height=430,status=no,toolbar=no,menubar=no,scrollbars=yes');
}

function init()
{
	ponerEventos();
	//probando la ficha flotante
	//ponerFichaCarta("i/foto_detalleEJEMPLO.jpg", "Nabo de nube", "Cloud's nabb", "Maná - Artefacto", "4 ***", "4/4", "La nube de Helm se saca un huevito y una lluvia ancestral cae sobre la cabeza. Ganas dos gallifantes.");

        // capturamos el raton
        document.onmousemove=get_mouse;
}

function ordena(campo,sentido) 
{
  if(!document.forms['formulario'].orderby) {
    return;
  }

  if (document.forms['formulario'].orderby.value==campo) {
    if (document.forms['formulario'].sentido.value.toUpperCase()=="ASC" ) {
      document.forms['formulario'].sentido.value="DESC";
    }
    else {
      document.forms['formulario'].sentido.value="ASC";
    }
  } else {
    if (sentido.length>0) {
      if (sentido.toUpperCase()=="ASC" || sentido.toUpperCase()=="DESC") {
        document.forms['formulario'].sentido.value=sentido;
      }
    }
  }
  
  document.forms['formulario'].orderby.value=campo;
  document.forms['formulario'].submit();
}

function filtra() 
{  
  //campoFoil=document.getElementById('sfoil');
  //document.forms['formulario'].foil.value=campoFoil.options[campoFoil.selectedIndex].value;

  campoFrec=document.getElementById('sfrecuencia');
  document.forms['formulario'].frecuencia.value=campoFrec.options[campoFrec.selectedIndex].value;

  document.forms['formulario'].submit();

}

function filtraColor(color) 
{  
  document.forms['formulario'].color.value=color;
  document.forms['formulario'].submit();
}

