var precios = 'BsF. 40,00';
var costo = '40.00';
var cta = '0134-0214-11-2143016091';
var ctatipo = 'Corriente';
var banco = 'Banesco';
var anombre = 'Sociedad Civil El Cuarteto.';

function buyItem(newItem, newPrice, newQuantity) {
    if (newQuantity <= 0) {
            rc = alert('La cantidad ingresada es incorrecta');
            return false;
    }
    if (confirm('¿Agregar '+newQuantity+' '+newItem+' al carrito?')) {
            index = document.cookie.indexOf("TheBasket");
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
    if (countend == -1) {
                    countend = document.cookie.length;
            }
    document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+","+newPrice+"#"+newQuantity+"]";
    }
    return true;
}

function resetShoppingBasket() {
    index = document.cookie.indexOf("TheBasket");
    document.cookie="TheBasket=.";  
}

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 showItemsComprar() {
    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;

    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 * (0 - 0);
                    tax = Math.floor(tax * 100)/100
                    totprice = totprice + itemtotal + tax;
                    itemlist=itemlist+1;
                    document.writeln('<INPUT TYPE="hidden" NAME="Cantidad'+itemlist+'" VALUE="'+thequantity+'" SIZE="40">')
                    document.writeln('<INPUT TYPE="hidden" NAME="Producto'+itemlist+'" VALUE="'+theitem+'" SIZE="40">');
                    document.writeln('<INPUT TYPE="hidden" NAME="Sub'+itemlist+'" VALUE="BsF. '+alterError(itemtotal)+'" SIZE="40">');
            } 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;
            }
    }
}

function Total() {
    document.writeln(alterError(totprice));
    document.writeln('<input name="Total" id="Total" type=hidden  value="'+totprice+'">');
    }

function showItemsCarrito() {
    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('<form><table width="99%" border="0" cellpadding="0" cellspacing="0"  id="tienda" style="vertical-align:top; text-align:center">');

    document.writeln('<tr><th width="40%" >Producto</th><th width="12%">Cantidad</th><th width="18%">Costo x unidad</th><th width="16%">Costo Total</th><th width="14%">&nbsp;</th></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) == ']') {
                    theprice = theprice*1;
                    itemend = i;
                    thequantity = fulllist.substring(itemstart, itemend);
                    itemtotal = 0;
                    itemtotal = (eval(theprice*thequantity));
                    temptotal = itemtotal * 100;
                    var tax = itemtotal / 100 * (0 - 0);
                    tax = Math.floor(tax * 100)/100
                    totprice = totprice + itemtotal + tax;
                    itemlist=itemlist+1;
                    document.writeln('<tr><td class="tdtienda">'+theitem+'</td><td align="center">'+thequantity+'</td><td align="center">'+alterError(theprice)+'</td><td align="center">'+alterError(itemtotal)+'</td><td align="center"><input TYPE="button" NAME="remove" VALUE="Quitar" onclick="javascript:removeItem('+itemlist+')"></td></tr>');
                    document.writeln('<tr><td colspan="5" style="height:1px; background-color:#003366"></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" style="height:26px; text-align:right; vertical-align:middle"><strong>Total BsF.</strong></td><td align="center">'+alterError(totprice)+'</td><td>&nbsp;</td></tr>');
    document.writeln('</table></form>');
}

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;
    location = "estado-carrito.htm";
}

function clearBasket() {
    if (confirm('¿Confirmar que desea reestablecer el carrito?')) {
            index = document.cookie.indexOf("TheBasket");
            document.cookie="TheBasket=.";
            location = "estado-carrito.htm";
    }
}

function CheckEmpty() {
    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;

    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 * (0 - 0);
                    tax = Math.floor(tax * 100)/100
                    totprice = totprice + itemtotal + tax;
                    itemlist=itemlist+1;
            } 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('<INPUT TYPE="hidden" NAME="Total" VALUE="BsF. '+alterError(totprice)+'" SIZE="40">');
    if (totprice<=0) {
            alert('Debe agregar algún artículo al carrito antes de visitar esta página');
            top.$.fn.colorbox.close();
    }
}