function alterError(value) {
	if (value<=0.99) {
		n1 = '0';
	} else {
		n1 = parseInt(value);
	}
	n2 = parseInt((value+.0008 - n1)* 100);
	if (eval(n2) <= 9) n2='0'+n2;
	newString = n1 + '.' + n2;
	return (newString);
}

function buyItem(newItem, newPrice, newQuantity) {
	if (newQuantity <= 0) {
		rc = alert('La cantidad ingresada no es correcta');
		return false;
	}
	if (confirm('Agregar '+newQuantity+' x '+newItem+' a su compra?')) {
		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+"]";
	}
	location.href = "showcart.php";
	//return true;
}

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.href = "showcart.php";
}

function clearBasket() {
	if (confirm('Está seguro que desea devolver la totalidad de su compra?')) {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
		location.href = "showcart.php";
	}
}

function showCart() {
	index = document.cookie.indexOf("TheBasket");
	cntStrt = (document.cookie.indexOf("=", index) + 1);
       	cntEnd = document.cookie.indexOf(";", index);
       	if (cntEnd == -1) {
           		cntEnd = document.cookie.length;
       	}
	fulllist = document.cookie.substring(cntStrt, cntEnd);
	totprice = 0;
	document.write('<TABLE align="center" BORDER=1 CELLSPACING=0 CELLPADDING=2 style="border-color: #000099; border-collapse: collapse;">');
	document.write('<TR style="background-color: #000000;"><TD><b>Artículo</b></TD><TD><b>Cantidad</b></TD><TD><b>Costo unitario</b></TD><td><b>Costo total</b></td><TD><b>Devolver</b></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;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
	document.write('<tr><td>'+theitem+'</td><td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td><td><a href="javascript:removeItem('+itemlist+')">Devolver</a></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.write('<tr><td colspan=3><b>Total</b></td><td align=right>'+alterError(totprice)+'</td><td></td></tr>');
	document.write('</TABLE>');
}

function showBuy() {
	index = document.cookie.indexOf("TheBasket");
	cntStrt = (document.cookie.indexOf("=", index) + 1);
       	cntEnd = document.cookie.indexOf(";", index);
       	if (cntEnd == -1) {
           		cntEnd = document.cookie.length;
       	}
	fulllist = document.cookie.substring(cntStrt, cntEnd);
	totprice = 0;
	document.writeln('<TABLE align="center" BORDER=1 CELLSPACING=0 CELLPADDING=2 style="border-color: #000099; border-collapse: collapse;">');

document.writeln('<TR style="background-color: #000000;"><TD><b>Artículo</b></TD><TD><b>Cantidad</b></TD><TD><b>Costo unitario</b></TD><td><b>Costo total</b></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;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
document.writeln('<tr><td>'+theitem+'</td><td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td></tr>');
			document.writeln('<INPUT TYPE="hidden" NAME="articulo '+itemlist+'" VALUE="'+theitem+'" SIZE="40">');
			document.writeln('<INPUT TYPE="hidden" NAME="cantidad '+itemlist+'" VALUE="'+thequantity+'" SIZE="40">');
			document.writeln('<INPUT TYPE="hidden" NAME="precio unitario '+itemlist+'" VALUE="'+theprice+'" SIZE="40">');
			document.writeln('<INPUT TYPE="hidden" NAME="costo total '+itemlist+'" VALUE="'+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;
		}
	}

	document.writeln('<tr><td colspan=3><b>Total</b></td><td align=right>'+alterError(totprice)+'</td></tr>');
	document.writeln('<INPUT TYPE="hidden" NAME="Total de la compra" VALUE="'+alterError(totprice)+'" SIZE="40">');
	document.writeln('</TABLE>');

}

function resetShoppingBasket() {
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket=.";
}

//resetShoppingBasket();
