function redondeo(numero) {
	var lon = 1; // nº de decimales
	var resul = Math.round(numero*Math.pow(10,lon))/Math.pow(10,lon);
	return resul;        
}

var genxWin;
window.onunload = genxclose;
function genxclose() {
	if (genxWin && !window.opener) genxWin.close();
}
function isInteger(s){
	var i;
    	for (i = 0; i < s.length; i++){   
        	var c = s.charAt(i);
        	if (((c < "0") || (c > "9"))) return false;
    	}
    	return true;
}
function showCard(cardID, poss, imax, page) {
	try {
		genxWin = window.open("showcard.asp?id=" + cardID + "&poss=" + poss + "&imax=" + imax + "&page=" + page, "CardImage", 
			"directories=no, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no, height=405, width=480");
		genxWin.focus();
	}
	catch (x) {}
}
function addcart(code,letter,stock){
  var i = main.tmpcart.value.indexOf(code);
  var count = 0;
  while(i>0){
	i = main.tmpcart.value.indexOf(code,i+1);
	count++;
	}
  if (stock > count) {
	main.tmpcart.value += ", " + code + letter;
	main.vercesta.style.color = "#BD31AB";
	main.vercesta.value++;
  } else {
  	alert("Sólo disponemos de "+stock+" unidades de esa carta.");
  }
}
function addcart2(code,letter,stock){
  var i = 1;
  code = code + letter;
  if (letter!='') {
  	i = document.addqty.qty2.value;
  	document.addqty.qty2.value = 1;
  } else {
  	i = document.addqty.qty.value;
  	document.addqty.qty.value = 1;
  }
  if (i > 1000) {
  	alert("La cantidad máxima de un mismo producto es de 1000 unidades.");
  	i = 1000;
  } else if (i < 1) {
  	alert("La cantidad mínima de producto es de 1 unidad.");
  	i = 1;
  }
  if (window.opener.document.main != null) {

		var cod = window.opener.document.getElementById('q' + code);
		var pri = window.opener.document.getElementById('p' + code);
		var tot = window.opener.document.getElementById('t' + code);

  	var h = window.opener.document.main.tmpcart.value.indexOf(code);
  	var count = 0;
  	while(h>0){
			h = window.opener.document.main.tmpcart.value.indexOf(code,h+1);
			count++;
		}
  	if (stock > count) {
  		if (stock < (parseInt(count)+parseInt(i))) {
  		  alert("Sólo disponemos de "+stock+" unidades de esa carta.");
  			i = stock - count;
  		}
  		window.opener.document.main.vercesta.style.color = "#BD31AB";
  		for (var j = 1; j <= i; j++)	{
  			window.opener.document.main.tmpcart.value += ", " + code;
  			window.opener.document.main.vercesta.value++;
  			if (cod != null) {
  				cod.value++;
				tot.value = redondeo(parseInt(cod.value) * parseFloat(pri.value));
				if (tot.value.indexOf(".")<0) tot.value += ".0";
				}
			}
  	} else {
  		alert("Sólo disponemos de "+stock+" unidades de esa carta.");
  	}
  if (cod != null) refreshtot2();
  }
}
function subelem(code) {
	var cod = document.getElementById('q' + code);
	var pri = document.getElementById('p' + code);
	var tot = document.getElementById('t' + code);
	if (cod.value > 0) 
	{
		cod.value--;
		tot.value = redondeo(parseInt(cod.value) * parseFloat(pri.value));
		if (tot.value.indexOf(".")<0) tot.value += ".0";
		refreshtot();
	}
}
function addelem(code,stock) {
	var cod = document.getElementById('q' + code);
	var pri = document.getElementById('p' + code);
	var tot = document.getElementById('t' + code);
  if (stock <= cod.value) {
  		alert("Sólo disponemos de "+stock+" unidades de esa carta.");
  		cod.value = stock;
  		return;
	}
 	cod.value++;
	if (cod.value > 1000) {
  		alert("La cantidad máxima de un mismo producto es de 1000 unidades.");
  		cod.value = 1000;
  }
	tot.value = redondeo(parseInt(cod.value) * parseFloat(pri.value));
	if (tot.value.indexOf(".")<0) tot.value += ".0";
	refreshtot();
}
function xelem(code,stock) {
	var cod = document.getElementById('q' + code);
  if (!isInteger(cod.value)) {
       		alert("Sólo se admiten valores numéricos.");
		cod.focus();
		return false;
	}
  if (stock <= cod.value) {
  		alert("Sólo disponemos de "+stock+" unidades de esa carta.");
  		cod.value = stock;
  		return;
	}
	if (cod.value > 1000) {
  		alert("La cantidad máxima de un mismo producto es de 1000 unidades.");
  		cod.value = 1000;
  }
	var pri = document.getElementById('p' + code);
	var tot = document.getElementById('t' + code);
	tot.value = redondeo(parseInt(cod.value) * parseFloat(pri.value));
	if (tot.value.indexOf(".")<0) tot.value += ".0";
	refreshtot();
}
function refreshtot2() {
	var j = h = 0;
	var limit, len, code;
  	if (window.opener.document.main != null) {
  		len = window.opener.document.main.length;
		limit = window.opener.document.main.length - (3 * (1 + parseInt(window.opener.document.main.numitems.value)));
		if (window.opener.document.main.numitems.value > 0) {
			window.opener.document.main.tmpcart.value = "";
			for (var i = limit; i < len - 5 ; i += 3) {
				j = redondeo(parseFloat(j) + parseFloat(window.opener.document.main.item(i+2).value));
				h = parseInt(h) + parseInt(window.opener.document.main.item(i).value);
				code = (window.opener.document.main.item(i).id).substring(1,(window.opener.document.main.item(i).id).length);
				for (var k = 0; k < parseInt(window.opener.document.main.item(i).value); k++)
				  window.opener.document.main.tmpcart.value += ", " + code;
			}
			window.opener.document.main.item(len - 3).value = j
			if (window.opener.document.main.item(len - 3).value.indexOf(".")<0)
				window.opener.document.main.item(len - 3).value += ".0";
			window.opener.document.main.vercesta.value = h;
		}
	}
}
function refreshtot() {
	var j = h = 0;
	var limit, code;
	limit = main.length - (3 * (1 + parseInt(main.numitems.value)));
	if (main.numitems.value > 0) {
		main.tmpcart.value = "";
		for (var i = limit; i < main.length - 5 ; i += 3) {
			j = redondeo(parseFloat(j) + parseFloat(main.item(i+2).value));
			h = parseInt(h) + parseInt(main.item(i).value);
			code = (main.item(i).id).substring(1,(main.item(i).id).length);
			for (var k = 0; k < parseInt(main.item(i).value); k++) main.tmpcart.value += ", " + code;
		}
		main.item((main.length - 3)).value = j
		if (main.item((main.length - 3)).value.indexOf(".")<0) main.item((main.length - 3)).value += ".0";
		main.vercesta.value = h;
	}
}
function setnext(page) {
	main.nextpage.value = page;
	main.cachenm.value = parseInt(main.cachenm.value) + 1;
	main.submit();
}
function setnext2(page) {
	main.action = page;
	main.cachenm.value = parseInt(main.cachenm.value) + 1;
	main.submit();
}