// JavaScript Document
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_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;
}

// Para faqs
function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}


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];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function OpenBrWindow(theURL,winName,features, myWidth, myHeight, isCenter) { //v3.0
	if(window.screen)if(isCenter)if(isCenter=="true"){
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	features+=(features!='')?',':'';
	features+=',left='+myLeft+',top='+myTop;
	}
	window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}

function MM_preloadImages() { //v3.0
/*	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));*/
	
	var pageTracker = _gat._getTracker("UA-716153-33");
	pageTracker._initData();
	pageTracker._trackPageview();
}


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_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


function addQuantity(id)
{
	var f = document.getElementById('featured_quantity'+id);
	f.value++;
}

function removeQuantity(id)
{
	var f = document.getElementById('featured_quantity'+id);
	var value = f.value;
	if (value>1)
	{
		f.value--;
	}
}

function show_client()
{
	var showing = document.getElementById('cart_content').style.display!='none' &&  document.getElementById('cart_content').style.display!='';
	if (!showing)
	{
		(function($){$("#cart_content").slideDown("fast");})(jQuery)
	}else{
		(function($){$("#cart_content").slideUp("fast");})(jQuery)
	}
}

function show_minicart(url)
{
	var showing = document.getElementById('cart_content').style.display!='none' &&  document.getElementById('cart_content').style.display!='';
	/* Todo el lio de (function($){}(jQuery)) es para compatibilizar el funcionamiento de jQuery y prototype, que los dos usan el $ para 
	   hacer referencia a los objetos */
	
	update_minicart(url);
	if (!showing)
	{
		(function($){$("#cart_content").slideDown("fast");})(jQuery)
	}else{
		(function($){$("#cart_content").slideUp("fast");})(jQuery)
	}
}

function show_added(url,quantity)
{
		(function($){
			$('#cart_added').show("fast");
			if (quantity<=0 || isNaN(quantity))
			{
				$('#cart_added').html('Elemento no añadido. Por favor seleccione como mínimo un elemento.');
			}else{
				if (quantity>1)
				{
					$('#cart_added').html(quantity+' unidades añadidas al carrito');
				}else{
					$('#cart_added').html(quantity+' unidad añadida al carrito');
				}
			}
			
			setTimeout(function(){$('#cart_added').hide('fast');update_minicart(url)},2000);
		})(jQuery)
}

function update_minicart(url)
{

		(function($){

			$.get(url,null,function(data){
				$("#cart_content").html(data);
			}
			
			);
			
		})(jQuery)
}

function showLinkImage(id,width,height)
{
	if (width==undefined) width=40;
	if (height==undefined) height=70;
	var img = new Image();	
	img.src=url;
	alert(img.width);
	document.write('<img width="'+width+'" height="'+height+'" src="'+url+'" />');
}

function show_message(message,time)
{
		(function($,time){
			$('#cart_added').html(message);
			$('#cart_added').show("fast");
			if (time==undefined) time=2000;
			setTimeout(function(){$('#cart_added').hide('fast');},time);
		})(jQuery)
}

function full_cart_edit_remove(field,deleteForm)
{

	val = document.getElementById(field).value;

	show_message("Elemento eliminado.<br>Actualizando carrito, por favor espere",5000);
	if (val>1){
		document.getElementById(field).value--;
		document.getElementById(field).form.submit()
	}else{
		document.getElementById(deleteForm).submit();
	}
}



function full_cart_edit_add(field)
{

	show_message("Elemento añadido.<br>Actualizando carrito, por favor espere",5000);
	document.getElementById(field).value++;
	document.getElementById(field).form.submit();
}

function getURLData()
{
	var qs = location.search.substring(1);
	var nv = qs.split('&');
	var url = new Object();
      
	for(i = 0; i < nv.length; i++)
        {
		eq = nv[i].indexOf('=');
	        url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
	}

	return url;
}

function checkQuantity(form)
{
	var ret = form.quantity.value > 0;
	return ret;
}

