function alterError(value) {
if (value<=0.99) {
newPounds = '0';
} else {
newPounds = parseInt(value);
}
newPence = parseInt((value+.0008 - newPounds)* 100);
if (eval(newPence) <= 9) newPence='0'+newPence;
newString = newPounds + '.' + newPence;
return (newString);
}

function showItems() {
index = document.cookie.indexOf("TheBasket");
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
fulllist = document.cookie.substring(countbegin, countend);
totprice = 0;
document.writeln('<table style="border-style: solid; border-width: 1px; border-color: #FFFFFF" width="555">');
document.writeln('<TR><TD style="border-style: solid; border-width: 1px; border-color: #F1F3F7; background-color: #F1F3F7" width="280" align="center"><strong>Producto</strong></TD><TD style="border-style: solid; border-width: 1px; border-color: #F1F3F7; background-color: #F1F3F7" width="65" align="center"><strong>Cantidad</strong></TD><TD style="border-style: solid; border-width: 1px; border-color: #F1F3F7; background-color: #F1F3F7" width="65" align="center"><strong>Precio x unidad</strong></TD><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7; background-color: #F1F3F7" width="75" align="center"><strong>Precio total</strong><TD width="70">&nbsp;</TD></TR>');
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
thequantity = fulllist.substring(itemstart, itemend);
itemtotal = 0;
itemtotal = (eval(theprice*thequantity));
temptotal = itemtotal * 100;
var tax = itemtotal / 100 * (22 - 0);
tax = Math.floor(tax * 100)/100
totprice = totprice + itemtotal + tax;
itemlist=itemlist+1;
document.writeln('<tr><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7;" >'+theitem+'</td><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7;" align=right>'+thequantity+'&nbsp;</td><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7;" align=right>'+theprice+'&nbsp;</td><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7;" align=right>'+alterError(itemtotal)+'&nbsp;</td><td align=center><input TYPE="button" NAME="remove" VALUE="Quitar" onclick="javascript:removeItem('+itemlist+')"></td></tr>');
} else if (fulllist.substring(i,i+1) == ',') {
theitem = fulllist.substring(itemstart, i);
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == '#') {
theprice = fulllist.substring(itemstart, i);
itemstart = i+1;
}
}
document.writeln('<tr><td colspan=3 align="right"><strong>IVA&nbsp;&nbsp;</strong></td><td style="border-style: solid; border-width: 1px; border-color: #F1F3F7;" align=right>22%&nbsp;</td><td>&nbsp;</td></tr>');
document.writeln('<tr><td colspan=3 align="right"><strong>TOTAL DOLARES&nbsp;&nbsp;</strong></td><td style="border-style: solid; border-width: 1px; border-color: #FF0000;" align=right>'+alterError(totprice)+'&nbsp;</td><td>&nbsp;</td></tr>');
document.writeln('</TABLE>');
}

function removeItem(itemno) {
newItemList = null;
itemlist = 0;
for (var i = 0; i <= fulllist.length; i++) {
if (fulllist.substring(i,i+1) == '[') {
itemstart = i+1;
} else if (fulllist.substring(i,i+1) == ']') {
itemend = i;
theitem = fulllist.substring(itemstart, itemend);
itemlist=itemlist+1;
if (itemlist != itemno) {
newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
}
}
}
index = document.cookie.indexOf("TheBasket");
document.cookie="TheBasket="+newItemList;
self.location.reload(); //top.location = "index.php?do=estado";
}

function clearBasket() {
if (totprice == 0) {
 alert("El carrito ya está vacío");
 return; }
if (confirm('¿Confirma que desea vaciar el carrito?')) {
index = document.cookie.indexOf("TheBasket");
document.cookie="TheBasket=.";
self.location.reload(); //top.location = "index.php?do=estado";
}
}
