// JavaScript Document
function borrarLista(id)
{
	if (confirm("Estás a punto de borrar una de tus listas. Esta acción es irreversible.\n¿Estás seguro de borrarla?"))
	{
		ajaxLista = Ajax();
		variables = "id="+id;
		ajaxLista.open("POST", "modulos/tienda/ajax/borrarLista.php", true);
		ajaxLista.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajaxLista.send("&"+variables);
		ajaxLista.onreadystatechange = stateChangedLista;
	}
}

function stateChangedLista()
{
	if ( (ajaxLista.readyState == 4) && (ajaxLista.status == 200) )
	{
		var txt = ajaxLista.responseText;
		document.getElementById("mis_listas").innerHTML = txt;
	}
}



function stateChangedCarrito()
{
	if ((ajaxCarrito.readyState == 4) && (ajaxCarrito.status == 200))
	{
		var txt = ajaxCarrito.responseText.split("|");
		if (txt[0] == 1) // Hay rotura de Stock
		{
			if (txt[3] == 0)
			{
				alert("Ya no puede elegir más unidades");
			}
			else 
			{
				if (txt[1] == 1) 
				{
					if (confirm("Sólo queda una unidad de este producto.¿Desea adquirirla?"))
					{
						addCarrito(txt[2], 1, txt[4]);	
					}
				}
				else if (txt[1] > 1)
				{
					if (confirm("Sólo quedan "+txt[1]+" unidades de este producto.¿Desea adquirirlas todas?"))
					{
						addCarrito(txt[2], txt[3], txt[4]);	
					}
				}
			}
		}
		document.getElementById("cesta").innerHTML = txt[5];
	}
}


function stateChangedCarritoGrande()
{
	if ((ajaxCarrito.readyState == 4) && (ajaxCarrito.status == 200))
	{
		var txt = ajaxCarrito.responseText.split("|");
		if (txt[0] == 1) // Hay rotura de Stock
		{
			if (txt[3] == 0)
			{
				alert("Ya no puede elegir más unidades");
			}
			else 
			{
				if (txt[1] == 1) 
				{
					if (confirm("Sólo queda una unidad de este producto.¿Desea adquirirla?"))
					{
						addCarrito(txt[2], 1, txt[4]);	
					}
				}
				else if (txt[1] > 1)
				{
					if (confirm("Sólo quedan "+txt[1]+" unidades de este producto.¿Desea adquirirlas todas?"))
					{
						addCarrito(txt[2], txt[3], txt[4]);	
					}
				}
			}
		}
		document.getElementById("cestaGrande").innerHTML = txt[5];
	}
}

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.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- El campo '+nm+' debe ser un correo electrónico.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- El campo '+nm+' deber ser un número.\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+='- El campo '+nm+' deber ser un número entre '+min+' y '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- El campo '+nm+' es obligatorio.\n'; }
  } if (errors) alert('Errores:\n'+errors);
  document.MM_returnValue = (errors == '');
}

