var NYTax = 0.0825
function alterError(value) {
	return format (value, 2);
}

//  add item to the shoppingbasket
//function addItem(newItem, newPrice, newQuantity, newNumber, newWeight, newSize, newColor) 
function addItem(newItem, newPrice, newQuantity, newNumber, newSize, newColor, bNoTax, bNoShipping) 
{
	initBasket();
	if (newQuantity >= 1000) 
	{
		rc = alert('You entered an invalid quantity. Please try again.');
	} 
	else 
	{
       if (newQuantity >= 1) 
       {
       		var strSizeColor = "";
       		
       		if (newSize != ".")
       		{
	       		strSizeColor = '\nOption1 = ' + newSize;
	       	}
       		if (newColor != ".")
       		{
	       		strSizeColor += '\nOption2 = ' + newColor;
	       	}
			window.alert('Quantity = ' + newQuantity+'\nItem = ' + newItem +'\nTotal Cost = $' + alterError(eval(newQuantity)*eval(newPrice)) + strSizeColor + '\n\nhas been added to your shopping cart!');
			strSizeColor = "";
       		if (newSize != ".")
       		{
	       		strSizeColor = '<br>Option1: ' + newSize;
	       	}
       		if (newColor != ".")
       		{
		       	strSizeColor += '<br>Option2: ' + newColor;
	       	}
			index = document.cookie.indexOf("ShoppingBasket");
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);
			if (countend == -1) 
			{
				countend = document.cookie.length;
			}
			document.cookie="ShoppingBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+strSizeColor+"|"+newPrice+"|"+newNumber+"|"+bNoShipping+"|"+bNoTax+"|"+newQuantity+"]";
//			document.cookie="ShoppingBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newQuantity+"|"+newSize+"|"+newColor+"]";
//			document.cookie="ShoppingBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newWeight+"|"+newQuantity+"|"+newSize+"|"+newColor+"]";
		}
	}
}

function resetShoppingBasket() 
{
	index = document.cookie.indexOf("ShoppingBasket");
	document.cookie="ShoppingBasket=.";
}

function updateItem(itemno, newquant) {
	newItemList = ".";
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			thisitem = 1;
			itemstart = i+1;
			fullstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			itemlist=itemlist+1;
			if (itemlist != itemno) {
				newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
			} else {
				newItemList = newItemList + '['+theitem+'|'+theprice+'|'+thenumber+'|'+thenoshipping+'|'+thenotax+'|'+newquant+']';
			}
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
			if (thisitem==4) thenoshipping = fulllist.substring(itemstart, i);
			if (thisitem==5) thenotax = fulllist.substring(itemstart, i);

			thisitem++;
			itemstart=i+1;
		}
	}
//	index = document.cookie.indexOf("ShoppingBasket");
	document.cookie="ShoppingBasket="+newItemList;
	self.location = "basket.asp";
}

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("ShoppingBasket");
	document.cookie="ShoppingBasket="+newItemList;
	self.location = "basket.asp";
}

function clearBasket() {
	if (confirm('Clear Shopping Cart?')) {
		index = document.cookie.indexOf("ShoppingBasket");
		document.cookie="ShoppingBasket=.";
		self.location = "basket.asp";
	}
}

function clickIE4(){
	if (event.button==2){
		alert(message);
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			alert(message);
			return false;
		}
	}
}

function getTotalPrice()
{
	index = document.cookie.indexOf("ShoppingBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    itemtotal = 0;
   if (countend == -1)
    {
   		countend = document.cookie.length;
   	}
	fulllist = document.cookie.substring(countbegin, countend);	
	subtotal = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
			thisitem = 1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			subtotal = subtotal + itemtotal;
			itemlist=itemlist+1;
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
			if (thisitem==4) thenoshipping = fulllist.substring(itemstart, i);
			if (thisitem==5) thenotax = fulllist.substring(itemstart, i);

			thisitem++;
			itemstart=i+1;
		}
	}
	return alterError(subtotal);
}

function getShippingPrice()
{
	index = document.cookie.indexOf("ShoppingBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    itemtotal = 0;
   if (countend == -1)
    {
   		countend = document.cookie.length;
   	}
	fulllist = document.cookie.substring(countbegin, countend);	
	subtotal = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
			thisitem = 1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;

			if ((thenoshipping == "false"))
				subtotal += itemtotal;

//			else if ((!bShipping && thenotax))
//				subtotal += itemtotal;
			
			itemlist=itemlist+1;
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
			if (thisitem==4) thenoshipping = fulllist.substring(itemstart, i);
			if (thisitem==5) thenotax = fulllist.substring(itemstart, i);

			thisitem++;
			itemstart=i+1;
		}
	}
	return alterError(subtotal);
}

function getTaxPrice()
{
	index = document.cookie.indexOf("ShoppingBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    itemtotal = 0;
   if (countend == -1)
    {
   		countend = document.cookie.length;
   	}
	fulllist = document.cookie.substring(countbegin, countend);	
	subtotal = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
			thisitem = 1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;

			if ((thenotax == "false"))
				subtotal += itemtotal;
	
			itemlist=itemlist+1;
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
			if (thisitem==4) thenoshipping = fulllist.substring(itemstart, i);
			if (thisitem==5) thenotax = fulllist.substring(itemstart, i);

			thisitem++;
			itemstart=i+1;
		}
	}
	return alterError(subtotal);
}

function showBasket(bCheckout, bTax, bShipping, fShippingAmount, strShippingMethodName, bTaxOnShipping) {

	index = document.cookie.indexOf("ShoppingBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    noTaxAmount = 0;
   if (countend == -1)
    {
   		countend = document.cookie.length;
   	}
	fulllist = document.cookie.substring(countbegin, countend);	
	subtotal = 0;
	document.writeln('<CENTER>');
	if (!bCheckout)
	{
		document.writeln('<FORM NAME="updateform">');
		document.writeln('<font size="5" face="Verdana, Arial, Helvetica, sans-serif" color="#A80303"><b>Shopping Cart</B></FONT><p>');
	}
	document.writeln('<table cellspacing="0" cellpadding="0" border="0">');
	document.writeln('<TR><TD style="background-color: #A80303" height="15" width=40><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#DFDEA2">Qty.</FONT></TD><TD style="background-color: #A80303" width=60><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#DFDEA2">Item No.</FONT></TD><TD style="background-color: #A80303" align="center"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#DFDEA2">Item</FONT></TD><TD style="background-color: #A80303" align="right" width=50><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#DFDEA2">Price</FONT></TD><td style="background-color: #A80303" align="right" width=50><font size="-2" face="Verdana, Arial, Helvetica, sans-serif" color="#DFDEA2">Total</FONT></TD>');
	if (!bCheckout)
		document.writeln('<TD style="background-color: #A80303" width=154 nowrap><b><FONT COLOR="#A80303">.</FONT></b></TD>');
	document.writeln('</TR>');

	itemlist = 0;
//document.write('<tr><td height="25" align=middle class="alpTDShopLeftRightBottomLight" colspan=6>'+fulllist+'</td><tr>');
	  
	if (fulllist.length <= 4)
	{
	  document.write('<tr><td height="25" align=middle class="alpTDShopLeftRightBottomLight" colspan=6>Your shopping cart is empty</td><tr>');
	}
	else
	{
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
				thisitem = 1;
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);

				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				temptotal = itemtotal * 100;
				subtotal = subtotal + itemtotal;
				if (thenotax == "true")	noTaxAmount  += itemtotal;
				
				itemlist=itemlist+1;
				
				if (!bCheckout)
				{
					document.write('<tr><td height="45" align=middle class="alpTDShopLeftBottomLight"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3 class="inputShop"></td>');
				}
				else
				{
					document.write('<tr><td height="45" align=middle class="alpTDShopLeftBottomLight">'+thequantity+'</td>');
				}
				document.write('<td class="alpTDShopBottomLight"><a href=item.asp?I='+thenumber+'><font color="#A80303" size=1>'+thenumber+'<?font></A></td>');
				document.write('<td align=left class="alpTDShopBottomLight"><a href=item.asp?I='+thenumber+'><font color="#A80303" size=1>'+theitem+'</FONT></A></td><td align=right class="alpTDShopBottomLight">'+theprice+'</td>');
				
				if (!bCheckout)
				{
					document.write('<td align=right  class="alpTDShopBottomLight">'+alterError(itemtotal)+'</td>');
					document.write('<td align=right class="alpTDShopRightBottomLight"><INPUT TYPE="BUTTON" NAME="update'+itemlist+'" VALUE="Update" class="inputShop" ONCLICK="updateItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><IMG SRC="images/space.gif" WIDTH="3" HEIGHT="2" ALT=""><INPUT TYPE="BUTTON" NAME="update'+itemlist+'" VALUE="Remove" class="inputShop" ONCLICK="removeItem('+itemlist+')"></td>');
				}
				else
				{
					document.write('<td align=right  class="alpTDShopRightBottomLight">'+alterError(itemtotal)+'</td>');
				}
				document.write('</tr>');
			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) theprice = fulllist.substring(itemstart, i);
				if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
				if (thisitem==4) thenoshipping = fulllist.substring(itemstart, i);
				if (thisitem==5) thenotax = fulllist.substring(itemstart, i);

				thisitem++;
				itemstart=i+1;
			}
		}
	}

	if (!bCheckout)
	{
		if (bTax)
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>Product Sub Total&nbsp;US$</b></td><td align=right class="alpTDShopRightDark">'+alterError(subtotal)+'</td>');
			document.writeln('<td class="alpTDShopRightDark alpTDShopBottomDark">&nbsp;</td></tr>');
		}
		else
		{
			if (bShipping)
			{
				document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>Product Sub Total US$</b></td><td align=right class="alpTDShopBottomDark">'+alterError(subtotal)+'</td>');
				document.writeln('<td class="alpTDShopRightDark">&nbsp;</td></tr>');
			}
			else
			{
				document.writeln('<tr><td align=right class="alpTDShopLeftDark alpTDShopBottomDark" colspan=4><b>Product Sub Total US$</b></td><td align=right class="alpTDShopBottomDark">'+alterError(subtotal)+'</td>');
				document.writeln('<td class="alpTDShopRightDark alpTDShopBottomDark">&nbsp;</td></tr>');
			}
		}
	}
	else
	{
		if (bShipping)
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>Product Sub Total&nbsp;US$</b></td><td align=right class="alpTDShopRightDark">'+alterError(subtotal)+'</td></tr>');
		}
		else
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark alpTDShopBottomDark" colspan=4><b>Product Sub Total&nbsp;US$</b></td><td align=right class="alpTDShopRightDark alpTDShopBottomDark">'+alterError(subtotal)+'</td></tr>');
		}
	}
	if (bShipping)
	{
		if (bTax)
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>Shipping Charges ('+strShippingMethodName+')</b></td><td align=right class="alpTDShopRightDark">'+alterError(fShippingAmount)+'</td></tr>');
		}
		else
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>Shipping Charges ('+strShippingMethodName+')</b></td><td align=right class="alpTDShopBottomDark alpTDShopRightDark">'+alterError(fShippingAmount)+'</td></tr>');
		}
	}
	if (bTax)
	{
		if (bTaxOnShipping)
		{
			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>NYS Sales Tax ('+ NYTax*100 + '%)</b></td><td align=right class="alpTDShopBottomDark alpTDShopRightDark">'+alterError(NYTax * (subtotal+fShippingAmount-noTaxAmount))+'</td></tr>');
			document.writeln('<tr><td align=right class="alpTDShopBottomDark alpTDShopLeftDark" colspan=4><b>Total</b></td><td align=right class="alpTDShopBottomDark alpTDShopTopDark alpTDShopRightDark">'+alterError(NYTax * (subtotal+fShippingAmount-noTaxAmount) + subtotal+fShippingAmount)+'</td></tr>');
		}
		else
		{
//			document.writeln('<tr><td align=right class="alpTDShopLeftDark" colspan=4><b>NYS Sales Tax ('+ NYTax*100 + '%)</b></td><td align=right class="alpTDShopBottomDark alpTDShopRightDark">'+alterError(NYTax * (subtotal-noTaxAmount))+'</td></tr>');
			document.writeln('<tr><td align=right class="alpTDShopBottomDark alpTDShopLeftDark" colspan=4><b>Total</b></td><td align=right class="alpTDShopBottomDark alpTDShopTopDark alpTDShopRightDark">'+alterError(NYTax * (subtotal-noTaxAmount) + subtotal+fShippingAmount)+'</td></tr>');
		}
	}
	else if (bShipping)
	{
		document.writeln('<tr><td align=right class="alpTDShopBottomDark alpTDShopLeftDark" colspan=4><b>Total</b></td><td align=right class="alpTDShopBottomDark alpTDShopTopDark alpTDShopRightDark">'+alterError(subtotal+fShippingAmount)+'</td></tr>');
	}
	if (!bCheckout)
	{
		document.writeln('</TABLE></FORM>');
	}
	else
	{
		document.writeln('</TABLE>');
	}
}

function initBasket()
{
	if (0.00 == getTotalPrice())
	{
		resetShoppingBasket();
	}
}

function format (expr, decplaces) {
// raise incoming value by power of 10 times the
// number of decimal places; round to an integer; convert to string
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
// pad small value strings with zeros to the left of rounded number
while (str.length <= decplaces) {
str = "0" + str
}
// establish location of decimal point
var decpoint = str.length - decplaces
// assemble final result from: (a) the string up to the position of
// the decimal point; (b) the decimal point; and (c) the balance
// of the string. Return finished product.
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
